// JavaScript Document

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



Event.observe ( document, 'dom:loaded', init_gallery );

function init_gallery () {
	var current_image = readCookie('current_image');
	if ( current_image && $(current_image) ) {
		document.current = $(current_image);
		$('pack_in_clone').setStyle ({backgroundImage : 'url(' + document.current.select('img').first().src + ')'});
		$('description_in').update(document.current.select('.rotating_title').first().innerHTML);
	}
	setTimeout ( 'slideshow()', 8000 );
	$('play_stop_btn').observe('click', toggle_pause_play );
	$('arrow_left').observe('click', previous  );
	$('arrow_right').observe('click', next );
}


function slideshow() {
	if (document.current) { 
		if (document.current.next('.rotating_images') ) 
			document.current = document.current.next('.rotating_images');
		else 
			document.current = $$('.rotating_images').first();
	} else 
		document.current = $$('.rotating_images').first().next('.rotating_images');
	document.cookie='current_image=' + document.current.id;
	load_image();
	
	document.timer = setTimeout ( 'slideshow()', 8000 );
	
}

function load_image() {
	var a = new Element('img');
	$('navigation_loading').setStyle({visibility:'visible'});
	a.observe ( 'load', function() {
		
		$('pack_in_clone').fade({ afterFinish : function () {
			$('pack_in_clone').setStyle ({backgroundImage : 'url(' + a.src + ')'});
			$('description_in').update(document.current.select('.rotating_title').first().innerHTML);
			$('pack_in_clone').appear();
		
			$('navigation_loading').setStyle({visibility:'hidden'});
		}})
	});
	a.src = document.current.select('img').first().src;	
}

function toggle_pause_play() {
	if ( $('play_stop_btn').down('img').src.indexOf('play') > -1 ) {
		play();
	} else {
		pause();
	}
}

function pause() {
	clearTimeout( document.timer );
	document.timer = null;
	$('play_stop_btn').down('img').src = '/img/play_btn.png';
}

function play() {
	slideshow();
	document.timer = setTimeout ( 'slideshow()', 8000 );
	$('play_stop_btn').down('img').src = '/img/pause_btn.png';	
}

function previous() {
	if (document.current) { 
		if (document.current.previous('.rotating_images') ) 
			document.current = document.current.previous('.rotating_images');
		else 
			document.current = $$('.rotating_images').last();
	} else 
		document.current = $$('.rotating_images').last();
	document.cookie='current_image=' + document.current.id;
	pause();
	load_image();
}

function next() {
	if (document.current) { 
		if (document.current.next('.rotating_images') ) 
			document.current = document.current.next('.rotating_images');
		else 
			document.current = $$('.rotating_images').first();
	} else 
		document.current = $$('.rotating_images').first().next('.rotating_images');
	document.cookie='current_image=' + document.current.id;
	pause();
	load_image();
}
