var COUNT_COOKIE_NAME = 'viewCount';
var DONT_SHOW_COOKIE_NAME = 'dontShow';
var DEFAULT_COOKIE_EXPIRE_DAYS = 60;


function showPopup () {

	// temporary.
	// Lazzo, 12 aug 2009
//	if (location.href.match(/zenggi-red-actie-test$/)) {return;}

	//return;

	if (!getCookie('donotshowpopup'))
		showNewsletterPopup();	
	
	// hide when shoponline is clicked
	/*$('shoponline').observe('click', function(){
	
    var window = $('subscribeNewsletterPopup');
    if (window) {
		new Effect.Fade(window)
	}

		
	})*/
	
	
}
document.observe('dom:loaded', showPopup);



//Event.observe(window, 'load', showPopupIfNeeded);

function showPopupIfNeeded() {

    // The number of times the customer has visited the homepage.
    var viewCount = getNumberCookie(COUNT_COOKIE_NAME, 0);

    // Set to true if the user has indicated that he / she has already subscribed.
    var dontShowFlagSet = getBooleanCookie(DONT_SHOW_COOKIE_NAME, false);

    if (/*!dontShowFlagSet && */ shouldShowPopup(viewCount)) {
        showNewsletterPopup();
    }

    // increase the viewcount
    setCookie(COUNT_COOKIE_NAME, viewCount + 1, DEFAULT_COOKIE_EXPIRE_DAYS);
}

function handleSubscribePress() {
    // make sure we never see the popup again
	setCookie('donotshowpopup', '1', 60, '/');	

    // submit the form
    $('popupForm').submit();
}

function disablePopup() {
    hideNewsletterPopup();

    // make sure we never see the popup again
    setCookie(DONT_SHOW_COOKIE_NAME, DEFAULT_COOKIE_EXPIRE_DAYS, true);
}

function showNewsletterPopup() {
        var popupWindow = new Element('div', {id:'subscribeNewsletterPopup'}).hide();

		popupWindow.innerHTML =
			'<form id="popupForm" name="popupForm" action="/en/subscribeToNewsletter.html" method="post">'+
				'<input type="hidden" name="name" value="anonymous" />'+
				'<input name="name" style="position:absolute;left:94px;top:124px;width:207px;height:13px;border:0;background:transparent;" />'+
				'<input name="email" style="position:absolute;left:94px;top:156px;width:207px;height:13px;border:0;background:transparent;" />'+
				'<div id="popupSubscribeNow" onclick="handleSubscribePress()" style="position:absolute;width:56px;height:17px;cursor:pointer;top:183px;right:12px;"></div>'+
				'<a href="/en/subscribeToNewsletter.html" id="popupMoreInfo" style="position:absolute;width:167px;height:17px;cursor:pointer;top:183px;left:72px;display:block;"></a>'+
				'<div id="popupClose" onclick="hideNewsletterPopup()" style="position:absolute;width:47px;height:17px;cursor:pointer;top:183px;left:11px;"></div>'+
			'</form>';
			
        $('main').insert(popupWindow);
		new Effect.Appear(popupWindow);
}

function hideNewsletterPopup() {
    var window = $('subscribeNewsletterPopup');
    if (window) {
		new Effect.Fade(window)
		// hide popup for this session
		setCookie('donotshowpopup', '1', 0, '/');	
    }
}

function shouldShowPopup(viewCount) {
    // only show the popup every 3 visits to the homepage
    return viewCount % 3 == 0;
}
