﻿

$(document).ready(function() {
    $('#lowerFooterMenuLinks').append('<li><a href="" class="change_site mobilesite" data-ajax="false" rel="external">Mobile</a></li>');
	$('.smallGridWidth').css('width','880px');
	
	
	$(function () {
    $('a.fullsite').click(function () {
        mobile.redirectToFullSite(this);
    });
	$('.mobilesite').click(function () {
	
        mobile.redirectToMobileSite(this);
    });
});

var mobile = {
    redirectToFullSite: function (anchor) {
        var ecmCookie = mobile.getEcmCookie('ecm');
        mobile.modifyEcmCookieValue(ecmCookie, 'dvcMdl', 'Generic');
        window.location = $(anchor).attr('href');
    },
	
	redirectToMobileSite: function (device) {
		
        var ecmCookie = mobile.getEcmCookie('ecm');
        mobile.modifyEcmCookieValue(ecmCookie, 'dvcMdl', 'iPhone');
        window.location = $(anchor).attr('href');
    },

    getEcmCookie: function (cookie) {
        // first we'll split this cookie up into name/value pairs
        // note: document.cookie only returns name=value, not the other components
        var allCookies = document.cookie.split(';');
        var tempCookie = '';
        var cookieName = '';
        var isFound = false; // set boolean t/f default f

        for (i = 0; i < allCookies.length; i++) {
            // now we'll split apart each name=value pair
            tempCookie = allCookies[i].split('=');

            // and trim left/right whitespace while we're at it
            cookieName = tempCookie[0].replace(/^\s+|\s+$/g, '');

            // if the extracted name matches passed check_name
            if (cookieName == cookie) {
                isFound = true;
                // we need to handle case where cookie has no value but exists (no = sign, that is):
                tempCookie = tempCookie.join('=');
                tempCookie = tempCookie.substr(tempCookie.indexOf('=', 0) + 1);
                return tempCookie;
            }
        }
        if (!isFound) {
            return '';
        }
    },

    modifyEcmCookieValue: function (ecmCookieString, key, value) {
        var tempKey = '';
        var cookieValArray = ecmCookieString.split('&');
        if (cookieValArray.length > 1) {
            for (i = 0; i < cookieValArray.length; i++) {
                // now split so we have the key value pairs of the cookie
                tempKey = cookieValArray[i].split('=');
                if (tempKey[0] == key) {
                    tempKey[1] = value;
                    // set the original cookie string value to the passed in value
                    cookieValArray[i] = tempKey.join('=');
                    var today = new Date();
                    mobile.setCookie('ecm', 'deleted', -1); // expire the current ecm cookie
                    mobile.setCookie('ecm', cookieValArray.join('&') + ';path=/;', today.setDate(today.getDate() + 30)); // expire the current ecm cookie
                }
            }
        }
    },

    setCookie: function (cookieName, cookieValue, expireDays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expireDays);
        document.cookie = cookieName + '=' + cookieValue + ';expires=' + exdate.toGMTString();
    }

};
});
