/* 
	Title: AJAX javascript file
	Client: Barlclays
	Date: 11/09/2006
	Author: Ben Ellis
*/

// Create variables
var xmlHttp
var youTubeVars = new Array();
	youTubeVars['item1']= 'KwKyrWKEsE8';
	youTubeVars['item2']= '7oHtUM5o21k';
	youTubeVars['item3']= 'tjr6X4LNkfc';
	youTubeVars['item4']= 'CbwH83kF-ns';
	youTubeVars['item5']= 'zJQoDh_ouTc';
	youTubeVars['item6']= '8tM4Qm7TzCI';
	youTubeVars['item7']= 'GiCD8OnDDtc';
	youTubeVars['item8']= 'g_2sGXkDJEQ';
	youTubeVars['item9']= 'CBmfhbIkl0o';

// Function to create the http object to parse data through
function getXmlHttpObject(){ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

// Function to change page state every time http object changes
function stateChanged(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById("video").innerHTML=xmlHttp.responseText;
	} 
}

// Function to show data
function getData(id){ 
	// Create the http object
	xmlHttp=getXmlHttpObject();
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	// URL
	var url="php/video.php";
	// Data to parse
	url=url+"?id="+id;
	// Run the functions
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

/* 
	Title: addLoadEvent()
	Description: Function to add custom functions to the window onload event handler
	Arguments: 
		$func: function as an argument
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}	

/*
	Title: attachHooks()
	Description: Function to add custom custom css ids and class attributes to a collection of html elements, such as links, or list items etc
	Arguments: 
		$parent: The parent id of the html element
		$tag: The html tag to target
		$label: The css id to attach to each element
		$css: The css classname to attach to each element
*/
function attachHooks(parent,tag,label,css){
	
	// If we understand the DOM
	if(document.getElementById && document.getElementsByTagName){
		// If the parent is present
		if(document.getElementById(parent)){
			// Declare parent object
			var object = document.getElementById(parent);
			// Declare child objects
			var children = object.getElementsByTagName(tag);
			
			// Loop through the childrenn
			for(var i=0;i<children.length; i++){
				// Attach ids
				if(label != "") children[i].id = label+(i+1);
				// Add the onfocus events
				children[i].onfocus = function (){
				// Swtich css
				switchStyle(parent,this.id,tag,css);
				}
				// Add the onclick events
				children[i].onclick = function (){
				// Get data
				getData(youTubeVars[this.id]);
				}
			}
		}
	}
}

/* 
	Title: switchStyle()
	Description: Function to swap CSS
	Arguments: 
		$parent: The parent id of the html element
		$tag: The html tag to target
		$target: The id to target
		$css: The css classname to attach to each element
*/
function switchStyle(parent,target,tag,css){
	// If we understand the DOM
	if(document.getElementById && document.getElementsByTagName){
		// Declare parent object
		var object = document.getElementById(parent);
		// Declare child objects
		var children = object.getElementsByTagName(tag);
		// Loop through the children
		for(var i=0;i<children.length; i++){
			// Change css
			children[i].className = '';
		}
		// Set the active state for the a
		if(target) document.getElementById(target).className = css;
	}
}