/*
East Renfrewshire Council - Child Protection Committee
Teenagers Website

Javascript performs client side functions
-includes AJAX for poll voting

Susan Smith		16.04.09
*/

var xmlHttp;
//var char_arr=["boy1", "girl", "boy2"];

function rewriteArt()
{
   var soart = new SWFObject('art_loader.swf', 'art', '550', '400');
   soart.addParam('wmode', 'transparent');
   soart.write('streetart');

}

function processVote(int)
{
	xmlHttp=GetXmlHttpObject(int);
	if (xmlHttp==null)
	{
	 alert ("Sorry, your browser doesn't support the poll program");
	 return;
	} 
	var url="poll_result.php";
	url=url+"?vote="+int;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("innerPoll").innerHTML=xmlHttp.responseText;
		
swfobject.embedSWF(
  'open-flash-chart.swf', 'pollres', '199', '160',
  '9.0.0', 'expressInstall.swf',
  {'data-file':'chart.php','loading':'loading...'}
  );


	}
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		 objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

//swap chars when choose on game pages
function swapChars(chosen)
{
	//make the main char the chosen one and the heads the other two
	var mainChar="images/"+chosen+"big.png";
	
	var other1;
	var other2;
	if(chosen=="boy1")
	{
		other1="images/girl.png";
		other2="images/boy2.png";
		alt1="girl";
		alt2="boy2";
	}
	else if(chosen=="girl")
	{
		other1="images/boy2.png";
		other2="images/boy1.png";
		alt1="boy2";
		alt2="boy1";		
	}
	else
	{
		other1="images/boy1.png";
		other2="images/girl.png";
		alt1="boy1";
		alt2="girl";		
	}
	document.getElementById('headpic1').src=other1;
	document.getElementById('headpic2').src=other2;
	document.getElementById('mainchar').src=mainChar;
	document.getElementById('headpic1').title=alt1;
	document.getElementById('headpic2').title=alt2;
	document.getElementById('mainchar').title=chosen;
	document.getElementById('headpic1').alt=alt1;
	document.getElementById('headpic2').alt=alt2;
	document.getElementById('mainchar').alt=chosen;
	//<img src='images/".$char_img1.".png' id='headpic1' alt='".$char_img1."' title='".$char_img1."'/>
	
	//REASSERT SUPERSLEIGHT ???
	$('body').supersleight();
}

/*TAB STUFF*/
 var tabLinks = new Array();
    var contentDivs = new Array();

 function init() {

      // Grab the tab links and content divs from the page
      var tabListItems = document.getElementById('tabs').childNodes;
      for ( var i = 0; i < tabListItems.length; i++ ) {
        if ( tabListItems[i].nodeName == "LI" ) {
          var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
          var id = getHash( tabLink.getAttribute('href') );
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById( id );
        }
      }

      // Assign onclick events to the tab links, and
      // highlight the first tab
      var i = 0;

      for ( var id in tabLinks ) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == 0 )
		tabLinks[id].className = 'selected';
        i++;
      }

      // Hide all content divs except the first
      var i = 0;

      for ( var id in contentDivs ) {
        if ( i != 0 ) contentDivs[id].className = 'tabContent hide';
        i++;
      }
    }
function showTab() {
      var selectedId = getHash( this.getAttribute('href') );

      // Highlight the selected tab, and dim all others.
      // Also show the selected content div, and hide all others.
      for ( var id in contentDivs ) {
        if ( id == selectedId ) {
          tabLinks[id].className = 'selected';
          contentDivs[id].className = 'tabContent';
        } else {
          tabLinks[id].className = '';
          contentDivs[id].className = 'tabContent hide';
        }
      }

      // Stop the browser following the link
      return false;
    }
function getFirstChildWithTagName( element, tagName ) {
      for ( var i = 0; i < element.childNodes.length; i++ ) {
        if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
      }
    }
function getHash( url ) {
      var hashPos = url.lastIndexOf ( '#' );
      return url.substring( hashPos + 1 );
    }

//mobile functions
function processMobVote(vote)
{
//	alert(vote);
	xmlHttp=GetXmlHttpObject(vote);
	if (xmlHttp==null)
	{
	 alert ("Sorry, your browser doesn't support the question program");
	 return;
	} 
	var url="mob_result.php";
	url=url+"?vote="+vote;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedMob;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChangedMob() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("mobqus").innerHTML=xmlHttp.responseText;
	}
}

function getNext(top)
{ 
	xmlHttp=GetXmlHttpObject(top);
	if (xmlHttp==null)
	{
	 alert ("Sorry, your browser doesn't support the question program");
	 return;
	} 
	var url="mob_next.php";
	url=url+"?top="+top;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedNext;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChangedNext()
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("mobtask").innerHTML=xmlHttp.responseText;
	}
}

function processPollMob(int)
{ 
	xmlHttp=GetXmlHttpObject(int);
	if (xmlHttp==null)
	{
	 alert ("Sorry, your browser doesn't support the poll program");
	 return;
	} 
	var url="poll_result_mob.php";
	url=url+"?vote="+int;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChangedPollMob;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChangedPollMob() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("innerPoll").innerHTML=xmlHttp.responseText;
	}
}

