//Preview - Home page
$(document).ready(function(e){
	var $mySlideShowC = $('#blog_highlight');
	if($mySlideShowC.is('div')){
		var mySlideShow = new WWASlideShow($mySlideShowC);
	}
});

/* Constructeur */
/* @params container : elt html contenant le slide show */
/* @params delay : temps em millisecondes entre chaque animation */
function WWASlideShow(container, delay){
	this.container = container;
	this.active = false;
	this.items = new Array();
	this.interval = false;
	this.delay = delay || 10000;
	this.process = false;
	this.data = new Array();
	this.initialize();
};

/* initialise les parametres du slide show */
WWASlideShow.prototype.initialize = function(){
	if($.browser.msie){
		var version = $.browser.version;
		if(version.split('.')[0] <= 6){
			this.initDisplayImage();
			return;
		}
	}
	
	this.data = eval($('.data', this.container).text());
	var items = $('#previews a', this.container);
	var that = this;
	for(var i = 0; i < items.length; i++){
		var myItem = items[i];
		if(!$(myItem).is('a')) continue;
		$(myItem).click(function(e){
			that.doSlideShow(this, true);
			return false;
		});
		this.items.push(myItem);
	}
	this.setActive(0);
	this.initDisplayImage();
	this.setIntervalSlideShow();
};

WWASlideShow.prototype.initDisplayImage = function(){
	var $container = $('.previews span', this.container);
	var src = $('img', $container).attr('src');
	$container.css('background-image','url('+ src +')');
};

/* set la classe active pour l'item du menu gauche */
/* correspondant a la photo affichee */
WWASlideShow.prototype.setActive = function(index){
	if(this.items.length){
		var $elt = $(this.items[index]);
		if(!$elt.hasClass('active')){
			for(var i = 0; i < this.items.length; i++){
				var myItem = this.items[i];
				$(myItem).removeClass('active');		
			}
		}
		$elt.addClass('active');
		this.active = index;
	}
};

/* effectue l'animation entre chaque photo sur un click utilisateur */
WWASlideShow.prototype.doSlideShow = function(elt, byClick){
	if(byClick) this.removeIntervalSlideShow();
	var that = this;
	var $elt = ($(elt).is('a'))? $(elt) : $($(elt).parents('img').eq(0));
	if($elt.attr('rel') == this.active || this.process) return;
	var current = this.items[$elt.attr('rel')];
	if($elt.attr('rel') == 0) current.isLoaded = true;
	var $container = $('.previews span', this.container);
	var src = $('img', $elt).attr('rel');
	if(!current.isLoaded){
		var $loadIMG = $(new Image());
		$loadIMG.attr('src', src).bind('load', function(e){
			that.animate($container, src, $elt, current);
		});	
	}
	else this.animate($container, src, $elt, current);
	return false;
};

WWASlideShow.prototype.animate = function(container, src, elt, cItem){
	var that = this;
	this.process = true;
	var timer = setTimeout(function(){
	if(timer) clearTimeout(timer);
		container.fadeOut(300, function(){
			container.css('background-image', 'url('+ src +')');
			that.setActive(elt.attr('rel'));
			$('.highlight p:first').html(that.buildHTML(that.active, 'baseline'));
			$('.slider .content').html(that.buildHTML(that.active, 'default'));			
			container.fadeIn(300, function(){
				cItem.isLoaded = true;
				that.process = false;
			});			
		});			
	}, 700);
};

WWASlideShow.prototype.buildHTML = function(index, type){
	var html = '';
	switch(type){
		case 'baseline':
			html += '<em class="first">&nbsp;</em>' + this.data[index].baseline +'<em class="last">&nbsp;</em>';
		break;
		default: 
			html += '<h2><a href="' + this.data[index].url + '">' + this.data[index].title + '</a></h2>';
			html += '<p>' + this.data[index].resume + '</p>';
			html += '<a href="' + this.data[index].url + '" class="read_more">' + this.data[index].label + '<em>&raquo;</em></a>';			
		break;
	}
	return html;
};

/* effectue automatiquement a intervales fixes la transition entre photo */
WWASlideShow.prototype.autoSlideShow = function(){
	for(var i = 0; i < this.items.length; i++){
		var myItem = this.items[i];
		if($(myItem).hasClass('active')){
			var nextElt = (i == (this.items.length - 1))? this.items[0] : this.items[i + 1];
			this.doSlideShow(nextElt);	
			break;
		}
	}
};

/* initialise le delay d'execution des animations automatiques */
WWASlideShow.prototype.setIntervalSlideShow = function(){
	var that = this;
	this.interval = window.setInterval(function(){
		that.autoSlideShow();																
	}, that.delay);
};

/* supprime le delay d'execution des animations automatiques */
WWASlideShow.prototype.removeIntervalSlideShow = function(){
	if(this.interval) window.clearInterval(this.interval);
	this.setIntervalSlideShow();
};