/*
	Custom Banner Specific to Anders + Kern
	---------------------------------------
*/
var Azexis = Azexis || {};

/*
	Home Banner
	-----------------
*/
Azexis.homeBanner = new (function(){
	Azexis.eventDispatcher(this);
	
	this.slideShow = null;
	this.listItemSelector = '#Banner .menu-items li';
	this.lastImageRequest = null;
	this.items = null;
	this.isLoaded = false;
	
	this.load = function(){
		var tabs = $("#FeatureTabs").tabs();
		tabs.children().bind('click', function(e){
											   		Azexis.homeBanner.build($('a:first', this).attr('href'));
												});
		this.build('#FeaturedItems');
		this.isLoaded = true;
	};
	
	this.build = function(id){
		this.listItemSelector = id + ' li';
		var images = [];
		var items = [];
		var i = 0;
		$(this.listItemSelector).each(function(){
			var li = $(this);
			images.push({'image':$('img:first', li).attr('src'),'video':''});
			items.push({
					   	href: $('a:first', li).attr('href'),
						title: $('.label:first', li).text(),
						description: $('.description:first', li).text(),
						src: $('img:first', li).attr('src')
					   });
			// change the image on click
			$('a:first', li).attr('href', 'javascript:Azexis.homeBanner.changeImage('+i+');');
			//$('a:first', li).click(function(e){Azexis.homeBanner.changeImage(i); e.preventDefault();});
			++i;
		});
		this.items = items;
		if(!this.isLoaded){
			this.slideShow = Azexis.slideshow($("#Photo"), images);
			this.slideShow.addListener('onImageChange', function(e){Azexis.homeBanner.onImageChange(e);});
			this.slideShow.addListener('onBeforeImageChange', function(e){Azexis.homeBanner.onBeforeImageChange(e);});
			this.slideShow.addListener('onAfterImageChange', function(e){Azexis.homeBanner.onAfterImageChange(e);});
		} else {
			this.lastImageRequest = null;
			this.hideInfoBar(function(e){ Azexis.homeBanner.slideShow.loadImageSequence(images); });
		}
	};
	
	this.changeImage = function(index){
		this.hideInfoBar(function(e){ Azexis.homeBanner.slideShow.moveTo(index); });
	};
	
	
	this.onBeforeImageChange = function(e){
		// pause the slideShow whilst we do some stuff
		if(this.lastImageRequest != e.imageId){
			this.lastImageRequest = e.imageId;
			this.slideShow.pause();
			this.hideInfoBar(function(e){ Azexis.homeBanner.slideShow.resume(); });
		}
	};
	
	this.onImageChange = function(e){
		var index = e.imageId;
		$(this.listItemSelector).each(function(){
			var li = $(this);
			li.removeClass('active');
			if($('img:first', li).attr('src') == e.src){
				// select the curent item	
				li.addClass('active');
			}
		});
		
		// change the info-bar
		var bar = $('#PhotoMask .info-bar:first');
		$('.inner p:first', bar).text( this.items[index].description);
		$('.inner h2:first', bar).text( this.items[index].title);
		$('.right a:first', bar).attr('href', this.items[index].href);
		bar.css('height', $('.left', bar).height());
	};
	
	this.onAfterImageChange = function(e){
		// pause the slideShow whilst we do some stuff
		this.showInfoBar(null);
	};
	
	this.showInfoBar = function(onComplete){
		var bar = $('#PhotoMask .info-bar:first');
		bar.stop();
		bar.css('height', bar.height());
		var posY = ($('#Photo').height() - bar.height());
		bar.animate(
					{ top: posY },
					300,
					"swing",
					onComplete
					);
	};
	
	this.hideInfoBar = function(onComplete){
		var bar = $('#PhotoMask .info-bar:first');
		bar.stop();
		bar.css('height', bar.height());
		bar.css('top', bar.position().top);
		bar.animate(
						{ top: $('#Photo').height() },
						300,
						"swing",
						onComplete
					);
	};
});

/*
	On Ready Event
	---------------
*/
var slider = null;
$(document).ready(function() {
	Azexis.homeBanner.load();
	$('ul.tabbed-content').tabs();
	slider = Azexis.slides($('#Manufacturers'));
});

