var
	IE = document.all,
	IE7 = navigator.appVersion.match(/MSIE\s+7\./),
	IE8 = navigator.appVersion.match(/MSIE\s+8\./),
	IE9 = navigator.appVersion.match(/MSIE\s+9\./);

var browserIOS = false;

$(document).ready(function() {

	var
		coverWidth = $(".cover").width(),
		coverList = $(".cover ul"),
		coverLength = $(".cover li").length,
		coverDirection = "r",
		coverArray_Direction = false,
		coverDuration = 250,
		coverCurrent = 0,
		coverAnimation = false,
		coverTimer,
		coverInterval = 5000,
		coverReady = false;
		

	if (coverLength > 1) {

		$(".cover").addClass("coverWaiting");
		coverList.css({"width" : coverWidth * coverLength + "px"});
		
		$(".cover li").each(function(index, element) {
			var coverLinear_Element = $('<em />');
			coverLinear_Element.attr("index", index);
			$(".coverLinear").append(coverLinear_Element);
			
			var el = $(this),
				hElement = $(".coverArrow").height(),
				hContent = $(this).find(".controller").height();
			
			if(hElement < hContent) 
			{
				$(".coverArrow").height(hContent);
			}
			
		});
		
		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({
							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) {
				if (!IE7 && !IE8 && !browserIOS) {
					$(".coverArrow").stop(true, false).animate({"opacity" : (action ? "1" : "0")}, 150);
				}
			}
		}
		
		$(".coverArrowButton_l").attr("title", "Previous").bind({
			click: function() {
				coverArray_Direction = "l";
				var coverQueue_Local = window.setInterval(function() {
					if (!coverAnimation) {
						if (coverArray_Direction == "l") {
							setCover_Animation("l");
						}
						window.clearInterval(coverQueue_Local);
					}
				}, 10);
			}
		});
		
		$(".coverArrowButton_r").attr("title", "Next").bind({
			click: function() {
				coverArray_Direction = "r";
				var coverQueue_Local = window.setInterval(function() {
					if (!coverAnimation) {
						if (coverArray_Direction == "r") {
							setCover_Animation("r");
						}
						window.clearInterval(coverQueue_Local);
					}
				}, 10);
			}
		});
		
		$(".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();
		});
		
	}
	
	//
	
	//$(window).load(function() {});

});
