var ctype=0;  
var cid=0;    // current id
var ntype=0;  // next up type 
var nid=0;    // next up id

var fadeintimer;
var fadeouttimer;

var statiivis=false;
var centrevis=false;
var modIndice= false;
var indiceGeneral= false;
var indiceSpecific= false;
var statiiOrarvis = false;
var statiiGeneralvis = false;
var statiiOrarGeneralvis = false;
var statiiZilnicvis = false;

var outside=true;
var ceas = 0;

var cmarkertype=0;

var timers=[];
var assoc=[];

var path=[];

var agt=navigator.userAgent.toLowerCase();

var is_opera8 = (agt.indexOf("opera 8") != -1 || agt.indexOf("opera/8") != -1);

var clockUrl = 'ceas.php';

$(document).ready( function(){
	if ( ceas == 0 ) {
		getTimp();
		setInterval(getTimp, 30000);
		ceas = 1;
	}
	path[0]=0;
	if (typeof MM_preloadImages == 'function')
		MM_preloadImages('img1.jpg','img2.jpg','img3.jpg','img4.jpg');
});

function isIE()
{
	var detect = navigator.userAgent.toLowerCase();
	if(detect.indexOf("msie")+1) return true;
	else return false;
}

function httpsend(url,callback) {
	try { var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"); } 
	catch (e) { } 
	xmlhttp.onreadystatechange = function() {
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) 
		 {
		   callback(xmlhttp.responseText);
		 }
	};
	xmlhttp.open("POST", url); 
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(""); 
}

function fillinfo(text) {
	if (!outside) {
	 	resps = eval('(' + text + ')');

	 	var ltot=508;
		var lstart=133;
		
		$('#markerdetails').html(resps.title);
		
		var inds1 = resps.indiceOra;
		var inds2 = resps.indiceZi;
		
		if(inds1) {
			var s1pos=Math.round(ltot*inds1/6) + (lstart/2) + 7;
		 	$('#sageata1').html(inds1).show().animate({'left': s1pos+'px'}, parseInt(inds1)*100);
		}

		if(inds2) {
			var s2pos=Math.round(ltot*inds2/6) + (lstart/2) + 7;
		 	$('#sageata2').html(inds2).show().animate({'left': s2pos+'px'}, parseInt(inds2)*100);
		}

	 	$('#indzilei').show().html(resps.date2);
	 	$('#indora').show().html(resps.hour);
	 	$('#inddata').show().html(resps.date);
	 	outside=true;
	}
}

function showinfo(id, type) {
 	if (outside) {
		$('#markerdetails').html('<span class="detalii-loading"></span>');
		rand = Math.round(Math.random()*1000000);
		$.get("info.php?id="+id+"&zoom="+ctype+"&type="+type+"&rand="+rand, fillinfo);
		outside=false;
	}
}

function hideinfo(id) {
 	$('#markerdetails').html('<span class="detalii"> Pentru a afla calitatea aerului intr-una din statiile de monitorizare din cadrul RNMCA, faceti click pe harta alaturata respectand calea: Regiune / Judet / (Centre) / Statii </span>');
 	$('#sageata1').animate({'left': '120px'}, 200).html('').hide();
 	$('#sageata2').animate({'left': '120px'}, 200).html('').hide();
 	$('#indora').html('').hide();
 	$('#inddata').html('').hide();
 	$('#indzilei').html('').hide();
	outside=true;
}

function placecenters(text) {
	$('#centers').html(text);
	stopLoad();
}

function hideindinfo(type) {
	$('#detaliiindice'+type).hide(); 
}

function showindinfo(type) {
 	$('#detaliiindice'+type).show(); 
}

function placemarkers(text) {
 	$('#markers').html(text).css('z-Index', '40');
	stopLoad();
}

function showmarkers(type, isDaily, isHourly) {
	if (!isDaily)
		isDaily = false;
	if (!isHourly)
		isHourly = false;

	startLoad();
	rand = Math.round(Math.random()*1000000);
	$.get("get.php?zoom="+ctype+"&id="+cid+"&type="+type+"&isDaily="+isDaily+"&isHourly="+isHourly+"&rand="+rand, placemarkers);
	
	if (cmarkertype>0)	{
		var id = (isDaily == true)?mtypetoid(cmarkertype):mtypetoidorar(cmarkertype);
		$('#'+id).removeClass("active");
		hideindinfo(cmarkertype);
	}
	
	cmarkertype=type;
	if (cmarkertype>0)	{
		var id = (isDaily == true)?mtypetoid(type):mtypetoidorar(type);
		$('#'+id).addClass("active");
		showindinfo(cmarkertype);
	}
}

function showcenters() {
 	startLoad();
 	rand = Math.round(Math.random()*1000000);
	if(ctype==2 || ctype==3)
		$.get("get.php?zoom="+ctype+"&id="+cid+"&type=x"+"&nid="+nid+"&rand="+rand, placecenters);
	else
		$.get("get.php?zoom="+ctype+"&id="+cid+"&type=x&rand="+rand, placecenters);
}

