/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(key, value, options) {

    //key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires,
                t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IEoptions.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
            ].join(''));
    }

    //key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ?
    function(s) {
        return s;
    } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
}; /*     ---  END OF PLUGINS  ---  */


function mobileRedirect(tomobile) {

    if (tomobile) {
        //redirect to mobile
        jQuery.cookie('mode', 'mobile', {
            expires: 31,
            domain: '.incnow.com'
        });
    }
    else {
        //redirect to full
        jQuery.cookie('mode', 'full', {
            expires: 31,
            domain: '.incnow.com'
        });
    }

}



jQuery(function() {
    jQuery("a[href*='m.incnow.com']").click(function() {
        mobileRedirect(true);
    });
	jQuery("a[href*='m.incnow.com']").click(function() {
        mobileRedirect(true);
    });
	
    //are they using a mobile device
    if (navigator.userAgent.match(/Mobile/i) !== null) { //means they have mobile device next check if they have cookie
        var redirectto = "";

        //check which page they're on
        switch (location.pathname) {
        case '/faq.shtml':
            redirectto = "http://m.incnow.com/#FAQs"; //if url = faq.shtml then redirect to mobile site
            break;
        case '/other_services.shtml':
            redirectto = "http://m.incnow.com/#OtherServices"; // redirect to mobile site
            break;
        case '/incorporation-white.asp?IncorpEntity=LLC':
            redirectto = "http://m.incnow.com/LLCOrderForm.aspx"; // redirect to mobile site
            break;
        case '/incorporation-white.asp?IncorpEntity=Incorporation':
            redirectto = "http://m.incnow.com/CorpOrderForm.aspx"; // redirect to mobile site
            break;
        case '/':
        case '/index.shtml':
            redirectto = "http://m.incnow.com"; // redirect to mobile site
            break;
        default:
            redirectto = ""; //don't redirect
            break;
        }
        //Check to see if a cookie exists, if it does check for type of mode if not then mobile
        if ((jQuery.cookie('mode') === null || jQuery.cookie('mode') == 'mobile') && redirectto !== "") {
            mobileRedirect(true);
            window.location.href = redirectto;
        }
        else {mobileRedirect(false);}
    }
});
