var app = {};
app.home = {};

app.home.Photo = {
	_interval: null,
	_seconds: 10,
	init: function (seconds) {
		var self = this;
		clearTimeout(app.home.Photo._interval);
		if (seconds != null) {
			this._seconds = seconds;
		}
		$("#home div.images").each(function () {
			var imgs = $(this).find("img");
			var ww = $(window).width();
			if (ww > 1700) {
				ww = 1700;
			}
			imgs.each(function () {
				var img = $(this).get(0);

				$(this).attr("data-orwidth", img.width);
				$(this).attr("data-orheight", img.height);
				$(this).width(ww);
			});
			var maxh = 0;
			imgs.each(function () {
				var h = ww * parseInt($(this).attr("data-orheight")) / parseInt($(this).attr("data-orwidth"));
				if (h > maxh) {
					maxh = h;
				}
			});
			$(this).height(maxh);
			$(this).find("img").removeClass("active");
			$(this).find("img:not(:first)").hide();
			$(this).find("img").css("visibility","visible");
			$(this).find("img:first").addClass("active").show();
			if ($(this).find("img:first").hasClass("white")) {
				$("#home #logo, #home #menu").removeClass("black").addClass("white");
			}
			else {
				$("#home #logo, #home #menu").removeClass("white").addClass("black");
			}
			app.home.Photo._interval = setTimeout(function () {
				self.showNext();
			},self._seconds * 1000);
			self.resize();
		});
	},
	resize: function () {
		var self = this;
		$("#home div.images").each(function () {
			var imgs = $(this).find("img");
			var ww = $(window).width();
			if (ww > 1700) {
				ww = 1700;
			}
			imgs.each(function () {
				$(this).css({
					width: "auto",
					height: "auto"
				});
				var img = $(this).get(0);
				$(this).attr("data-orwidth", img.width);
				$(this).attr("data-orheight", img.height);
				$(this).width(ww);
			});
			var maxh = 0;
			imgs.each(function () {
				var h = ww * parseInt($(this).attr("data-orheight")) / parseInt($(this).attr("data-orwidth"));
				if (h > maxh) {
					maxh = h;
				}
			});
			$(this).height(maxh);
		});
	},
	showNext: function () {
		var self = this;
		var prev = $("#home div.images img.active");
		var next = $("#home div.images img.active").next();
		if (!next.is("img")) {
			next = $("#home div.images img:first-child");
		}
		$("#home div.images img.active").removeClass("active");
		next.addClass("active");
		if (next.get(0).complete) {
			prev.fadeOut();
			if (next.hasClass("white")) {
				$("#home #logo, #home #menu").removeClass("black").addClass("white");
			}
			else {
				$("#home #logo, #home #menu").removeClass("white").addClass("black");
			}
			next.fadeIn(function () {
				app.home.Photo._interval = setTimeout(function () {
					self.showNext();
				},app.home.Photo._seconds * 1000);
			});
		}
		else {
			next.get(0).load = function () {
				prev.fadeOut();
				if (next.hasClass("white")) {
					$("#home #logo, #home #menu").removeClass("black").addClass("white");
				}
				else {
					$("#home #logo, #home #menu").removeClass("white").addClass("black");
				}
				next.fadeIn(function () {
					app.home.Photo._interval = setTimeout(function () {
						self.showNext();
					},app.home.Photo._seconds * 1000);
				});
			}
		}
	}
}

app.Catalogue = {
	init: function () {
		$("#content div.products").each(function () {
			$(this).find("div.item").each(function () {
				var span = $(this).find("a span");
				if (span.size() > 0) {
					span.css({
						bottom : "-"+span.outerHeight()+"px",
						visibility: "visible"
					});
				}
			});
		});
		$("#content div.products div.item a").mouseenter(function () {
			$(this).find("span").stop().animate({bottom:0});
		});
		$("#content div.products div.item a").mouseleave(function () {
			var span = $(this).find("span");
			span.stop().animate({bottom:"-"+span.outerHeight()+"px"});
		});
	},
	initDetail: function () {
		var self = this;
		$("#productdetail").each(function () {
			var self2 = this;
			$(window).load(function () {
				self.centerPhoto(true);
				$(self2).find("div.scroll").css({visibility:"visible"}).find("div.wrap").css({opacity:0}).animate({opacity:1}, function () {
					$(this).css("opacity","");
				});

			});
			if ($(this).find("div.item").size() > 1) {
				$(this).find("div.item").append("<div class='tleft'></div><div class='tright'></div>");
				var items = $(this).find("div.item").clone();
				var items2 = items.clone();
				$(this).find("div.item:first").addClass("active");
				items.insertBefore($(this).find("div.item.active"));
				items2.insertAfter($(this).find("div.item:last"));
				self.centerPhoto(true);
				$(this).find("div.item").click(function () {
					if ($(this).hasClass("active")) {
						return false;
					}
					// back
					if ($(this).next().hasClass("active")) {
						var last = $(self2).find("div.item:last").clone(true);
						$(self2).find("div.item:last").addClass("remove");
						last.insertBefore($(self2).find("div.item:first"));
					}
					// forward
					else {
						var first = $(self2).find("div.item:first").clone(true);
						$(self2).find("div.item:first").addClass("remove");
						first.insertAfter($(self2).find("div.item:last"));
					}
					$(self2).find("div.item.remove").remove();
					self.centerPhoto(true);
					$(self2).find("div.item.active").removeClass("active");
					$(this).addClass("active");

					self.centerPhoto();
				});
				$(this).find("div.item div.tleft").click(function (e) {
					if ($(this).parent().hasClass("active")) {
						e.stopPropagation();
						$(this).parent().prev().click();
					}
				});
				$(this).find("div.item div.tright").click(function (e) {
					if ($(this).parent().hasClass("active")) {
						e.stopPropagation();
						$(this).parent().next().click();
					}
				});
			}
			else {
				$(this).find("div.item:first").addClass("active");
			}
		});
	},
	centerPhoto: function (static) {
		$("#productdetail").each(function () {
			var active = $(this).find("div.item.active");
			var w = active.width();
			var ww = $("#container").width();
			var left = active.position().left;
			var move = -left + (ww - w) / 2;
			if (static) {
				$(this).find("div.scroll").css({left: move+"px"});
			}
			else {
				$(this).find("div.scroll").stop().animate({left: move+"px"});
			}
		});
	}
}

app.adjustRight = function () {
	$("#content div.wrap").each(function () {
		var width = ($("#content").width() - 560) * 60 / 100;
		$(this).find("div.right:first").width(width);
	});
}

$(document).ready(function () {
	$("#container").css({
		minHeight: $(window).height() - 40
	});
	app.Catalogue.init();
	app.Catalogue.initDetail();
	$("#home #logo, #home #menu").removeClass("white").addClass("black");
	$("#home div.images").height(parseInt($("#home #menu").outerHeight()) + 60);
	app.adjustRight();
});

$(window).load(function () {
	$("#home div.images").css({
		height: "auto",
		background: "none"
	});
	app.home.Photo.init();
});

$(window).resize(function () {
	app.home.Photo.resize();
	app.Catalogue.centerPhoto();
	$("#container").css({
		minHeight: $(window).height() - 40
	});
	app.adjustRight();
});
