$(document).ready(function() {
	$("#box ul li").last().addClass("lastbox");
	
	$("#box ul li a").hover(function() {
		$(this).stop(true, false).animate({
			"bottom" : "10px"
		}, 50);		
	}, function() {
		$(this).animate({
			"bottom" : "0px"
		}, 200);		
	})
	
	// Quotes
	if($("#quote").length) {
		$("#quote > div").show();
		$("#quote > div:gt(0)").hide();

		setInterval(function() {
		  $('#quote > div:first')
		    .fadeOut(1100)
		    .next()
		    .fadeIn(1100)
		    .end()
		    .appendTo('#quote');
		},  9000);
	}
	
	// Video
		
	if($("#video").length) {		
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
			var	
				videoSrc = "video/"+$("#video").attr("class")+"_mobile.mp4";					
			$("#video").html("<video width='1024' style='margin-top: 65px;' height='408' src='"+videoSrc+"' controls>");					
			$("#video video").bind("ended", function() {
			   $("#description, .easytouse_box").fadeIn(800);
			});
		} else {
			if($("#description").length) {
				$("#description, .easytouse_box").delay(7500).fadeIn(600);
			}			
		}
	}
	
	// Easy to use box
	
	var
		descriptionText = $("#description p").html();
	
	if($(".easytouse_box").length) {
		$(".easytouse_box ul li a").hover(function() {
			var
				textHeight
				text = $("span", this).html();
			$("#description p").slideUp(200, function() {
				$("#description p").html(text).slideDown(200);
			});			
		}, function() {			
			$("#description p").delay(300).slideUp(200, function() {
				$("#description p").html(descriptionText).slideDown(200);
			});	
		})
	}
	
	// Image Slider	
	if($(".image_slider").length) {	   
	   
	    var 
	    	cancelFollow = true;
	    	imageSlider = $('div.image_slider'),
	        ulImageSlider = $('ul.image_slider'), 
	  		imageSliderWidth = $("li", ulImageSlider).width() * $("li", ulImageSlider).length + $("li", ulImageSlider).length * 60;
	  	
	  	$("#overlay").hide();
	  	
	  	ulImageSlider.css({"width": imageSliderWidth+"px"});	   	
	   	
	 	ulImageSlider.draggable({ 
	    	axis : 'x', 
	 	    stop: function(e, ui) { cancelFollow = false; checkArrows(ui.position.left); }, 
	  		drag: function(event, ui) { 
	  			if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) { }
	  			else {  			
		  			if(ui.position.left > 300) {
			  			$(this).draggable( "disable" );
			  			focusSlide(0);				
						return false;
					} else if(ui.position.left < (-1)*imageSliderWidth+1024-300) {
						focusSlide((-1)*imageSliderWidth+1024);
						return false;
					}
				}
	  		}
	  	});
	  	
	  	$("a.forward").click(function(e) {
	  		e.preventDefault();
	  		var	
	  			positionLeft = ulImageSlider.position().left;	  		
	  		if(positionLeft > (-1)*imageSliderWidth+1024) ulImageSlider.stop(true, false).animate({"left": positionLeft-200 + "px" }, 450);
	  		checkArrows(positionLeft-200);
	  	});
	  	
	  	$("a.backward").click(function(e) {	  		
	  		e.preventDefault();
	  		var	
	  			positionLeft = ulImageSlider.position().left;	  		
	  		if(positionLeft < -20) ulImageSlider.stop(true, false).animate({"left": positionLeft+200 + "px" }, 450);
	  		checkArrows(positionLeft+200);
	  	});
	  	
	  	function checkArrows(positionLeft) {
	  		if(positionLeft >= 0) {
	  			$("a.backward").fadeOut(300);
	  			$("a.forward").fadeIn(300);
	  		} else if(positionLeft < (-1)*imageSliderWidth+1024) {
	  			$("a.backward").fadeIn(300);
	  			$("a.forward").fadeOut(300);
	  		} else {
	  			$("a.backward").fadeIn(300);
	  			$("a.forward").fadeIn(300);
	  		}
	  	}
	  	
	  	function focusSlide(sliderPos) {
	  		ulImageSlider.draggable( "enable" );
	  		ulImageSlider.animate({"left": sliderPos + "px" }, 450);
	  	}
	  	
	  	ulImageSlider.mousedown(function() { cancelFollow = true; });  	
	  	
	  	$("li a", ulImageSlider).click(function() {
	  		if (cancelFollow) {	 	  				
		  		var
		  			imgSrc = $("img", this).attr("src").substr(0, $("img", this).attr("src").length-4)+"_large.gif";
		  			  			  		
		  		$("#overlay div").css({
		  			"background" : "url("+imgSrc+") no-repeat center center"
		  		});
		  		$("#overlay").css({	  	
		  			"background" : "url(images/overlay_background.jpg) no-repeat center center #fff"
		  		}).fadeIn(300);
		  		return true; 	  
		  	}	
		  	return false;
	  	});	  	
	  	  	  	
	  	$("#overlay a, #overlay").click(function(e) {
	  		e.preventDefault();
	  		$("#overlay").fadeOut(300);
	  	});  
	  	
	}

});
