var ajaxRequest;
function GetXmlHttpObject(){
	var xmlHttp=null;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){ // Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function XmlHttpSendGET(url){

	ajaxRequest.xmlHttp.onreadystatechange=ajaxRequest.stateChange;
	ajaxRequest.xmlHttp.open("GET",url,true);
	ajaxRequest.xmlHttp.send(null);
}

function XmlHttpSendPOST(url,params){
	ajaxRequest.xmlHttp.onreadystatechange=ajaxRequest.stateChange;
	ajaxRequest.xmlHttp.open("POST",url,true);
	ajaxRequest.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.xmlHttp.setRequestHeader("Content-length", params.length);
    ajaxRequest.xmlHttp.setRequestHeader("Connection", "close");

	ajaxRequest.xmlHttp.send(params);
}



function doUrl(getParams){
	return '/'+httpDirectory+'popPage.php?ajax=1&'+getParams;
}

function page(area){
	this.xmlHttp=GetXmlHttpObject();
	this.areaName=area;
	this.area=document.getElementById(area);


	this.getPage=function(str){
		if (this.xmlHttp==null){
			return false;
		}
		var url=doUrl('str='+str);

		XmlHttpSendGET(url);
		return true;
	}

	this.stateChange=function(){
		if(ajaxRequest.xmlHttp.readyState==4){
			var html=ajaxRequest.xmlHttp.responseText;

			if(!ajaxRequest.area)
				return;
			
			
			var time=fadeOut(ajaxRequest.areaName);
			
			setTimeout(function (){ajaxRequest.area.innerHTML=html;fadeIn(ajaxRequest.areaName);},time);
		}
	}
}

function BOX(area){
	this.xmlHttp=GetXmlHttpObject();
	this.areaName=area;
	this.area=document.getElementById(area);


	this.getBOX=function(module,method,addParams){
		if (this.xmlHttp==null){
			return false;
		}
		var url=doUrl('module='+module+'&method='+method+'&'+addParams);

		XmlHttpSendGET(url);
		return true;
	}

	this.stateChange=function(){
		if(ajaxRequest.xmlHttp.readyState==4){
			var html=ajaxRequest.xmlHttp.responseText;

			if(!ajaxRequest.area)
				return;
						
			ajaxRequest.area.innerHTML=html;;
		}
	}
}

function showUploadForm(){
	var div=document.getElementById('uploadBox')

	if(!div)
		return;

	ajaxRequest=new BOX('uploadBox');
	ajaxRequest.getBOX('upload','box2');
}

function getPage(str,area){
	ajaxRequest=new page(area);
	return !ajaxRequest.getPage(str);
}

