
if (typeof(navigator.userAgent) !== "undefined" && /iP(hone|ad|od)/.test(navigator.userAgent)) {
	document.write("<link href=\"" + siteURL + "/css/style-ipad.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\" />");
}

$(window).load(
	function()
	{
		/**
		 * Home page carousel
		 */
		if ($("#collection-carosel").length > 0) {
			if ($("#collection-carosel ul li").length > 4) {
				$("#collection-carosel").jCarouselLite({
					"btnNext": "#home-section .Next",
					"btnPrev": "#home-section .Prev",
					"visible": 4,
					"scroll": 1
				});
			} else {
				$("#home-section .arrows").hide();
			}
		}

		/**
		 * Collection page carousel
		 */
		if ($("#collection-product-images").length > 0) {
			if ($("#collection-product-carousel ul li").length > 8) {
				$("#collection-product-carousel").jCarouselLite({
					"btnNext": "#collection-product-images .next",
					"btnPrev": "#collection-product-images .prev",
					"visible": 8,
					"scroll": 1
				});
			} else {
				$("#collection-product-images .arrow").hide();
				$("#collection-product-carousel").css("width", "auto");
			}
		}

		/**
		 * News items carousel
		 */
		if ($("#news-items-footer").length > 0) {
			if ($("#news-items-carousel ul li").length > 0) {
				$("#news-items-carousel").jCarouselLite({
					"btnNext": "#news-items-footer .next",
					"btnPrev": "#news-items-footer .prev",
					"scroll": 1,
					"circular": true
				});
				
			//	window.setTimeout("$('#news-items-carousel').css('width', '');", 5);
				window.setTimeout("\
					$('#news-items-carousel ul li').css('width', '');\
					$('#news-items-carousel').css('width', '');", 5);

				window.setTimeout("\
					var width=0;\
					$('#news-items-carousel ul li').each(function() { width += $(this).outerWidth(); });\
					$('#news-items-carousel ul').width(width);", 10);
	
			} else {
				$("#news-items-footer .arrow").hide();
			}

			//$("#news-items-carousel ul li img").css("visibility", "visible");
			$("#news-items-footer").css("visibility", "visible");
		}

		/**
		 * Product page carousel
		 */
		if ($("#product-images").length > 0) {
			if ($("#product-images div.Container ul li").length > 0) {
				$("#product-images div.Container ul li img").css("display", "inline");
				$("#product-images div.Container").jCarouselLite({
					"btnNext": "#product-images .next",
					"btnPrev": "#product-images .prev",
					"visible": 1,
					"scroll": 1
				});
			} else {
				$("#product-images .arrow").hide();
			}
		}

		/**
		 * News page carousel
		 */
		if ($("#news-page-ticker").length > 0) {
			$("#news-page-carousel ul li img").css("display", "inline");
			if ($("#news-page-carousel ul li").length > 1) {
				$("#news-page-carousel").jCarouselLite({
					"btnNext": "#news-page-ticker .next",
					"btnPrev": "#news-page-ticker .prev",
					"visible": 1,
					"scroll": 1,
					"start": 0,
					"afterEnd": newsTickerCallback
				});

				$.globalEval("newsTickerCallback($('#news-page-carousel ul li').eq(1));");
				//window.setTimeout("newsTickerCallback($('#news-page-carousel ul li').eq(1));", 10);

			} else {
				$("#news-page-ticker .arrow").hide();
			}
		}

		/**
		 * The collections main page
		 */
		if ($("#collections-ul").length > 0) {
			var widths = [];
			var currentRow = 0;

			widths[currentRow] = 0;

			$("li", "#collections-ul").each(
				function()
				{
					var img = $("img", $(this));

					if (img.length > 0) {
						$(this).width(img.width()+5);
						$(this).height(img.height());
					}

					if ($(this).hasClass("BreakLine")) {
						currentRow++;
						widths[currentRow] = 0;
					} else {
						widths[currentRow] += $(this).outerWidth();
					}
				}
			);

			if (widths.length > 0) {
				var maxWidth = 0;

				for (var i=0; i<widths.length; i++) {
					if (widths[i] > maxWidth) {
						maxWidth = widths[i];
					}
				}

				$("#collections-ul").css("margin-left", (Math.round(($("#collections-ul").outerWidth() - maxWidth) / 2)) + "px");
			}

			/**
			 * Now apply the mover over functionality
			 */
			$("#collections-ul li img").each(
				function()
				{
					$(this).mouseover(
						function()
						{
							/*if ($(this).data("inTransition")) {
								$(this).queue(
									function()
									{
										$(this).mouseover();
									}
								);

								return;
							}*/

							$(this).data("inTransition", true);

							var coords = $(this).offset();

							$(this).css({
								"position": "fixed",
								"z-index": "1000",
								"left": parseInt(coords.left) + "px",
								"top": parseInt(coords.top) + "px"
							});

							$(this).attr("src", $(this).attr("overImg"));

							$(this).animate(
								{
									"top": (parseInt(coords.top)-21) + "px",
									"left": (parseInt(coords.left)-21) + "px",
									"height": "174px"
								},
								{
									"duration": 250,
									"queue": true,
									"complete":
										function()
										{
											$(this).data("inTransition", false);
										}
								}
							);

							/*
							$(this).fadeOut(250, 
								function()
								{
									$(this).attr("src", $(this).attr("overImg"));
								}
							);
							$(this).fadeIn(250, function()
								{
									$(this).data("inTransition", false);
								}
							);
							*/
						}
					);

					$(this).mouseout(
						function()
						{
							/*if ($(this).data("inTransition")) {
								$(this).queue(
									function()
									{
										$(this).mouseout();
									}
								);

								return;
							}*/

							$(this).data("inTransition", true);

							var coords = $(this).offset();

							$(this).attr("src", $(this).attr("origImg"));

							$(this).animate(
								{
									"top": (parseInt(coords.top)+21) + "px",
									"left": (parseInt(coords.left)+21) + "px",
									"height": "132px"
								},
								{
									"duration": 250,
									"queue": true,
									"complete":
										function()
										{
											$(this).data("inTransition", false);

											$(this).css({
												"position": "static",
												"z-index": "0",
												"left": "auto",
												"top": "auto"
											});
										}
								}
							);
							/*
							$(this).fadeOut(250, 
								function()
								{
									$(this).attr("src", $(this).attr("origImg"));
								}
							);
							$(this).fadeIn(250, function()
								{
									$(this).data("inTransition", false);
								}
							);
							*/
						}
					);
				}
			);
		}

		/**
		 * Applying the position:absolute to the footer if the body is smaller than the document
		 */
		if (!/iP(hone|ad|od)/.test(navigator.userAgent) || $("div#indexBackground").hasClass("IsHomePage")) {
			if (/iP(hone|ad|od)/.test(navigator.userAgent)) {
				$("#footer").css("position", "absolute");
			}

			window.setTimeout("resizeWindow(true)", 15);
			$(window).resize(resizeWindow);
		} else if (/iP(hone|ad|od)/.test(navigator.userAgent) && $("body").height() < $(window).height()) {
			$("#footer").css("position", "absolute");
		}
	}
);

