if (!motorola) motorola = { } ;
if (!motorola.controls ) motorola.controls = {};

motorola.controls.HomepageCookie = new Class({
    cookieName: 'HomepageURL',
    
    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 = this.cookieName;
		
		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 myCookie = new Cookie();
					try {
						Cookie.write("HomepageURL", cookieValue, {
							duration: 365,
							path: '/'
						});
					}catch(e){
					}
					var myCookieVal = Cookie.read( "HomepageURL" );
					return true;	
					
				});
			}
			i++;
		}
		
		
    },
	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(this.cookieName, {
				path: '/'
			});
		}catch(e){}
    },
    
    redirectIfCookieFound: function(){
		
        var myCookie;
		//alert("redirectIfCookieFound:" + this.cookieName);
		try {
			myCookie = Cookie.read(this.cookieName);
		}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("HomepageURL", cookieValue, {
					duration: 365,
					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("HomepageURL", cookieValue, {
					duration: 365,
					path: '/'
				});
			}catch(e){
			}
	}

}


