var T = {
	timeout : 0,
	fx : null,
	
	init : function(){
		for (var i =0 ; i < PE.photos.length; i++)
			Event.observe(document.getElementById('votes_'+i), 'mouseout', T.resetStars , false)
	},
	
	scrolldone : function(){
		window.clearTimeout(T.timeout);
		
		T.timeout = window.setTimeout(T.doscroll, 300);
	},
	
	doscroll : function(){
		var scrollY = 0;
		if (document.all)
		{
		   if (!document.documentElement.scrollTop)
		      scrollY = document.body.scrollTop;
		   else
		      scrollY = document.documentElement.scrollTop;
		}   
		else
		{
		   scrollY = window.pageYOffset;
		}
		
		var h = Math.max(0,scrollY-130);
		
		if (T.fx == null){
			T.fx = new fx.Height(document.getElementById('floatSpacer'), {duration: 300}); 
		}
		
		var c = parseInt(document.getElementById('floatSpacer').style.height);
		T.fx.custom(c, h);
		//document.getElementById('floatSpacer').style.height = h+"px";
	},
	
	locationCheckboxChanged : function(){
		var cont = document.getElementById('location_continent').value;
		var loc_count = document.getElementById('location_country');
		
		T.removeChildren(loc_count);
		loc_count.disabled = true;
		if (cont == '') {
			return;
		}
		
		document.getElementById('loading').style.display = "";
		var url = '/interface/lookup/country-by-continent.php?continent='+cont;
		
		httpgetter_doget(url, T.ajaxLookupCallback);
		
	},
	
	removeChildren : function(node){
		while (node.childNodes.length > 0){
				node.removeChild(node.childNodes[0]);
		}
		
		node.appendChild(T.makeOption('','Entire continent'));
	},
	
	ajaxLookupCallback : function(result){
		var data = eval("("+result+")");
		
		document.getElementById('loading').style.display = "none";
		
		
		var loc_count = document.getElementById('location_country');
		loc_count.disabled = false;
		
		for (var  i=0; i < data.length; i++){
			loc_count.appendChild(T.makeOption(data[i]['countrycode'], data[i]['countryname']));
		}
		
	},
	
	makeOption : function(value, name){
		var o = document.createElement('option');
		o.setAttribute('value', value);
		o.innerHTML = name;
		
		return o;
	},
	
	showSearch : function(){
		document.getElementById('searchbutton').style.display = "none";
		document.getElementById('searchform').style.display = "";
	},
	
	updateVote : function(idx, score){

		if (!T.checkLogin()) return;

		T.setScore(idx,score);
		
		var span =document.getElementById('thanks_'+idx)
		span.className = 'thanks';
		span.innerHTML = 'Thanks for the vote!';
		
		//AJAX
		
		var url = '/interface/vote/set.php';
		
		http = GXmlHttp.create();
		http.onreadystatechange = function() {T.ajax_statechange};

		var item = PE.photos[idx];
		var data = {'item_id': item.item_id, 'vote' : score};
		PE.photos[idx]['yourvote'] = score;
		data = JSON.stringify(data);

		http.open('POST',url, true);
		http.send(data);
	},
	
	ajax_statechange : function(s){
		
	},
	
	hilightStar : function(s, idx, score){
		T.lastHighlight_idx = idx;
		T.lastHighlight_span = s.parentNode;		
		T.setScore(idx,score);
	},
	
	checkLogin : function(){
		if (MS.user == null){
			MS.loginOrSignup();
			return false;
		} else {
			return true;
		}
	},
	
	/*
	*	For score rollovers
	*/
	lastHighlight_idx : null,
	lastHighlight_span : null,
	
	resetStars : function(e){
		
		if (T.lastHighlight_idx == null) return;

		
		//if (!e) var e = window.event;
		var target = e.relatedTarget || e.toElement;
		
		if (target.parentNode == T.lastHighlight_span) return;
		
		
		var idx = T.lastHighlight_idx;
		var item = PE.photos[idx];
		var score = item['yourvote'];
		
		if (score == null) score =0;
		
		T.setScore(idx, score);
		T.lastHighlight_idx = null;
	},
	
	setScore : function(idx, score){
		var span = document.getElementById('votes_'+idx);
		var stars = span.childNodes;
		
		for (var  i =1; i <= 5; i ++){
			if (i <= score){
				stars[i-1].src = '/images/stars/star.gif';
				stars[i-1].oldsrc = '/images/stars/star.gif';
			} else {
				stars[i-1].src = '/images/stars/starun.gif';
				stars[i-1].oldsrc = '/images/stars/starun.gif';
			}
		}
	}
	
	
}

window.onscroll = T.scrolldone;