var popupPath = "elements/popup_moneyro_eresearch.html";
var strURLBaseER = "www.eresearch.ro";
var bDebug = false;
////// CONSTANTS //// 
var USER_STATUS_NOT_EXPOSED = 1;
var USER_STATUS_TAKING_PART = 2;

var cookieNameBase = "erWebCampStatus"

var wnd;

function erMain(campaignUID)
{
	xdebug('clear');		
	
	var cookieName  = cookieNameBase  + "_" + campaignUID;
	var cookieValue = Get_Cookie(cookieName);
	//alert (cookieValue);
	//return;
	
	syncCookie(cookieNameBase  + "_" + campaignUID, cookieValue, 0, aftersyncCookie) ;

	function aftersyncCookie()
	{
		xdebug(resultCookieValue, "resultCookieValue");
		var userStatus = getUserStatus(campaignUID);
		//alert(userStatus)
		switch(userStatus + "")
		{
		case USER_STATUS_NOT_EXPOSED +"":
			xdebug("USER_STATUS_NOT_EXPOSED")
			
			displayInvitationPopup(campaignUID);
			//alert('after displayInvitationPopup');
			//displayInvitation(campaignUID);
			
			
			break;
		case  USER_STATUS_TAKING_PART+"":
			reCheckPlugin();
			xdebug("USER_STATUS_TAKING_PART")
			break;
		default:
			xdebug("USER_STATUS_UNKNOWN")
			break;
		
		}
	}
}
function reCheckPlugin()
{
	var bIsPluginInstalled = false;
	var browser = "";
	if (document.all)
		browser = "ie"
	else
		browser = "ff";
		
		if (browser == "ie")
		{
				try
				{
					var checkCookieValue = "";
					Set_Cookie("erWebCampPLUGINPresence_check", "0");
					checkCookieValue = Get_Cookie("erWebCampPLUGINPresence_check");
					if(checkCookieValue=="42")
						bIsPluginInstalled = true;
				}
				catch(ee)
				{
					bIsPluginInstalled =false;
				}
		
		
		if(!bIsPluginInstalled)
		{
			
			var cookietriedonce = "";
			cookietriedonce  = Get_Cookie("erWebCampPLUGIN_tried_redirect");
			if(cookietriedonce == "1")
			{
			wnd = window.open("about:blank" , "popuper",  	"satus=1,width=350,height=150");
			wnd.location.href = "http://" + strURLBaseER + "/webc/plugins/getPlugin.php?url=" + document.location.href;
			wnd.document.write("<a href='" +"http://" + strURLBaseER + "/webc/plugins/getPlugin.php?url=" + document.location.href +"' > click here to install needed addon</a>");
			}
			else
			{
				Set_Cookie("erWebCampPLUGIN_tried_redirect", "1");
				document.location.href="http://" + strURLBaseER + "/webc/plugins/getPlugin.php?url=" + document.location.href;
			}
			
		}	
	}
}
function getUserStatus(campaignUID)
{
	var result = USER_STATUS_NOT_EXPOSED ;
	var cookieName  = cookieNameBase  + "_" + campaignUID;
	var cookieValue = Get_Cookie(cookieName);
	xdebug(cookieValue , "cookieValue");

	var bShouldWriteCookieFromSync = false;
	
	if (
		(resultCookieValue!= null)  
		&& ( (cookieValue == null) || (cookieValue != resultCookieValue) )
		)
	{
		bShouldWriteCookieFromSync = true;
		cookieValue  = resultCookieValue;
	}
	//alert(cookieValue);
	if(cookieValue != null)
	{
		if(bShouldWriteCookieFromSync )
		{
			//alert(cookieName+" "+cookieValue);
			Set_Cookie(cookieName ,cookieValue, 1000 );
		}
		result  = cookieValue;
	}
	return result ;
}

//////////////////////////////////////////////////
/// displayInvitation
////////////////////////////////////////////////
function recheckCookiesAfterInvitationComplete()
{
	//alert(43);
	syncCookie(cookieNameBase  + "_" + erWebCampaignUID, null, 0, afterSyncCookieFromInstallMain) ;
}
function afterSyncCookieFromInstallMain()
{
	getUserStatus(erWebCampaignUID);
}

