/*
« Copyright 2007 Max Barel a_x@ac-mb.info»

This file is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this file; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

var point_rouge
	, img_cour
	, img_suiv
	, img_prec
	, commande
	, sable
	, marche
	, n_cour
	, etiq_span
	;

$(function () {
	//init point rouge
	point_rouge = $("#point_rouge");
	if (items[0].pt_rouge != '1') point_rouge.hide();
	etiq_span =  $("#case_menu_A p.etiquette span");
	
	//init images
	img_cour = $("#img_cour");
	img_suiv = $("#img_suiv").hide();
	img_prec = $("#img_prec").hide();
	// img_suiv.hide(); img_prec.hide();
	//commandes
	commande = $('img', $("#commande"));
	aff_commande();

	//IE6 png
	if ($.browser.msie && $.ifixpng)  {
		$.ifixpng('js/pixel.gif');
		$('img[@src$=.png]', img_cour).ifixpng('scale');
		// commande.ifixpng('scale');
	}

	// commande.mouseover(function () { clearTimeout(decompte_cmd); $(document).unbind('mousemove'); });
	// commande.mouseout(function () {alert('hover out commande')}); //masquage_commande
	$("#marche").hover(function () { clearTimeout(decompte_cmd); $(document).unbind('mousemove'); }, masquage_commande); //function () {alert('hover out commande')}
	$("#prec").hover(function () { commande.attr('src', marche ?'views/prev.png' : 'views/play_prev.png'); clearTimeout(decompte_cmd); $(document).unbind('mousemove'); }
		, function () { commande.attr('src',  marche ? 'views/pause.png' : 'views/play.png'); masquage_commande(); });
	$("#suiv").hover(function () { commande.attr('src', marche ? 'views/next.png' : 'views/play_next.png'); clearTimeout(decompte_cmd); $(document).unbind('mousemove'); }
		, function () { commande.attr('src',  marche ? 'views/pause.png' : 'views/play.png'); masquage_commande(); });

	// $("#marche").toggle(arreter, demarrer); //interaction avec touches
	$("#marche, .diapo img").click(function () { if (marche) arreter(); else demarrer(); });
	$("#suiv").click(function () { if (marche) sable.stop(); dia_suiv(); });
	$("#prec").click(function () { if (marche) sable.stop(); dia_prec(); });

	//clavier
	// $(document).keypress(function (ev) {
	$(document).keyup(function (ev) {
		switch (ev.which) {
			case 32 :
				if (marche) arreter(); else demarrer();
				break;
			case 37 : //prec
			case 63234 : //suiv
				if (marche) sable.stop(); dia_prec();
				commande.attr('src', marche ? 'views/play_prev.png' : 'views/prev.png');
				break;
			case 39 : //suiv
			case 63235 : //suiv
				if (marche) sable.stop(); dia_suiv();
				commande.attr('src', marche ? 'views/play_next.png' : 'views/next.png');
				break;
			default :
				// alert(ev.which);
		}
		aff_commande();
	});
	
	//amorce diapo
	n_cour = 0;
	sable = $('#sable');
	$('img', img_cour).ready(function () {
		demarrer();
		$('img', img_suiv).attr('src', 'images_m/'+items[(n_cour + 1) % items.length].image) ;
		$('img', img_prec).attr('src', 'images_m/'+items[(n_cour + items.length - 1) % items.length].image);
	});
})

function demarrer() {
	marche = true;
	sable.animate({width: 144}, 5000, null, dia_suiv);
	commande.attr('src', 'views/play.png');
}

function arreter() {
	marche = false;
	sable.stop();
	sable.animate({width: 0}, 'fast');
	commande.attr('src', 'views/pause.png');
}

function aff_commande () {
	commande.show();
	masquage_commande();
}

function masquage_commande() {
	decompte_cmd = setTimeout(function () {
		commande.fadeOut(function(){ commande.attr('src',  marche ? 'views/pause.png' : 'views/play.png'); });
	}, 1000);
	$(document).mousemove(function () {
		commande.show();
		clearTimeout(decompte_cmd);
		decompte_cmd = setTimeout(function () {
			commande.fadeOut(function(){ commande.attr('src',  marche ? 'views/pause.png' : 'views/play.png'); });
		}, 1000);
	});	
}

function dia_suiv() {
	sable.animate({width: 0}, 'fast');
	n_cour = (n_cour + 1) % items.length;
	if (n_cour == 0) {
		marche = false;
		commande.attr('src', 'views/play.png');
	}
	if ($.browser.msie && $.ifixpng) {
		img_suiv.show();
		$('img[@src$=.png]', img_suiv).ifixpng('scale');
		img_suiv.hide();
	}
	img_suiv.fadeIn(1500);
	img_cour.fadeOut(1500, function () {
		etiq_span.eq(0).text(items[n_cour].indice + '/' + items.length);
		etiq_span.eq(1).text(items[n_cour].technique);
		etiq_span.eq(2).text(items[n_cour].date);
		etiq_span.eq(3).text(items[n_cour].dimensions);
		if (items[n_cour].pt_rouge != '1') point_rouge.hide();
		else point_rouge.show();
		img_tmp = img_prec;
		img_prec = img_cour;
		img_cour = img_suiv;
		img_suiv = img_tmp;
		if (marche) sable.animate({width: 144}, 5000, null, dia_suiv);
		$(' img', img_suiv).attr('src', 'images_m/'+items[(n_cour+1) % items.length].image);
	});
}

function dia_prec() {
	sable.animate({width: 0}, 'fast');
	n_cour = (n_cour + items.length - 1) % items.length;
	if ($.browser.msie && $.ifixpng) {
		img_prec.show();
		$('img[@src$=.png]', img_prec).ifixpng('scale');
		img_prec.hide();
	}
	img_prec.fadeIn(1500);
	img_cour.fadeOut(1500, function () {
		etiq_span.eq(0).text(items[n_cour].indice + '/' + items.length);
		etiq_span.eq(1).text(items[n_cour].technique);
		etiq_span.eq(2).text(items[n_cour].date);
		etiq_span.eq(3).text(items[n_cour].dimensions);
		if (items[n_cour].pt_rouge != '1') point_rouge.hide();
		else point_rouge.show();
		img_tmp = img_suiv;
		img_suiv = img_cour;
		img_cour = img_prec;
		img_prec = img_tmp;
		if (marche) sable.animate({width: 144}, 5000, null, dia_suiv);
		$(' img', img_prec).attr('src', 'images_m/'+items[(n_cour + items.length - 1) % items.length].image);
	});
}
