if (!motorola) motorola = { } ;
if (!motorola.controls ) motorola.controls = {};

motorola.controls.HomepageCookie = new Class({
    
    cookieName: "BusinessLocaleURL",
	
    initialize: function(){

		window.addEvent('domready', this.onDomReady);
	},
	onDomReady:function(){

		// attach onClick events for global home page & global header tabs
		var anchorsRed = $$('.redLinks a', '.motoRedBtn a', '.js_homecookie_red_link');
		var anchorsBlue = $$('.blueLinks a', '.motoBlueBtn a', '.js_homecookie_blue_link');
		//alert("anchors:" + anchors);
		var anchors;
		
		var cname = 'BusinessLocaleURL';
		
		var i=0;
		var myColor;
		var ref  = this;
		while(i<2){
			if (i == 0) {
				anchors = anchorsRed;		
				myColor = "red"
			}else{
				anchors = anchorsBlue;
				myColor = "blue"
			}
			if (anchors) {
				anchors.each(function(el){
					el.store("checkboxColor", myColor);
				});
				anchors.addEvent('click', function(){
					//alert("myColor:" + myColor + " this.retrieve " + this.retrieve("checkboxColor") + " ref.isCheckboxSelected:" + ref.isCheckboxSelected);
					//alert("ref:" + ref);
					var isChecked = hpCookie.isCheckboxSelected( this.retrieve("checkboxColor") );
					//alert("isChecked:" + isChecked);
					//is checkbox checked? no - return
					if (!isChecked) return;
					//var cookieValue = this.href;
					var cookieValue = $$('.motoRedBtn a')[0];
					if (this.retrieve("checkboxColor")  == "blue"){
						cookieValue = $$('.motoBlueBtn a')[0];
					}	
					var myCookie = new Cookie();
					try {
						Cookie.write("BusinessLocaleURL", cookieValue, {
							duration: 182,
							path: '/'
						});
					}catch(e){
					}
					var myCookieVal = Cookie.read( "BusinessLocaleURL" );
					return true;	
					
				});
			}
			i++;
		}
		
		try{
			if (window.doRandomRedirect != undefined && window.doRandomRedirect){
				hpCookie.randomRedirect();
			}
		}catch(e){}
    },
	/* Added randomRedirect function - KO 11/14/11 5:00pm cst*/
	/* Added code that will only cookie users that are not redirected - KO 11/18/11 3:00pm cst */
	randomRedirect:function(){
		var ranNum = Math.random();
		if (ranNum <= .1) {
			$$('.consumerCheck').set('checked', false);
			window.location.href = redirectUrl;
		} else {
			$$('.consumerCheck').set('checked', true);
		}
		
	},
	isCheckboxSelected:function(which){
	
		var out = false;
		//alert("isCheckboxSelected:" + which);
		if (which == "red"){
			out = document.redCheckboxForm.checkboxRed.checked;
		}else{
			out = document.blueCheckboxForm.checkboxBlue.checked;
		}
		//alert("out:" + out);
		return out;
	},
    clear: function(){
		var myCookie;
		try {
			myCookie = Cookie.dispose('BusinessLocaleURL', {
				path: '/'
			});
		}catch(e){}
    },
    
    redirectIfCookieFound: function(){
		
        var myCookie;
		
		try {
			myCookie = Cookie.read('BusinessLocaleURL');
		}catch(e){}
        if (myCookie != null) {
            // redirect user
            document.location = myCookie;
        }
    }
});

var hpCookie = new motorola.controls.HomepageCookie();

window.elcSearchOnUnload = function (){
	//alert("elcSearchOnUnload: " + hpCookie);
	if (hpCookie){
		//alert('hpCookie.isCheckboxSelected( "red" )' + hpCookie.isCheckboxSelected( "red" ));
		if (!hpCookie.isCheckboxSelected( "red" )) return;
		var cookieValue = $$('.motoRedBtn a')[0];
		//alert("cookieValue:" + cookieValue);
		try {
				Cookie.write("BusinessLocaleURL", cookieValue, {
					duration: 182,
					path: '/'
				});
			}catch(e){
			}
	}

}

window.bmsSearchOnUnload = function (){
	//alert("elcSearchOnUnload: " + hpCookie);
	if (hpCookie){
		if (!hpCookie.isCheckboxSelected( "blue" )) return;
		var cookieValue = $$('.motoBlueBtn a')[0];
		//alert("cookieValue:" + cookieValue);
		try {
				Cookie.write("BusinessLocaleURL", cookieValue, {
					duration: 182,
					path: '/'
				});
			}catch(e){
			}
	}

}


