// this file retireves an XML doc from xml_vote.php
// and parses its node values into the XHTML doc (inc_getDilemma.php)
// error count = 1


var gside, l, r, o, d, agree, total, feedback, oroffset, url, endoftheline;
	
function showLoader(){
		Element.setStyle('loader', {visibility: 'visible'});
		Element.setStyle('dilemma', {visibility: 'hidden'});
		Element.setStyle('or', {visibility: 'hidden'});

		// work on this to refresh google ads to reflect content
		// resetAds();
}
	
function hideLoader(){
	
	// no more dilemmas to see
	if(endoftheline == 1){
		Element.setStyle('dilemma', {visibility: 'visible'});
		Element.setStyle('loader', {visibility: 'hidden'});
		Element.setStyle('optionA', {visibility: 'hidden'});
		Element.setStyle('optionB', {visibility: 'hidden'});
		Element.setStyle('or', {visibility: 'hidden'});
		Element.setStyle('tags', {visibility: 'hidden'});
		Element.setStyle('link', {visibility: 'hidden'});		
	}else{
	
		$('submitl').value = 'Choose this one';
		$('submitr').value = 'Choose this one';
		//position 'or' element (aclled from within the img tag (see below)
		oroffset = ((Math.round(($('optionA').offsetHeight+$('optionB').offsetHeight)/4))-35)+$('optionA').offsetTop+'px';
		Element.setStyle('or', {top: oroffset});
		Element.setStyle('loader', {visibility: 'hidden'});
		Element.setStyle('dilemma', {visibility: 'visible'});
		Element.setStyle('or', {visibility: 'visible'});
		// round the corners the first time the page is loaded (requires niftycorners.js)
		if(side="b"){
			Nifty("div.overlay,div.option", "transparent");
		}
	}
}

function initUchoo(){ //only used first time screen is displayed
	showLoader();
	// parse the URL variables (used for linking to a specific dilemma
	var qs = location.search.substring(1);
	var nv = qs.split('&');
	var url_array = new Array();
	for(i = 0; i < nv.length; i++)
	{
		eq = nv[i].indexOf('=');
		url_array[nv[i].substring(0,eq).toLowerCase()] = unescape(nv[i].substring(eq + 1));
	}
	if(url_array['dilemma'] != undefined){
		var url = "xml_single.php?dilemma="+url_array['dilemma'];
	} else {
		var url = "xml_vote.php?start=1";
	}
	// set the side var so that the stats aren't displayed
	gside = "b";
	getXML(url);

}
	
function getVote(side){ //only used after user selects an option
		// determine which option has been chosen
	if(side=="l"){ // optionA
		$('submitl').value = 'Chosen';
		showLoader();
		gside = "l"; // set the side var with global scope
		var url = "xml_vote.php?o="+$F('lo')+"&d="+$F('ld');
	}else if(side=="r"){ //optionB
		$('submitr').value = 'Chosen';
		showLoader();
		gside = "r"; // set the side var with global scope
		var url = "xml_vote.php?o="+$F('ro')+"&d="+$F('rd');
	}else if(side=="b"){ // this shouldn't happen anymore (legacy way of calling getVote()
		alert('An error has occured \n error code: uchoo_getDil-01');
	}
	getXML(url);
}
    

// for updating the flash movie
function sendToFlash() {
	thisMovie("swf_feedback").sendText(feedback);
	}
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	} else {
		return document[movieName]
	}
}


function getXML(url){
	var xotree = new XML.ObjTree();
	var func = function ( tree ) {

		$('dilTitle').innerHTML = tree.dilemma.dilTitle;
		$('dilDesc').innerHTML = tree.dilemma.dilDesc;
		
		// display the options
		$('optionAtitle').innerHTML = tree.dilemma.option[0].opTitle;
		$('optionBtitle').innerHTML = tree.dilemma.option[1].opTitle;
		
	
		
		// option descriptions are optional 
		if(tree.dilemma.option[0].opDesc == undefined){
			$('optionAdesc').innerHTML = '';
		}else{
			$('optionAdesc').innerHTML = tree.dilemma.option[0].opDesc;
		}
		
		if(tree.dilemma.option[1].opDesc == undefined){
			$('optionBdesc').innerHTML = '';
		}else{
			$('optionBdesc').innerHTML = tree.dilemma.option[1].opDesc;
		}
				
		// pics are optional
		if(tree.dilemma.option[0].opPic == undefined){
			$('optionApic').innerHTML = '<img src="../images/shim.png" onLoad="hideLoader();" class="dilImage">';
		}else{
			$('optionApic').innerHTML = '<img src="'+tree.dilemma.option[0].opPic+'" onLoad="hideLoader();" class="dilImage">';	
		}
		if(tree.dilemma.option[1].opPic == undefined){
			$('optionBpic').innerHTML = '<img src="../images/shim.png" onLoad="hideLoader();" class="dilImage">';
		}else{
			$('optionBpic').innerHTML = '<img src="'+tree.dilemma.option[1].opPic+'" onLoad="hideLoader();" class="dilImage">';
		}		
		
		// set the hidden form fields for tracking
		$('lo').value = tree.dilemma.option[0].opNo;
		$('ld').value = tree.dilemma.dilid;
		$('ro').value = tree.dilemma.option[1].opNo;
		$('rd').value = tree.dilemma.dilid;
		
		// show the housekeeping info
		if(tree.dilemma.tags == undefined){
			$('tags').innerHTML = '<img src="../images/icon_tag.png" alt="Tags" class="icon" /> This user didn\'t tag their dilemma. Sorry.';
		}else{
			$('tags').innerHTML = '<img src="../images/icon_tag.png" alt="Tags" class="icon" />'+tree.dilemma.tags;
		}

		$('link').innerHTML = 'Link to this dilemma: <input type="text" size="45" onFocus="this.select();" value="http://www.uchoo.com/d/'+tree.dilemma.dilid+'">';
		
		
		// for stats
		if(gside != "b"){
			agree = tree.dilemma.pstats.agree;
			total = tree.dilemma.pstats.total;
			feedback = Math.round((agree/total)*10) +'';
			
			//feedback = feedback + '/10 people agree with your last choice';

		} else {
			feedback = -1;
		}
			
		
	sendToFlash();
	
	
	// redundant now flash is used
	// $('feedback').innerHTML = feedback;
	
		// hide things since there are no more dilemmas to see
	if(tree.dilemma.option[0].opTitle == 'End'){
		endoftheline = 1;
		
	}
	
	}
	
	// make the request
	xotree.parseHTTP( url, {}, func );
	// hide loader to display result
	
}