/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (getArgs()['DEVICE_STATUS'] != undefined && getArgs()['DEVICE_STATUS'] != null) {
    if (getArgs()['DEVICE_STATUS'] == 'PC') {
        setCookie('DEVICE_STATUS', 'PC', 1, '/');
    }
} 


if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/Android/i))) {
    with(document) {
        var mobileMode = true;
        if (document.cookie.length > 0) {
            var cookies = document.cookie.split(";");
            for (var i = 0; i < cookies.length; i++) {
                try {
                    var _cookie = cookies[i].split("=");
                    if (_cookie.length == 2) {
                        if (_cookie[0].replace(/(^\s*)|(\s*$)/g, "") == "DEVICE_STATUS" && _cookie[1].replace(/(^\s*)|(\s*$)/g, "") == "PC") {
                            mobileMode = false;
                        }
                    }
                } catch (e) {}
            }
        }
        if (mobileMode) {
        	if (getArgs()['SEARCH'] != undefined) {
				console.log(getArgs()['SEARCH']);
				
				location.href = "http://mobile.lib.ntu.edu.tw/?key=" + getArgs()['SEARCH'];
			} else if (getArgs()['searcharg'] != undefined) {
				console.log(getArgs()['searcharg']);
			
				location.href = "http://mobile.lib.ntu.edu.tw/?key=" + getArgs()['searcharg'];
			} else {
            	location.href = "http://mobile.lib.ntu.edu.tw";
            }
        } else {
            write("<script src=\"http://dbi.lib.ntu.edu.tw/Webpac/NTUWebAPI.js\"></script>");
        }
    }
} else {
    with(document) {
        write("<script src=\"http://dbi.lib.ntu.edu.tw/Webpac/NTUWebAPI.js\"></script>");
    }
}

function getArgs() {
    var args = new Object( );
    var query = location.search.substring(1);     // Get query string
    var pairs = query.split("&");                 // Break at ampersand
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');          // Look for "name=value"
        if (pos == -1) continue;                  // If not found, skip
        var argname = pairs[i].substring(0,pos);  // Extract the name
        var value = pairs[i].substring(pos+1);    // Extract the value
        value = decodeURIComponent(value);        // Decode it, if needed
        args[argname] = value;                    // Store as a property        
    }
    return args;                                  // Return the object
}

function setCookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name+'='+escape( value ) +
        ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
        ( ( path ) ? ';path=' + path : '' ) +
        ( ( domain ) ? ';domain=' + domain : '' ) +
        ( ( secure ) ? ';secure' : '' );
}
