$(document).ready(function(){

    $("body").removeClass("noJS").addClass("JS");

    /* SHOPPING CART HOVER EFFECT */

    $("#shopping_cart_wrapper").hover(function(){
        $(this).find("#shopping_cart").stop().animate({height: $(this).find("ul").outerHeight()});
    },
    function(){
        $(this).find("#shopping_cart").stop().animate({height: "47px"});
    });

    $("#shop_actions a.cart").hover(function(){
        $(this).next("#shopping_cart_wrapper").children("#shopping_cart").stop().animate({height: $(this).next("#shopping_cart_wrapper").find("ul").outerHeight()});
    },
    function(){
        $(this).next("#shopping_cart_wrapper").children("#shopping_cart").stop().animate({height: "47px"});
    });

    /* LOGIN FOCUS TEXT EMPTY */

    $('input[type=text], input[type=password]', '#login').focus(function(){
        if($(this).val() == $(this).attr('title')) $(this).val('');
    }).blur(function(){
        if($(this).val() == '') $(this).val($(this).attr('title'));
    });

    /* INDEX MOUSE HOVER EFFECT */

    $("#index_campaigns > ul > li.index_campaign").hoverIntent({
        sensitivity: 3,
        interval: 50,
        over: function() {
            $(this).find('div.tabs div').hide().first().show();
            $(this).find("div.tabs").removeAttr("style").show("slow");
        },
        timeout: 300,
        out: function() {
            $(this).find('ul.tabs_nav li').removeClass('selected');
            $(this).find("div.tabs").stop().hide("normal");
        }
    });

    /* FOOTER HOVER EFFECT */
    $("#footer").hover(function(){
            $(this).stop().animate({height: (26+$(this).find("#footer_content").outerHeight())})
            .css({"borderTop": "1px solid #707173", "borderLeft": "1px solid #707173", "borderRight": "1px solid #707173"});
        },
        function(){
            $(this).stop().animate({height: "26px"})
            .css({"borderTop": "1px solid #FFFFFF", "borderLeft": "1px solid #FFFFFF", "borderRight": "1px solid #FFFFFF"});
        }
    );

    /* INDEX TABS */

    $("div.tabs ul.tabs_nav a").click(function () {
        $(this).parent().parent().parent().children("div").hide().filter(this.hash).fadeIn();
        $(this).parent().addClass("selected").siblings().removeClass("selected");
        return false;
    });

    /* PRODUCT CAROUSEL  */

    $("div.product_carousel_index").text("1/"+$("div.product_carousel ul li").size());

    $("div.product_carousel").jCarouselLite({
        btnNext: ".product_carousel_next",
        btnPrev: ".product_carousel_prev",
        visible: 1,
        circular: false,
        speed: 1000,
        afterEnd: function(a) {
            $("div.product_carousel_index").text((a.index()+1)+"/"+$("div.product_carousel ul li").size());
        }
    });

    /* PRODUCT IMAGE CAROUSEL AND ZOOM */

    $("div.product_img_carousel").jCarouselLite({
        btnNext: ".product_img_carousel_next",
        btnPrev: ".product_img_carousel_prev",
        visible: 1,
        circular: false,
        speed: 500
    });
   
   $("a.sample_img_zoom").fancybox({
		titlePosition: 'over',
		padding: 0,
		overlayShow: false
	});
	
	$("a.product_img_zoom").fancybox({
		titlePosition: 'over',
		padding: 0,
		overlayShow: false,
        onComplete: function(){
        	$('#fancybox-content').hoverZoom();
        }
	});
    
    /* OFFERS CAROUSEL */
    var offers_carousel_size = $("div.offers_carousel ul li").size()-4 < 1 ? 1 : $("div.offers_carousel ul li").size()-4;
    $("div.offers_carousel_index").text("1/"+offers_carousel_size);

    $("div.offers_carousel").jCarouselLite({
        btnNext: ".offers_carousel_next",
        btnPrev: ".offers_carousel_prev",
        visible: 5,
        circular: false,
        speed: 200,
        afterEnd: function(a) {
            $("div.offers_carousel_index").text((a.index()+1)+"/"+offers_carousel_size);
        }
    });

    /* POPUP CLOSE */
    $("#popup a.popup_close").click(function(event){
        $("#popup").fadeOut();
        event.preventDefault();
    });
    
    /* HOVER ZOOM */
    
	$.fn.hoverZoom = function() {
		return this.each(function(){
			var div =  $(this);
			div.addClass('hoverZoom');
			var divW = div.width();
			var divH = div.height();
			var divO = div.offset();
			var img = div.children();
	        var imgW = img.width();
	        var imgH = img.height();
			
			div.mousemove(function(e){
				var leftP = (e.pageX - divO.left) * (divW - imgW) / divW;
				var topP = (e.pageY - divO.top) * (divH - imgH) / divH;
				img.css({left: leftP, top: topP});
			});
		});
	};
	
	/* indexOf for IE */
	
	if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}
	
	/* LIMITED EDITIONS COLOR CHANGER */
	
	var limited_editions_colors = ['#f1ac4d', '#cd1628', '#c1cc70', '#fbd542', '#3f6c57', '#3f668d', '#795241'];
	var limited_editions = $('#limited_editions');
	var limited_editions_letters = limited_editions.find('span');
	
	limited_editions.data("interval", setInterval(function() {
		var random_letters = new Array();
		
		while(random_letters.length<8)
		{
			r = Math.floor(Math.random() * limited_editions_letters.length);
			if (random_letters.indexOf(r) == -1) random_letters.push(r);
		}
		
		limited_editions_letters.css('color', '');
		
		for(i=0;i<random_letters.length;i++)
		{
			if (i<4) limited_editions_letters.eq(random_letters[i]).css('color', limited_editions_colors[Math.floor(Math.random() * limited_editions_colors.length)]);
			else limited_editions_letters.eq(random_letters[i]).css('color', '#c4c4c4');
		}		
		
	}, 400));
	
	/* LIMITED EDITIONS CYCLE */
	
	$('#limited_editions_top_right').cycle({
		speed: 1000,
		timeout: 3000
	});
	
	/* IE6 MENU FIX */
	
	$('#category_links > ul > li').hover(function(){
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	/* PRODUCT LIST BOX TOOLTIP*/
	
    $('div.product_list_box a').tooltip({
		position: 'top center',
		offset: [50, 0],
		delay: 100,
		predelay: 300,
		relative: true,
		effect: 'fade'
    }).dynamic();
    
    /* PRODUCT TIP */
   
	 $('#product_right div.tip a').tooltip({
		position: 'center left',
		offset: [0, 50],
		delay: 100,
		predelay: 300,
		relative: true,
		effect: 'fade'
	 }).dynamic();
	 
    /* PRODUCT INPUT LIST */
   
	 $('#product_right div.input_label_list > label > img').tooltip({
		position: 'top center',
		offset: [10, 0],
		delay: 100,
		predelay: 300,
		relative: true,
		effect: 'fade'
	 });
	 
	 /* PRODUCT INPUT LIST FIX */
	$('#product_right').delegate('label img', "click", function() {
		$("#" + $(this).parents("label").attr("for")).click();
	});
	 
});
