/*
 * custom jrso js functions
*/
function jrsoJs()
{

//set the request object
var http = createRequestObject();

//for handling the gutters bone answers
var boneAns = false;
var setAns = false;

/*
 * jquery on ready code
*/
$(document).ready(function(){
	
	/*
	 * hide all objects with the class hide
	*/
	$('.hide').hide();
	
	/*
	 * set the fancy box code
	*/
	$('.showhover').fancybox();
	
	/*
	 * set the choose article button to choose an article, bound in a function to be recalled
	*/
	function setArticleButton()
		{
		$('#article_button').click(function(e){
			e.preventDefault();
			//set the start address if not already set
			var startAddress = ($(this).attr('href') == '#' ? window.location.href : $(this).attr('href'));
			window.location.href = startAddress + '&article='+$('#choose_article').attr('value');
			});
		}
	setArticleButton();
	/*
	 * invoked whena region is selected in the members only news section
	*/
	$('#select_region').click(function(e){
		e.preventDefault();
		//post the selected region and grab the 
		$('#regions_form').ajaxSubmit(function(data){
			$('#select_newsarticle').html(data);
			$('#select_newsarticle').show();
			setArticleButton();
			});
		});
	
	/*
	 Set the height for the box border
	*/
	$('#box_mid').each(function(){
		//grab the height of the container and match the height
		var bHeight = $(this).parent().find('#box_content_inner').height();
		$(this).height(bHeight+60);
	});
	
	/*
	 * sets the print button - used in the UN Section
	*/
	$('#print_page').click(function(e){
		e.preventDefault();
		window.print();
	});
	
	/*
	 * set the green corss game button
	*/
	$('#greencross_street_sense').fancybox({
		'type':'swf',
		'autoScale': false,
		width:800,
		height:600,
		'padding':0,
		'titleShow':false,
		'autoDimensions':false
		
		});
	
});








/*
 * checks the browser and creates an appropriate request object
*/
function createRequestObject() 
{
var requestObject;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
	{
    requestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
else
	{
    requestObject = new XMLHttpRequest();
    }
return requestObject;
}

/*
 * starts the login form
*/
function selectCharacter(character)
{
//alert("select character running: " + character);
var theForm = document.forms.login1;
var theElement = theForm.elements.character;
//set the form field value
theElement.value = character;
theForm.submit();
}
this.selectCharacter = selectCharacter;

/*
 * centres the navigation
*/
function centreNav()
{
var theNav = document.getElementById('default_subnav');
var theHolder = document.getElementById('nav_container');
theHolder.style.width = theNav.offsetWidth + 'px';
//alert("here is the nav: " + theNav.offsetWidth);
}
this.centreNav = centreNav;


/*
 * sets the beckground image to a set path
*/
function setBackground(path)
{
//get the box and set the background image
var box = document.getElementById('box');
if(this.origBack == undefined)
	{
	this.origBack = box.style.backgroundImage;
	}
if(path != '')
	{
	box.style.backgroundImage  = 'url('+path+')';
	}
else
	{
	box.style.backgroundImage  = this.origBack;
	}
}
this.setBackground = setBackground;


////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////Gutters Bone Game
////////////////////////////////////////////////////////////////////////////////////////////
/* 
* starts up the where's gutters bone game
*/
function startGutterBoneGame()
{
var vars = 'startguttergame=1';
http.open('post',window.location, true);
//headers must be set for sending the post variables
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", vars.length);
http.setRequestHeader("Connection", "close");
//uncomment the lower line when ready - at the moment just using ajax to set sessions
http.onreadystatechange = gutterGameStarted;
http.send(vars);
}
this.startGutterBoneGame = startGutterBoneGame;

/*
 * invoked when the gutter's bone game has been successfully started
*/
function gutterGameStarted()
{
var done = false;
var response = http.responseText;
//set the speach bubble to contain the response text
var speachBub = document.getElementById('gutter_sbubble');

speachBub.innerHTML = response;
}

/*
 * sets a gutters bone question to show
*/
function showBoneQuestion()
{
//grab the question div
var qDiv = document.getElementById('bone_question');
if(qDiv.style.display != 'block')
	{
	//show the question
	qDiv.style.display = 'block';
	//grab the darken image and show it
	//alert(document.body.offsetHeight);
	var darkImg = document.getElementById('gbone_darken');
	darkImg.style.height = document.body.offsetHeight +'px';
	darkImg.style.display = 'block';
	//set the background at random
	var randNum=Math.floor(Math.random()*4)+1;
	qDiv.style.backgroundImage = 'url(images/gbone_bubbles/gutter_'+randNum+'.png)';
	}
}
this.showBoneQuestion = showBoneQuestion;

/*
 * sets a gutters bone answer
*/
function boneAnswer(ans)
{
//if the user has already answered boneAns will be set
if(!boneAns)
	{
	//set the answer as a class var
	setAns = ans;
	//check the answer for this page
	var vars = 'getboneans=1&bans='+ans;
	http.open('post',window.location, true);
	//headers must be set for sending the post variables
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", vars.length);
	http.setRequestHeader("Connection", "close");
	//uncomment the lower line when ready - at the moment just using ajax to set sessions
	http.onreadystatechange = gotBoneAns;
	http.send(vars);
	}
}
this.boneAnswer = boneAnswer;

/*
 * invoked when the answer has been returned
*/
function gotBoneAns()
{
if(http.readyState == 4)
	{
	//split the repsonse text and grab the answer/ number correct
	var qDetails = http.responseText.split('|');
	var cAns = qDetails[0]; //the correct answer
	var qAnswered = qDetails[1]; //the number of questions answered
	var qLeft = 4 - qAnswered; //the number of questions remaining
	
	var bBubble = document.getElementById('bone_question'); //the speach bubble
	
	//alert("the answer: " + http.responseText +" - "+ setAns);
	
	var respText = '';
	
	//check if the users answer was correct
	if(cAns == setAns)
		{
		//set the correct answer to green
		var correctAns = document.getElementById("gbone_"+ cAns);
		correctAns.className = "correct";
		//create the response text
		respText = 'Correct!';
		//set the bubble to a correct answer background
		bBubble.style.backgroundImage = 'url(images/gbone_bubbles/right_ans.png)';
		//set a timer to show the next screen
		setTimeout("jrsoJs.gbonesLeft('"+qLeft+"')",1000);
		//set the boneAns
		boneAns = setAns;
		}
	else
		{
		//create the reponse text
		respText = 'Try Again!';
		//set the player answer to red
		var badAns = document.getElementById("gbone_"+setAns);
		badAns.className = 'incorrect';
		//set the bubble to a correct answer background
		bBubble.style.backgroundImage = 'url(images/gbone_bubbles/wrong_ans.png)';
		}
	//get the reponse div and set its content
	var resDiv = document.getElementById('q_reponse');
	resDiv.className = ((cAns == setAns) ? 'correct' : 'incorrect');
	resDiv.innerHTML = respText;
	}
}

/*
 * shows how many bones are left for the user
*/
function gbonesLeft(qLeft)
{
//alert("there are "+ qLeft +" questions left");
//set the content of the gbone question bubble
var respText = '';
if(qLeft > 0)
	{
	respText = '<div id="morebones">'+qLeft+' more bone'+(qLeft > 1 ? 's' : '')+' to find!</div>';
	//set a timer to remove the bubble and the shading
	setTimeout("jrsoJs.removeBubble()",1500);
	}
else
	{
	respText = '<div id="bones_complete"><h2>Well Done! You have found all of Gutter\'s bones.</h2>You have made him very happy.<br/><br/><a href="funzone/guttersbone"><b>Click here</b> to see Gutter do tricks to thank you.</a></div>';
	}
var bBubble = document.getElementById('bone_question');
bBubble.innerHTML = respText;
}
this.gbonesLeft = gbonesLeft;


/*
 * removes the bone, bubble and shading from a given page
*/
function removeBubble()
{
document.getElementById('gbone_darken').style.display = "none";
document.getElementById('gbone').style.display = "none";
}
this.removeBubble = removeBubble;
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////


}
var jrsoJs = new jrsoJs();
