//Global variables
purchaseAlertEnabled	= true;
generalAlertEnabled		= true;
autohideTimeout			= 10000;
specialsLine			= 'HUSK: Fri fragt i DK ved k&oslash;b over 1.000 kr.';

// Cookie handling
function getCookie(cookieName)
{
	if (document.cookie.length>0)
	  {
	  	cookieStart = document.cookie.indexOf(cookieName + "=");
  	
		if (cookieStart != -1)
		{
			cookieStart = cookieStart + cookieName.length+1;
			cookieEnd = document.cookie.indexOf(";",cookieStart);
			if (cookieEnd == -1)
			{
				cookieEnd = document.cookie.length;
			}
			return unescape(document.cookie.substring(cookieStart,cookieEnd));
		}
	  }
	return "";
}

function setCookie(cookieName,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = cookieName+"="+escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

//Alert handling
function Alert(isVisible,alertBoxContent)
{
	$('body').scrollTop(0);
	$('body').prepend('<div id="MD_dim" style="height:'+ $('#ShopMainLayOutTable').height()+'px"></div><div id="MD_alert"><table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%"><tr><td align="center" valign="center"><div id="MD_alertBox">'+ alertBoxContent +'</div></td></tr></table></div>');
	$('#MD_dim').css('opacity', 0.25);

	if(isVisible)
	{
		showAlert();
		setTimeout("hideAlert();", autohideTimeout);
	}
}

function showAlert() {
	$('#MD_dim').fadeIn('slow');
	$('#MD_alert').show();
}

function hideAlert() {
	$('#MD_alert').hide();
	$('#MD_dim').fadeOut('slow');
}

function keepShopping()
{
	hideAlert();
	if ($('#CookieSave').is(':checked') == true && getCookie('PopupCookie') == '')
	{
		setCookie('PopupCookie',1,7);
	}
}

function showBasket()
{
	location.href = '/shop/showbasket.html';
	if ($('#CookieSave').is(':checked') == true && getCookie('PopupCookie') == '')
	{
		setCookie('PopUpCookie',1,7);
	}
}

old_alert = window.alert;

window.alert = function(txt)
{
	isPurchaseAlert = false;
	userCookieDisabled = true;
	
	//Hvis alert indeholder "kurven" er der tale om købs-alert
	if(txt.indexOf("nu") !== -1 && txt.indexOf("kurven") !== -1)
	{
		isPurchaseAlert = true;
	};

	if(isPurchaseAlert)
	{
		if (getCookie('PopupCookie') != '')
		{
			if (getCookie('PopupCookie')=="1" || getCookie('PopupCookie')=="2")
			{
				hideAlert();
				userCookieDisabled = false;
			}
		}

		if(userCookieDisabled)
		{
			//Purchase alert
			Alert(purchaseAlertEnabled,'<div id="MD_alertPurchased"><p id="MD_alertPurchasedMessage">'+ txt +'</p><p id="MD_alertPurchasedSpecials">'+ specialsLine +'</p><div id="MD_alertPurchasedButtons"><a id="MD_alertPurchasedKeepShopping" href="javascript:keepShopping();"></a><a id="MD_alertPurchasedShowBasket" href="javascript:showBasket();"></a><div class="clear"></div></div><input type="checkbox" name="CookieSave" id="CookieSave" value="1"><span id="MD_alertCookieText">Vis ikke denne boks igen</span></div>');
		}
	}
	else
	{
		//General alert
		Alert(generalAlertEnabled,'<div id="MD_alertGeneral"><p>'+ txt +'</p><a id="MD_alertGeneralButton" href="javascript:hideAlert();void(0);"></a></div>');
	}

};
