var searchfiltersvisible = false;

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 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';
		 
	$('#calendar-container').load(url, { 'month': month, 'size': size, 'id': id, 'year': year } );

}

function homepage_change_area(area,page) {
	$('#changearea').load('/templates/includes/homepage_home.php', { 'page': page } );
}

function whatson(theatre,month) {
	$('#lhs_panel').load('/templates/includes/whats-on.php', { 'theatre': theatre, 'month': month, 'whatson': 1 } );
}

function whatsonall(theatre) {
	$('#lhs_panel').load('/templates/includes/whats-on-all.php', { 'theatre': theatre } );
}

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) {
	$('#lhs_panel').load(lpb_docroot+'/templates/includes/whats-on-touring.php', { 'theatre': theatre, 'month': month } );
}

function archiveshow(letter) {
	$('#archive_area').load(lpb_docroot+'/templates/includes/whats-on-archive-list.php', { 'letter': letter } );
}

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
		whatson(whichMonth);
		createCookie('month', whichMonth);
}


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');
	
}

function showVideoPlayer() {
	document.getElementById('vidplayerholder').style.display="none";
	document.getElementById('performancePanel').style.background="none";
	document.getElementById('brightcovevideo').style.display="block";
}

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();
}

function showBasket() {
	document.write('<sc'+'ript type="text/javascript" src="https://tickets.sadlerswells.com/json.asp"></scr'+'ipt>');
}

function GetBasketContent(data) {

		if(data) {
			var basketURL = (data.checkoutURL);
			var basketValue = (data.total);		
			var basketitems = (data.basket.length)-1;
				$('#basket').css('display','block');
				$('#basketlink').attr('href', basketURL);
				if (basketitems>=1) {
					$('#basketitems').html(basketitems +" items");
				} 
				if (basketitems==1) {
					$('#basketitems').html("1 item");
				} 
				$('#basketvalue').html(basketValue);
		}



}



function prepsearchfilter(cat,cal) {
	var filter = '<div style="float:left;color:#EF3326;margin-left:10px;"><input type="checkbox" checked="checked" name="filter_'+cat+'" id="filter_'+cat+'"><label for="filter_'+cat+'" value="'+cal+'">'+cal+'</label></div>';
	return filter;
}

function prepfilterslink(cats,catl) {
	if(searchfiltersvisible == false) {
		var filteroutput = "<div style=\"float:left; background:url('./images/filter-arrow.png') no-repeat 0 50%; padding-left:13px; color:#EF3326;\"><strong><a id=\"filters\" href=\"#filters\">Filter results</a></strong></div><div class=\"clearer\">&nbsp;</div>";
		
		$('#searchfilters').html(filteroutput);
		$('#searchfilters').css('display','block');
	}
	
	$('#searchfilters a#filters').click(function(e) { 
		e.preventDefault();
		if(searchfiltersvisible == false) {
			showsearchfilters(cats,catl);
		}
		else {
			searchfiltersvisible = false;
			prepfilterslink(cats,catl);
		}
	});
}

function showsearchfilters(cats,catl) {

	searchfiltersvisible = true;
	var filteroutput = "<div style=\"float:left; background:url('./images/filter-arrow-open.png') no-repeat 0 50%; padding-left:13px; color:#EF3326;\"><strong><a id=\"filters\" href=\"#filters\">Filter results</a></strong></div><div class=\"clearer\">&nbsp;</div>";
	var showsubtitle = false;
	var categories = "";
	var catsExist = false;
	
	if(($('#div_currentshows').length)) { 
		if(showsubtitle == false) {
			filteroutput += "<div style=\"float:left;\"><strong>Shows:</strong></div>";
			showsubtitle = true;
		}
		filteroutput += prepsearchfilter('currentshows','Current Shows'); 
	}
	if(($('#div_archiveshows').length)) { 
		if(showsubtitle == false) {
			filteroutput += "<div style=\"float:left;\"><strong>Shows:</strong></div>";
			showsubtitle = true;
		}
		filteroutput += prepsearchfilter('archiveshows','Past Shows'); 
	}
	
	for (var cat in cats) {
		//console.log($('#div_'+cats[cat]).length);
		if(($('#div_'+cats[cat])).length) { catsExist = true; categories += prepsearchfilter(cats[cat],catl[cat]); }
	}
	if(catsExist == true) {
		filteroutput += "<div class=\"clearer\">&nbsp;</div>";
		filteroutput += "<div style=\"float:left;\"><strong>Videos:</strong></div>";
		filteroutput += categories;
	}
	
	filteroutput += "<div class=\"clearer\">&nbsp;</div>";
	
	$('#searchfilters').html(filteroutput);
	$('#searchfilters').css('display','block');
	
	initfilteractions(cats);
	prepfilterslink(cats,catl)
}

function initfilteractions(cats) {
	
		$('#filter_currentshows').change(function(e) {
			var thisfilter = this.id;
			var thisfilterid = thisfilter.replace('filter_','');
			//alert(thisfilterid);
			filterbycat(thisfilterid);
			//return false;
		});
		
		$('#filter_archiveshows').change(function(e) {
			var thisfilter = this.id;
			var thisfilterid = thisfilter.replace('filter_','');
			//alert(thisfilterid);
			filterbycat(thisfilterid);
			//return false;
		});
	
	for (var cat in cats) {
		//console.log('#filter_'+cats[cat]+' click adding');
		$('#filter_'+cats[cat]).change(function(e) {
			var thisfilter = this.id;
			var thisfilterid = thisfilter.replace('filter_','');
			//alert(thisfilterid);
			filterbycat(thisfilterid);
			//return false;
		});
	}
	
}

function filterbycat(cat) {
	
	//alert(cat);
	
	if ($('#div_'+cat).css('display')=='block') {
		//alert(($('#div_'+cat).css('display')));
		$('#div_'+cat).css('display','none');
		//alert(($('#div_'+cat).css('display')));
	} else {
		$('#div_'+cat).css('display','block');
	}
}


