if(typeof(console)=='undefined') { C = function() { this.log = function() {} }; console = new C(); };

function init_autolabels() {
                function _set_input_default(input, initial_text) {
                                if (!input || (input.value && input.value!=initial_text)) return;
                                input.addEvent("focus", function () { if (this.value == initial_text) this.value = ''; this.removeClass("inactive"); });
                                input.addEvent("blur", function () { if (this.value == '') { this.value = initial_text; this.addClass("inactive");}});
                                input.value = initial_text;
                                input.addClass("inactive");
                                function _reset() {
                                                if (input.value==initial_text) {
                                                                input.value = '';
                                                }
                                }
                                $(input.form).addEvent("submit", _reset);
                }
                $$("INPUT.autolabel, TEXTAREA.autolabel").each(function(input) {
                                                var label = input.title;
                                                input.title = '';
                                                _set_input_default(input, label);
                                });
}

function init_slider() {
		DEFAULT_HEIGHT = 300;
		INITIAL_DELAY = 4000;

		var slider = $('slider');
		if (!slider) return;
		var holder = slider.getElement(".holder");
		var holder_fx = new Fx.Tween(holder, {'fps':20});
		var images = slider.getElements(".holder IMG");
		var buttons = slider.getElements(".controls A");

		// IE7 hotfix
		if (Browser.Engine.trident) {
				var culprit = $$('.bottom_background')[0];
				if (culprit) culprit.setStyle("left", 0);
		}

		var init_cancel = false;

		for (var i=1; i<images.length;i++) {
				var image = images[i];
				image.setOpacity(0);
				image.set("fade", {'fps':10});
		}
		
		var current_image_index = 0;
		var resize_mode = 0; // 1 -> really resize


		function resize_holder() {
				if (resize_mode) 
						holder_fx.start("height", images[current_image_index].getSize().y);
				else
						holder_fx.start("height", DEFAULT_HEIGHT);
		}

		function set_resize_mode(new_mode) {
				resize_mode = new_mode;
				if (new_mode)
						buttons[1].addClass("toggle-active");
				else
						buttons[1].removeClass("toggle-active");
				resize_holder();
		}

		function _move(delta) {
				var new_index = (current_image_index+delta+images.length) % images.length;
				//images[new_index].fade("in");
				//images[new_index].setOpacity(1);
				new Fx.Tween(images[new_index]).start("opacity", 1)

				//images[current_image_index].fade("out");
				//images[current_image_index].setOpacity(0);
				new Fx.Tween(images[current_image_index]).start("opacity", 0)

				current_image_index = new_index;
				resize_holder();
		}

		// handlers
		buttons[0].addEvent("click", function (ev) {
						init_cancel = true;
						new Event(ev).stop();
						_move(-1);
				});

		buttons[1].addEvent("click", function (ev) {
						init_cancel = true;
						new Event(ev).stop();
						set_resize_mode(!resize_mode);
				});

		buttons[2].addEvent("click", function (ev) {
						init_cancel = true;
						new Event(ev).stop();
						_move(1);
				});

		// initial animation

		// wait for the image to be loaded to resize accordingly
		holder_fx.set("height", DEFAULT_HEIGHT);
		function _waiting() {
				$(document.body).setOpacity(0.01);
				$(document.body).setStyle("cursor", "wait");
		}
		function _not_waiting() {
				$(document.body).setOpacity(1);
				$(document.body).setStyle("cursor", "default");
		}

		_waiting();
		_not_waiting.delay(5000); // just in case

		set_resize_mode(1);
		function _init() {
				console.log("init");
				if (!init_cancel) set_resize_mode(0);
		}
		// delay to deal with image loading
		function _onload() {
				function _preinit() { 
						holder_fx.set("height", this.getSize().y);
						_not_waiting();
				}
				_preinit.delay(500, this);
				_init.delay(INITIAL_DELAY);
		}
		if (Browser.Engine.trident) {
				window.onload = _onload; // why this way ?
		} else {
				images[current_image_index].addEvent("load", _onload);
		}


}

function init_slideshows() {
		function _init_slideshow(div) {
				var container = div.getElement("UL");
				var images = div.getElements("UL LI IMG");
				var buttons = div.getElements(".controls A");
				var scroll = new Fx.Scroll(div.getElement(".scroll"));
				var total_width = container.getSize().x;

				var current_image_index = 0;

				function _move(delta) {
						var count = images.length - 2;
						var new_index = (current_image_index+delta+count) % count;
						scroll.toElement(images[new_index]);
						current_image_index = new_index;
				}

				buttons[0].addEvent("click", function (ev) {
								new Event(ev).stop();
								_move(-1);
						});

				buttons[1].addEvent("click", function (ev) {
								new Event(ev).stop();
								_move(1);
						});
				
				
		}
		$$('.slideshow').each(_init_slideshow);
}

function init_product_list() {
		function _init_product(li) {
				var a = li.getElement(".center a");
				var layer = li.getElement(".center .layer");
				var description = li.getElement(".description");
				if (!layer) return;
				layer.store('tip:title', a.title);
				if (description) {
						description.dispose();
						layer.store('tip:text', description.get("html"));
						if (Browser.Engine.trident4) a.store('tip:text', description.get("html"));
				}
				new Tips(layer, {'className':"tool-tip"});
				/* special behaviour for ie6 */
				if (Browser.Engine.trident4) new Tips(a, {'className':"tool-tip"});
				li.addEvent("mouseover", function () { li.addClass("hover"); });
				li.addEvent("mouseleave", function () { li.removeClass("hover"); });
				layer.addEvent("click", function (e) { a.fireEvent("click"); });
		}
		$$('.product_list li').each(_init_product);
}

function warn_ie6() {
		if (Browser.Engine.trident4) {
				$(document.body).set("html", '<P style="padding: 2em; color: white">Télcharger : <a href="http://getfirefox.com">Firefox</a>, <a href="http://www.microsoft.com/windows/internet-explorer/">Internet Explorer</a> ou <a href="http://www.google.com/chrome/">Google Chrome</a></p>');
				alert("Votre navigateur est trop ancien pour naviguer sur ce site. Merci de votre compréhension.");
		}
}

//window.addEvent("domready", warn_ie6);
window.addEvent("domready", init_slider);
window.addEvent("domready", init_autolabels);
window.addEvent("domready", init_slideshows);
window.addEvent("domready", init_product_list);
