/* NOTE: the following code was extracted from the UFO source and extensively reworked/simplified */

/* Unobtrusive Flash Objects (UFO) v3.20 <http://www.bobbyvandersluis.com/ufo/>
	Copyright 2005, 2006 Bobby van der Sluis
	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

function createCSS(selector, declaration) {
	// test for IE
	var ua = navigator.userAgent.toLowerCase();
	var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));

	// create the style node for all browsers
	var style_node = document.createElement("style");
	style_node.setAttribute("type", "text/css");
	style_node.setAttribute("media", "screen"); 

	// append a rule for good browsers
	if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));

	// append the style node
	document.getElementsByTagName("head")[0].appendChild(style_node);

	// use alternative methods for IE
	if (isIE && document.styleSheets && document.styleSheets.length > 0) {
		var last_style_node = document.styleSheets[document.styleSheets.length - 1];
		if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
	}
};

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function getCookie(name) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie(name, path, domain) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function initializeList() {
	var thumbWindowSize = 10;
	var thumbWidth = 77;
	var numberOfThumbsToSlide = 1;
	
	var cookieName = ($('thumb-browser').getAttribute('class') + '-sliderPosition').replace(/[^\w]/g, '');
	
	
	var savedPosition = (getCookie(cookieName)) ? getCookie(cookieName) : '0';
		
	var thumbBrowser = $('thumb-browser');
	var thumbList = $('thumbs');
	var listItems = thumbList.getElementsByTagName('li');
	var detailsParagraph = $('details');
	
	var isScrollableContent = listItems.length > thumbWindowSize;
		
	// Create wrapper div needed for slider
	var wrapperDiv = document.createElement('div');
	wrapperDiv.id = 'wrapper-div';
	wrapperDiv.appendChild(thumbList);
	thumbBrowser.appendChild(wrapperDiv);
	thumbBrowser.insertBefore(wrapperDiv, detailsParagraph);
	
	
	if (isScrollableContent) {
		var leftArrow = document.createElement('a');
		var leftArrowText = document.createTextNode('Back');
		leftArrow.id = 'nav-left';
		leftArrow.setAttribute('href', '#');
		leftArrow.appendChild(leftArrowText);
	
	
		var rightArrow = document.createElement('a');
		var rightArrowText = document.createTextNode('Forward');
		rightArrow.id = 'nav-right';
		rightArrow.setAttribute('href', '#');
		rightArrow.appendChild(rightArrowText);
	
	
		var leftDiv = document.createElement('div');
		var rightDiv = document.createElement('div');
		leftDiv.id = 'left-div';
		rightDiv.id = 'right-div';
		leftDiv.appendChild(leftArrow);
		rightDiv.appendChild(rightArrow);
	
	
		thumbBrowser.appendChild(leftDiv);
		thumbBrowser.appendChild(rightDiv);
	
		thumbBrowser.insertBefore(leftDiv, wrapperDiv);
		thumbBrowser.insertBefore(rightDiv, detailsParagraph);
		
		var lock = false;

		var statusUpdateArrows = function () {
			if (lock) {
				leftArrow.className = 'deactivated';
				rightArrow.className = 'deactivated';
			}
			else if (parseInt(thumbList.style.left) == ((listItems.length - thumbWindowSize) * - thumbWidth)) {
				leftArrow.className = '';
				rightArrow.className = 'deactivated';
			}
			else if (parseInt(thumbList.style.left) == 0) {
				leftArrow.className = 'deactivated';
				rightArrow.className = '';
			}
			else {
				leftArrow.className = '';
				rightArrow.className = '';
			}
		}
	}
	
	// Apply CSS rules needed for the slider
	thumbBrowser.style.overflow = 'hidden';
	thumbBrowser.style.height = '79px';
	
	thumbList.style.width = '10000px';
	thumbList.style.height = '57px';
	thumbList.style.position = 'absolute';
	thumbList.style.top = '0';
	
	thumbList.style.left = savedPosition;
	
	var saveState = function () {
		setCookie(cookieName, thumbList.style.left)
	}
		
	if (isScrollableContent) {
		statusUpdateArrows();
		
		// Apply click events to navigational arrow buttons 
		rightArrow.onclick = function () {
			var finalPosition = parseInt(thumbList.style.left) - (thumbWidth * numberOfThumbsToSlide);
		
			var slide = function () {
				if (parseInt(thumbList.style.left) > finalPosition) {
					var newPosition = (parseInt(thumbList.style.left) - 6);
					newPosition = (newPosition <= finalPosition) ? finalPosition : newPosition;
					thumbList.style.left = newPosition + 'px';
					setTimeout(slide, 33);
				} else {
					lock = false;
					statusUpdateArrows();
					saveState();
					return true;
				}
			}
		
			if (!lock && parseInt(thumbList.style.left) != ((listItems.length - thumbWindowSize) * - thumbWidth)) {
				lock = true;
				statusUpdateArrows();
				slide();
			}
		
			return false;
		}
	
		leftArrow.onclick = function () {
			var finalPosition = parseInt(thumbList.style.left) + (thumbWidth * numberOfThumbsToSlide);
		
			var slide = function () {
				if (parseInt(thumbList.style.left) < finalPosition) {
					var newPosition = (parseInt(thumbList.style.left) + 6);
					newPosition = (newPosition >= finalPosition) ? finalPosition : newPosition;
					thumbList.style.left = newPosition + 'px';
					setTimeout(slide, 33);
				} else {
					lock = false;
					statusUpdateArrows();
					saveState();
					return true;
				}
			}
		
			if (!lock && parseInt(thumbList.style.left) != 0) {
				lock = true;
				statusUpdateArrows();
				slide();
			}
		
			return false;
		}
	}
	
	thumbList.style.margin = '0';
}

function initializeDetailsPane() {
	var detailsVisible = false;
	var lock = false;
	
	var imagePane = $('image-content');
	var detailsPane = $('scene-info');
	
	detailsPane.style.display = 'block';
	
	var detailsParagraph = $('details');
	var detailsLink = detailsParagraph.getElementsByTagName('a')[0];
	var printLink = $('print-link').getElementsByTagName('a')[0];
	
	var detailsWrapperDiv = document.createElement('div');
	detailsWrapperDiv.id = 'details-wrapper-div';
	detailsWrapperDiv.appendChild(detailsPane);
	imagePane.appendChild(detailsWrapperDiv);
	
	var detailsSlider = new Fx.Slide('scene-info', 
			{
				duration: 500,
				onStart: function () { lock = true ; },
				onComplete: function () { lock = false; }
			}
		);
	
	imagePane.style.position = 'relative';
	detailsWrapperDiv.style.position = 'absolute';
	detailsWrapperDiv.style.top = '0';
	detailsWrapperDiv.style.left = '0';
	
	detailsSlider.hide();
	
	detailsLink.onclick = function () {
		if (!lock) {
			detailsSlider.toggle();
			if (detailsVisible = !detailsVisible) {
				detailsParagraph.style.width = '223px';
				detailsLink.style.backgroundImage = "url('/images/inspiration-gallery/hide-details.gif')";
			} else {
				detailsParagraph.style.width = '274px';
				detailsLink.style.backgroundImage = "url('/images/inspiration-gallery/get-details.gif')";
			}
		}
		return false;
	}
	
	printLink.onclick = function() {
    window.print();
    return false;
  }
	
}

addLoadEvent(initializeList);
addLoadEvent(initializeDetailsPane);

createCSS('#content #scene-info', 'display: none');
createCSS('#content #thumb-browser', 'height: 79px; overflow: hidden;');
