//Marquee variables
var marqueeText="Oundle wins <a href='/news/newsItem.php?id=395'>Lords Taverners</a> cricket final.";
var marqueeSpeed="30"; //NB smaller is faster -it's actually the delay in moving the text along the line
var marqueeJumpInterval=0.5; //The distance as a percentage of the width of the page that the marquee message jumps

function whatBrowser()
{
if (document.all)
	{
		return "isIE";
	}
else
	{
		return "notIE";
	}
}

function externalLinks() {
if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

window.onload = externalLinks;

function patternOfAssessment(newURL){
//Opens the pattern of assessment pages in a new window in the 
///academic/sixthform/subjects directory
window.open(newURL,"","alwaysRaised,dependent,scrollbars,width=800,height=600");
}

function chapelwindows(newURL){
window.open(newURL,"","alwaysRaised,dependent,width=600,height=200");
}

function galleryWindow(url,width,height)
	{
		window.open(url,"","alwaysRaised,dependent,width="+(width+50)+",height="+(height+50));
	}

function popUpMap(url,width,height)
	{
		window.open(url,"","alwaysRaised,dependent,width="+(width+50)+",height="+(height+50));
	}
	
function matchpopup(teamId,opponentId)
	{
		window.open("/sport/matchpopup.php?teamId=" + teamId+"&opponentId="+opponentId,"","width=500,height=500");
	}

function newmatchpopup(fixtureId)
	{
		window.open("/sport/matchpopup.php?fixtureId=" + fixtureId,"","width=500,height=500");
	}

function increaseFontSizes()
	{
		changeFontSize("body",1.2,"n");
		changeFontSize("leftnav",1.1,"n");
		changeFontSize("breadcrumb",1.1,"n");
		changeFontSize("search",0.95,"n");
	}

function decreaseFontSizes()
	{
		changeFontSize("body",0.9,"n");
		changeFontSize("leftnav",0.9,"n");
		changeFontSize("breadcrumb",0.95,"n");
		changeFontSize("search",0.95,"n");
	}

function resetFontSizes()
	{
		document.cookie="FontSizeleftnav=0.9;path=/";
		document.cookie="FontSizebody=1.0;path=/";
		document.cookie="FontSizebreadcrumb=0.9;path=/";
		document.cookie="FontSizesearch=1.0;path=/";
		history.go();
	}

function changeFontSize(element,multiplier,debug)
	{
		var fontSize=document.getElementById(element).style.fontSize;
		//Strip off the units (em)
		fontSize=fontSize.substring(0,fontSize.length-2);
		fontSize=multiplier*fontSize;
		document.getElementById(element).style.fontSize=fontSize+"em";
		//Store the current font size as a cookie so we can get it on the next page
		var exp = new Date();
		var oneYearFromNow = exp.getTime() + (365*24*60*60*1000);
		exp.setTime(oneYearFromNow);
		var cookieString="FontSize"+element+"="+fontSize+"; path=/; expires=" + exp.toGMTString();
		document.cookie=cookieString;
		
		if (debug=="y")
			{
				alert (document.getElementById('tester').style.fontSize);
			}
	}

function setFontSizes()
	{
		//Called when a page is loaded to check the cookie values for relevant font sizes
		//Find out if any cookies have been set or if the cookie string starts off with a zero setting
		if (document.cookie.substring(0,18).toLowerCase()=="fontsizeleftnav=0;")
			{
				return;
			}

		var cookieDelim=";";
		var arrFontSizes = document.cookie.split(cookieDelim);
		var re=/fontsize/i;
		var browser=whatBrowser();
		var fontSizes=document.styleSheets[0];
		for (i=0;i<arrFontSizes.length;i++)
			{
				if (re.exec(arrFontSizes[i]))
					{
						//Match has been found, ie the cookie key has fontsize (case insensitive) in it somewhere
						//Extract the element and its value
						var arrDetails = arrFontSizes[i].split("=");
						var element=arrDetails[0].replace(/^(\s*FontSize)/,"");
						document.getElementById(element).style.fontSize=arrDetails[1]+"em";	
					}
			}
	}

	var mouseOverMarq=false;

function marquee(message)
{
	//NB configuration is done via global variables at the top of the page
	var marText=document.getElementById('marqueeText');
	marText.innerHTML=marqueeText; //Not standards compliant, but getting tags such as links in there would mean adding new nodes for each tag. MarqueeText defined at top of page
	marText.style.position='relative';
	marText.style.left='99%';
	setInterval ("moveText()",marqueeSpeed);	//Calls the moveText function repeatedly every 100ms
}

function moveText()
{
	if (mouseOverMarq){return;}
	var marText=document.getElementById('marqueeText');
	var leftPosn=parseFloat(marText.style.left);
	//Find out the width of marText as a percentage of the width of the containing div so that we know when it's outside the containing div
	var mar=document.getElementById('marquee');
	var percentageToClear=100*((marText.offsetWidth)/(mar.offsetWidth));
	if (leftPosn<-percentageToClear)
		{
			marText.style.left="100%";
			leftPosn=100;
		}
	marText.style.left=(leftPosn-marqueeJumpInterval)+"%";
}

function mouseOverMarquee()
{
	mouseOverMarq=true;
}

function mouseNotOverMarquee()
{
	mouseOverMarq=false;
}

function addBookmark(title,url) {
	
	if (window.sidebar) { window.sidebar.addPanel (title,url,"");} // Mozilla Firefox Bookmark 
	else if( window.external ) { window.external.AddFavorite( url, title); } // IE Favorite
 }

function getHTTPObject(){
  if (window.ActiveXObject) 
    return new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest) 
    return new XMLHttpRequest();
  else {
    alert("Your browser does not support AJAX.");
    return null;
  }
}

