$(function(){

	$("#homepage-top li.ani").hover(function(){
		$(".bottom", this).hide()
		$(".over", this).show()
	}, function(){
		$(".over", this).hide()
		$(".bottom", this).show()
	})
	
	
	$("#homepage-content .product-list .product:last-child").addClass("last-product")
	
	
	/////////////////
	/// SLIDESHOW ///
	/////////////////
	var s_intr    = 1000 * 5
	var $sh       = $("#homepage-content .slideshow")
	var $scene    = $sh.find(".scene")
	var $sn       = $scene.find(".slides-num")
	var $slink    = $scene.find(".scene-link")
	
	function slideshow() {
		var i = 0
		
		if ($sn.has("li.active") && $sn.find("li.active").next().length) {
			i = $sn.find("li.active").next().index()
		}

		$sn.find("li").removeClass("active").eq(i).addClass("active")
		
		var slide = $sh.find(".slides > li").get(i)
		$slink.attr("href", $(slide).find(".a").text()).html('<img src="' +  $(slide).find(".image").text() + '" alt="" />')
		
		setTimeout(slideshow, s_intr)
	}
	
	slideshow()
	
	$sn.find("a").click(function(){
		var i = $(this).closest("li").index()
	
		$sn.find("li").removeClass("active").eq(i).addClass("active")
		
		var slide = $sh.find(".slides > li").get(i)
		$slink.attr("href", $(slide).find(".a").text()).html('<img src="' +  $(slide).find(".image").text() + '" alt="" />')
		
		return false
	})
	
	
	///////////////////////////
	/// PRODUCTS SLIDESHOW ////
	///////////////////////////
	var ps_intr  = 1000 * 7
	var $ps      = $("#homepage-content .product-slideshow")
	
	function productSlideshow() {
		$ps.each(function(){
			var $ctx  = $(this)
			var $next = $(".title li.active", $ctx).next()
			var i     = $next.length ? $next.index() : 0
			
			$ctx.find(".title li").removeClass("active").eq(i).addClass("active")
			$ctx.find(".products > div").hide().eq(i).fadeIn(1500)
		})
		
		setTimeout(productSlideshow, ps_intr)
	}

	setTimeout(productSlideshow, ps_intr)

	$ps.find(".title li a").click(function(){
		var $ctx = $(this).closest(".product-slideshow")
		var i    = $(this).closest("li").index()
		
		$ctx.find(".title li").removeClass("active").eq(i).addClass("active")
		$ctx.find(".products > div").hide().eq(i).fadeIn(1500)
		
		return false
	})
})