function displayInvitation(campaignUID)
{
	var divInv = document.getElementById("erWebcInvitation");
	
	if(divInv == null)
	{
		//alert('divInv == null');
		var strIFrameInv = '<div style="display:none;" id="erWebcInvitation">\n<BR>No content here ER\n<iframe id="frameInvitation" name="frameInvitation" src="about:blank"></iframe>\n<BR>No content here ER';
		document.write(strIFrameInv);
		divInv = document.getElementById("erWebcInvitation");
	
	}
	divInv.style.display="block";
	var resultCookieValue0 = resultCookieValue;
	var frameInv = document.getElementById("frameInvitation");
	if(frameInv) 
	{
		
		var cookieName  = cookieNameBase  + "_" + campaignUID;
		var strURL  = "http://" + strURLBaseER +"/webc/initial/entercampaign.php?state=start&campaignUID=" + campaignUID + "&cookieName=" + cookieName;
		frameInv.onload = new function(){
			//alert(42);
			//syncCookie(cookieNameBase  + "_" + campaignUID, null, 0, afterSyncFromInstall) ;
			//alert("done");
		}
		frameInv.src= strURL  ;
	}
		
	function afterSyncFromInstall()
	{
		if (resultCookieValue0 != resultCookieValue)
		{
			alert("should reload , old cookie : " +resultCookieValue0  + " , new cookie : " + resultCookieValue );
		}		
	}
	//alert(1)
	
}

//////////////////////////////////////////////////
////junk
////////////////////////////////////////////////
function xdebug(value, name,supp, frm)
{
	return;
	var txt = ""
	if (name)
		txt += "<b>" +name + "</b>="
	txt += value;
	txt+= "<BR/>";
	
	
	
	try{
		if(frm == null)
			frm = document.getElementById("dbgForm");
		frm.txt.value = txt;
		frm.submit();
	}catch(err)
	{
/*		if(supp)
			alert("err:" +err);
		else
*/		
			document.write(txt); 
	}
	
	
}



////////////////////////////////////////////
////added popup
//////////////////////////////////////////
var container;
var background; 
function displayInvitationPopup(campaignUID)
{
	background = jQuery('<div></div>').attr('id','popup_eresearch');
	container = jQuery('<div></div>').attr('id','popup_eresearch_container');
	$(function () {
				$('body').css('margin', '0');
				$('body').css('padding', '0');
				
				background.attr('style','background-color: #000; position: absolute; top: 0; left: 0; z-index: 100000;');
				background.fadeTo(0, 0);
				background.appendTo(document.body);
				
				
				container.attr('style','position: absolute; width: 500px; height: 400px; top: 0; left: 0; z-index: 100001;');
				container.load(popupPath, null, function(){
					$('#inchide_popup').css('text-decoration','underline');
					$('#inchide_popup').click(fadeOutPopup);
					$('#inchide_popup').hover().css('cursor','pointer');
					/*$('#popup_no').click(fadeOutPopup);
					$('#popup_no').hover().css('cursor','pointer');
					$('#popup_yes').click( function() {
						//window.open('http://www.esurveyspro.com/Survey.aspx?id=f0cdcc81-6ebc-436b-bc8b-b9c03f951f8e');
						alert('accepted');
						fadeOutPopup();
					});
					$('#popup_yes').hover().css('cursor','pointer');
					*/
					$('#logoEr').hover().css('cursor','pointer');
					$('#logoEr').click(function() {
						window.open('http://www.eresearch.ro');
					});
					displayInvitation(campaignUID);	
				});
				container.fadeTo(0, 0);
				container.appendTo(document.body);
				
				arrangePopup();
				background.fadeTo("slow", 0.8, function(){
					container.fadeTo("slow", 1);
				});
				
				$(window).scroll(function () {
					var topDist = ($(window).height() - 400);
					container.css('top', $(window).scrollTop() + topDist/2);
					background.css('top', $(window).scrollTop());
				});
				
				//arrange and resize everything
				$(window).resize(arrangePopup);
				//alert(99);
				//displayInvitation(campaignUID);
				/*$(document).ready(function(){
					displayInvitation(campaignUID);	
				}
				);*/
				
			});
			function arrangePopup() {
				var topDist = ($(window).height() - 400);
				var leftDist = ($(window).width() - 500);
				container.css('top', topDist/2);
				container.css('left', leftDist/2);
				background.css('height', $(window).height());
				background.css('width', $(window).width());
			}
			function fadeOutPopup() {
				//alert(000);
				background.fadeTo("slow", 0);
				container.fadeTo("slow", 0);
				container .remove();
					background.remove();
			}
}

//erMain('0be03a0c-7861-4af5-a96e-cf364c9bbcd9');

//erMain(erWebCampaignUID);
//window.document.onload = new function ()
//{
//	erMain('699c7283-6fd4-458d-b8dc-b2edf75c9ecb');
//}
