﻿//0 means disabled; 1 means enabled;
var popupStatus = 0;
var popupHeightSt = 0;
var enableCookie = 0;

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }

        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


function loadPopup(){

	if(popupStatus==0){
		jQuery("#backgroundPopup").css({"opacity": "0.05"});
		//jQuery("#backgroundPopup").fadeIn("slow");
		//jQuery("#popupContact").fadeIn("slow");
		
		var windowHeight = document.documentElement.clientHeight;
		//jQuery("#popupContact").css('').show();
		
		jQuery("#popupContact").css({"height": 0, "top": windowHeight - 30}).show().animate({ height: popupHeightSt, top: (windowHeight - popupHeightSt - 30) }, { duration: 300}); 
		popupStatus = 1;
		//jQuery.cookie("showPopup", "1");
		//jQuery.cookie("the_cookie", null);
	}
}

function disablePopup(){
	if(popupStatus==1){
		var windowHeight = document.documentElement.clientHeight;
		//jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupContact").fadeOut("normal");
				
		//jQuery("#popupContact").hide();
		
		/*jQuery("#popupContact").animate({ height: 0, top: windowHeight - 30 }, { duration: 800, complete: function () {
				jQuery("#popupContact").hide();
			}
		});*/ 
		popupStatus = 0;
		if(enableCookie == 1) jQuery.cookie("disablePopup", "1");
	}
}

function rightPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var popupWidth = jQuery("#popupContact").width();
	
	/*var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupContact").height();*/
	
	//right
	jQuery("#popupContact").css({
		"position": "fixed",
		//"top": windowHeight - 30, //-popupHeight-30,
		"left": windowWidth-popupWidth-33
		//"top": windowHeight/2-popupHeight/2,
		//"left": windowWidth/2-popupWidth/2
	});
		
	//only need force for IE6
	/*jQuery("#backgroundPopup").css({
		"height": windowHeight
	});*/
	
}

function startPopup() {
	rightPopup();
	loadPopup();
}

jQuery(document).ready(function(){
	if(enableCookie == 1) 
		if(jQuery.cookie("disablePopup")) return false;

	popupHeightSt = jQuery("#popupContact").height();
	setTimeout("startPopup()",3000);
	
	jQuery("#popupContactClose").click(function(){
		disablePopup();
	});
	/*
	jQuery("#backgroundPopup").click(function(){
		disablePopup();
	});
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});*/
});