/****************************************************************************
 File: popup.js
 Author: Richard Coleman
 Date: 19/05/2009
 
 © Red Kite Business Systems, 2009
 
 ****************************************************************************
 
 Description:
 ------------
 
 Provides smooth popup functionality
 ****************************************************************************/

// Setting up the popup
// 0 means disabled; 1 means enabled; 
var popupStatus = 0;
var popupWidth, popupHeight, popupOverflow;
var cookieName;
var button1Expire, button2Expire, button3Expire, buttonCloseExpire;


// Prepare popup dimensions and behaviour
function preparePopup() {
	$("#popupAd").css({
		"width": (popupWidth ? popupWidth : 350),
		"height": (popupHeight ? popupHeight : 150),
		"overflow": (popupOverflow ? popupOverflow : "hidden")
	});
}

// Loading popup with jQuery
function loadPopup() {
	// loads popup only if it is disabled
	if (popupStatus == 0) {
		$("#backgroundPopup").css( { "opacity": "0.7" } );
		$("#backgroundPopup").fadeIn("slow");
		$("#popupAd").fadeIn("slow");
		popupStatus = 1;
	}
}

// Disabling popup with jQuery
function disablePopup() {
	// disables popup only if it is enabled
	if (popupStatus == 1) {
		$("#backgroundPopup").fadeOut("slow");
		$("#popupAd").fadeOut("slow");
		popupStatus = 0;
	}
}

// Centreing popup
function centrePopup() {
	// request data for centreing
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupAd").height();
	var popupWidth = $("#popupAd").width();
	
	// centreing
	$("#popupAd").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	// only need force for IE 6
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

$(document).ready(function(){
	// LOADING POPUP
	// Click the button event
	/*$("#popupTrigger").click(function(){
		preparePopup();
		centrePopup();
		loadPopup();
	});*/
	
	// Click the x event
	$("#popupAdClose").click(function(){
		$.cookie(cookieName,1, {expires: buttonCloseExpire});
		disablePopup();
	});
	
	// Click the out event
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	// Press escape event
	$(document).keypress(function(e){
		if (e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	$("#Button1").click(function(){
		$.cookie(cookieName,1, {expires: button1Expire});
		disablePopup();
	});
	
	$("#Button2").click(function(){
		$.cookie(cookieName,1, {expires: button2Expire});
		disablePopup();
	});
	
	$("#Button3").click(function(){
		$.cookie(cookieName,1, {expires: button3Expire});
		disablePopup();
	});
	
	// onload event, check cookies and display
	// immediately if cookie does not exist
	if (!$.cookie(cookieName)) {
		preparePopup();
		centrePopup();
		loadPopup();
	}
});