/*
Background Takeover script by Stephen D'Andrea || sadandrea@gmail.com
*/

/* GLOBALS */
function getCurrentTime(){
	var d = new Date();
	return Math.round(d.getTime()/1000);
}
var currentTime = getCurrentTime();
/* END GLOBALS */

var CampaignManager = {
		launch : function(campaign){
			
				var head = document.getElementsByTagName('head')[0];
	    		var style = document.createElement('style');
				style.type = 'text/css';
	    		rules = document.createTextNode('body{background: ' + campaign.bg_color + ' url(' + campaign.bg_img + ') ' + campaign.bg_repeat + ' '+ campaign.bg_attachment + ' ' + campaign.bg_position + ' !important; height: auto;}');

				if(style.styleSheet)
	    			style.styleSheet.cssText = rules.nodeValue;
				else style.appendChild(rules);
				head.appendChild(style);
				
				window.onload = function(){
					CampaignManager.setClick(campaign);
				}
				
		}, // end CampaignManager.launch() method
		loopPosts : function() {
			for(i in campaignArray){ 
				//console.log(campaignArray[i].postIds)
				var y = new Date(campaignArray[i].startDate);
				var z = new Date(campaignArray[i].endDate);
				var sT = y.getTime()/1000;
				var eT = z.getTime()/1000;
			
				if(currentTime >= sT && currentTime <= eT){
					for(a in campaignArray[i].postIds){
						if(post == campaignArray[i].postIds[a]){
							//console.log('FOUND '+ campaignArray[i].postIds[a]);
							return true;
							break;
						}
					}
				}
			}
		},
		loopSections : function(campaign) {
			for(i in campaignArray){ 
				//console.log(campaignArray[i].sections)
				var y = new Date(campaignArray[i].startDate);
				var z = new Date(campaignArray[i].endDate);
				var sT = y.getTime()/1000;
				var eT = z.getTime()/1000;
				if(currentTime >= sT && currentTime <= eT){
					for(b in campaignArray[i].sections){
						if(section == campaignArray[i].sections[b]){
							//console.log('FOUND '+ campaignArray[i].sections[b]);
							return true;
						}else{
							if(campaignArray[i].sections[b] == "ROS"){
								//console.log('FOUND '+ campaignArray[i].sections[b]);
								return true;
							}
						}	
					}
				}
			}
		},
		setClick : function(campaign){
			var rand = Math.random();
			// Set mouse events
			// Write tracking pixel to the body
				if(document.getElementById("tracking-pixel")){ // If campaign was already launched
					pixel = document.getElementById("tracking-pixel");
					p = campaign.pixelURL+rand;
					pixel.src = p;
				}else{ // If this is the first time campaign is being launched
					var pixel = document.createElement("img");
					p = campaign.pixelURL+rand;
					pixel.id = "tracking-pixel"
					pixel.src = p;
					document.body.appendChild(pixel);
				}

			if(campaign.clickURL != ''){
				var url = campaign.clickURL+rand; // define clickthrough for local use
				document.body.onmouseover =	function() {
				document.body.style.cursor = "pointer";
				}
				
				document.getElementById(SiteContainer).onmouseover = function() {
				document.getElementById(SiteContainer).style.cursor = "default";
				}
				
				document.body.onclick = function(e){
				//depending on the browser, either srcElement or 
			 	//originalTarget will be populated with the first
		     	//element that intercepted the click before it bubbled up
				try{
					if(event.srcElement){
						throw "ie"
					}
				}
				catch(er){
					if(er=="ie"){
						var originalElement = event.srcElement;
					}else{
					var originalElement = e.originalTarget;	
					}
				}
                 
				if (originalElement.tagName == 'BODY'){ // If BODY was clicked
							window.open(url);
						}
				}
			}
			
			if(campaign.callback){ // Process campaign callback() method if present
				campaign.callback();
			}
		},
		look : function(){
			// Parse URL to find first Name Value variable 
			var searchString = document.location.search;
			searchString = searchString.substring(1);
			var nvPairs = searchString.split("&");
			var firstNvPair = nvPairs[0].split("=");
			var c = "cId";
			var cId = firstNvPair[1];
			if(firstNvPair[0] == c){// if first URL variable == cId
				for(i in campaignArray) { // Loop through campaings to match id with 
					if(cId == campaignArray[i].id){
					CampaignManager.launch(campaignArray[i]);
					}
				}
			}
			else{// THIS IS NOT A TEST!!
				if(CampaignManager.loopPosts()){
				 	CampaignManager.launch(campaignArray[i]);
				}else if(CampaignManager.loopSections()){
					CampaignManager.launch(campaignArray[i]);
				}else{
					var activeCampaign=false;
				}
			} // end if/else this is a test			
		} // end CampaignManager.look() method
	} // end CampaignManager object

	// Look for campaigns
	CampaignManager.look();