var xmlhttpIndex;

function parseJavascript(){
	//alert("function parse start");
	script=document.getElementById("container").getElementsByTagName("script");
	//alert(script.length);
	/*tag=new Array();
	s=0;
	call=new Array();
	t=0;*/
	//alert("for");
	for(i=0;i<script.length-1;i++){
		//alert("script no"+(i+1));
		//alert(script[i+1].text);
		if(script[i+1].text!=null){ 
			//alert("script with text");
			//alert(script[i+1].text);
			eval(script[i+1].text);
			/*call[t]=script[i+1].text;
			t++;*/
		}
		/*if(script[i+1].hasAttribute("src")){
			//alert("script with src");
			src=script[i+1].getAttribute("src");
			//alert(src);
			tag=document.createElement("script");
			tag.setAttribute("type","text/javascript");
			tag.setAttribute("src",src);
			document.getElementsByTagName("head")[0].appendChild(tag);
			//tag[s]=document.createElement("script");
			//tag[s].setAttribute("type","text/javascript");
			//tag[s].setAttribute("src",src);
			//s++;
			alert("script tag created: "+src);
		}*/
	}
	//alert("endfor");
	//alert("for2");
	//alert(tag.length);
	/*for(j=0;j<tag.length;j++){
		document.getElementsByTagName("head")[0].appendChild(tag[j]);	
	}*/
	//alert(j);
	//alert("endfor2");
	//alert("for3");
	//alert(call.length);
	/*for(k=0;k<call.length;k++){
		//alert(k);
		setTimeout("eval(call["+k+"])", 10);	
	}*/
	//alert(k);
	//alert("endfor3");
	//alert("function parse end");
}

function parseJavascriptMenu(){
	scriptm=document.getElementById("header").getElementsByTagName("script");
	//alert(scriptm.length);
	for(i=-1;i<scriptm.length-1;i++){
		if(scriptm[i+1].text!=null){ 
			//alert(scriptm[i+1].text);
			eval(scriptm[i+1].text);
		}
	}
}

