var IsAuth =  getCookie(".ASPXAUTH");
var IframeWidth_DefaultBlocker = '350px';
var IframeHeight_DefaultBlocker = '500px';
var IframeWidth_TakeOverBlocker = '350px';
var IframeHeight_TakeOverBlocker = '565px';
var ImgBG_DefaultBlocker = '/images/Blockers/SearchResultGuestBlocker.jpg';
var ImgBG_TakeOverBlocker = '/images/Blockers/CellcomBlocker1_350x565.jpg';
var ZoneID_TakeOverBlocker = '192';
var BannerCounter_TakeOverBlocker = '7626';
var BannerClick_TakeOverBlocker = '7627';
var Url_TakeOverBlocker = 'http://ad-emea.doubleclick.net/clk;234497049;58414657;v';//if there is no zone and no banner click - go to this url

var str_TakeOverSearchResultBlockerCategories = '692,693,694,695,696,697,699,861,923,1275,1285,1364,1460,1569,1570,1573,1575,310,378,493,495,496,498,499,500,502,503,504,525,595,596,598,600,601,604,605,693,819,905,1053,1078,1079,1139,1295,1342,1343,1367,1420,1453,1515,1516,1517,1566,1569,1571,1572,1575,1576,1578,1579,1580,1581,1582,1583,1584,1585,1586,1587,1588,1589,1590,1591,1592,1593,1594,1595,1597,1598';
var Is_TakeOverSearchResultBlockerCategories_Active = false;

function ShowCampaignBlockUI(IsYoungMode, IsSoldierMode)
{
    if(getCookie('UserLastState') != null)
        return;

	if(ShowTakeOverSearchResultBlocker())
		return;
	
	//if this is young mode, don't open blocker and reutrn;
	if(IsYoungMode == "1" || IsSoldierMode == "1")
		return;
	
	//Not authenticated user
	if(IsAuth == "" || IsAuth == null)
	{
		if(getCookie("SearchResultGuestBlocker") != null)
			return;
			
		OpenBlockerSearchResult('SearchResultGuestBlocker',ImgBG_DefaultBlocker,'1',IframeWidth_DefaultBlocker,IframeHeight_DefaultBlocker,'');
	}
}

function ShowTakeOverSearchResultBlocker()
{
	//return false if blocker is not active
	if(!Is_TakeOverSearchResultBlockerCategories_Active)
		return false;

	//return false if there is cookie return false
	if(getCookie("SearchResultTakeOverBlocker") != null)
		return false;
	
	//if there are no category that match
	if(!IsCategoryMatch())
		return false;
		
	//Open Blocker	
	OpenBlockerSearchResult('SearchResultTakeOverBlocker',ImgBG_TakeOverBlocker,'0',IframeWidth_TakeOverBlocker,IframeHeight_TakeOverBlocker,Url_TakeOverBlocker);
	
	//Write Regular [SearchResultGuestBlocker] for 10 minutes so User wont see another blocker one after the other
	var expires = new Date();
	var MinutesExpire = 2;
	expires.setTime(expires.getTime() + (1000 * 60 * MinutesExpire));
	setCookie('SearchResultGuestBlocker',"1", "/", expires);
	return true;	
}

function OpenBlockerSearchResult(cookieName,blockerImgBG,isDefaultBlocker,IframeWidth,IframeHeight,goToUrl)
{
	var paramPageRegister = "/User/Register/Search.aspx";
	var paramAffiliate = "-1";
	if (RequestQueryString('paramAffiliate') != '')
		paramAffiliate = RequestQueryString('paramAffiliate');
	
	var paramSearchResult = '&position=' + RequestQueryString('position') + 
							'&region=' + RequestQueryString('region') +
							'&type=' + RequestQueryString('type');
							
	//Home Page Default Blocker
	var OverlayBgColor = '#000000';
	var TopPos = '10%';
	var LeftPos = '32%';		
	var content;
	var strUrl = '';
	var strUrlParam = '';		
		
	//Parameters For Google Analytics     
	if(RequestQueryString('utm_source') != "")
		strUrlParam += "&utm_source=" + RequestQueryString('utm_source');
		
	if(RequestQueryString('utm_medium') != "")
		strUrlParam += "&utm_medium=" + RequestQueryString('utm_medium');
		    
	if(RequestQueryString('utm_campaign') != "")
		strUrlParam += "&utm_campaign=" + RequestQueryString('utm_campaign');

	
	//After 5 sec open blocker And Set Cookie for 10 days.
	setTimeout(
		function()
		{
			//open blocker
			content = "<iframe  src='" + paramPageRegister + '?paramAffiliate=' + paramAffiliate + strUrlParam + paramSearchResult + 
					  '&imgBG=' + blockerImgBG + '&isDef=' + isDefaultBlocker + '&width=' + IframeWidth + '&height=' + IframeHeight + 
					  '&goToUrl=' + goToUrl + '&ZoneID=' + ZoneID_TakeOverBlocker + '&BannerID_Count=' + BannerCounter_TakeOverBlocker +
					  '&BannerID_Click=' + BannerClick_TakeOverBlocker + "' frameborder='0' scrolling='no' width='" + IframeWidth +"' height= '" + IframeHeight + "' marginheight='0px' marginwidth='0px'></iframe>";
					  
			$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: OverlayBgColor, opacity: .6});		
			$.extend($.blockUI.defaults.pageMessageCSS, {	width:IframeWidth,
															top:TopPos,		//set position on axis Y
															left:LeftPos,	//set position on axis X
															height:IframeHeight
														});
			$.blockUI(content);		

			//set Cookie For 10 days.
			var expires = new Date();
			var daysExpire = 10;
			expires.setTime(expires.getTime() + (1000 * 60 * 60 * 24 * daysExpire));
			setCookie(cookieName,"1", "/", expires);
			
		},
		1500
	);	
}

function IsCategoryMatch()
{
	var positions = '';
	
	try
	{
		if(typeof(JobParentsCategories) != 'undefined') 
			//Use Categories from variable JobParentsCategories
			positions = JobParentsCategories;
		else
			//Use Categories from url 'position'
			positions = RequestQueryString('position');
	
		//if there are no positions or categories to check, return false
		if (positions == '' || arr_TakeOverSearchResultBlockerCategories == '')
			return false;

		//arr_positions								= positions on the url
		//arr_TakeOverSearchResultBlockerCategories = Categories To search
		var arr_TakeOverSearchResultBlockerCategories = str_TakeOverSearchResultBlockerCategories.split(',');
		var arr_positions = positions.split(',');
		
		var i,j;
		//Only when atleast 1 of the arr_positions exisit in arr_TakeOverSearchResultBlockerCategories, return true
		for(i = 0; i < arr_positions.length; i++)
		{
			for(j = 0; j < arr_TakeOverSearchResultBlockerCategories.length; j++)
			{
				if(arr_positions[i] == arr_TakeOverSearchResultBlockerCategories[j])
					return true;
			}
		}
	}
	catch(e){}	
	
	return false;	
}