function log(s) {
	$('#logbox').html( $('#logbox').html() + s+"<br/>");
}

function mtypetoid(type) {
	if (type==1) return "so2"; else
	if (type==2) return "pm10"; else
	if (type==3) return "no2"; else
	if (type==4) return "o3"; else
	if (type==5) return "co"; else
	if (type==6) return "pm25"; 
}

function mtypetoidorar(type) {
	if (type==1) return "so2orar"; else
	if (type==2) return "pm10orar"; else
	if (type==3) return "no2orar"; else
	if (type==4) return "o3orar"; else
	if (type==5) return "coorar"; else
	if (type==6) return "pm25orar"; 
}


function startLoad() {
 	$('#loading').css('opacity', '0').show().animate({'opacity': '0.9'}, 500);
}
function stopLoad() {
 	$('#loading').css('opacity', '0.9').animate({'opacity': '0.9'}, 500).hide();
}

function showoverlay(id) {
	$('#'+id).show().animate({'opacity':'0.9'}, 500);
	overlib($('#PopUp'+id).html(), CAPTION, '');
	
}

function hideoverlay(id) {
 	$('#'+id).stop().css('opacity', '0').hide();
	nd();
}

function filldata(text) {
 	stopLoad();
	$('#container').html(text);
//	nd();
	
	cid=nid; ctype=ntype;
	
	path[ctype]=cid;
	$('#title').html("type: "+ctype+", id="+cid);
	
	centrevis=false;
	$('#centrbutton').removeClass('active');
	
	if (ctype>1) {
		$('#statbutton').removeClass('active');
		$('#statbutton').show();
	} else {
	 	$('#statbutton').hide();
	}

	if (cmarkertype>0) hideindinfo(cmarkertype);
	$('#indici-harta').hide();
	statiivis=false;
	statiiOrarvis = false;
	modIndice=false;
	indiceSpecific = false;
	indiceGeneral = false;
	statiiGeneralvis = false;
	statiiOrarGeneralvis = false;

	var x=ctype-1;
	
	if (ctype>0) {
		$('#backbutton').html("up one level, to "+x+" / "+path[ctype-1]);
		$('#backbutton').show();
	} else 
		$('#backbutton').hide();
	
	$('#container img').each( function() {
	 	if ( $(this).attr('src').indexOf('blank.gif') != -1 ) {
	 	 	$('#overall').css('height', $(this).css('height'));
		 	return;
	 	}
	});
}

function romania() {
	startLoad();
	$.get("romania.php", filldata);
	ntype=0; nid=0;
}

function zona(id) {
 	startLoad();
	$.get("zona.php?id="+id, filldata);
	ntype=1; nid=id;
}

function judet(id) {
 	startLoad();
	$.get("judet.php?id="+id, filldata);
	ntype=2; nid=id; 
	nd();
}

function resedinta(id, idJud) {
 	startLoad();
	$.get("resedinta.php?id="+id, filldata);
	ntype=3; nid=idJud;
	cid = nid;
	ctype=ntype;
	path[2]=cid; 
}

function hideIndici() {
	indiceGeneral= false;
	indiceSpecific= false;
	statiiOrarvis = false;
	statiiGeneralvis = false;
	statiiOrarGeneralvis = false;
	statiiZilnicvis = false;

	$('#general').removeClass('active');
	$('#indice-general').hide();
	$('#tip-indice-general').hide();
	
	$('#specific').removeClass('active');
	$('#indice-specific').hide();
	$('#indice-specific').css('top', '30px'); 
	$('#tip-indice-specific').hide();
	$('#indici-harta').hide();
	$('#indici-harta-orar').hide();
}

function goback() {
	
	if( $('#title').html().indexOf('id=42')!=-1 || $('#title').html().indexOf('id=16')!=-1 )
	{
		romania();
	}
	else
	{
		if (ctype-1==0) romania(); else
		if (ctype-1==1)zona(path[1]); else	
		if (ctype-1==2) judet(path[2]); 
	}
	hideIndici();
}

function goBackStation(id, id2)
{	
	path[1] = id2;
	path[2] = id;
	ctype=2;
	nid = id;
	judet(id);
}

function initialize() {
	path[0]=0;
}
function getTimp()
{
	$('#clock').load(clockUrl);
}

function tipIndiceGeneral(){
	indiceGeneral = !indiceGeneral;
	indiceSpecific = false;
	$('#specific').removeClass('active');
	$('#zilnic-specific').removeClass('active');
	$('#orar-specific').removeClass('active');
	$('#zilnic-general').removeClass('active');
	$('#orar-general').removeClass('active');
	$('#'+mtypetoidorar(cmarkertype)).removeClass("active");
	$('#'+mtypetoid(cmarkertype)).removeClass("active");
	$('#indici-harta').hide(); 
	$('#indici-harta-orar').hide(); 
	if(indiceGeneral)
	{
		$('#general').addClass('active');
		$('#tip-indice-general').show(); 
		$('#indice-specific').css('top', '90px'); 
		$('#tip-indice-specific').hide(); 
		statiivis =false;
	}
	else
	{
		$('#general').removeClass('active');
		$('#tip-indice-general').hide(); 
		$('#indice-specific').css('top', '30px'); 
		$('#tip-indice-specific').hide(); 
		statiivis =false;
	}
}


