function geId(key){
	if(isdefined(document.getElementById(key)))
		return document.getElementById(key);
}

function valId(key){
	if(isdefined(document.getElementById(key)))
		return geId(key).value;
}

function isset(varname) {
	try{
		var t = document.getElementById(varname);
	}
	catch(e) {
		return false;
	}
	
	if(t!=='undefined') 
		return true;
	else
	 	return false;
}

function isdefined( constant_name )  {
   return (typeof document.getElementById(constant_name) !== null);
}

function popupChat() {
	if(!$('#afs-info').val()) {
		var newWindow;
		newWindow = window.open('/_chat/client.php','ChatWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=no,width=545,height=650,screenX=150,screenY=150,top=150,left=150');
		if(newWindow!=null && window.focus) newWindow.focus();
	}
	else
		popupChatAFS();
}

function popupChatAFS() {
	var newWindow;
	newWindow = window.open('/_chat/','ChatWindowAdmin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=no,width=1080,height=750,screenX=150,screenY=150,top=150,left=150');
	if(window.focus) newWindow.focus();
}

function ShowHideDiv(div) {
	if(geId(div).style.display=='block')
		$('#'+div).slideUp();
	else
		$('#'+div).slideDown();
}

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

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//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 popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//check new chat and by the way update customer activity
var isVisiblePopup = false;
function pingLogin() {
	$.getJSON('/_chat/client.json.php', {code:'check_chat'}, function(json){
		if(json.has_chat){
			if(!isVisiblePopup) {
				centerPopup();
				loadPopup();
				isVisiblePopup = true;
				popupChat();
			}
			
//			$('#top-alert').css('display', 'block');
//			$('#alert-message').css('text-decoration', 'blink');
		}
		else {
//			if($('#top-alert').css('display')=='block')
//				$('#top-alert').css('display', 'none');
			if(isVisiblePopup)
				disablePopup();
			isVisiblePopup = false;
		}
	});
	
//	$('#empty').load('/_ajax/cust_ping.php');
	setTimeout("pingLogin()",10000)
}

window.onload=pingLogin;


$(document).ready(function(){
	
	$("#popupContactClose").click(function(){
		$.getJSON('/_chat/client.json.php', {code: 'close_new'});
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		$.getJSON('/_chat/client.json.php', {code: 'close_new'});
		disablePopup();
	});
	$('#alert-message').click(function(){
		popupChat();
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	//correct alert div position
	var windowWidth = document.documentElement.clientWidth;
	if(windowWidth>1240) {
//		position = parseInt((windowWidth - 1240)/2 + 835);
//		$('#top-alert').css('left', position);
	}
});
