//2009 MDBLAB

//0 means disabled; 1 means enabled;
var popupStatus = 0;
var qStack;

//Events
$(document).ready(function()
{
	//
	//$("#popupContactClose").click(function()
	//{
	//	disablePopup();
	//});

	//Click out event!
	//$("#backgroundPopup").click(function()
	//{
	//	disablePopup();
	//});

	//Press Escape event!
	$(document).keypress(function(e)
	{
		if(e.keyCode==27 && popupStatus==1)
		{
			//disablePopup();
			PopUpClose();
		}
	});
});

//function show_popup(_name,_img,_info)
//{
	//
	//document.getElementById('_name').innerHTML = _name;
//	document.getElementById('_img').src='/catalog/'+_img;
	//document.getElementById('_info').innerHTML = _info;
	//
	//centerPopup();
	//
	//loadPopup();
//}

//
//function centerPopup()
//{
	//
	//document.getElementsByTagName('body')[0]
	//var windowWidth = $("#slave").width();
	//var windowHeight = $("#main").height();
	//var windowWidth = document.getElementsByTagName('div')[0].scrollWidth;//offsetWidth;
	//var windowHeight = document.getElementsByTagName('div')[0].offsetHeight;//offsetWidth;
	//var windowHeight = document.body.scrollHeight;
//	var popupWidth = $("#popupbox").width();
	//var popupHeight = $("#popupbox").height();


	//if (getInternetExplorerVersion()==7)
	//{
	//	$(".cover").css({"left":"0px"});
//		$("#popupbox").css({"left":"0px"});
//	}
//	$(".cover").css({"height":windowHeight,"width":windowWidth});

	//
	//$("#popupbox").css({"margin-left":(windowWidth/2)-(popupWidth/2),
	//	"margin-top":(windowHeight/2)-(popupHeight/2)});
	//$("#popupbox").css({"margin-left":(windowWidth/2)-(popupWidth/2),
	//	"margin-top":"10px"});
//}

//function loadPopup()
//{
	//
	//if(popupStatus==0)
	//{
	//	$(".content").css({"opacity":"0.1"});
		
	//	$(".cover").css({"display":"block"});

		//$("#popupbox").fadeIn('fast');
		
	//	popupStatus = 1;
	//	doit();
//	}
//}

//function disablePopup()
//{
	//
	//if(popupStatus==1)
	//{
	//	$("#popupbox").fadeOut(20);

	//	$(".cover").css({"display":"none"});

		//$(".content").css({"opacity": "1"});
		
	//	popupStatus = 0;
	//}
//}

function getInternetExplorerVersion()
{
   var rv = -1;
   if (navigator.appName == 'Microsoft Internet Explorer')
   {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
         rv = parseFloat( RegExp.$1 );
   }
   return rv;
}

//function doit()
//{
//	metatags = document.getElementsByTagName("title");
//	metatags.setAttribute("content", "Better document title");
//}

//Add 01-07-2010


function PopUp()
{
	//PopUp Position
	var windowWidth = document.getElementsByTagName('div')[0].scrollWidth;
	var windowHeight = document.body.scrollHeight;
	var popupWidth = $("#PopUp").width();
	var popupHeight = $("#PopUp").height();

	if (getInternetExplorerVersion()==7)
	{
		$("#PopUpCover").css({"left":"0px"});
		$("#PopUp").css({"left":"0px"});
	}
	$("#PopUpCover").css({"height":windowHeight,"width":windowWidth});

	$("#PopUp").css({"margin-left":(windowWidth/2)-(popupWidth/2),
		"margin-top":(windowHeight/2)-(popupHeight/2)});
	
	//Show PopUp
	if(popupStatus==0)
	{
		$(".content").css({"opacity":"0.1"});
		
		$("#PopUpCover").css({"display":"block"});

		$("#PopUp").fadeIn('fast');
		
		popupStatus = 1;
	}
}

function PopUpClose()
{
	//
	if(popupStatus==1)
	{
		$("#PopUp").fadeOut(20);

		$("#PopUpCover").css({"display":"none"});

		$(".content").css({"opacity": "1"});
		
		popupStatus = 0;
	}
}

function Stack(first, second)
{
	//inizialize the array if not defined
	if (qStack==undefined)
	{
		qStack = new Array();
	}

	//add to stack
	qStack.push(first);
	qStack.push(second);
}

function Post()
{
	//stack has to be inizialized
	if (qStack==undefined)return;
	//stack needs to be not empty
	if (qStack.length==0)return;
	//stack minimum length is set to 2, command are processed in pairs at this stage
	if (qStack.length<2)return;

	var target = qStack[0];
	var stream = qStack[1];

	//CallBack
	if (target=='Callback')
	{
		window[stream]();
		qStack.shift();
		qStack.shift();
		Post();
		return;
	}

	//Page Refresh
	if (stream=='Refresh')
	{
		qStack.shift();
		qStack.shift();
		location.reload();
		return;
	}

	//ask for confirmation
	if (stream=='Confirm')
	{
		if(!confirm('Sei Sicuro?'))
		{
			qStack = [];
			return;
		}
		qStack.shift();
		qStack.shift();
		Post();
		return;
	}

	var t;
	if (target == 'null')//null target, using temp
	{
		t = $("<div></div>");
	}
	else//otherwise lets use it
	{
		t = $("#"+target);
	}

	//Close the target
	if (stream=='Close')
	{
		$(t).empty();
		qStack.shift();
		qStack.shift();
		Post();
		return;
	}

	//
	var data = BuildPost(stream);
	
	$(t).load('services.php', data, function(e)
	{
		if (e=='Forbidden')
		{
			qStack.shift();
			qStack.shift();
			qStack = [];
			location.reload();
			return;
		}
		if (target=='null')
		{
			if (e!='Passed')
			{
				alert(e);
				//clear the stack
				qStack = [];
				return;
			}
		}
		qStack.shift();
		qStack.shift();
		Post();
	});
}

function BuildPost(stream)
{
	var pair = stream.split('|');
	var data = "({";
	for (i=0; i<pair.length; i++)
	{
		if (i>0)
			data += ",";
		single = pair[i].split(':');
		if (single[1] == '#')
		{
			value = document.getElementById(single[0]).value;
			value = value.replace(/\n/g,"%BR");
			value = value.replace("'","%AP");
			data += "'p"+i+"':'"+value+"'";
		}
		else
		{
			data += "'p"+i+"':'"+single[1]+"'";
		}
	}
	data += "})";
	//alert(data);
	return eval(data);
}