function tipIndiceSpecific(){
 	indiceSpecific = !indiceSpecific;
 	indiceGeneral = false;
	statiivis = false;
	statiiOrarvis = false;

	$('#general').removeClass('active');
	$('#zilnic-general').removeClass('active');
	$('#orar-general').removeClass('active');
	$('#zilnic-specific').removeClass('active');
	$('#orar-specific').removeClass('active');
	$('#'+mtypetoidorar(cmarkertype)).removeClass("active");
	$('#'+mtypetoid(cmarkertype)).removeClass("active");
	$('#indici-harta').hide(); 
	$('#indici-harta-orar').hide(); 
	if(indiceSpecific)
	{
		$('#specific').addClass('active');
		$('#tip-indice-general').hide(); 
		$('#indice-specific').css('top', '30px'); 
		$('#tip-indice-specific').show(); 
	}
	else
	{
		$('#specific').removeClass('active');
		$('#tip-indice-general').hide(); 
		$('#indice-specific').css('top', '30px'); 
		$('#tip-indice-specific').hide(); 
		statiivis = false;
	}
	
}

function statii(isSpecific) {
	$('statbutton').blur();
	statiivis = !statiivis;
	if (statiivis) {
		statiiOrarvis = false;
		$('#indici-harta').show(); 
		$('#indici-harta-orar').hide(); 
		$('#zilnic-specific').addClass('active');
		$('#orar-specific').removeClass('active');
		showmarkers(0, isSpecific, false);
	} else {
	 	$('#markers').html('');
		$('#indici-harta').hide(); 
		if (cmarkertype>0)
			hideindinfo(cmarkertype);
	}
}


function statiiOrar(isSpecific, isHourly) {
	$('#statbutton').blur();
	statiiOrarvis = !statiiOrarvis;
	if (statiiOrarvis) {
		statiivis = false;
		$('#indici-harta').hide(); 
		$('#indici-harta-orar').show(); 
		$('#zilnic-specific').removeClass('active');
		$('#orar-specific').addClass('active');
		showmarkers(0, isSpecific, isHourly);
	} else {
		$('#markers').html('');
		$('#indici-harta-orar').hide();
		if (cmarkertype>0)
			hideindinfo(cmarkertype);
	}
}

function statiiZilnic() {
	$('#statbutton').blur();
	statiiZilnicvis = !statiiZilnicvis;
	if (statiiZilnicvis) {
		showmarkers(-1);
		statiiOrarGeneralvis = false;
		
		$('#orar-general').removeClass('active');
		$('#zilnic-general').addClass('active');
	} else {
		$('#markers').html('');
		if (cmarkertype>0)
			hideindinfo(cmarkertype);
	}
}

function statiiOrarGeneral() {
	$('#statbutton').blur();
	statiiOrarGeneralvis = !statiiOrarGeneralvis;
	if (statiiOrarGeneralvis) {
		showmarkers(-2);
		statiiOrarGeneralvis = false;
		
		$('#orar-general').addClass('active');
		$('#zilnic-general').removeClass('active');
	} else {
		$('#markers').html('');
		if (cmarkertype>0)
			hideindinfo(cmarkertype);
	}
}

function centre() {
 	if (isIE())
		$('#centers').css('z-Index', '45');
	
	$('#'+mtypetoidorar(cmarkertype)).removeClass("active");
	$('#'+mtypetoid(cmarkertype)).removeClass("active");
	
	centrevis = !centrevis;
	if (centrevis) {
		$('#centrbutton').addClass('active');
		showcenters();
	} else {
		$('#centrbutton').removeClass('active');
		$('#centers').html('');
	}
	
	return false;
}

function indici(){
	$('#indice-specific').css('top', '30px');

	$('#'+mtypetoidorar(cmarkertype)).removeClass("active");
	$('#'+mtypetoid(cmarkertype)).removeClass("active");

	modIndice = !modIndice;

	if(modIndice) {
		$('#statbutton').addClass('active');
		rand = Math.round(Math.random()*1000000);

		startLoad();
		if($('#isResedinta').length>0 && $('#isResedinta').val() == 1) {
			$.get("get.php?zoom=3&id="+cid+"&type=0&isDaily=false&isHourly=false&rand="+rand, placemarkers);
		} else {
			$.get("get.php?zoom=2&id="+nid+"&type=0&isDaily=false&isHourly=false&rand="+rand, placemarkers);
		}
		stopLoad();

		$('#indice-general').show();
		$('#indice-specific').show();
	} else {
		$('#statbutton').removeClass('active');
		$('#markers').html('');

		hideIndici();
	}

	return false;
}
