// JavaScript Document

window.PopupsStack = [];

window.onresize = function(e)
{
	var bg_scr = document.getElementById('back_screen');
	if(bg_scr)
	{
		bg_scr.style.top = getScrollTop()+"px";
		bg_scr.style.left = getScrollLeft()+"px";
	}
}
window.onscroll = function(e)
{
	var bg_scr = document.getElementById('back_screen');
	if(bg_scr)
	{
		bg_scr.style.top = getScrollTop()+"px";
		bg_scr.style.left = getScrollLeft()+"px";
	}
}

function getScrollTop()
{
	if (window.pageYOffset){  
		scrollTop = window.pageYOffset 
	} else if(document.documentElement && document.documentElement.scrollTop){ 
		scrollTop = document.documentElement.scrollTop; 
	} else if(document.body){ 
		scrollTop = document.body.scrollTop; 
	} 
	return scrollTop;
}

function getScrollLeft()
{
	if (window.pageXOffset){  
		scrollLeft = window.pageXOffset 
	} else if(document.documentElement && document.documentElement.scrollLeft){ 
		scrollLeft = document.documentElement.scrollLeft; 
	} else if(document.body){ 
		scrollLeft = document.body.scrollLeft; 
	} 
	return scrollLeft;
}

function getScrollOffsets()
{
	var l = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
	var t =  window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	var result = [l, t];
	result.left = l;
	result.top = t;
    return result;
}

function center_popup(name)
{
	var elm = document.getElementById(name);
	var offs = getScrollOffsets ();
	var vdim = {width: document.documentElement['clientWidth'] || document.body['clientWidth'],
				height: document.documentElement['clientHeight'] || document.body['clientHeight']};

	var x = (vdim.width - ($(elm).width()||820)) /2 + offs.left;
//	document.title = x+";"+$(elm).width();
	var y = (vdim.height - ($(elm).height()||320)) /2 + offs.top;
	if(y<0)y=0;
	if(x<0)x=0;

	var popups = $('#'+name).parents("div.popup");
	
	for(var i=0;i<popups.length;i++)
	{
		{
			x -= parseInt(popups[i].style.left);
			y -= parseInt(popups[i].style.top);
		}
	}
	elm.style.left = x + "px";
	elm.style.top = y + "px";
}

function show_popup(name, width)
{
	if (width != undefined && width.length!=0) $('#'+name).width(width+"px");
	$('#'+name).show();

	var vdim = {width: document.documentElement['clientWidth'] || document.body['clientWidth'],
				height: document.documentElement['clientHeight'] || document.body['clientHeight']};

	$('#'+name+"_bg").css({height:vdim.height-5+"px"});	
	$('#'+name+"_bg").show();
	center_popup(name);
	if(window.PopupsStack.length==0)
	{
		show_backscreen();
	}
	window.PopupsStack.push(name);
}


function close_popup(name)
{
	window.PopupsStack.pop();
	$('#'+name).hide();
	$('#'+name+"_bg").hide();
	if(window.PopupsStack.length>0)
	{
		$("#"+window.PopupsStack[window.PopupsStack.length-1])[0].style.zIndex = 1000;
	}
	else
	{
		$('#back_screen').remove();
	}
}
function show_backscreen()
{
	try
	{
		hide_loader();
		if(window.innerHeight){ 
			windowHeight=window.innerHeight; 
		} else if(document.documentElement && document.documentElement.clientHeight){ 
			windowHeight=document.documentElement.clientHeight; 
		} else if(document.body){ 
			windowHeight=document.body.clientHeight; 
		}
		var back = 	document.createElement("div");
		back.id = "back_screen";
		back.className = "back_screen";
		back.style.height = windowHeight;
		back.style.top =  getScrollTop()+"px";
		back.style.left =  getScrollLeft()+"px";
		document.body.appendChild(back);	
	}catch(e){
	}
}

function open_popup(name, width)
{
	show_popup(name, width);
	$("#"+name).fadeIn("slow", function (){center_popup(name);});
}

function cumulativeOffset(element) 
{
	var valueT = 0, valueL = 0;
	do {
	  valueT += element.offsetTop  || 0;
	  valueL += element.offsetLeft || 0;
	  if (element.offsetParent == document.body)
	    if (element.style.position == 'absolute') break;
	  element = element.offsetParent;
	} while (element);
	return {left:valueL, top:valueT};
};

function position_popup(name, x, y)
{
	var popup = $('#'+name)[0];
	popup.style.left = x+'px';
	popup.style.top = y+'px';
	popup.style.postion = 'absolute';
}

function hide_loader(keep_backscreen)
{
	if (!keep_backscreen) $('#back_screen').remove();
	$('#preloader').remove();
}

function show_loader(message, shadow, color)
{
	if (!message) message = 'Loading';
	if (!shadow) shadow = 1;
	if (!color) color = 'white';
	
	show_backscreen();
	var div = document.createElement("div");
	div.id='preloader';
	div.style.textAlign='center';
	div.style.zIndex = "1000";
	div.style.padding = "5px";
	div.style.color=color;
	div.style.position = 'absolute';
	div.style.top =  300 + getScrollTop()+"px";
	div.style.left = $('#back_screen').width() /2 + getScrollLeft()+"px";
	div.innerHTML = "<img src='/images/preloader2.gif' border='0'/><br/><b>"+message+" ...</b>";
	document.body.appendChild(div);	
}

function set_title(name, title)
{
	$('#'+name+'_title').html(title);
}

function set_popup_class(name, className)
{
	$('#'+name).attr('class', className);
}