function clearContent(id){
	document.getElementById(id).innerHTML='';
}

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject){// code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function getIndex(){
	xmlhttpIndex=GetXmlHttpObject();
	
	if (xmlhttpIndex==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="getIndex.php";
	url=url+"?sid="+Math.random();
	xmlhttpIndex.onreadystatechange=function(){
		if (xmlhttpIndex.readyState!=4){
			document.getElementById("container").innerHTML="<div class='load'></div>";
		}
		if (xmlhttpIndex.readyState==4){
			document.getElementById("container").innerHTML=xmlhttpIndex.responseText;
			//alert("parse start");
			parseJavascript();
			//alert("parse end");
		}
	}
	xmlhttpIndex.open("GET",url,true);
	xmlhttpIndex.send(null);
}

function getIndexMenu(){
	xmlhttpIndexMenu=GetXmlHttpObject();
	
	if (xmlhttpIndexMenu==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="getIndexMenu.php";
	url=url+"?sid="+Math.random();
	xmlhttpIndexMenu.onreadystatechange=function(){
		if (xmlhttpIndexMenu.readyState!=4){
			document.getElementById("header").innerHTML="<div class='load'></div>";
		}
		if (xmlhttpIndexMenu.readyState==4){
			document.getElementById("header").innerHTML=xmlhttpIndexMenu.responseText;
			//alert("parse start");
			parseJavascriptMenu();
			//alert("parse end");
		}
	}
	xmlhttpIndexMenu.open("GET",url,true);
	xmlhttpIndexMenu.send(null);
}

function clock(){
	
	if(typeof(window['clock_timeout'])!="undefined"){//if an auto-refresh timer is set, unset it and set another one (the same) again (down)
		clearTimeout(window['clock_timeout']);
	}
	
	var date=new Date();
	
	var hour=date.getHours();
	var minute=date.getMinutes();
	var second=date.getSeconds();
	var day=date.getDate();
	var month=date.getMonth();
	var year=date.getFullYear();
	
	hour=checkClock(hour);
	minute=checkClock(minute);
	second=checkClock(second);
	day=checkClock(day);
	month+=1;//java script return values from 0 to 11 for months
	month=checkClock(month);
	year=checkClock(year);
	
	if(document.getElementById('clock')!=null)
		document.getElementById('clock').innerHTML=hour+":"+minute+":"+second+", "+day+"."+month+"."+year;
	clock_timeout=setTimeout('clock()', 500);
}
	
function checkClock(i){
	if(i<10)
	  i="0"+i;
	return i;
}

function hoverNotifications(){
	if(document.getElementById('notifications_popup')!=null&&document.getElementById('notifications_popup').style.display!="none")
		document.getElementById('notifications_popup').setAttribute('hovered', '1');
}

function removeHoverNotifications(){
	if(document.getElementById('notifications_popup')!=null&&document.getElementById('notifications_popup').style.display!="none")
		document.getElementById('notifications_popup').removeAttribute('hovered');
}

function checkIfNotificationsHovered(){
	if(document.getElementById('notifications_popup')!=null&&document.getElementById('notifications_popup').style.display!="none"&&document.getElementById("notifications")!=null)//last one onle because menu is also loaded with ajax from some pages
		if(!document.getElementById('notifications_popup').hasAttribute('hovered')) 
			hideNotifications();
}

function checkNewNotifications(){
	
	xmlhttpIndex=GetXmlHttpObject();
	
	if (xmlhttpIndex==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="igluu/notifications/checkNewNotifications.php";
	url=url+"?sid="+Math.random();
	
	xmlhttpIndex.open("GET",url,false);
	xmlhttpIndex.send(null);
	
	if(xmlhttpIndex.responseText==0)
		document.getElementById("notifications").firstChild.className="ImagesNotifications";
	else
		document.getElementById("notifications").firstChild.className="ImagesNotificationsNew";
}

function showNotifications(p_st){
	document.getElementById("notifications_popup").style.display="block";
	document.getElementById('notifications_popup').setAttribute('hovered', '1');
	if(document.getElementById("notifications").firstChild.getAttribute("onclick")=="hideNotifications()")
		document.getElementById("notifications").firstChild.setAttribute("onclick", "showNotifications()");
	else
		document.getElementById("notifications").firstChild.setAttribute("onclick", "hideNotifications()");
																									  
	
	xmlhttpIndex=GetXmlHttpObject();
	
	if (xmlhttpIndex==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="igluu/notifications/showNotifications.php";
	url=url+"?sid="+Math.random();
	
	if(p_st==undefined)//if function was called without the pagination start parameter, set it to 0 (e.g.: show first page)
		p_st=0;
	url=url+"&p_st="+p_st;
	
	xmlhttpIndex.onreadystatechange=function(){
		if (xmlhttpIndex.readyState!=4){
			document.getElementById("notifications_popup").innerHTML="<div class='load'></div>";
		}
		if (xmlhttpIndex.readyState==4){
			document.getElementById("notifications_popup").innerHTML=xmlhttpIndex.responseText;
			checkNewNotifications();
		}
	}
	xmlhttpIndex.open("GET",url,true);
	xmlhttpIndex.send(null);
}

function viewNotification(id,p_st){//notification's id and last page viewed from notifications where to go when the user clicks the "back" button
	document.getElementById("notifications_popup").style.display="block";
	
	xmlhttpIndex=GetXmlHttpObject();
	
	if (xmlhttpIndex==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="igluu/notifications/viewNotification.php";
	url=url+"?id="+id+"&p_st="+p_st+"&sid="+Math.random();
	
	xmlhttpIndex.onreadystatechange=function(){
		if (xmlhttpIndex.readyState!=4){
			document.getElementById("notifications_popup").innerHTML="<div class='load'></div>";
		}
		if (xmlhttpIndex.readyState==4){
			document.getElementById("notifications_popup").innerHTML=xmlhttpIndex.responseText;
			checkNewNotifications();
		}
	}
	xmlhttpIndex.open("GET",url,true);
	xmlhttpIndex.send(null);
}

function deleteNotification(id,p_st){//notification's id and last page viewed from notifications where to go after delete
	document.getElementById("notifications_popup").style.display="block";
	
	xmlhttpIndex=GetXmlHttpObject();
	
	if (xmlhttpIndex==null){
		alert ("Browser does not support HTTP Request");
	  	return;
	}
	
	var url="igluu/notifications/deleteNotification.php";
	url=url+"?id="+id+"&sid="+Math.random();
	
	xmlhttpIndex.onreadystatechange=function(){
		if (xmlhttpIndex.readyState!=4){
			document.getElementById("notifications_popup").innerHTML="<div class='load'></div>";
		}
		if (xmlhttpIndex.readyState==4){
			if(xmlhttpIndex.responseText==1)
				showNotifications(p_st);
			else
				document.getElementById("notifications_popup").innerHTML=xmlhttpIndex.responseText;
			checkNewNotifications();
		}
	}
	xmlhttpIndex.open("GET",url,true);
	xmlhttpIndex.send(null);
}

function hideNotifications(){
	//document.getElementById("notifications_popup").style.display="none";
	$("#notifications_popup").slideUp("fast");
	document.getElementById("notifications").firstChild.setAttribute("onclick", "showNotifications()");
}
