var shownInfo = 0;

function showInfo(id) {
	if (shownInfo!=0 && shownInfo!=id) {
		hideInfo(shownInfo);
	}
	document.getElementById('info_'+id).style.display = 'Block';
	shownInfo = id;
}

function hideInfo(id) {
	document.getElementById('info_'+id).style.display = 'None';
}

function searchSubmit() {
	document.getElementById('query_submit').disabled = true;
}

var sizeVote = 5;

function vote(val, count, type, id, permit_vote) {

	var html = '<div class="vote"><table>';

	// Show current rank
	html = html + '<tr><td>';


	for (i=1;i<=sizeVote;i++) {

		if (i<=val) {
			img = 'a';
		} else {
			img = 'n';
		}

		html = html + '<img src="/images/stars/' + img + '.gif" width="15" height="15">';
	}

	html = html + '</td><td class="vote_stat more">';

	stat = 'You can make first vote!';

	if (count>0) {
		stat = 'Voted <b>'+count+'</b> time(s)';
	}

	html = html + stat + '</td></tr>';

	// Show user's choice
	if (permit_vote == true) {
		html = html + '<tr><td id="vote_status" colspan="2">';

		img = 'n';

		for (i=1;i<=sizeVote;i++) {

			html = html + '<div class="img_n" id="' + type + '_' + id + '_' + i + '" onmouseout="javascript:setVote(\'' + type +'\', ' + id + ', 0);" onmouseover="javascript:setVote(\'' + type +'\', ' + id + ', ' + i + ');" ><a href="javascript:execVote(\'' + type +'\', ' + id + ', ' + i + ');"><img src="/images/1x1.gif" width="15" height="15" border="0"></a></div>';
		}

		html = html + '</td></tr>';
	}

	// Finish
	html = html + '</table></div>';

	document.write(html);
}

function setVote(type, id, n) {

	img = 'a';

	for (i=1; i<=n; i++) {
		document.getElementById(type + '_' + id + '_' + i).className = 'img_'+img;
	}

	img = 'n';

	for (i=n+1; i<=sizeVote; i++) {
		document.getElementById(type + '_' + id + '_' + i).className = 'img_'+img;
	}
}

function execVote(type, id, val)
{
	vote_status_places = document.getElementById('vote_status');

	if (type == 'photo') {
		getXML('/casting/xml/vote/'+id+'/'+val+'/', 'vote_parse_result', 'vote_waiting');
	}
}

function vote_waiting()
{
	vote_status_places = document.getElementById('vote_status');
	vote_status_places.innerHTML = '<img src="/images/loading.gif" width="76" height="8">';
}

function vote_parse_result()
{
	var status 	= getElementTextNS('status', XMLResult, 0);

	if (status == 'yes') {
		message = 'Thanks for voting!';
	} else {
		message = 'Voting failed';
	}

	vote_status_places.innerHTML = '<span class="vote_'+status+'">'+message+'</span>';
}

var XMLResult;

function getXML(url, callback_function, waiting_function) {

	var xmlHttpRequest;


	try {
		xmlHttpRequest = new XMLHttpRequest();
		if (xmlHttpRequest.overrideMimeType) {
			xmlHttpRequest.overrideMimeType('text/xml');
		}
	} catch (exception) {
		xmlHttpRequest = false;
	}


	if (!xmlHttpRequest || typeof XMLHttpRequest == 'undefined') {
		if (window.ActiveXObject) {

			var a_objects = [
			'MSXML2.XMLHTTP.6.0',
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP'
			];
			for (var i = 0, length = a_objects.length; i < length; ++i) {
				try {
					xmlHttpRequest = new ActiveXObject(a_objects[i]);
					break;
				} catch(e) {}
			}
		}
	}

	var result = false;

	xmlHttpRequest.onreadystatechange = function(){

		if (xmlHttpRequest.readyState == 4){
			if(xmlHttpRequest.status == 200 && xmlHttpRequest.responseXML != null) {

				XMLResult = xmlHttpRequest.responseXML;

				if (callback_function!='') {
					eval(callback_function+'();');
				}
			}
		} else {
			if (waiting_function!='') {
				eval(waiting_function+'();');
			}
		}

	}

	if (xmlHttpRequest) {

		xmlHttpRequest.open("GET", url, true);
		xmlHttpRequest.send(null);
	}

	if (waiting_function!='') {
		eval(waiting_function+'();');
	}
}

function getElementTextNS(local, parentElem, index) {
	var result = "";
	result = parentElem.getElementsByTagName(local)[index];

	if (result) {
		if (result.childNodes.length > 1) {
			return result.childNodes[1].nodeValue;
		} else {
			return result.firstChild.nodeValue;
		}
	} else {
		return "n/a";
	}
}
