// Liste des images avec leur lien et leur titre. 

/* Utilise cette structure d'information :
 * img: "Nom de l'image avec son extention"
 * url: "Lien de l'image"
 * title: "Titre de l'image" (en général la marque)
 * On sépare les attributs img, url et title par le caractère "," (virgule).
 * Le tout doit être contenu dans des accolades "{ Attributs }".
 * 
 *  Exemple:
 *  { img: "xxx.gif", url: "http://www.lightonline.fr/catalogue/recherche?q=xxx", title: "xxx" }
 * 
 * Attention :
 *  -> "{ Attributs }" affiche une seul image
 *  -> rajouter une virgule après la dernière accolades pour rajouter une autre image
 *  -> la dernière image ne doit pas avoir de virgule (en fin d'accolades)
 *  
 *  Exemple:
 *  { img: "xxx.gif", url: "http://www.lightonline.fr/catalogue/recherche?q=xxx", title: "xxx" },
 *  { img: "xxx.gif", url: "http://www.lightonline.fr/catalogue/recherche?q=xxx", title: "xxx" }
 * 
 *  -> Ne pas oublier de changer le nombre max d'image dans la fonction "mycarousel_itemLoadCallback" à l'endroit indiqué plus bas
 * */

var mycarousel_itemList = [
	{img: "tsetse.gif", url: "http://www.lightonline.fr/luminaire/marque/40/tse-tse", title: "Luminaires Tsé & Tsé"},
   {img: "luminaires-&-tradition.gif", url: "http://www.lightonline.fr/luminaire/marque/10/-tradition", title: "Luminaires & Tradition"},
   {img: "luminaires-adonde.gif", url: "http://www.lightonline.fr/luminaire/marque/50/¿adonde", title: "Luminaires ¿adónde?"},
   {img: "luminaires-artemide.gif", url: "http://www.lightonline.fr/luminaire/marque/31/artemide", title: "Luminaires Artemide"}
];
var nb_bannieres = 4;
var first_banniere = Math.floor(Math.random()*4);

var tmp = new Array();

if(first_banniere == 1) {
	tmp[0] = mycarousel_itemList[1];
	tmp[1] = mycarousel_itemList[2];
	tmp[2] = mycarousel_itemList[3];
	tmp[3] = mycarousel_itemList[0];
	mycarousel_itemList = tmp;
}
if(first_banniere == 2) {
	tmp[0] = mycarousel_itemList[2];
	tmp[1] = mycarousel_itemList[3];
	tmp[2] = mycarousel_itemList[0];
	tmp[3] = mycarousel_itemList[1];
	mycarousel_itemList = tmp;
}
if(first_banniere == 3) {
	tmp[0] = mycarousel_itemList[3];
	tmp[1] = mycarousel_itemList[0];
	tmp[2] = mycarousel_itemList[1];
	tmp[3] = mycarousel_itemList[2];
	mycarousel_itemList = tmp;
}

function mycarousel_itemLoadCallback(carousel, state){
	for (var i = carousel.first; i <= carousel.last; i++) {
		if (carousel.has(i)) {
			continue;
		}
		if (i > nb_bannieres) { //nombre d'image total 
			i = 1; 
			break;
		}
		$('li.jcarousel-item').remove();
		
		carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1]));
		$('#homepage-evenement ul').removeAttr('style');
	}
};
    
   
function mycarousel_getItemHTML(item){
	return '<li><a href="' + item.url + '"><img src="public/images/bans/' + item.img + '" alt="' + item.url + '" /></a></li>';
};

function recharge() {
	window.location.reload();
}

function windowOpener(url, wName, w, h) {
    var centerW = (window.screen.width - w) / 2;
    var centerH = (window.screen.height - h) / 2;

    newWindow = window.open(url, wName, 'scrollbars=yes,width=' + w + 
        ',height=' + h + 
        ',left=' + centerW + 
        ',top=' + centerH);

    newWindow.focus();
    return newWindow.name;
}

$(document).ready(function() {
	$('#homepage-evenement ul').jcarousel({
		scroll: 1,
		auto: 17, // seconde avant changement d'image
		wrap: 'both',
		size: nb_bannieres, //nombre d'image total
		itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
	});
	
	$('.scroll-pane').jScrollPane({
		showArrows:true,
		reinitialiseOnImageLoad: true
	});
	
	$(".oney-link-mentions").fancybox({
		"type" 	:	"iframe",
		"centerOnScroll" : true,
		"padding" : 10,
		"width" : 	550,
		"height" :	310,
		"href" : 	$(this).attr("href")
	});
	
	$("#oney-link-simu").fancybox({
		"type" 	:	"iframe",
		"centerOnScroll" : true,
		"titleShow": false,
		"padding" : 10,
		"width" : 	550,
		"height" :	650,
		"href" : 	$(this).attr("href")
	});
	
	$(".oney-link-simu2").fancybox({
		"type" 	:	"iframe",
		"centerOnScroll" : true,
		"titleShow": false,
		"padding" : 10,
		"width" : 	550,
		"height" :	500,
		"href" : 	$(this).attr("href")
	});
	
	
	// Autocomplétion du form de recherche
	if($("input#q").length) {
		
		//we will be using this to cache the responses from the server
		var ajaxCache = {};
		
		$("input#q").autocomplete({
			
			source: function(request, response) {
							
				var cachedTerm = (request.term) . toLowerCase();
				//if the data is in the cache and the data is not too long, use it
				if (ajaxCache[cachedTerm] != undefined && ajaxCache[cachedTerm].length < 13) {
					//map the data into a response that will be understood by the autocomplete widget
					response($.map(ajaxCache[cachedTerm], function(item) {
						return {
							label: item.value,
							value: item.value
						}
					}));
				}
				//get the data from the server
				else {
					$.ajax({
						type: "POST",
						url: "/rech/autocomplete",
						dataType: "json",
						data: {
							q: request.term
						},
						success: function(data) {
							//cache the data for later
							ajaxCache[cachedTerm] = data;
							//map the data into a response that will be understood by the autocomplete widget
							response($.map(data, function(item) {
								return {
									label: item.value,
									value: item.value
								}
							}));
						}
					});
				}
			},
			//start looking at X characters
			minLength: 3,
			//when you have selected something
			select: function(event, ui) {
				//close the drop down
				this.close
			},
			//show the drop down
			open: function() {
				//$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
			},
			//close the drop down
			close: function() {
				//$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
			}
		});
	}
});

