/*
  http://d3.zedo.com/jsc/d3/ff2.html?n=790;c=4594;s=3606;d=16;w=1024;h=768
*/

var urlToShow = new Array(); // Adds to be shown in specified order
//urlToShow[0] = "http://d3.zedo.com/jsc/d3/ff2.html?n=790;c=4594;s=3606;d=16;w=1024;h=768";

urlToShow[0] = "http://d7.zedo.com/jsc/fm.html?n=790&c=4594&s=3606&d=16&w=1024&h=768&z=[cache_buster]";
urlToShow[1] = "http://wmedia.rotator.hadj7.adjuggler.net/servlet/ajrotator/75527/0/vh?z=wiget&dim=79059&kw=&click=";

var popCookieName = new Array();
popCookieName[0] = "gunggo_tn_";
popCookieName[1] = "wigetmedia_tn_";

var expireHours = new Array();
expireHours[0] = "24";
expireHours[1] = "24";

var alreadyExecuted = new Array();
alreadyExecuted[0] = false;
alreadyExecuted[1] = false;

var browserUserAgent = navigator.userAgent;
var config = "width=1000,toolbar=1,menubar=1,resizable=1,scrollbars=1";

function displayTheWindow() {
	var didPop = new Array();
	didPop[0] = false;
	didPop[1] = false;
	
	var urlToShow_length = urlToShow.length;
	
	// Get through the variable arrays 
	for (var i = 0; i < urlToShow_length; i++){
		//console.log('running...')
			// check wheater first popunder is shown or not, IF TRUE THEN dont popup the second popunder
		//if ( i > 0 && alreadyExecuted[i] == false){
			//alert(i);
			//if(alreadyExecuted[i] == false && Get_Cookie(i) === null){
			if(alreadyExecuted[i] == false && Get_Cookie(i) != "1"){
				
				
				if (i >0 && didPop[i-1] == true ){
					if(alreadyExecuted[i] == false && Get_Cookie(i) != "1"){
						//i = urlToShow_length+1;
						
						//alert('stop: i:'+ i)
						return;
					}
					//i = urlToShow_length+1;
				}else{
					alreadyExecuted[i] = true;	// set the executed window array to true
				}
				//console.log(i+ ': window is executed->' + alreadyExecuted[i]);
			

				if(browserUserAgent.search("Firefox/3") > -1){
					config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
					var w = window.open(urlToShow[i],popCookieName[i],config).blur();
					window.focus();
					if(w)
						didPop[i] = true;
				}
				else if(browserUserAgent.search("Firefox") > -1){
					
					config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
					var w = window.open(urlToShow[i],popCookieName[i],config);
					var temp = w.window.open("about:blank");
					temp.close();
					if(w)
						didPop[i] = true;
					
					/*
					config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
					var w = window.open(urlToShow[i],popCookieName[i],config).blur();
					window.focus();
					if(w)
						didPop[i] = true;
					*/
				}
				else if(browserUserAgent.search("Opera") > -1){

					var w = window.open(urlToShow[i],popCookieName[i],config);
					if(w)
						didPop[i] = true;
				}
				else if(browserUserAgent.search("Chrome") > -1){
					config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
					var w = window.open(urlToShow[i],popCookieName[i],config).blur();
					window.focus();
					if(w)
						didPop[i] = true;
				}
				else if(browserUserAgent.search("MSIE") > -1){
					config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
					var w = window.open(urlToShow[i],popCookieName[i],config);
					window.setTimeout(window.focus, 750);
					window.setTimeout(window.focus, 850);
					if(w){
						w.blur();
						didPop[i] = true;
					}
				}
				else if(browserUserAgent.search("Safari") > -1){
					config = "width=" + screen.width + ", height=" + screen.height + ",toolbar=1,menubar=1,resizable=1,scrollbars=1";
					var w = window.open(urlToShow[i],popCookieName[i],config).blur();
					if(w)
						didPop[i] = true;
				}
				else{
					var w = window.open(urlToShow[i],popCookieName[i],config);
					if(w)
						didPop[i] = true;
				}

				Set_Cookie( i, "1", i);
			}
			alreadyExecuted[i] = true;
		//}
	}
}

document.onclick = displayTheWindow;

function Set_Cookie( popCookieName_array, value, expireHours_array)
{
	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + (1000 * 60 * 60 * expireHours[expireHours_array]) );
	document.cookie = popCookieName[popCookieName_array] + "=" +escape( value ) + ";expires=" + expires_date.toGMTString() + ";path=/";
	
}

function Get_Cookie( popCookieName_array ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == popCookieName[popCookieName_array] )
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
