/*
	
	Copyright 2007 Artlogic Media Limited - http://www.artlogic.net/
	All rights reserved.

*/


//	COOKIE FUNCTIONS


	function setCookie(cookieName,cookieValue,nDays,path) {
		var today = new Date();
		var expire = new Date();
		if (nDays==null || nDays==0) {
			nDays=1;
		}
		expire.setTime(today.getTime() + 3600000*24*nDays);
		if (typeof path=="undefined") {
			document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();
		} else {
			document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString()+";path="+escape(path);
		}
	}

	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));
	}



//	ARE COOKIES ENABLED?

	var cookiesEnabled = true;
	
	setCookie("cookiesEnabled", "true", 30, "/");
	if (readCookie(cookiesEnabled) == "" ) {
		cookiesEnabled = false;
	}




//	PAGE FUNCTIONS

	doOnLoadOnResize = function() {
	
		//	the variables 'responsePath', 'section' and 'currentPage' are
		//	set in the header of the main template
	
		if (responsePath == "/collection/") { 
			
			initHomepage();
		
		}
		
		resizePage();
	
	}
	
	
	if (responsePath == "/collection/") { 
		
		initHomepage = function() {
		
			//	home page script
			
				slides.onChange = function(obj) {
					$("slideshowCaption").innerHTML = (fl_captions[obj.slides[obj.current].image]) ? fl_captions[obj.slides[obj.current].image] : "";
					//moveHomeHighlight(obj.current, obj.slides.length);
				}
				
				// set initial caption
				$("slideshowCaption").innerHTML = slides.slides[0].caption;
				$("slideshowCaption").innerHTML = (fl_captions[slides.slides[0].image]) ? fl_captions[slides.slides[0].image] : "";
		
				//setupHomeHighlights();
		
		}
		
		var thumbwidth = 68;
		var highlightOffsetLeft = 2;
				
		var setupHomeHighlights = function() {
			var obj = $("slideshowThumbs");
			var thumbImages = document.getElementsByClassName("thumbImage", obj);
			var thumbwidths = ((thumbwidth * thumbImages.length) + (2 * thumbImages.length) + 42) + "px";
			obj.style.position = "relative";
			obj.style.width = thumbwidths;
			var highlightEl = new Insertion.Bottom(obj, '\
				<div id="slideshowThumbs_highlight" class="slideshowThumbs_highlights" style="position: absolute; top: 0; left: 0px; padding-left: 2px;">\
					<img src="/site/images/home_highlight_blue.gif" width="68" height="30" border="" alt=" " />\
				</div>\
				');
		}
		
		var moveHomeHighlight = function(current, noOfSlides) {
			var movement = false;
			if (movement) {
				// move the highlight object to the next slide (slightly buggy)
				var lefts = [0];
				var leftTotal = 0
				for (var i = 0; i < noOfSlides; i ++) {
					leftTotal += (thumbwidth + 2);
					lefts[lefts.length] = leftTotal
				}
				var el = $("slideshowThumbs_highlight");
				new Effect.Move(el, { x: lefts[current] - parseInt(el.style.left), y: 0, transition: Effect.Transitions.sinoidal, delay: 0.8, duration: 0.7});
			} else {
				// activate a different slide highlight
			}
		}
	
	}


//	FUNCTIONS WHICH STOP AND START THE SLIDE SHOWS

	/* not used in this site...

	var pauseImage = new Image(); pauseImage.src = "/site/images/pause-button.gif";
	var playImage = new Image(); playImage.src = "/site/images/play-button.gif";
	
	var goTo = function(n) {
		slides.gotoSlide(n); 
		slides.stop(); 
		$("stopStartImage").src = playImage.src;
	}
	
	var togglePlayImage = function() {
		if (slides.paused) {
			slides.start();
			$("stopStartImage").src = pauseImage.src;
		} else {
			slides.stop();
			$("stopStartImage").src = playImage.src;
		}
	}
	
	*/




//	FUNCTIONS WHICH HANDLE THE WINDOW RESIZE

	var resizePage = function() {
		if (cookiesEnabled) {	
			var winW = (document.body.clientWidth) ? document.body.clientWidth : window.innerWidth;
			var winH = (document.body.clientHeight) ? document.body.clientHeight : window.innerHeight;
			var offsetHorizontal = 60;
			var offsetVertical = 20
			/*	Note: Firefox does not correctly calculate the inner height (it only calculates the *used* content), so we just test for the width. */
			var winSize = (winW < 1205 + offsetHorizontal) ? "small" : "large";
			var prevWinSize = readCookie("winSize");		
			if (prevWinSize != winSize) {
				setCookie("winSize", winSize, 30, "/");
				document.body.style.visibility = "hidden";
				document.location.href = document.location.href.split("#")[0].split("?")[0];
		}
		}
	}


//	FUNCTIONS WHICH HANDLE SCOLLBAR HANDLE ROLLOVERS

	scrollbarImage = new Image(); scrollbarImage.src = "/site/images/scroller.gif";
	scrollbarImageOver = new Image(); scrollbarImageOver.src = "/site/images/scroller_over.gif";

	function scrollHandleOver(obj) {
		obj.style.cursor = "pointer";
		obj.style.backgroundImage = "url(/site/images/scroller_over.gif)"
	}

	function scrollHandleOut(obj) {
		obj.style.cursor = "default";
		obj.style.backgroundImage = "url(/site/images/scroller.gif)"
	}



//	FUNCTIONS FOR LOADING / PLAYING MOVIES...

	var loadMovie = function(url, w, h) {
		if (slides) {
			if (slides.stop) {
				slides.stop();	
			}
			if ($("footerToolbar")) {
				$("footerToolbar").style.display = "none";
			}
		}
		url = unescape(url);
		w = parseInt(unescape(w));
		h = parseInt(unescape(h)) + 16;
		var html = '<div style="float: right">\
					<object width="' + w + '" height="' + h + '" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">\
					<param name="src" value="' + url + '">\
					<param name="controller" value="true">\
					<param name="autoplay" value="true">\
					<embed src="' + url + '" width="' + w + '" height="' + h + '" autoplay="true" controller="true" pluginspage="http://www.apple.com/quicktime/download/">\
					</embed>\
					</object>\
					</div>';
		$("projectSlideshow").innerHTML = html;
	}



//	FUNCTIONS FOR DISPLAYING ARTISTS IN EXHIBITION PAGES

	var exhibitionDetails = "";

	var exhibitions_showArtistDetails = function(artist) {
		if (exhibitionDetails == "") { // store original details
			exhibitionDetails = $('sidebarContents').innerHTML;
		}
		var url = "/logic/ajax/artist_details/?artist=" + artist;
		new Ajax.Updater('sidebarAjaxContents', url, {asynchronous:true, evalScripts: true});
		$('sidebarAjaxContents').style.display = 'block';
		$('sidebarContents').style.display = 'none';
	}
	
	var exhibitions_reset = function() {
		if (exhibitionDetails) {
			$('sidebarAjaxContents').style.display = 'none';
			$('sidebarContents').style.display = 'block';
		}
	}