var browserIOS = false;

$(document).ready(function() {

	$("#guestbookAdd a").html('<img src="/i/btn-add-testimonial.png" width="181" height="37" alt="Add testimonial" />')

	if ($(".currency").length) {
		var
			currency = $(".currency").first(),
			currencyType = $(".currencyType").first(),
			currencySelect = $(".currencySelect").first(),
			currencyDuration = 200,
			currencyTimer;
			
		var currencyShow = function() {
			currencySelect.css({
				"display" : "block"
			}).stop(true, false).animate({
				"opacity" : "1"
			}, {
				duration: currencyDuration,
				easing: "linear"
			});
		};
		
		var currencyHide = function(reload) {
			currencySelect.stop(true, false).animate({
				"opacity" : "0"
			}, {
				duration: currencyDuration / 1.5,
				easing: "linear",
				complete: function() {
					currencySelect.css({"display" : "none"});
					if (reload == true) 
					{
						window.location.href = window.location.href.replace(/\?.*/, '') + '?setCurrency=' + (currencyType.hasClass("currencyGBP") ? 'GBP' : 'EUR');
					}
				}
			});
		};
		
		currency.bind({
			mouseenter: function() {
				currencyTimer = window.setTimeout(function() {currencyShow();}, 150);
			},
			mouseleave: function() {
				window.clearTimeout(currencyTimer);
				currencyHide();
			}
		});
		
		currency.find("em").bind({
			mouseenter: function() {
				currency.addClass("currencyOver");
			},
			mouseleave: function() {
				currency.removeClass("currencyOver");
			},
			click: function() {
				if (currencyType.hasClass("currencyGBP")) {
					currencyType.removeClass("currencyGBP");
					currencyType.addClass("currencyEUR");
				} else {
					currencyType.removeClass("currencyEUR");
					currencyType.addClass("currencyGBP");
				}
				currencyHide(true);
			}
		});

		currency.css({"display" : "block"});
		currencySelect.css({"opacity" : "0"});
	}
	
	//
	
	if ($(".cover").length) {
		var
			cover = $(".cover").first(),
			coverImage = $(".coverImage").first(),
			coverArray = $(".coverArray").first(),
			coverDescription = $(".coverDescription").first(),
			coverFirst = true,
			coverActive = false,
			coverTimer = false;
		
		var setCover = function(element) {
			if (!coverActive && !$(element).hasClass("current")) {
			
				coverActive = true;
				window.clearInterval(coverTimer);
				
				coverArray.find("li").removeClass();
				$(element).addClass("current");
				
				var
					coverAttr_src = $(element).find("img").attr("src"),
					coverAttr_title = $(element).find("img").attr("title");
				
				if (coverFirst) {
				
					coverImage.css({
						"background-image" : "url('" + coverAttr_src + "')"
					});
					cover.css({
						"background-image" : "url('" + coverAttr_src + "')"
					});
					if (coverAttr_title) {
						coverDescription.html('<table class="pure"><tr><td><p>' + coverAttr_title + '</p></td></tr></table>');
					}
					coverFirst = false;
					coverActive = false;
					
				} else {
				
					coverDescription.animate({
						"opacity" : "0"
					}, {
						duration: 100,
						complete: function() {
							coverImage.css({
								"opacity" : "0",
								"background-image" : "url('" + coverAttr_src + "')"
							}).stop(true, false).animate({
								"opacity" : "1"
							}, {
								duration: 250,
								complete: function() {
									cover.css({
										"background-image" : "url('" + coverAttr_src + "')"
									});
									if (coverAttr_title) {
										coverDescription.html('<table class="pure"><tr><td><p>' + coverAttr_title + '</p></td></tr></table>');
										coverDescription.animate({
											"opacity" : "1"
										}, {
											duration: 100,
											complete: function() {
												coverActive = false;
											}
										});
									} else {
										coverActive = false;
									}
								}
							});
						}
					});
					
				}
				
			}
		};
		
		var coverCheck = 0;

		coverArray.find("li").each(function(index, element){
			if(index == 0) {
				$(element).attr("id", "coverItem0").css({
					"background-image" : "url('/i/cover/item1.png')"
				})
			}
			if(index == 1) {
				$(element).attr("id", "coverItem1").css({
					"background-image" : "url('/i/cover/item2.png')"
				})
			}
			if(index == 2) {
				$(element).attr("id", "coverItem2").css({
					"background-image" : "url('/i/cover/item3.png')"
				})
			}
			if(index == 3) {
				$(element).attr("id", "coverItem3").css({
					"background-image" : "url('/i/cover/item4.png')"
				})
			}
		});
			
		if (coverArray.find("li").length == 4) {

			var
				coverAuto,
				coverInterval = 7000,
				coverCurrent;
			
			var coverStop = function() {
				window.clearInterval(coverAuto);
			};
		
			var coverStart = function() {
				coverStop();
				coverAuto = window.setInterval(function() {
				
					coverCurrent = coverArray.find("li.current").attr("index") * 1;
					coverCurrent++;
					if (coverCurrent > 3) {
						coverCurrent = 0;
					}
					setCover(coverArray.find("li[index=" + coverCurrent + "]"));
					
				}, coverInterval);
			};
			
			coverArray.find("li").each(function(index, element) {
				$(element).attr("index" , index);
				if (index == 0) {
					setCover(element);
					if (!browserIOS) {
						coverStart();
					}
				}
				$(element).bind({
					mouseenter: function() {
						window.clearInterval(coverTimer);
						coverTimer = window.setInterval(function() {setCover(element);}, 100);
					},
					mouseleave: function() {
						window.clearInterval(coverTimer);
					}
				});
			});

			if (!browserIOS) {
				cover.bind({
					mouseenter: function() {coverStop();},
					mouseleave: function() {coverStart();}
				});
			}
			
			if (browserIOS) {
				coverArray.find("a").bind({
					click: function(event) {
						if ($(this).attr("clicked")) {
							$(this).removeAttr("clicked");
						} else {
							coverArray.find("a").each(function(index, element) {
								$(element).removeAttr("clicked");
							});
							$(this).attr("clicked", true);
							event.preventDefault();
						}
					}
				});
			}

		} else {
			
			cover.css({"display" : "none"});
			
		}
		
	}
	
	//
	
	if ($("#rootHome").length) {
		$("#rootHome").find("p").each(function (index, element) {
			if (index == 0) {
				$(element).css({"margin-top" : "0"});
			}
			if (index == $("#rootHome").find("p").length - 1) {
				$(element).css({"margin-bottom" : "0"});
			}
		});
	}
	
	//
	
	if ($("#productPreview").length) {
		var
			productPreview_Array = $("#productPreview .arrayItem p"),
			productPreview_Difference = $("#productPreview span").first().outerWidth(),
			productPreview_Length = $("#productPreview .arrayItem img").length,
			productPreview_Current = 0,
			productPreview_Active = false,
			productPreview_Timer;

		if (productPreview_Length > 4) {
			productPreview_Array.css({"margin-left" : "0"});
	
			$('<input type="text" value="" />').addClass("selectionRemove").insertBefore(productPreview_Array);
	
			$(".productArrow").bind({
				click: function() {
					window.clearInterval(productPreview_Timer);
					if (!productPreview_Active) {
						productPreview_Active = true;
						
						var arrow = $(this);
					
						if (arrow.hasClass("productArrow_l")) {
							productPreview_Current--;
							$(".productArrow_r").removeClass("hidden");
						} else {
							productPreview_Current++;
							$(".productArrow_l").removeClass("hidden");
						}
	
						if (productPreview_Current < 0 || productPreview_Current == productPreview_Length - 2) {
							if (productPreview_Current < 0) {
								productPreview_Current = 0;
							}
							if (productPreview_Current == productPreview_Length - 2) {
								productPreview_Current--;
							}
							productPreview_Active = false;
						} else {
							productPreview_Array.animate({
								"margin-left" : parseInt(productPreview_Array.css("margin-left")) + (arrow.hasClass("productArrow_l") ? '+136' : '-136') * 1 + "px"
							}, {
								duration: 250,
								complete: function() {
									productPreview_Active = false;
								}
							});
						}
						
						if (productPreview_Current == 0) {
							$(".productArrow_l").addClass("hidden");
						}
						if (productPreview_Current == productPreview_Length - 3) {
							$(".productArrow_r").addClass("hidden");
						}
						
					} else {
						if ($(this).hasClass("productArrow_l")) {
							productPreview_Timer = window.setInterval(function() {$(".productArrow_l").trigger("click");}, 50);
						} else {
							productPreview_Timer = window.setInterval(function() {$(".productArrow_r").trigger("click");}, 50);
						}
					}
					$(".selectionRemove").focus();
				}
			});
			
			$(".productArrow_l").trigger("click");
			
		} else {
			$("#productPreview .productArrow").addClass("none");
			$("#productPreview .arrayItem").css({"margin" : "0"});
		}
		$("#productPreview").css({"visibility" : "visible"});
	}
	
	//
	
	if ($(".tabItem").length) {
	
		var
			tabCaption = $(".tabCaption a"),
			tabData = $(".tabData"),
			tabHeight = 0;
		
		$(".tabCaption td").css({"width" : (100 / (tabCaption.length * 1)) + "%"}).last().css({"border" : "none"});
		
		tabCaption.each(function(index, element) {
			$(element).bind({
				click: function() {
					if (!$(element).hasClass("current")) {
						tabCaption.removeClass("current");
						$(element).addClass("current");
						tabData.each(function(indexData, elementData) {
							/*if ($(elementData).outerHeight() > tabHeight) {
								tabHeight = $(elementData).outerHeight();
							}*/
							if (indexData == index) {
								$(elementData).removeClass("none");
							} else {
								$(elementData).addClass("none");
							}
						});
						/*tabData.each(function(indexData, elementData) {
							if (parseInt($(elementData).css("min-height")) != tabHeight) {
								$(elementData).css({"min-height" : tabHeight + "px"});
							}
						});*/
					}
				}
			});
		});
		
		$(".tabItem").css({"visibility" : "visible"});
	}
	
	//$(window).load(function() {});

});