var newsCarouselWidth = null;

function newsTickerCallback(li)
{
	var nameImg = $("img", li).attr("alt");
	var targetDiv = $("#news-item-name-images");
	var origImg = targetDiv.css("background-image");

	if (origImg !== "") {
		origImg = origImg.replace("url(", "");
		origImg = origImg.replace("'", "");
		origImg = origImg.replace('"', '');
	}

	if (nameImg == origImg) {
		return;
	}

	if (nameImg == "") {
		targetDiv.hide();
		targetDiv.css({
			"background-image": ""
		});
	} else {
		var imgObj = new Image();
		imgObj.onload =
			function()
			{
				targetDiv.height(imgObj.height);
				targetDiv.css({
					"background-image": "url('" + nameImg + "')"
				});
				targetDiv.show();
			}

		imgObj.src = nameImg;
	}
}

function resizeWindow(firstTime)
{
	if ($("div#workspace").hasClass("Resizing")) {
		return;
	}

	$("div#workspace").addClass("Resizing");

	var documentHeight = $(window).height();
	var headerHeight = $("div#header").outerHeight();
	var footerHeight = $("div#footer").outerHeight();

	$("div#workspace").height(documentHeight - (headerHeight + footerHeight));

	/**
	 * Index background images
	 */
	if ($("div#indexBackground").length > 0) {
		if (typeof(firstTime) !== "undefined" && firstTime === true) {
			$("div#indexBackground").css("bottom", footerHeight+"px");
		}

		if (jQuery.support.changeBubbles) {
			var indexWidth = $("div#indexBackground").outerWidth();
			var indexHeight = $("div#indexBackground").outerHeight();
		} else {
			var indexWidth = $("body").width();
			var indexHeight = documentHeight - footerHeight;
			$("div#indexBackground").height(indexHeight);
		}

		var imgWidth = 1440;
		var imgHeight = 642;

		if ((indexWidth / imgWidth) > (indexHeight / imgHeight)) {
			$("div#indexBackground img").width(indexWidth);
			$("div#indexBackground img").height(imgHeight * (indexWidth / imgWidth));
		} else {
			$("div#indexBackground img").height(indexHeight);
			$("div#indexBackground img").width(imgWidth * (indexHeight / imgHeight));
		}

		if (typeof(firstTime) !== "undefined" && firstTime === true) {
			$("div#indexBackground").show();

			if ($("div#indexBackground").hasClass("Rotate")) {
				setInterval("reloadIndexBackground()", 5000);
			}
		}
	}

	$("div#workspace").removeClass("Resizing");
}

var counter = null;

function reloadIndexBackground()
{
	var images = $("div#indexBackground img");

	if (counter == null) {
		counter = 0;
	}

	var index = 0;

	if (counter < (images.length-1)) {
		index = counter+1;

		images.eq(counter).fadeOut(1000);
		images.eq(index).fadeIn(1000);

		counter++;
	} else {
		images.eq(0).fadeIn(1000,
			function()
			{
				images.slice(1).fadeIn(1);
			}
		);

		counter = 0;
	}
}

