
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var topf ;
var leftf;
//set image to loader
function set_loader_image()
{
	 $('#backgroundloader').html('<img src="images/ajax-loader.gif" />');
         $("#popupContact").html('');
}

//loading loader image slow
function load_loader(){
	
		
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		centerloader();
		$("#backgroundPopup").fadeIn("slow");
		$("#backgroundloader").fadeIn("slow");
		$("#backgroundloader").fadeOut("slow");	
}

//loading loader image fast
function load_loader_fast(){
	
	
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		centerloader();
		$("#backgroundPopup").fadeIn("fast");
		$("#backgroundloader").fadeIn("fast");
		$("#backgroundloader").fadeOut("slow");	
	
}

// this will call after file is been load
function after_load_complete(var_pop_height,var_pop_width)
{
   //alert("in");
   disablePopup_loader();
   $("#backgroundloader").fadeOut("slow",centerPopup(var_pop_height,var_pop_width));
   $("#backgroundloader").fadeOut("slow");	
   //alert("out");
   
}
function disablePopup_loader(){
	//disables popup only if it is enabled
		$("#backgroundloader").fadeOut("slow");	
}

// pop up will load
function loadPopup(){
	//loads popup only if it is disabled

    disablePopup_loader();
    
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
		
       // alert(popupStatus);
	}
	
	$("#backgroundloader").fadeOut("slow");	
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
            
		if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
       
		popupStatus = 0;
	}
	
}

//centering the loader
function centerloader(){
	//request data for centering
     //disablePopup_loader();
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#backgroundloader").height();
	var popupWidth = $("#backgroundloader").width();
	//centering
        
        get_height_width(popupHeight,popupWidth);
        
	
        topf=CalculateTop_popup('32');
        
        
        $("#backgroundloader").css({
		"position": "absolute",
		"top": topf,
		"left": leftf
	});
        
        /*
        $("#backgroundloader").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
        */
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	$("#backgroundloader").fadeOut("slow");	
}
//centering popup
function centerPopup(var_pop_height,var_pop_width){
	//request data for centering
	//alert(var_pop_height);
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	/*var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();*/
	var popupHeight = var_pop_height;
	var popupWidth = var_pop_width;
	
	get_height_width(popupHeight,popupWidth);
	
	
	//alert(topf);
	//alert(leftf);
	
        topf=CalculateTop_popup('32');
        topf=topf-250;
        //alert(topf);
        
        if(ie)
            {
                leftf=leftf-30;
            }
            //alert(leftf);
	//leftf=CalculateLeft('32')
	//centering
        /*
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	*/
       
	$("#popupContact").css({
		"position": "absolute",
		"top": topf,
		"left": leftf
	});
	
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
	loadPopup();
}

// closing the popup
function styledPopupClose(evt)
{

	var key=(window.Event)?evt.which:evt.keyCode;
    
	if(key==27 && popupStatus==1)
	{
       

		disablePopup();
	}
}
function close_popup(e){
    var unicode=e.keyCode? e.keyCode : e.charCode
    
    if (unicode == '27')
    {
       
     //      disablePopup();
    }
}
// function to get top and left
function get_height_width(popupHeight,popupWidth)
{
	var popuph = popupHeight;
	var popupw = popupWidth;
	var popuph2 = popuph/2;
	var popupw2 = popupw/2;
	var winW = screen.width;
	var winH = screen.height;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			//winW = window.innerWidth;
			//winH = window.innerHeight;
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
			//winW = window.innerWidth;
			//winH = window.innerHeight;
		}
	
	}
	//alert(window.innerWidth + ":" + window.innerHeight );
	var sh = winH;
	var sw = winW;
	var sh2 = sh/2;
	var sw2 = sw/2;
	
	topf = sh2 - popuph;
	leftf = sw2 - popupw2;
	
}

// this is used for ajax when click on submit
function loader_in()
{
	$('#backgroundloader').html('<img src="images/xajax-loader.gif" />');
	$("#backgroundPopup").css({
			"opacity": "0.7"
		});
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#backgroundloader").height();
	var popupWidth = $("#backgroundloader").width();
        
        get_height_width(popupHeight,popupWidth);
        
	
        topf=CalculateTop_popup('32');
        
        
        $("#backgroundloader").css({
		"position": "absolute",
		"top": topf,
		"left": leftf
	});
	//centering
        
    /*
	$("#backgroundloader").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
   */
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
	$("#backgroundPopup").fadeIn("fast");
	$("#backgroundloader").fadeIn("fast");
	$("#backgroundloader").fadeOut("slow");	
}

function CalculateTop_popup(Height)
{
	var ScrollTop=document.body.scrollTop;
	if(ScrollTop==0)
	{
		if(window.pageYOffset)
			ScrollTop=window.pageYOffset;
		else
			ScrollTop=(document.body.parentElement)?document.body.parentElement.scrollTop:0;
	}

	var BodyHeight=document.body.clientHeight;

	if(BodyHeight==0)
	{
		BodyHeight=window.innerHeight;
	}
	if(BodyHeight==0)
	{
		BodyHeight=document.documentElement.clientHeight
	}

	var myWidth = 0, myHeight = 0;
  	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
    //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	var FinalTop=((myHeight-Height)/2)+ScrollTop;

	return FinalTop;
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	$("#backgroundloader").fadeOut("slow");	
	//LOADING POPUP
	//Click the button event!
	
	/*$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});*/
				
	//CLOSING POPUP
	//Click the x event!
	/*$("#popupContactClose").click(function(){
		disablePopup();
	});*/
	//Click out event!
	/*$("#backgroundPopup").click(function(){
		
	});*/
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			
		}
	});

});
