var bgn = 5;
var videoBgPic = '';

var changeBgrd = function() {
	if ($('bgoverlay')) {
		var height = document.documentElement.clientHeight;
		if (height < 753) height = 753;
		$('bgoverlay').setStyle('height', height);
		$('bgoverlay').setStyle('display', 'block');
		$('bgoverlay').setStyle('background-color', '#000000');
		$('bgoverlay').set('opacity', '1.0');
		$('bgoverlay').set('tween', {duration: 5000});
		$('bgoverlay').fade('out');
		fadeIn.delay(25000);
		if (bgn > 12) bgn = 1;
		else if (bgn == 11) bgn = 12;
		$(document.body).setStyle('background', '#000000 url(/x/img/static/bg' + bgn + '.jpg) no-repeat');
		$('credit').set('html', creditText());
		var myCookie = Cookie.write('bgrd', bgn);
		bgn++;
		changeBgrd.delay(30000);
	} else {
		var myCookie = Cookie.read('bgrd');
		if (myCookie) var bgimg = myCookie;
		$(document.body).setStyle('background', '#000000 url(/x/img/static/bg' + bgimg + '.jpg) no-repeat');
	}
}

var creditText = function() {
	var bg = bgn - 0;
	switch (bg) {
		case 2:
			return '<i>Image</i> Alvin Ailey American Dance Theater / <i>Photographer</i> Andrew Eccles';
		case 3:
			return '<i>Image</i> Suzhou Kun Opera Theatre / <i>Photographer</i> Pei-Hung Hsu';
		case 4:
			return '<i>Image</i> Angela Denoke / <i>Photographer</i> Wilfried Hösl';
		case 5:
			return '<i>Image</i> Cedric Tiberghien / <i>Photographer</i> Benjamin Ealovega';
		case 6:
			return '<i>Image</i> Louis Langrée / <i>Photographer</i> Benjamin Ealovega';
		case 7:
			return '<i>Image</i> Toby Spence / <i>Photographer</i> Franck Ferville';
		case 8:
			return '<i>Image</i> David Afkham / <i>Photographer</i> Chris Christodoulou';
		case 9:
			return '<i>Image</i> Xuefei Yang and Natalie Clein / <i>Photographer</i> Damil Kalogjera';
		case 10:
			return '<i>Image</i> Yannick Nézet-Séguin / <i>Photographer</i> Marco Borggreve';
		case 11:
			return '<i>Image</i> Julian Rachlin / <i>Photographer</i> Mike Hoban';
		case 12:
			return '<i>Image</i> Dmitri Hvorostovsky / <i>Photographer</i> Ken Howard';
		case 13:
			return '<i>Image</i> Gustavo Dudamel / <i>Photographer</i> Chris Christodoulou';
		default:
			return '<i>Image</i> Staatskapelle Dresden / <i>Photographer</i> Matthias Cruetziger';
	}
}

var fadeIn = function() {
	$('bgoverlay').set('opacity', '0.0');
	$('bgoverlay').set('tween', {duration: 5000});
	$('bgoverlay').fade('in');
}

var setn = function() {
	/*
	var ran = Math.floor(3*Math.random());
	switch (ran) {
		case 0: bgn = 5; break;
		case 1: bgn = 6; break;
		case 2: bgn = 10; break;
	} */
	var ran = Math.floor(12*Math.random());
	bgn = ran + 1;
}

var fixHeight = function() {
	if ($('padding')) {
		if (this.khtml) var height = this.innerHeight;
		else if (this.opera || this.gecko || this.ie6) var height = document.body.clientHeight;
		else var height = document.documentElement.clientHeight;
		// base values: #padding - 510px; #content, #thisweekblock - 452px; #colleagues - 450px; #artistslist - 430px; #artistspanel - 101px; #details - 421px; #mediacopy - 436px;
		if (height > 753) {
			if ($('padding')) $('padding').setStyle('height', (height - 243));
			if ($('content')) $('content').setStyle('height', (height - 301));
			if ($('thisweekblock')) $('thisweekblock').setStyle('height', (height - 301));
			if ($('artistslist')) $('artistslist').setStyle('height', (height - 323));
			if ($('colleagues')) $('colleagues').setStyle('height', (height - 303));
			if ($('teasers')) $('teasers').setStyle('height', (height - 303));
			if ($('artistspanel')) $('artistspanel').setStyle('height', (height - 632));
			if ($('tourspanel')) $('tourspanel').setStyle('height', (height - 632));
			if ($('details')) $('details').setStyle('height', (height - 332));
			if ($('mediacopy')) $('mediacopy').setStyle('height', (height - 317));
		}
	}
}

var showCal = function() {
	if ($('calendarholder') && $('date')) myCal = new Calendar({ date: 'd/m/Y' });
}

var primeSearch = function() {
	if ($('searchinput') && $('searchbutton')) {
		$('searchbutton').setStyle('display', 'none');
		$('searchinput').addEvent('click', function() {
			$('searchbutton').setStyle('display', 'block');
		});
		$('searchinput').addEvent('blur', function() {
			if ($('searchinput').get('value') == '') {
				$('searchbutton').setStyle('display', 'none');
			}
		});
	}
}

var showDetailBlocks = function() {
	if ($('details')) {
		var lnks = $$('#linkblock .showblock');
		var divs = $$('#details .detailblock');
		divs.each(function(div, index) {
			if (index != 0) div.setStyle('display', 'none');
		});
		lnks.each(function(lnk, mark) {
			lnk.addEvent('click', function(e) {
				e.stop();
				lnks.each(function(link) {
					link.removeClass('showblockon');
				});
				lnk.addClass('showblockon');
				divs.each(function(div, index) {
					if (index == mark) div.setStyle('display', 'block');
					else div.setStyle('display', 'none');
				});
			});
		});
	}
}

