var home_image = (function(){
	var home_image={

		//home_image.init is called on dom.ready, it sets variables and calls home_image.check_cookie
		init: function(){
			var picId=home_image.get_cookie("picId");
			var randomnumber=1+(Math.floor(Math.random()*3));
			var background= document.getElementById('main-content');
			home_image.check_cookie(picId,randomnumber,background);
		},
		
		/**
		* home_image.check_cookie is called from home_image.init, checks if cookie/background needs to be changed
		* @param {string} picId: this is the cookie
		* @param {number} randomnumber: randomnumber between 1-3
		* @param {dom element} background: dom element that gets its background changed
		**/
		check_cookie: function(picId,randomnumber,background){
			if(picId!=randomnumber){
				home_image.set_cookie("picId",randomnumber,365);
				//check for IE
				if (!background.style.setAttribute){
					background.setAttribute('style','	background: url(/images/backgrounds/bg-home-page-'+randomnumber+'.jpg) no-repeat');
				}else{//IE:
					background.style.setAttribute('cssText', 'background: url(/images/backgrounds/bg-home-page-'+randomnumber+'.jpg) no-repeat', 0)
				}
			}else{
				home_image.init();			
			}			
		},
		
		/**
		* home_image.get_cookie is called from home_image.init, gets document.cookie and parses 
		* @param {string} c_name: name assigned to the cookie you want to get
		**/
		get_cookie: function(c_name){
			var i,x,y,ARRcookies=document.cookie.split(";");
			for (i=0;i<ARRcookies.length;i++)
			{
			  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			  x=x.replace(/^\s+|\s+$/g,"");
				if (x==c_name){
					return unescape(y);
				}
			}
		},
		
		/**
		* home_image.set_cookie is called from home_image.check_cookie when the cookie needs to be set
		* @param {string} c_name: name you wish to assign the cookie
		* @param {string} value: content for the cookie
		* @param {string} exdays: when do you want the cookie to expire
		**/
		set_cookie: function(c_name,value,exdays){
			var exdate=new Date();
			exdate.setDate(exdate.getDate() + exdays);
			var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
			document.cookie=c_name + "=" + c_value;
		}
	};
	mcd.dom.ready(function(){
		home_image.init();
	});
})();