function Rate(contentId,what){
	this.xmlHttp=GetXmlHttpObject();
	this.contentId=contentId
	this.what=what
	
	this.addRating=function(rating){
		if (this.xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		
		var url=doUrl('module='+this.what+'&method=addRating&id='+this.contentId+'&rate='+rating);

		XmlHttpSendGET(url);
	}

	this.stateChange=function(){
		if(ajaxRequest.xmlHttp.readyState==4){
			var nr=ajaxRequest.xmlHttp.responseText;
			if(nr>0){
				for(i=1;i<6;i++){
					var im=document.getElementById('star'+ajaxRequest.contentId+'_'+i)
					var starImage='';

					if(i==nr)
						starImage='_a';
					else if(i-1==parseInt(nr) && i-1<nr)
						starImage='_c';
					else if(i<nr)
						starImage='_a';
					im.src='/'+httpDirectory+'img/star1'+starImage+'.gif';
					im.style.cursor='default';
					starImages[i]=null;
				}
				/*alert(langvars['rated']);*/
			}
		}
	}
}

function RateComment(contentId,what){
	this.xmlHttp=GetXmlHttpObject();
	this.contentId=contentId
	this.what=what
	
	this.addRating=function(rate){
		if (this.xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		
		var url=doUrl('module='+this.what+'&method=rateComment&commentID='+this.contentId+'&rate='+rate);

		XmlHttpSendGET(url);
	}

	this.stateChange=function(){
		if(ajaxRequest.xmlHttp.readyState==4){
			var result=ajaxRequest.xmlHttp.responseText;
			/*alert(result);*/
		}
	}
}

function addComRating(contentID,rate){
	ajaxRequest=new RateComment(contentID,'content');
	ajaxRequest.addRating(rate);
}
function addComRating2(contentID,rate){
	ajaxRequest=new RateComment(contentID,'news');
	ajaxRequest.addRating(rate);
}


function Favourite(contentId,what){
	this.xmlHttp=GetXmlHttpObject();
	this.contentId=contentId
	this.what=what
	
	this.addRating=function(){
		if (this.xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		
		var url=doUrl('module='+this.what+'&method=addToFavourites&'+this.what+'ID='+this.contentId);

		XmlHttpSendGET(url);
	}
	
	this.remove=function(){
		if (this.xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		
		var url=doUrl('module='+this.what+'&method=removeFavourite&'+this.what+'ID='+this.contentId);

		XmlHttpSendGET(url);
	}

	this.stateChange=function(){
		if(ajaxRequest.xmlHttp.readyState==4){
			var result=ajaxRequest.xmlHttp.responseText;
			if(result)
				alert(result);
		}
	}
}

function addFavourite(contentID){
	c=getCookie('contentFavourites')
	if(c)
		var cc=doArray(c);

	if(cc && in_array(contentID,cc))
		return ;	

	ajaxRequest=new Favourite(contentID,'content');
	ajaxRequest.addRating();
	
}

function addFavourite2(newsID){
	c=getCookie('newsFavourites')
	if(c)
		var cc=doArray(c);

	if(cc && in_array(newsID,cc))
		return ;	

	ajaxRequest=new Favourite(newsID,'news');
	ajaxRequest.addRating();
}

function removeFavourite(contentID,div){
	c=getCookie('contentFavourites')
	

	ajaxRequest=new Favourite(contentID,'content');
	ajaxRequest.remove();
	div=document.getElementById('fav_'+contentID);
	if(div)
		div.style.display='none';
}

function removeFavourite2(newsID,div){
	c=getCookie('newsFavourites')


	ajaxRequest=new Favourite(newsID,'news');
	ajaxRequest.remove();
	div=document.getElementById(div);
	if(div)
		div.style.display='none';
}

function BoxListing(what){
	this.xmlHttp=GetXmlHttpObject();
	this.what=what
	this.sortBy='new';
	this.page=1;
	this.mode=0;
	
	this.getList=function(sortBy,page){
		if (this.xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		this.sortBy=sortBy;		
		this.page=page;
		
		var url=doUrl('module='+this.what+'Box&method=showList&sortBy='+sortBy+'&'+this.what+'bPage='+page);

		XmlHttpSendGET(url);
	}
	
	this.getNavBar=function(sortBy,page){
		if (this.xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url=doUrl('module='+this.what+'Box&method=navigation&'+this.what+'bPage='+page);

		XmlHttpSendGET(url);
	}

	this.stateChange=function(){
		if(ajaxRequest.xmlHttp.readyState==4){
			var result=ajaxRequest.xmlHttp.responseText;

			if(ajaxRequest.mode==1){
				var div=document.getElementById(ajaxRequest.what+'BlockFooter');
				if(!div)
					return;

				div.innerHTML=result;
			}
			else{
				var div=document.getElementById(ajaxRequest.what+'BlockListing');
				if(!div)
					return;
				div.innerHTML=result;
				
				var page=ajaxRequest.page
				ajaxRequest=new BoxListing(ajaxRequest.what);
				ajaxRequest.mode=1;
				ajaxRequest.getNavBar(page)
			}
		}
	}
}


function getBoxListing(what,sortBy,page,obj){

	if(obj=='left'){
		document.getElementById(what+'TabLeft').className='leftActive';
		document.getElementById(what+'TabRight').className='right';
		document.getElementById(what+'TabCenter').className='center';
	}
	else if(obj=='right'){
		document.getElementById(what+'TabLeft').className='left';
		document.getElementById(what+'TabRight').className='rightActive';
		document.getElementById(what+'TabCenter').className='center';
	}
	else if(obj=='center'){
		document.getElementById(what+'TabLeft').className='left';
		document.getElementById(what+'TabRight').className='right';
		document.getElementById(what+'TabCenter').className='centerActive';
	}

	ajaxRequest=new BoxListing(what);
	ajaxRequest.getList(sortBy,page);
}

function addRating(contentID,rate){
	c=getCookie('contentVotes')

	document.getElementById('show_vote').innerHTML = ' Täname hindamise eest';


	if(c)
		var rated=doArray(c);

	if(rated && in_array(contentID,rated)){

		return ;	
	}

	ajaxRequest=new Rate(contentID,'content');
	ajaxRequest.addRating(rate);
	
}

function addRating2(newsID,rate){
	document.getElementById('show_vote').innerHTML = ' Täname hindamise eest';
	c=getCookie('newsVotes')
	if(c)
		var rated=doArray(c);

	if(rated && in_array(newsID,rated))
		return ;	

	ajaxRequest=new Rate(newsID,'news');
	ajaxRequest.addRating(rate);
}


function Comments(){
	this.xmlHttp=GetXmlHttpObject();
	
	this.getComments=function(page,id,module){
		if (this.xmlHttp==null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		
		var url=doUrl('module='+module+'&method=comments&'+module+'ID='+id+'&page='+page);

		XmlHttpSendGET(url);
	}

	this.stateChange=function(){
		if(ajaxRequest.xmlHttp.readyState==4){
			var result=ajaxRequest.xmlHttp.responseText;
			var div=document.getElementById('commentContainer');
			if(!div)
				return;
			div.innerHTML=result;
		}
	}
}

function commentPage(page,id,module){
	ajaxRequest=new Comments();
	ajaxRequest.getComments(page,id,module);
}