/**
*	Scripts for USO Quest. Timers and more 
* @author Alex Eftimie
*/

// Global settings
var
	CHALLENGE_TIMER_INTERVAL = 1000,
	CHALLENGE_FORM = 'challenge_form',
	CHALLENGE_MSG = 'Timpul a expirat!\n Formularul va fi trimis automat.';
	

/** Initiates the timer.
*/
function init( id_element, seconds )
{
	timp = seconds2time( seconds );
	document.getElementById( id_element ).innerHTML = timp;
	update( id_element );
}

/** Updates the timer. On 00:00 calls force_submit()
*/
function update( id_element )
{
	timp = document.getElementById( id_element ).innerHTML;
	realt = time2seconds( timp.toString() ) - 1;
	timp = seconds2time( realt );
	document.getElementById( id_element ).innerHTML = timp;

	// Some fancy highlighting AE:disabled because of background mismatch
	//fancy ( realt );
	// Timer update
	if(realt > 0)
		setTimeout( "update('"+id_element+"')", CHALLENGE_TIMER_INTERVAL );
	else
		force_submit();
}

/** Submit the form when time has passed.
*/
function force_submit()
{
	alert( CHALLENGE_MSG );
	document.getElementById( CHALLENGE_FORM ).submit();
}

/** Switch the background color from RED to WHITE
*/
function fancy( time )
{
	if ( time < 6 ) {
		if ( time % 2 == 0 )
			document.body.style.backgroundColor = 'white';
		else
			document.body.style.backgroundColor = 'red';
	}
}

/** Convertor
*/
function time2seconds( time )
{
	parti = time.split(':');
	if(parti.length < 2) {parti.push(0);}
	return eval(parti[0]*60)+eval(parti[1]);
}

/** Convertor
*/
function seconds2time( seconds )
{
	seconds = seconds%3600;
	minute = Math.floor(seconds/60);
	secunde = seconds%60;
	if(minute<10) minute ='0'+minute;
	if(secunde<10) secunde = '0'+secunde;
	return minute+':'+secunde;
}

function explodeArray(item,delimiter) {
	tempArray=new Array(1);
	var Count=0;
	var tempString=new String(item);
	while (tempString.indexOf(delimiter)>0) {
		tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
		tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
		Count=Count+1
	}
	tempArray[Count]=tempString;
	return tempArray;
}


function setCookie(id) {	
	$.cookie(id, 1);
}

function delCookie(id) {
	$.cookie(id, 0);
}
function checkstatus() {
	y = new Array();
	y[0] = "qotd";
	y[1] = "pro";
	y[2] = "quest";
	y[3] = "wquest";
	y[4] = "tp";
	y[5] = "cl";
	y[6] = "leg";
	y[7] = "bv";
	y[8] = "cu";
	y[9] = "sspnd_chall";
	y[10] = "sspnd_qotd";
	y[11] = "sspnd_quest";
	y[12] = "sspnd_wquest";
	y[13] = "sspnd_gen";
	for ( j=0;j<y.length;j++ ) {
		ok = 0;
		if ( $.cookie(y[j]) == 1 ) {
			$(document).ready(function(){
					$("#"+y[j]).hide();	
					$("#"+y[j]+"h").hide();									
			});	
			ok = 1;
			
		}			
		if ( ok == 0 ) {
			$(document).ready(function(){
					$("#"+y[j]+"s").hide();
			});	
		}			
	}	
}
$(document).ready(function(){
	checkstatus();
});

function ascunde(id) {
	setCookie(id);
	$(document).ready(function(){
	   $("#"+id).hide("slow");
	   $("#"+id+"h").hide("slow");
	   $("#"+id+"s").show("slow");	 
	   return false;  
	});
}

function arata(id) {	
	delCookie(id);
	$(document).ready(function(){
	   $("#"+id+"s").hide("slow");
	   $("#"+id+"h").show("slow");
	   $("#"+id).show("slow");
	   return false;
	});
}
/** Scripturi pentru autocomplete. */
/* Format item:
row contine (din search.php):
	0 - Nume Prenume
	1 - Puncte
	2 - Nivel (nume)
	3 - Nivel (numar)
	4 - Nivel (icon)
	5 - Locatie search.php
*/ 
function formatItem( row ) {
	var str = location.pathname;
	str = str.replace(row[5],"");
	str = str.replace(/[^\/]/g,"");
	str = str.replace(/\//g,"../");
	
	return row[0] + "<br /><img align=\"absmiddle\" src=\""+ 
		str+row[4]+"\" /> "+
		"<small><i>" + row[2] + " (" + row[3] + ") " +
		row[1] + " puncte</small></i>";
}
function selectItem(li) {
	$("#nume_user").focus();
}