var setOpacities = function() {
	if ($('headpad')) $('headpad').set('opacity',0.6);
	if ($('footpad')) $('footpad').set('opacity',0.6);
}

var launchAccordion = function() {
	var togglers = new Array();
	var cnt;
	var acc = new Accordion('.toggler', 'div.js_content', {
		alwaysHide: true,
		//opacity: false,
		onActive: function(toggler, element) {
			//toggler.addClass("open");
		},
		onBackground: function(toggler, element) {
			togglers[cnt] = toggler;
			cnt++;					
		},
		onComplete: function(toggler, element) {
			var i;
			for (i = 0; i < togglers.length; i++) {
				//togglers[i].removeClass("open");
			}
			togglers.length = 0;
			cnt = 0;				
		}
	});
}

var audioLinking = function() {
	if ($('mediablock') && $('js_mediaplayer')) {
		var lnks = $$('.medialink');
		lnks.each(function(lnk) {
			lnk.addEvent('click', function(e) {
				e.stop();
				$('js_mediaplayer').set('src', lnk.get('href'));
			});
		});
	}
}

var textTog = function(element, text){
	if (element.get('value') == '') element.set('value', text);
	element.addEvent('click', function() {
		if (element.get('value') == text) element.set('value', '');
	});
	element.addEvent('blur', function() {
		if (element.get('value') == '') element.set('value', text);
	});
}

var setAudioDesc = function(id) {
	var req = new Request({
		method: 'get',  
		url: '/ajaxaudiodescription/' + id,
		onComplete: function(response) { 
			$('mediablurb').set('html', response);
			remoteURLs();
		}
	}).send();
}

var setVideoDesc = function(id) {
	var req = new Request({
		method: 'get',  
		url: '/ajaxvideodescription/' + id,
		onComplete: function(response) { 
			$('mediablurb').set('html', response);
			remoteURLs();
		}
	}).send();
}

var formTags = function() {
	if ($('contactblockform')) {
		textTog($('js_firstname'), 'First name'); 
		textTog($('js_lastname'), 'Last name'); 
		textTog($('js_title'), 'Title'); 
		textTog($('js_address1'), 'Address'); 
		textTog($('js_email'), 'Email'); 
		textTog($('js_phone'), 'Phone'); 
		textTog($('js_town'), 'Town/City');
		textTog($('js_county'), 'County/State');
		textTog($('js_country'), 'Country');
		textTog($('js_postcode'), 'Postcode/Zip code');
	}
}

var paginateGallery = function() {
	if ($('gallerywindow')) {
		var imgs = $$('#gallerywindow img');
		var leftmargin = 0;
		if ($('newsgallery')) {
			var galleryWidth = 200;
			var pages = Math.ceil(imgs.length/3);
		} else {
			var galleryWidth = 266;
			var pages = Math.ceil(imgs.length/4);
		}

		if (pages > 1) {
		$('prevgallery').setStyle('background', 'transparent url(/x/img/static/gallery-scroll-bwd.gif) no-repeat 1px 25px;');
		$('prevgallery').setStyle('cursor', 'pointer');
		$('nextgallery').setStyle('background', 'transparent url(/x/img/static/gallery-scroll-fwd.gif) no-repeat 0px 25px;');
		$('nextgallery').setStyle('cursor', 'pointer');
			$('nextgallery').addEvent('click', function() {
				leftmargin -= galleryWidth;
				if (leftmargin <= (pages * -galleryWidth)) leftmargin = 0;
				$('gallerywindow').setStyle('margin-left', leftmargin + 'px');
			});
			$('prevgallery').addEvent('click', function() {
				leftmargin += galleryWidth;
				if (leftmargin > 0) leftmargin = ((pages-1) * -galleryWidth);
				$('gallerywindow').setStyle('margin-left', leftmargin + 'px');
			});
		}
	}
}

var isdefined = function(variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

var videoControlsBg = function() {
	if ($('video')) {
		var imgs = $$('.image img');
		if (imgs.length > 0) {
			var url = imgs[0].get('rel');
			if (url != '') {
				$('video').setStyle('background', 'transparent url(/pic/155/'+url+') no-repeat scroll 0px 0px');
			}
		}
	}
}

var setVideoBgPic = function() {
	if (videoBgPic != '' && $('video')) {
		$('video').setStyle('background', '#333333 url(/pic/180/' + videoBgPic + ') no-repeat');
	}
}

var remoteURLs = function() {
	var lnks = $$('#details a, #mediacopy a');
	lnks.each(function(lnk) {
		lnk.addEvent('click', function(e) {
			var href = lnk.get('href');
			var clss = lnk.get('class');
			if (clss == 'js_remote' || href.match('^http:')) {
				e.stop();
				var newWindow = window.open(href, '_blank');
				newWindow.focus();
				return false;
			}
		});
	});
	var joblnks = $$('.job a');
	joblnks.each(function(joblnk) {
		joblnk.addEvent('click', function(e) {
			e.stop();
			var href = joblnk.get('href');
			var newWindow = window.open(href, '_blank');
			newWindow.focus();
			return false;
		});
	});
}

window.addEvent('domready', function(){
	setn();
	changeBgrd();
	fixHeight();
	showCal();
	primeSearch();
	new UvumiDropdown('sitenav');
	showDetailBlocks();
	setOpacities();
	launchAccordion();
	audioLinking();
	formTags();
	paginateGallery();
	videoControlsBg();
	setVideoBgPic();
	remoteURLs();
});

window.addEvent('resize', fixHeight);
