//Site Wide Script File
//This file contains scripting for the entire site
		//Popup Script that defines window size ect for popups (Used in photo album link and others)
		
		//Google API For Calling the Google via Search Box (Used in the google search box)
			//**also requires the script ref to google in the head**
			
		//Site Apearence Code (Adds in extra HTML to properly format the design)
			//**Uses jquery so keep that script #ref!
		//Site Drop Down Menu Code (for Fast Links)
		
		//A Stylesheet switcher for dynamically changing the apearence (used for accessability options and themes)
			//**This must be at the end of the head code!!**

//Popup Script

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}
//End Popup Script

//Google API Code:
     //<![CDATA[
    google.load('search', '1');
    
    function OnLoad() {
      // Dynamically load CSS to override defaults
      var css = document.createElement('link');
      css.href = '../../css/gsearch_green.css';
      css.type = 'text/css';
      css.rel = 'stylesheet';
      document.getElementsByTagName('head')[0].appendChild(css);
      
      // Create a search control
      var searchControl = new google.search.SearchControl();

      // site restricted web search with custom label
      // and class suffix
      var siteSearch = new google.search.WebSearch();
      siteSearch.setUserDefinedLabel("Balcarras Website");
      siteSearch.setUserDefinedClassSuffix("siteSearch");
      siteSearch.setSiteRestriction("balcarras.gloucs.sch.uk");
      searchControl.addSearcher(siteSearch);

      // local search
      var localSearch = new google.search.LocalSearch();
      searchControl.addSearcher(localSearch);


      // standard, unrestricted web search
      searchControl.addSearcher(new google.search.WebSearch());

      // site restricted news search
      var newsSearch = new google.search.NewsSearch();
      newsSearch.setUserDefinedLabel("BBC News");
      newsSearch.setSiteRestriction("BBC News");
      searchControl.addSearcher(newsSearch);

      // Establish a keep callback
      searchControl.setOnKeepCallback(null, DummyClipSearchResult);

	  // Set the Local Search center point
      localSearch.setCenterPoint("Cheltenham, UK");

      // tell the searcher to draw itself and tell it where to attach
      searchControl.draw(document.getElementById("searchcontrol"));

      // execute an inital search (!Inactive!)
      //searchControl.execute("Balcarras School");
    }

    function DummyClipSearchResult(result) {}

    google.setOnLoadCallback(OnLoad, true);
    //]]>	 
//End Google API Code

//Main Menu Drop Down Script

sfHover = function() {
	var sfEls = document.getElementById("globalnav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

//End Menu Drop Down

//Site Apearence
	$(document).ready(function(){		//starts a jquery function (requires jquery libary script ref)
		//prepend span tags to start of headings (for gradient text and css layout customisation)
		$("#pagecontent h1").prepend("<span></span>");
		$("#pagecontent h2").prepend("<span></span>");
		$("#pagecontent h3").prepend("<span></span>");
		$("hr").wrap("<div class='hr'></div>"); //puts all hori lines in a div for presentation
	});
//End Site Apearence

//Fast Links Drop Down Menu
	function MM_jumpMenu(targ,selObj,restore){ //v3.0
	  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
	}
//End Fast Links Drop Down Menu


//Start Stylesheet switcher
	//Style Sheet Switcher version 1.1 Oct 10th, 2006
	//Author: Dynamic Drive: http://www.dynamicdrive.com
	//Usage terms: http://www.dynamicdrive.com/notice.htm
	
	var manual_or_random="manual" //"manual" or "random"
	var randomsetting="3 days" //"eachtime", "sessiononly", or "x days (replace x with desired integer)". Only applicable if mode is random.
	
	//////No need to edit beyond here//////////////
	
	function getCookie(Name) { 
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
	return document.cookie.match(re)[0].split("=")[1] //return its value
	return null
	}
	
	function setCookie(name, value, days) {
	var expireDate = new Date()
	//set "expstring" to either future or past date, to set or delete cookie, respectively
	var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)
	document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/";
	}
	
	function deleteCookie(name){
	setCookie(name, "moot")
	}
	
	
	function setStylesheet(title, randomize){ //Main stylesheet switcher function. Second parameter if defined causes a random alternate stylesheet (including none) to be enabled
	var i, cacheobj, altsheets=[""]
	for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
	if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title
	cacheobj.disabled = true
	altsheets.push(cacheobj) //store reference to alt stylesheets inside array
	if(cacheobj.getAttribute("title") == title) //enable alternate stylesheet with title that matches parameter
	cacheobj.disabled = false //enable chosen style sheet
	}
	}
	if (typeof randomize!="undefined"){ //if second paramter is defined, randomly enable an alt style sheet (includes non)
	var randomnumber=Math.floor(Math.random()*altsheets.length)
	altsheets[randomnumber].disabled=false
	}
	return (typeof randomize!="undefined" && altsheets[randomnumber]!="")? altsheets[randomnumber].getAttribute("title") : "" //if in "random" mode, return "title" of randomly enabled alt stylesheet
	}
	
	function chooseStyle(styletitle, days){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie
	if (document.getElementById){
	setStylesheet(styletitle)
	setCookie("mysheet", styletitle, days)
	}
	}
	
	function indicateSelected(element){ //Optional function that shows which style sheet is currently selected within group of radio buttons or select menu
	if (selectedtitle!=null && (element.type==undefined || element.type=="select-one")){ //if element is a radio button or select menu
	var element=(element.type=="select-one") ? element.options : element
	for (var i=0; i<element.length; i++){
	if (element[i].value==selectedtitle){ //if match found between form element value and cookie value
	if (element[i].tagName=="OPTION") //if this is a select menu
	element[i].selected=true
	else //else if it's a radio button
	element[i].checked=true
	break
	}
	}
	}
	}
	
	if (manual_or_random=="manual"){ //IF MANUAL MODE
	var selectedtitle=getCookie("mysheet")
	if (document.getElementById && selectedtitle!=null) //load user chosen style sheet from cookie if there is one stored
	setStylesheet(selectedtitle)
	}
	else if (manual_or_random=="random"){ //IF AUTO RANDOM MODE
	if (randomsetting=="eachtime")
	setStylesheet("", "random")
	else if (randomsetting=="sessiononly"){ //if "sessiononly" setting
	if (getCookie("mysheet_s")==null) //if "mysheet_s" session cookie is empty
	document.cookie="mysheet_s="+setStylesheet("", "random")+"; path=/" //activate random alt stylesheet while remembering its "title" value
	else
	setStylesheet(getCookie("mysheet_s")) //just activate random alt stylesheet stored in cookie
	}
	else if (randomsetting.search(/^[1-9]+ days/i)!=-1){ //if "x days" setting
	if (getCookie("mysheet_r")==null || parseInt(getCookie("mysheet_r_days"))!=parseInt(randomsetting)){ //if "mysheet_r" cookie is empty or admin has changed number of days to persist in "x days" variable
	setCookie("mysheet_r", setStylesheet("", "random"), parseInt(randomsetting)) //activate random alt stylesheet while remembering its "title" value
	setCookie("mysheet_r_days", randomsetting, parseInt(randomsetting)) //Also remember the number of days to persist per the "x days" variable
	}
	else
	setStylesheet(getCookie("mysheet_r")) //just activate random alt stylesheet stored in cookie
	} 
	}
//End Style sheet Switcher