function imgZoomProductOption(img)
{
	var args = "";

	if ($(img).attr("type") == "resized") {
		$(img).attr("type", "original");
		args = $(img).attr("original");
	} else {
		$(img).attr("type", "resized");
		args = $(img).attr("resized");
	}

	var dimensions = args.split("x");

	$(img).animate({
		"width": dimensions[0],
		"height": dimensions[1]
	}, "fast");
}

function changeProductOption(img)
{
	var thisBody = $("div.Body", $(img).parents("div.Option").eq(0));

	if ($(thisBody).css("display") == "none") {
		$(thisBody).slideDown("slow",
			function()
			{
				var src = $(img).attr("src");
				$(img).attr("src", src.replace("arrow-down", "arrow-up"));
			}
		);
	} else {
		$(thisBody).slideUp("slow",
			function()
			{
				var src = $(img).attr("src");
				$(img).attr("src", src.replace("arrow-up", "arrow-down"));
			}
		);
	}	
}

function subscribeCheck(form)
{
	var subscribe = $("input[name=subscribe]", form).val();

	if ($.trim(subscribe) == "") {
		alert("Please enter in your email address");
		return false;
	}

	if (subscribe.indexOf("@") == -1) {
		alert("Please enter in a valid email address");
		return false;
	}

	return true;
}

function showDealerCountries(regionId)
{
	$("#dealers-menu table td.CountryTD").each(
		function()
		{
			var id = "Country-Region-" + regionId;

			if ($(this).attr("id") == id) {
				$(this).css("visibility", "visible");
				$("div.Country:first", this).click();
			} else {
				$(this).css("visibility", "hidden");
			}
		}
	);
}

function showDealers(countryId)
{
	var i, html = "";
	var regionId = null;

	for (i=0; i<dealers.length; i++) {
		if (parseInt(dealers[i].CountryID) == parseInt(countryId)) {
			if (regionId == null) {
				regionId = dealers[i].RegionID;

				$("#dealers-menu table td#Country-Region-" + regionId + " div.Country").removeClass("Green");
				$("#dealers-menu table div#Country-" + countryId).addClass("Green");
			}
	
			html += "" +
			"	<div class=\"Dealer\">" +
			"		<div class=\"Container\">" +
			"			<span class=\"Name Green\">" + dealers[i].Name + "</span>" +
			"			<span class=\"Address\">" + dealers[i].Address + "</span>" +
			"			<span class=\"Green\">PH.</span> " + dealers[i].PhoneNo + "<br />" +
			"			<span class=\"Green\">E.</span> <a href=\"mailto:" + dealers[i].EmailAddress + "\">" + dealers[i].EmailAddress + "</a></span>" +
			"		</div>";

			if (dealers[i].GoogleMapsRef !== "") {
				html += "<div class=\"Maps\"><a href=\"" + dealers[i].GoogleMapsRef + "\" target=\"_blank\"><img src=\"./img/dealers-maps.png\"></a></div>";
			}

			html += "</div>";
		}
	}

	$("#dealers-contacts").html(html);
}

function viewNewsImg(src, width, height)
{
	/**
	 * Don't need to check if the image exists as it was already done server side
	 */
	var win = window.open("", "", "width="+(width+20)+",height="+(height+20)+",location=no,manubar=no,status=no,toolbar=no,titlebar=no");

	win.document.write("<ht"+"ml><body style=\"margin: 10px; padding: 0;\"><img src=\"" + src + "\" width=\"" + width + "\" height=\"" + height + "\" /></body></ht"+"ml>");
}

function changePageOptions(img)
{
	var thisBody = $("div.OptionImages", $(img).parents("div.OptionCell").eq(0));

	if ($(thisBody).css("display") !== "none") {
		$(thisBody).slideUp("slow",
			function()
			{
				var src = $(img).attr("src");
				$(img).attr("src", src.replace("arrow-up", "arrow-down"));
			}
		);
	} else {

		/**
		 * Do we already have one open?
		 */
		var closeAllFirst = false;

		$("div.OptionCell div.OptionImages").each(
			function()
			{
				if ($(this).get(0) === $(thisBody).get(0)) {
					return;
				}

				if ($(this).css("display") !== "none") {

					$(this).slideUp("slow",
						function()
						{
							var tmpImg = $("div.OptionHeading img", $(this).parents("div.OptionCell").eq(0));
							var src = $(tmpImg).attr("src");
							$(tmpImg).attr("src", src.replace("arrow-up", "arrow-down"));
						}
					);

					if (!closeAllFirst) {
						changePageOptionsDisplay(thisBody, img);
						closeAllFirst = true;
					}
				}
			}
		);

		if (!closeAllFirst) {
			changePageOptionsDisplay(thisBody, img);
		}
	}
}

var pageOptionLock = false;

function changePageOptionsDisplay(body, img)
{
	if (pageOptionLock) {
		return;
	}

	pageOptionLock = true;

	$(body).slideDown("slow",
		function()
		{
			var src = $(img).attr("src");
			$(img).attr("src", src.replace("arrow-down", "arrow-up"));
			pageOptionLock = false
		}
	);
}
