﻿(function ($) {

	var position = -1;

	var defaults = {
		delay: 2000,
		path: "/images",
		stopOnMouseover: true,
		data: []
	};

	var o;
	var hoverAutoRestart;
	var lastDelay;

	var methods = {
		init: function (options) {
			o = $.extend(defaults, options);

			// Start by loading up all images, so they are ready in the cache to display straight away
			for (i = 0; i < o.data.length; i++) {
				var image = new Image();
				var data = o.data[i]
				image.src = o.path + "/" + data.image;
			}
			hoverAutoRestart = true;
			return this.each(function (index) {
				var $t = $(this);

				if (o.stopOnMouseover) {
					$t.hover(function () {
						clearTimeout($t.data("timer"));
					}, function () {
						//next(true);
						if (hoverAutoRestart && o.data.length > 1) {
							$t.data("timer", setTimeout(function () { methods.next(true); }, lastDelay));
						}
					});
				}

				$t.click(function () {
					var url = $t.data("url");
					var codeArticle = $t.data("codeArticle");
					var quantity = $t.data("quantity");
					var code = $t.data("code");
					if (url != undefined) {
						window.open(url, "_blank");
					} else if (codeArticle != undefined && quantity != undefined) {
						addToCart(codeArticle, quantity, $(this), null, null, code);
					}
				});

				methods.next(true, true);
			});
		},
		previous: function (autorestart, first) {
			var newPosition = position - 1;
			if (newPosition <= -1) newPosition = o.data.length - 1;
			methods.goto(newPosition, autorestart, first);
		},
		next: function (autorestart, first) {
			var newPosition = position + 1;
			if (newPosition >= o.data.length) newPosition = 0;
			methods.goto(newPosition, autorestart, first);
		},
		goto: function (newPosition, autorestart, first) {
			position = newPosition;
			var data = o.data[position];
			refreshAccueil(data, first);
			hoverAutoRestart = autorestart;
			if (autorestart && o.data.length > 1) {
				var delay = (data.delay > 0 ? data.delay : o.delay) * 1000;
				lastDelay = delay;
				$("img.imgAccueil").data("timer", setTimeout(function () { methods.next(true); }, delay));
			}
		}
	};

	function refreshAccueil(data, first) {
		var $t = $("img.imgAccueil");
		if (first != undefined && first) {
			$t.attr("src", o.path + "/" + data.image);
			$t.attr("title", data.tooltip);
			$t.attr("alt", data.tooltip);
		} else {
			$t.stop().fadeTo(200, 0, function () {
				$t.attr("src", o.path + "/" + data.image);
				$t.attr("title", data.tooltip);
				$t.attr("alt", data.tooltip);
				$t.fadeTo(300, 1);
			});
		}

		// Indicate the current page
		$("div.divAccueilControls td.tdAccueilPage.currentPage").removeClass("currentPage");
		$("div.divAccueilControls td#tdAccueilPage" + position).addClass("currentPage");

		// Show the banner as clickable if there is a url or there is a codeArticle and quantity
		var clickable = false;
		if (data.url == undefined) {
			$t.removeData("url");
		} else {
			$t.data("url", data.url);
			clickable = true;
		}

		if (data.codeArticle == undefined || data.quantity == undefined) {
			$t.removeData("codeArticle");
			$t.removeData("quantity");
		} else {
			$t.data("codeArticle", data.codeArticle);
			$t.data("quantity", data.quantity);
			clickable = true;
		}
		if (data.code == undefined) {
			$t.removeData("code");
		} else {
			$t.data("code", data.code);
		}

		if (clickable) {
			$t.css("cursor", "pointer");
		} else {
			$t.css("cursor", "default");
		}

	};

	$.fn.accueil = function (method) {
		// Method calling logic
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.accueil');
		}
	}
})(jQuery);


