//Home scripts
var currentPosition = 0;
var maskWidth = 719;
var playIntervalId = 0;
var itemCount = 9;
var currentlyOpen = -1;

function gotoModule(position) {
	
	stopCurrent(currentPosition);
	pause();
	if ($('link0'+(Math.abs(currentPosition)))) {
		$('link0'+(Math.abs(currentPosition))).style.fontWeight = "normal";
	}
	currentPosition = -1*position;
	var scroller = document.getElementById("scroller");
	
	new Effect.Move(scroller, { x: currentPosition*maskWidth, y:0, mode:'absolute' });
	if ($('link0'+(Math.abs(currentPosition)))) {
		$('link0'+(Math.abs(currentPosition))).style.fontWeight = "bold";
	}

	return false;	
}

function stopCurrent(id) {
	if (Math.abs(id)>0) {
		if (window.document['player'+Math.abs(id)]) {
			window.document['player'+Math.abs(id)].sendEvent('STOP');
		}
	}
}



function play() {
	moveForward();
	playIntervalId = setInterval ( "moveForward()", 5000 );
}

function pause() {
	clearInterval ( playIntervalId );
}

function moveForward() {
	stopCurrent(currentPosition);
	pause();
	if ($('link0'+(Math.abs(currentPosition)))) {
		$('link0'+(Math.abs(currentPosition))).style.fontWeight = "normal";
	}

	var scroller = document.getElementById("scroller");
	scrollerWidth = parseInt(scroller.style.width.replace("px",""));
	if (((currentPosition-1)*maskWidth)+scrollerWidth>0) {
		currentPosition = currentPosition - 1;
		new Effect.Move(scroller, { x: currentPosition*maskWidth, y:0, mode:'absolute' });
	} else {
		currentPosition = 0;
		new Effect.Move(scroller, { x: currentPosition*maskWidth, y:0, mode:'absolute' });
	}
	if ($('link0'+(Math.abs(currentPosition)))) {
		$('link0'+(Math.abs(currentPosition))).style.fontWeight = "bold";
	}

	return false;
}

function moveBack() {
	stopCurrent(currentPosition);
	pause();
	if ($('link0'+(Math.abs(currentPosition)))) {
		$('link0'+(Math.abs(currentPosition))).style.fontWeight = "normal";
	}

	var scroller = document.getElementById("scroller");
	if ((currentPosition+1)<=0) {
		currentPosition = currentPosition + 1;
		new Effect.Move(scroller, { x: currentPosition*maskWidth, y:0, mode:'absolute' });
	} else {
		currentPosition = ((-1)*itemCount) + 1;
		new Effect.Move(scroller, { x: currentPosition*maskWidth, y:0, mode:'absolute' });
	}
	if ($('link0'+(Math.abs(currentPosition)))) {
		$('link0'+(Math.abs(currentPosition))).style.fontWeight = "bold";
	}

	return false;
}

//Quiz scripts
currentQuestionShowing = 1;

function showQuestion(id) {
	if (id>currentQuestionShowing) {
		$('question'+id).appear({duration: 1, from: 0, to: 1});
		currentQuestionShowing = id;
		$('q'+id).style.fontWeight="bold";
	}
}

//Checklist scripts
function addRemove(id) {

	if (modules[id]=="active") {
		$('module'+id).morph("background-color:#efefef;");
		$('module'+id+'Link').innerHTML = "Add";
		modules[id] = "inactive";
		changeModuleStatus(id, "inactive");
	} else {
		$('module'+id).morph("background-color:#ffcc00;");
		$('module'+id+'Link').innerHTML = "Remove";
		modules[id] = "active";
		changeModuleStatus(id, "active");
	}
}

//Misc functions
function showHide(id) {
	//close any open divs
	if ((currentlyOpen>-1)&&(currentlyOpen!=id)) {
		div = document.getElementById("content_"+currentlyOpen);
		div.style.display = "none";
	}
	
	div = document.getElementById("content_"+id);
	if (div.style.display=="none") {
		div.style.display = "block";
		currentlyOpen = id;
	} else {
		div.style.display = "none";
		currentlyOpen = -1;
	}
}

function changeModuleStatus(id, change) {
	var xmlHttp;
	try {
  		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {
			//document.getElementById(type+"CountryDiv").innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","/en/change_status.php?id="+id+"&change="+change,true);
	xmlHttp.send(null);
}

function completeTask(ta_id, me_id, module_no) {
	var xmlHttp;
	try {
  		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {
			//document.getElementById(type+"CountryDiv").innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","/en/complete_task.php?ta_id="+ta_id+"&me_id="+me_id+"&module_no="+module_no,true);
	xmlHttp.send(null);
	element = document.getElementById("task_"+ta_id);
	if (tasks[ta_id]=="complete") {
		element.src="/en/images/misc/task_incomplete.gif";
		tasks[ta_id]="incomplete";
	} else {
		element.src="/en/images/misc/task_complete.gif";
		tasks[ta_id]="complete";
	}
	if (document.getElementById("moduleBox")) {
		var complete = 0;
		var total = 0;
		for (task in tasks) {
			if (tasks[task]=="complete") {
				complete = complete+1;
			}
			if ((tasks[task]=="complete")||(tasks[task]=="incomplete")) {
				total = total+1;
			}
		}
		moduleBox = document.getElementById("moduleBox");
		if (complete==0) {
			moduleBox.style.backgroundImage="url('/en/images/misc/step_module_notstarted.jpg')";
		}
		if (complete==total) {
			moduleBox.style.backgroundImage="url('/en/images/misc/step_module_complete.jpg')";
		}
		if ((complete<total)&&(complete>0)) {
			moduleBox.style.backgroundImage="url('/en/images/misc/step_module.jpg')";
		}				
	}
}

function getAnchor() {
	var url = document.location.toString();
	var anchor = url.substring((url.lastIndexOf("#")+1));
	return anchor.toLowerCase();
}

var endDate=new Date(2012,11,31,00,00,00);
var dateTMR=0;

function countDown() {
	var now=new Date();
	var t=new Date(endDate-now);
	
	if(t>0) {
		var dd = t/1000/60/60/24+'.'; dd=dd.replace(/([^.]*)[.][\s\S]*/,"$1");
		var mm = t.getMonth();
		var yy = t.getFullYear()-1970;
		var hh = t.getHours()-2;
		var mi = t.getMinutes();
		var ss = t.getSeconds();
		var ms = t.getMilliseconds();
		document.getElementById('days').innerHTML=dd+" Days";
		document.getElementById('time').innerHTML=hh+"h "+mi+"m "+ss+"s";
	} else {
		document.getElementById('days').innerHTML="00:00:00:00.0";
		clearInterval(dateTMR);
	}
  }


