var browserIOS = false;

$(document).ready(function() {

	var
		coverWidth = $(".cover").width(),
		coverList = $(".cover ul"),
		coverLength = $(".cover li").length,
		coverDirection = "r",
		coverDuration = 250,
		coverCurrent = 0,
		coverAnimation = false,
		coverTimer,
		coverInterval = 7000,
		coverReady = false;

	if (coverLength > 1) {

		$(".cover").addClass("coverWaiting");
		coverList.css({"width" : coverWidth * coverLength + "px"});
		$(".coverArrow").css({"opacity" : "0"});
		
		$(".cover li").each(function(index, element) {
			var coverLinear_Element = $('<em />');
			coverLinear_Element.attr("index", index);
			$(".coverLinear").append(coverLinear_Element);
		});
		
		var setCover_Linear = function(count) {
			if (coverReady) {
				if (coverCurrent > coverLength - 1) {
					coverCurrent = 0;
				} else if (coverCurrent < 0) {
					coverCurrent = coverLength - 1;
				}

				$(".coverLinear em").each(function(index, element) {
					$(element).removeClass();
					$(element).unbind();
					if (index == coverCurrent) {
						$(element).addClass("current");
					} else {
						$(element).bind({
							mouseenter: function() {$(element).addClass("over");},
							mouseleave: function() {$(element).removeClass("over");},
							click: function() {
								if (!coverAnimation) {
									setCover_Animation(coverCurrent > (1 * $(element).attr("index")) ? "l" : "r", Math.abs((1 * $(element).attr("index")) - coverCurrent));
									coverCurrent = $(element).attr("index");
								}
							}
						});
					}
				});
			}
		};
		
		var setCover_Animation = function(direction, count) {
			if (coverReady) {
				if (typeof(count) == "undefined") {
					count = 1;
				}

				if (!coverAnimation && (direction == "l" || direction == "r")) {
					coverAnimation = true;
					coverDirection = direction;
					if (direction == "l") {
						coverCurrent = (1 * coverCurrent) - (1 * count);
						for (var i = 0; i < count; i++) {
							$(".cover li").last().prependTo(coverList)
						}
						coverList.css({"margin-left" : "-" + (coverWidth * count) + "px"});
						coverList.animate({
							"margin-left" : "0"
						}, coverDuration, function() {
							coverAnimation = false;
						});
					} else {
						coverCurrent = (1 * coverCurrent) + (1 * count);
						coverList.animate({
							"margin-left" : "-" + (coverWidth * count) + "px"
						}, coverDuration, function() {
							for (var i = 0; i < count; i++) {
								$(".cover li").first().appendTo(coverList);
							}
							coverList.css({"margin-left" : "0"});
							coverAnimation = false;
						});
					}
					setCover_Linear(count);
				}
			}
		};
		
		var setCover_Arrow = function(action) {
			if (coverReady) {
				$(".coverArrow").stop(true, false).animate({"opacity" : (action ? "1" : "0")}, 150);
			}
		}
		
		$(".coverArray, .coverArrow").bind({
			mouseenter: function() {setCover_Arrow(true);},
			mouseleave: function() {setCover_Arrow(false);}
		});
		
		$(".coverArrow_l").attr("title", "Previous").bind({
			click: function() {setCover_Animation("l");}
		});
		
		$(".coverArrow_r").attr("title", "Next").bind({
			click: function() {setCover_Animation("r");}
		});
		
		/*
		$(".coverArray").touchwipe({
			wipeLeft: function() {setCover_Animation("r");},
			wipeRight: function() {setCover_Animation("l");},
			min_move_x: 25,
			preventDefaultEvents: true
		});
		*/
		
		var coverStop = function() {
			window.clearInterval(coverTimer);
		};
		
		var coverAuto = function() {
			coverStop();
			coverTimer = window.setInterval(function() {setCover_Animation(coverDirection);}, coverInterval);
		};
		
		$(".cover").bind({
			mouseenter: function() {coverStop();}, mouseover: function() {coverStop();}, mousemove: function() {coverStop();},
			mouseleave: function() {coverAuto();}
		});
		
		$(window).load(function() {
			coverReady = true;
			$(".cover").removeClass("coverWaiting");
			$(".coverArrow").css({"display" : "block"});
			setCover_Linear();
			coverAuto();
		});
		
	}
	
	//
	
	if ($(".coverRetailer").length) {
		var
			cover = $(".coverRetailer").first(),
			coverImage = $(".coverRetailerImage").first(),
			coverArray = $(".coverRetailerArray").first(),
			coverFirst = true,
			coverActive = false,
			coverTimer = false;
		
		if (coverArray.find("li").length == 3) {
			cover.addClass("coverRetailer_Three");
		}
		
		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");
				
				if (coverFirst) {
				
					coverImage.css({
						"background-image" : "url('" + coverAttr_src + "')"
					});
					cover.css({
						"background-image" : "url('" + coverAttr_src + "')"
					});
					coverFirst = false;
					coverActive = false;
					
				} else {
				
					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 + "')"
							});
							coverActive = false;
						}
					});
					
				}
				
			}
		};
		
		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 > (cover.hasClass("coverRetailer_Three") ? '2' : '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();
					}
				}
			});
			
		}
		
	}
	
	//$(window).load(function() {});

});
