// JavaScript Document




var S = {
	
	duration: 1000,
	begin: true,
	xml: '',
	current: -1,
	total:-1,
	int: '',
	images:[],
	
	slideshow: function(){
		var i = S.current + 1;
		if( i > S.total ) i = 0;
		S.goTo(i);
	},
	
	beginSlideshow: function(){
		S.int = window.setInterval( S.slideshow, 5000 );
	},
	
	endSlideshow: function(){
		window.clearInterval( S.int );
	},
	
	goTo: function(i){
		if( S.current == i ) return;
		S.current = i;
		if(arguments.length == 1){
			S.show1(false);
		}else{
			S.show1(true);
		}
	},
	
	arrowClick: function(){
		var i;
		if( this.id == 'slideshow_next' ){
			i = S.current + 1;
		}else if( this.id == 'slideshow_prev'){
			i = S.current - 1;	
		}
		if( i < 0 ) i = $('#slideshow-buttons a').length - 1;
		if( i > ( $('#slideshow-buttons a').length - 1) ) i = 0;
		S.goTo(i);
	},
	
	hide: function(){
		$('#slideshow h2').text('');
	},
	
	show1: function(first){
		//$('#slideshow img').fadeOut(S.duration);
		//$('#slideshow h2').clip([70, 936, 70, 0 ], S.duration, S.setData);
		//$('#slideshow h3').clip([0, 936, 0, 0], S.duration);
		//$('#slideshow h2').fadeOut(S.duration, S.setData);
	//	$('#slideshow h3').fadeOut(S.duration, S.setData);
		S.setData(first);
	},
	
	setData: function(first){
		var s = S.getSlide(S.current);
		$('#slideshow h2').text( S.getText(S.getElement(s, 'title')) );
		S.setPosition('#slideshow h2', s, 'title');
		//$('#slideshow h3').text( S.getText(S.getElement(s, 'strapline')) );
		//S.setPosition('#slideshow h3', s, 'strapline');
		var img = $('<img src="' + s.getAttribute('img') + '" />');
		img.hide();
		$('#slideshow img').after(img);
		S.show2(first);
	},
	
	setPosition: function(selector, node, name){
		var el = $(selector);
		var n = S.getElement(node, name).attributes;
		var css = {};
		for(var i = 0; i< n.length; i++){
			css[n[i].name] = n[i].value;	
		}
		el.css({'left':'auto', 'right':'auto', 'top':'auto', 'bottom':'auto'});
		el.css(css);
	},
	
	show2: function(first){
		if(first){
			$('#slideshow img:first').hide();
			$('#slideshow img:last').show();
			S.show3();	
		}else{
			$('#slideshow img:first').fadeOut(S.duration*1.5, S.show3);
			$('#slideshow img:last').fadeIn(S.duration*1.5);
		}
	},
	
	show3: function(){
		//$('#slideshow h2').clip([ 0,936,70,0 ], S.duration);
		//$('#slideshow h3').clip([ 0, 936, 40, 0], S.duration);
		$('#slideshow h2').fadeIn(S.duration);
		$('#slideshow img:first').remove();
	},
	
	showButtons: function(){
		var a = 400;
		var c = 1;
		for(var i=$('#slideshow-buttons a').length; i >= 0; i--){
			$( $('#slideshow-buttons a')[i] ).fadeIn(a*c);
			c++
		}
	},
	
	
	getElement: function(node, name){
		return S.getElements(node, name)[0];	
	},
	
	getElements:function(node, name){
		var c = node.childNodes;
		var r = [];
		for(var i=0; i<c.length; i++){
			if(c[i].nodeName == name){
				r.push(c[i]);
			}
		}
		return r;
	},
	
	getAttribute: function(node, name, attr){
		var el = S.getElement(node, name);
		return el.hasAttribute(attr) ? el.getAttribute(attr) : false;
	},
	
	getText:function(node){
		var c = node.childNodes;
		var r = '';
		for(var i=0; i<c.length; i++){
			if(c[i].nodeType == 3){
				r = c[i].nodeValue;
			}
		}
		return r;
	},
	
	getSlide: function(i){
		return S.getElements(S.xml, 'slide')[i];	
	},
	
	dataLoaded: function(data, txt, request){
		S.xml = data.documentElement;
		var a;
		for( var i=0; i < S.getElements(S.xml, 'slide').length; i++){
			S.images.push( new Image().src = S.getSlide(i).getAttribute('img') );
			S.total++;
		//	a = $('<a href="javascript:void(0)" onclick="S.goTo('+i+')" index="'+i+'">'+(i+1)+'</a>');
		//	a.hide();
		//	$('#slideshow-buttons').prepend(a);
		}
		//$('.slideshow-arrow').click( S.arrowClick );
		S.beginSlideshow();
		S.goTo(0, true);
	},
	
	loadData: function(){
		//$.get( 'images/slideshow/slideshow.xml', null, S.dataLoaded );
		$.get( '/index.php/home/slideshow.xml', null, S.dataLoaded );
	},
	
	init: function(){
		S.hide();
		S.loadData();
	}
	
	
}



$(document).ready( S.init );
