function ReadCookie(cookieName)
{
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return "";
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length;
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function showBasket()
{

	if (ReadCookie("havebasket") == 'true') // reads in cookie that has been set on the secure site
	{
		var object = document.getElementById('basket');
		if (object != null)
		{
			object.style.display = 'inline';
		}
	}
}

function calendar_show(month,size,id,year){
	 if (size<12) {
	     var url = lpb_docroot+'/templates/includes/calendar.php';
	 } else {
	 	 var url = lpb_docroot+'/templates/includes/homepage_home-calendar.php';
	 }
	     var pars = 'month='+month+"&size="+size+"&id="+id+"&year="+year;
	     var target = 'calendar-container';
	 var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, evalScripts: true});
}

function home_next_results(page) {
	var url = '/templates/includes/homepage_eventslistings_caller.php';
	var pars = 'page='+page;
	var target = 'changearea';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, evalScripts: true});
}

function homepage_change_area(area,page) {
	var url = '/templates/includes/homepage_home.php';
	var pars = 'page='+page;
	var target = 'changearea';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, evalScripts: true} );
}

function showpage_change_area(area,page,show) {
	var url = '/templates/includes/showpage_'+area+'.php';
	var pars = 'page='+page+"&show="+show;
	var target = 'loadablearea';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, evalScripts: true} );
}

function whatson(theatre,month) {
	var url = '/templates/includes/whats-on.php';
	var pars = 'theatre='+theatre+'&month='+month+'&whatson=1';
	var target = 'lhs_panel';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, evalScripts: true} );
}

function whatsonall(theatre) {
	var url = '/templates/includes/whats-on-all.php';
	var pars = 'theatre='+theatre;
	var target = 'lhs_panel';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars, evalScripts: true} );
}

function whatsoninline(showmonth) {

	for (i=1;i<=12;i++) {
		document.getElementById('monthshow_'+i).style.display = "none";
		swapIt('monthlink_'+i, "redwo");
		swapIt('monthlinkd_'+i, "redwo");
		swapIt('monthlink_all', "redwo");
	}
	if (showmonth!="all") {
		document.getElementById('monthshow_'+showmonth).style.display = "block";
			if (document.getElementById('nomsg_'+showmonth)) {
				document.getElementById('nomsg_'+showmonth).style.display = "block";
			}		
		swapIt('monthlink_'+showmonth, "whitewo");
		swapIt('monthlinkd_'+showmonth, "whitewo");
	} else {
		for (i=1;i<=12;i++) {
			document.getElementById('monthshow_'+i).style.display = "block";
			if (document.getElementById('nomsg_'+i)) {
				document.getElementById('nomsg_'+i).style.display = "none";
			}
		}
		swapIt('monthlink_all', "whitewo");
		swapIt('monthlinkd_all', "whitewo");
	}
}

function whatsont(theatre,month) {
	var url = lpb_docroot+'/templates/includes/whats-on-touring.php';
	var pars = 'theatre='+theatre+'&month='+month;
	var target = 'lhs_panel';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars} );
}

function archiveshow(letter) {
	var url = lpb_docroot+'/templates/includes/whats-on-archive-list.php';
	var pars = 'letter='+letter;
	var target = 'archive_area';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars} );
}

function toggleregisterform() {
if (document.getElementById('emailregister')) {
	if (document.getElementById('emailregister').style.display=="none") {
		document.getElementById('regbutton').style.display="none";
		document.getElementById('emailregister').style.visibility="visible"
		new Effect.Appear('emailregister');
	} else {
		new Effect.Fade('emailregister');
		var t = setTimeout( "document.getElementById('regbutton').style.display='block';",1000);
	}
}
}

function setfocus() {
	document.searchSite.searchInput.focus();
}

function swapIt(id, newclass) {
	if (document.getElementById) {
		document.getElementById(id).className = newclass;
	}
}

function spawn(url, nameW, w, h) {
	window.open(url, nameW, 'width='+w+',height='+h+',scrollbars=1,resizable=0,menubar=0,toolbar=0,status=0,location=0,directories=0,left=100,top=100');
}	

function calCookie(month, year, id) {
	createCookie('calYear',year);
	createCookie('calMonth',month);
	createCookie('calId',id);
}

function calCookieSize(size) {
	createCookie('calSize',size);
}


function calendar_selectmonth(){
	var history_cmonth = _readCookie('calMonth');
	var history_cyear = _readCookie('calYear');
	var history_csize = _readCookie('calSize');
	var history_cId = _readCookie('calId');

	if(history_cmonth && history_cyear && history_csize==12){
		calendar_show(history_cmonth, 12, history_cId, history_cyear);
		eraseCookie('calMonth');
		eraseCookie('calYear');
		eraseCookie('calSize');
		eraseCookie('calId');
	}
}

function whatsonCookie(theatre, month) {

	if(theatre){
		createCookie('theatre', theatre);
	}
	
	if(month){
		createCookie('month', month);
	}	
	
	if(month==null){
		eraseCookie('month');	
	}
	
	createCookie('load', 'whatson');
	
	if(theatre!=null){
		whatson_selectview();
	}else if(theatre==null){
		whatsoninline(month);
	}
	
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function _readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function whatson_onload(whichTheatre, whichMonth){

	var history_month = _readCookie('month');
	var history_theatre = _readCookie('theatre');
	var history_load = _readCookie('load');

	if (whichMonth === undefined) {
		whichMonth = 1;
	}
	
	//first load
	//if(!history_month && !history_theatre){
		//alert(whichTheatre);
		whatson(whichMonth);
		createCookie('month', whichMonth);
	//}else{
	//	whatson_selectview();
	//}
}


function whatson_selectview(){
	var history_month = _readCookie('month');
	var history_theatre = _readCookie('theatre');
	
	if(history_month && history_theatre){
		whatson(history_theatre, history_month);
	}else if(!history_month && history_theatre){
		whatsonall(history_theatre);
	}else if(history_month && !history_theatre){
		whatsoninline(history_month);
	}
	
}

function pagCookie(pagNum){
	createCookie('pagNum', pagNum);
}

function r_pagCookie(){
	var pagHistory = _readCookie('pagNum');
	if(pagHistory){
		homepage_change_area('eventslistings_caller', pagHistory);
	}
	
	eraseCookie('pagNum');
}

function show_pagCookie(pagNum, pagShow){
	createCookie('show_pagNum', pagNum);
	createCookie('pagShow', pagShow);
}

function r_showPagCookie(){
	var pagHistory = _readCookie('show_pagNum');
	var pagShowHistory = _readCookie('pagShow');
	var pagShowLoad = _readCookie('pagShowLoad');
		
	if(pagHistory && pagShowHistory && !pagShowLoad){
		showpage_change_area('eventslistings', pagHistory, pagShowHistory);
	}
	
	eraseCookie('pagShowLoad');
	
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};


BrowserDetect.init();

function init_all(){
	calendar_selectmonth();
}