// JavaScript Document




var H = {
	
	current : 0,
	duration : 1000,
	imgs : [],
	thumbs : [],
	int:'',
	
	loadData: function(){
		$.get( '/home/home.xml', null, H.dataLoaded );
	},
	
	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;
	},
	
	begin: function(){
		H.imgs = $('#home-banner-images img');
		H.imgs.hide();
		$('#home-banner-thumbs img:first').addClass('active');
		$('#home-banner-thumbs img').click( H.thumbClicked );
		H.current = 0;
		H.showImage();
		H.slideshow();
	},
	
	show: function(i){
		H.current = i;
		H.hideImage();
	},
	
	thumbClicked: function(){
		window.clearInterval(H.int);
		var i = parseInt( $(this).attr('order') );
		var th = $('#home-banner-thumbs img');
		th.removeClass('active');
		$(this).addClass('active');
		H.show( i );
									  
	},
	/*
	nextClicked: function(){
		var n;
		if( (H.current + 1) > (H.imgs.length - 1) ){
			n = 0;				
		}else{
			n = H.current + 1;	
		}
		$( $('#home-banner-thumbs img')[n] ).click();
	},
	
	prevClicked: function(){
		var n;
		if( (H.current - 1) < 0 ){
			n = H.imgs.length - 1;				
		}else{
			n = H.current - 1;	
		}
		$( $('#home-banner-thumbs img')[n] ).click();
	},
	*/
	showImage: function(){
		$('#home-banner-text').hide();
		var i = $('#home-banner-overlay');
		H.imgs.hide();
		var n = H.imgs[H.current];
		i.fadeOut(1000);
		$(n).fadeIn(1000, H.setText);
	},
	
	setText: function(){
		var h = $('#home-banner-text');
		h.text( H.imgs[H.current].title );
		h.css({'color' : $(H.imgs[H.current]).attr('color')});
		h.fadeIn();
		
	},
	
	hideImage: function(){
		$('home-banner-text').text('');
		$('#home-banner-overlay').fadeIn(600, H.showImage);
		/*
		var i = $('#home-banner-overlay');
		i.clip([i.height() / 2, i.width() / 2, i.height() / 2, i.width() / 2]);
		i.show();
		i.clip([0, i.width(), i.height(), 0], 1000, H.showImage);
		*/
	},
	
	slideshow: function(){
		H.int = window.setInterval(H.nextImage, 5000);
	},
	
	nextImage: function(){
		var n = H.current + 1;
		if( n > (H.imgs.length - 1) ) n = 0;
		$('#home-banner-thumbs img').removeClass('active');
		$( $('#home-banner-thumbs img')[n] ).addClass('active');
		H.show(n);
	},
	
	dataLoaded: function(data, txt, request){
		var els = H.getElements(data.documentElement, 'image');
		var imgs = [];
		var thumbs = [];
		$.each( els, function(i, el){
			imgs.push( $('<img src="'+el.getAttribute('src')+'" alt="Homepage Banner" title="'+el.getAttribute('title')+'" color="'+el.getAttribute('color')+'" />') );				  
			thumbs.push( $('<a href="javascript:void(0)"><img src="'+el.getAttribute('thumb')+'" alt="Homepage Thumb" order="'+i+'" /><a/>') );	
		});
		for( var i=0; i < imgs.length; i++ ){
			imgs[i].appendTo( '#home-banner-images');
			thumbs[i].appendTo( '#home-banner-thumbs' );
		}
		$('#home-banner-thumbs').append( '<div class="clear"></div>' );
		H.begin();
	},

	init: function(){
		H.loadData();
	}
	
}

$(document).ready( H.init );
