// handles rating images

var savedValues = new Array(0,0,0);
var itemid = 0;
var modalBackgroundWindow = 0;

var g_sortorder = "down";
var g_sort = "name";
var g_location = "";

function mousemove_rating(event) {
	// mozilla || internet explorer
	value = event.layerX || event.clientX;
	// mozilla || internet explorer
	target = event.target || event.srcElement;
	value = _getCoordinate(event);
	target.style.backgroundPosition	= "0px -" + value + "px";
}

function mouseout_rating(event, index) {
	// mozilla || internet explorer
	target = event.target || event.srcElement;
	target.style.backgroundPosition	= "0px -" + savedValues[index] + "px";
}

function mouseclick_rating(event, index) {
	savedValues[index] = _getCoordinate(event);	
}

function _getCoordinate(event) {
	value = event.layerX || event.clientX;
	if(event.layerX) {
		return Math.floor((value+14)/15)*15;
	} else {
		target = event.srcElement;
		offset = target.style.left;
		offset = offset.replace("px", "");
		return Math.floor((value-offset)/15)*15-345;
	}
}

function show_contest() {
	var div = document.getElementById('divContest');
	div.style.display = "inline";
}

function super_close() {
	close_contest();
	close_rating_result();
	close_rating(true);
	close_info(true);
}

function close_contest() {
	var div = document.getElementById('divContest');
	div.style.display = "none";
}

function close_info(closeModal) {
	var div = document.getElementById('divInfo');
	if(div) {
		div.style.display = "none";
		if(closeModal)
			modalBackgroundWindow.style.display = "none";
	}
}

function close_rating(closeModal) {
	var div = document.getElementById('divEnterRating');
	if(div) {
		div.style.display = "none";
		if(closeModal)
			modalBackgroundWindow.style.display = "none";
		div = document.getElementById('divResultRating');
		div.style.display = "none";
	}
}

function show_info(id, event) {
	var div = document.getElementById('divInfo');
	if(div) {
		itemid = id;
		modalBackgroundWindow.style.display = "block";
		div.style.display = "block";
		ajaxUpdate('divInfoHead','ajax/nameAndInfo.php?id='+itemid+'&type=info');
		get_comments();
	}
}
function show_rating(id, event) {
	var div = document.getElementById('divEnterRating');
	if(div) {
		itemid = id;
		savedValues = new Array(0,0,0);
		
//		var x = 321;
//		var y = 303;
//		var y = event.clientY-150 + document.body.scrollTop;
//		y = y>10+document.body.scrollTop?y:document.body.scrollTop+10;
//		y = y+350>document.body.scrollTop+document.body.clientHeight?document.body.scrollTop+document.body.clientHeight-350:y;
//		div.style.top = y;
//		var x = event.clientX-50;
//		x = x>10?x:10;
//		div.style.left = x;
		
		document.getElementById('score1').style.backgroundPosition	= "0px 0px"
		document.getElementById('score2').style.backgroundPosition	= "0px 0px"
		document.getElementById('score3').style.backgroundPosition	= "0px 0px"

		document.getElementById('comment').value = "";
		
		modalBackgroundWindow.style.display = "block";
		div.style.display = "block";
		ajaxUpdate('divVoteName','ajax/nameAndInfo.php?id='+itemid+'&type=rating');
		get_comments();
	}
}

function show_rating_result() {
	var div1 = document.getElementById('divResultRating');
	var div2 = document.getElementById('divEnterRating');
	if(div1) {
		var result = document.getElementById('result');
		result.innerHTML = "sparar röst och eventuell kommentar...";
		div1.style.top = div2.style.top;
		div1.style.left = div2.style.left;
		div1.style.display = "block";	
	}
}

function close_rating_result() {
	var div = document.getElementById('divResultRating');
	if(div) {
		modalBackgroundWindow.style.display = "none";
		div.style.display = "none";
	}
}

function get_comments() {
	ajaxUpdate('commentslist','ajax/getComments.php?id='+itemid);
}


function save_rating() {
	var comment = escape(document.getElementById('comment').value);
	var username = escape(document.getElementById('username').value);
	var email = escape(document.getElementById('email').value);
	var v1 = Math.floor((savedValues[0])/15);
	var v2 = Math.floor((savedValues[1])/15);
	var v3 = Math.floor((savedValues[2])/15);
	ajaxUpdate("result",'ajax/saveRating.php?id='+itemid+"&userid="+userid+"&comment="+comment+"&v1="+v1+"&v2="+v2+"&v3="+v3+"&username="+username+"&email="+email);
}

function init_rating() {
	modalBackgroundWindow = document.createElement("div");
	modalBackgroundWindow.id = "modalBackground";
	modalBackgroundWindow.onclick = super_close;
	document.body.appendChild(modalBackgroundWindow);
}

function set_location_filter(name) {
	var item = document.getElementById('location_filter');
	item.value = name;
	main_update2(name)
}

function init_location_filter() {
	var item = document.getElementById('location_filter');
	item.value = '';
	item.focus();
}

function top_update() {
	ajaxUpdate('toplisttable','ajax/getList.php?sort=rating&sortorder=up&count=5&type=toplist');
}

function main_update(sortorder, sort, location) {
	g_sortorder = sortorder;
	g_sort = sort;
	g_location = location;
	ajaxUpdate('mainlisttable','ajax/getList.php?sort='+sort+'&sortorder='+sortorder+'&location='+escape(location)+'&type=mainlist');
}

function main_update2(location) {
	main_update(g_sortorder, g_sort, location);
}

function main_update3() {
	main_update(g_sortorder, g_sort, g_location);
}