function loadCalendar() {
  cal1 = new YAHOO.widget.Calendar("cal1Container");
  myTooltips=new Array();
  cal1.renderEvent.subscribe(add_tooltips, this);
  cal1.selectEvent.subscribe(date_clicked, this);
  cal1.render();
}

function date_clicked() {
  var dates=cal1.getSelectedDates(); //Returns an array of js date objects. we should only get 1 date object back
  location.href='calendarNew.php#A'+(dates[0].getDate()<10?'0':'')+dates[0].getDate()+(dates[0].getMonth()<10?'0':'')+(parseInt(dates[0].getMonth())+1)+dates[0].getFullYear();
}
function add_tooltips() {
  //Get the info for the calendar
  var xmlhttp = getHTTPObject();
  var q="start_date="+cal1.cellDates[0]+"&end_date="+cal1.cellDates[41];
  var url="get_events_xml.php";
  url=url+"?"+encodeURI(q);
  xmlhttp.onreadystatechange=function () {
    if (xmlhttp.readyState==4) {
      get_events_xml(xmlhttp.responseXML);
    }
  }
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  for (i=0;i<42;i++) {
    add_tooltip(i);
  }
}

function get_events_xml(xml) {
  //Update the tooltips' contents
  for (i=0;i<42;i++) {
    //Find which date we're looking at on the calendar
    var cal_date=cal1.cellDates[i];

    //Get the xml dates, ie the ones from the database with their corresponding event details
    var dates=xml.getElementsByTagName('date');
//alert (dates[i].getAttribute('value'));
    for (j=0;j<dates.length;j++) {
      if (dates[j].getAttribute('value')!=cal_date) {
        continue;
      }

      //got the date in question so put together the tooltip info
      var tt_info='<table class="tt_info">';

      //Put the table headers on
      tt_info+='<tr class="headers">';
      tt_info+='<td>Time</td>';
      tt_info+='<td>Event</td>';
      tt_info+='<td>Venue</td>';
      tt_info+='<td>Info</td>';
      tt_info+='<td>Opponent</td>';
      tt_info+='</tr>';
      //Go through each of the day's items
      var items=(dates[j].getElementsByTagName('item'));
      for (k=0;k<items.length;k++) {
        tt_info+='<tr>';
        if (items[k].getAttribute('time')!='0000') {
          tt_info+='<td class="time">'+items[k].getAttribute('time')+'</td>';
        } else {
          tt_info+='<td class="time"></td>';
        }
        tt_info+='<td class="event">'+items[k].getAttribute('event')+'</td>';
        tt_info+='<td class="venue">'+items[k].getAttribute('venue')+'</td>';
        tt_info+='<td class="info">'+items[k].getAttribute('info')+'</td>';
        tt_info+='<td class="opponent">'+items[k].getAttribute('opponent')+'</td>';
        tt_info+='</tr>';
      }
    }
    tt_info+='</table>'
    myTooltips[i].cfg.setProperty('text',tt_info);
  }
}

function add_tooltip(i) {
  myTooltips[i] = new YAHOO.widget.Tooltip("myTooltip"+i, { 
    context: "cal1Container_t_cell"+i, 
    text: "Getting events for "+cal1.cellDates[i],
    showDelay: 1000,
    autodismissdelay:99999999
  });
}