// JavaScript Document


var S = {
	
	duration: 1000,
	hold: 5000,
	txt: '',
	current: -1,
	auto: true,
	int:'',
	working : false,
	
	sortout: function(index){
		$('#homepage-slider img').each( function(i){
			if( i != index ){
				$(this).css('opacity', 0.7);
				$(this).css('zIndex', 1);
				$(this).mouseover( function(){
						$(this).css('opacity', 1);
						$(this).css('cursor', 'pointer');
				});
				$(this).mouseout( function(){
						$(this).css('opacity', 0.7);	
						$(this).css('cursor', 'auto');
				});
				$(this).click( S.handleClick );
			}else{
				$(this).css('opacity', 1);
				$(this).css('zIndex', 10);
				$(this).unbind('mouseover');
				$(this).unbind('mouseout');
				$(this).unbind('click');
			}
		});
		var l = $('#homepage-slider-buttons a.button')[index]; 
		$('#homepage-slider-buttons a.button').removeClass('active');
		$(l).addClass('active');
	},
	
	openImage: function(index){
		if( index == S.current ) return;
		S.working = true;
		var px = 0;
		var total = $('#homepage-slider img').length;
		S.hideText();
		var i, a, w, d;
		$('#homepage-slider img').each( function(){
			i = parseInt( $(this).attr('index') );
			if( i < index ){
				$(this).animate({
					'clip' : 'rect(0px ' + ((i*50) + 50) + 'px ' + $(this).height() + 'px ' + (i*50) + 'px)'
				}, {
					'duration' : S.duration
					
				});
				px = px + 50;				
			}else if( i == index){
				a = (total - (i + 1) ) * 50;
				w = $(this).width() - a;
				$(this).animate({
					'clip' : 'rect(0px ' + w + 'px ' + $(this).height() + 'px ' + (i * 50) + 'px)'
				}, {
					'duration' : S.duration
				});
				S.txt = $(this).attr('caption');
				px = 0;
			}else if(i > index){
				d = i - index;
				px = w + (d*50);
				$(this).animate({
					'clip' : 'rect(0px ' + px + 'px ' + $(this).height() + 'px ' + (px - 50) + 'px)'
				}, {
					'duration' : S.duration
				});
			}
		});
		S.current = index;
		S.sortout(index);
	},
	
	showTextManual: function(){
		var el = $('#homepage-slider-text');
		el.html(S.txt);
	},
	
	hideText: function(){
		var el = $('#homepage-slider-text');
		el.fadeOut(S.duration, S.showText);
	},
	
	showText: function(){
		var el = $('#homepage-slider-text');
		el.html(S.txt);
		el.hide();
		el.fadeIn(S.duration / 2, S.afterFade);
	},
	
	afterFade: function(){
		$('#homepage-slider-text p').css('opacity', '1');
	},
	
	handleClick: function(){
		S.stopSlideshow();
		var index = 0;
		var el = this;
		$('#homepage-slider img').each( function(i){
			if( this == el ) index = i;										 
		});
		S.openImage(index);
	},
	
	buttonClick: function(e){
		e.preventDefault();
		S.stopSlideshow();
		var n = parseInt( this.id.replace('button', '') );	
		S.openImage(n - 1);
	},
	
	gotoNext: function(e){
		S.working = true;
		e.preventDefault();
		S.stopSlideshow();
		var n = S.current + 1 > ($('#homepage-slider img').length - 1) ? 0 : S.current + 1;
		S.openImage(n);
	},
	
	gotoPrev: function(e){
		e.preventDefault();
		S.stopSlideshow();
		var n = S.current - 1 < 0 ? $('#homepage-slider img').length - 1 : S.current - 1;
		S.openImage(n);
	},
	
	slideshow: function(){
		var n = S.current + 1 > ($('#homepage-slider img').length - 1) ? 0 : S.current + 1;
		S.openImage(n);
	},
	
	beginSlideshow: function(){
		S.int = window.setInterval(S.slideshow, S.hold);	
	},
	
	stopSlideshow: function(){
		window.clearInterval(S.int);
	},
	
	init: function(){
		$('#homepage-slider').animate({'opacity':1}, {duration:800});
		$('#homepage-slider img').each( function(i){
			this.style.clip =  'rect(0px ' + $(this).width() + 'px ' + $(this).height() + 'px 0px)';
			$(this).attr('caption', this.title);
			$(this).attr('index', i);
			this.title = '';
		});
		$('#homepage-slider-text').queue('fx');
		$('#homepage-slider-buttons a.button').click( S.buttonClick );
		$('#homepage-slider-buttons a.right').click( S.gotoNext );
		$('#homepage-slider-buttons a.left').click( S.gotoPrev );
		window.setTimeout(S.beginSlideshow, S.hold);
		S.openImage(0);
	}
	
	
}

$(window).load( S.init );
