	//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){

	if (getCookie("GoingPopUp") != "")
		return;
		
	document.cookie = "GoingPopUp=true; expires=Tue, 31 Dec 2030 23:59:59 UTC; path=/";

	if (popupStatus==0){
		document.getElementById("backgroundPopup").style["opacity"] = 0.8;
		document.getElementById("backgroundPopup").style["filter"] = "alpha(opacity=80)";
		document.getElementById("backgroundPopup").style["z-index"] = 9998;
		document.getElementById("popupContact").style["z-index"] = 9999;
		document.getElementById("backgroundPopup").style["display"] = "block";
		document.getElementById("popupContact").style["display"] = "block";
		popupStatus = 1;
	}
	
	fadeAll();
}

//disabling popup with jQuery magic!
function disablePopup(){

	if (popupStatus == 1) {
		document.getElementById("backgroundPopup").style["display"] = "none";
		document.getElementById("popupContact").style["display"] = "none";
		popupStatus = 0;
	}
	
	fadeAll();
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = 300;
	var popupWidth = 420;

	document.getElementById("popupContact").style["position"] = "absolute";
	document.getElementById("popupContact").style["top"] = "50px";
	document.getElementById("popupContact").style["left"] = (windowWidth/2-popupWidth/2) + "px";

	//only need force for IE6
	document.getElementById("backgroundPopup").style["height"] = windowHeight;
	
}


//CONTROLLING EVENTS
function setPopupEvents() {
				
	document.getElementById("backgroundPopup").onclick = disablePopup;
	
	document.getElementById("closeButton").onclick = disablePopup;
}

function getCookie(name) {

	var search = name + "="
	var returnvalue = "";

	if (document.cookie.length > 0) {

		offset = document.cookie.indexOf(search)

		if (offset != -1) {

			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}

	return returnvalue;
}

// FADE
var  TimeToFade = 1000.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function  animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {

    if (eid == "popupContact") {
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = '
			+ (element.FadeState == 1 ? '100' : '0') + ')';
	} else {
		element.style.opacity = element.FadeState == 1 ? '0.8' : '0';
		element.style.filter = 'alpha(opacity = '
			+ (element.FadeState == 1 ? '80' : '0') + ')';
	}

    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  if (eid == "popupContact") {
	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  } else {
    element.style.opacity = newOpVal - 0.2;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*80) + ')';
  }
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

function fadeAll() {

	fade('backgroundPopup');
	fade('popupContact');
}

