﻿// Ajax function
function goAjax(xmlHttp,url,data) {
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", data.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(data);
  return xmlHttp;
}

// Set session variable
function adStatus(status)
{
  var xmlHttp;
  var url="includes/advert_status.php";
  var data = "status="+status;
  xmlHttp=goAjax(xmlHttp,url,data);
  xmlHttp.onreadystatechange=function() {
	if(xmlHttp.readyState==4)
    {
	  return;
	}
  }
}

// POPUP
// 0 = disabled; 1 = enabled;
var popupStatus = 0;

// loading popup
function loadPopup(){
  if(popupStatus==0){
	$("#popupBkg").css({ "opacity": "0.7" });
	$("#popupBkg").fadeIn("slow");
	$("#popup").fadeIn("slow");
	popupStatus = 1;
  }
}

// disabling popup
function disablePopup(){
  if(popupStatus==1){
	adStatus('off');
	$("#popupBkg").fadeOut("slow");
	$("#popup").fadeOut("slow");
	popupStatus = 0;
  }
}

// centering popup
function centerPopup(){
  // request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var popupHeight = $("#popup").height();
  var popupWidth = $("#popup").width();
  // centering
  $("#popup").css({
	"position": "absolute",
	"top": windowHeight/2-popupHeight/2,
	"left": windowWidth/2-popupWidth/2
  });
  // force for IE6
  $("#popupBkg").css({ "height": windowHeight	});
}

$(document).ready(function(){
	
  // Show popup
  $("#popupShow").click(function(){
	centerPopup();
	loadPopup();
  });
				
  // Close popup
  $("#popupClose").click(function(){
	disablePopup();
  });
  $("#popupBkg").click(function(){
	disablePopup();
  });
  $(document).keypress(function(e){
	if(e.keyCode==27 && popupStatus==1){
	  disablePopup();
	}
  });

});
