$(document).ready(function(){
    
    // WISHLIST Animation ------------------------------------------------------
    $('#wishlist_link').click(function(){
        $('#wishlist_list').stop().fadeToggle();
    });
    /*$('#wishlist_link').hover(
        function(){ $('#wishlist_list').stop().fadeIn(); },
        function(){ $('#wishlist_list').stop().fadeOut(); }
    );*/
    $('#wishlist_link').mouseleave(function(){
        $('#wishlist_list').hover(
            function(){ $(this).stop(); },
            function(){ $('#wishlist_list').stop().delay(3000).fadeOut(); }
        );
    });
    
    // PRODUCT Slider Animation ------------------------------------------------
        // --- Set width of wrapper ---
    var number_of_items = $('.product_item').length;
    var width_of_item = $('.product_item').width();
    var complete_width = (number_of_items > 0)? number_of_items*width_of_item : width_of_item;
    $('ul#all_product_items').css({ 'width':complete_width+'px' });
        
    var max_marginLeft = -(number_of_items-1)*width_of_item;
        
        // --- show correct state of controls ---
    //alert(parseFloat($('ul#all_product_items').css('margin-left'))+" = "+max_marginLeft);
    // disable RIGHT control if applicable
    if(parseFloat($('ul#all_product_items').css('margin-left')) == max_marginLeft){
        $('#slide_control_right').addClass('disabled');
    }
    // disable LEFT control if applicable
    if(parseFloat($('ul#all_product_items').css('margin-left')) == 0){
        $('#slide_control_left').addClass('disabled');
    }
        
        // --- make controls functionable - right ---
    $('#slide_control_right').click(function()
    {
        $('ul#all_product_items').stop(true,true);
        if(parseFloat($('ul#all_product_items').css('margin-left')) > max_marginLeft)
        {            
            // disable RIGHT control if applicable
            if(parseFloat($('ul#all_product_items').css('margin-left'))-width_of_item == max_marginLeft){
                $('#slide_control_right').addClass('disabled');
            }
            // enable LEFT control if applicable
            if(parseFloat($('ul#all_product_items').css('margin-left')) >= max_marginLeft){
                $('#slide_control_left').removeClass('disabled');
            }
            $('ul#all_product_items').fadeTo('fast',0.25);
            $('ul#all_product_items').animate({ 'margin-left':'-='+width_of_item+'px' });
            $('ul#all_product_items').fadeTo('fast',1);            
        }
    });
    
        // --- make controls functionable - left ---
    $('#slide_control_left').click(function()
    {
        $('ul#all_product_items').stop(true,true);
        if(parseFloat($('ul#all_product_items').css('margin-left')) < 0)
        {            
            // enable RIGHT control if applicable
            if(parseFloat($('ul#all_product_items').css('margin-left')) <= max_marginLeft){
                $('#slide_control_right').removeClass('disabled');
            }
            // disable LEFT control if applicable
            if(parseFloat($('ul#all_product_items').css('margin-left'))+width_of_item == 0){
                $('#slide_control_left').addClass('disabled');
            }
            
            // do animation
            $('ul#all_product_items').fadeTo('fast',0.25);
            $('ul#all_product_items').animate({ 'margin-left':'+='+width_of_item+'px' });
            $('ul#all_product_items').fadeTo('fast',1);            
        }
    });
    
    
    // RELATED PRODUCT Load Images ------------------------------------------------
        
        // --- Preload-Function ---
        /*function preloadImages(image_index,srcArray,parentObject)
        {
            $(parentObject).css({ 'background':'none' });
            
            var number_of_images = srcArray[image_index].length;
            for(var x=0; x<number_of_images; x++){
                $(parentObject).find('.related_options').append('<li><a href=""><span>Related product '+x+'</span></a></li>');
            }
            
            var relatedProduct = new Array();
            for(var z=0; z<number_of_images; z++){
                relatedProduct[z] = new Image();
                relatedProduct[z].src = srcArray[image_index][z];
            }
        
            for(var i=0; i<relatedProduct.length; i++){
                $(parentObject).find('.related_options').find('li:nth-child('+(i+1)+')').css({ 'background-image':'url("'+relatedProduct[i].src+'")' });
            }
        }
        
        // RELATED PRODUCT Slider Animation ------------------------------------------------
        function animateRelatedProducts()
        {
                // --- Set width of wrapper ---
            $('.related_products').each(function(){
                var number_of_items = $(this).find('li').length;
                var width_of_item = $(this).find('li').width();
                    width_of_item += parseFloat($(this).find('li').css('borderLeftWidth'));
                    width_of_item += parseFloat($(this).find('li').css('borderRightWidth'));
                    width_of_item += parseFloat($(this).find('li').css('margin-right'));
                $(this).find('.related_options').css({ 'width':number_of_items*width_of_item+'px' });
                    
                var max_marginLeft = -(number_of_items-6)*width_of_item;
                    
                    // --- show correct state of controls ---
                //alert(parseFloat($('ul#all_product_items').css('margin-left'))+" = "+max_marginLeft);
                // disable RIGHT control if applicable
                if( (parseFloat($(this).parent().find('ul.related_options').css('margin-left')) == max_marginLeft) || (number_of_items<=6) ){
                    $(this).parent().find('.related_control_right').addClass('disabled');
                }
                // disable LEFT control if applicable
                if(parseFloat($(this).parent().find('ul.related_options').css('margin-left')) == 0){
                    $(this).parent().find('.related_control_left').addClass('disabled');
                }
                
                    // --- make controls functionable - right ---
                $(this).find('.related_control_right').click(function()
                {
                    if(parseFloat($(this).parent().find('ul.related_options').css('margin-left')) > max_marginLeft)
                    {   
                        // disable RIGHT control if applicable
                        if(parseFloat($(this).parent().find('ul.related_options').css('margin-left'))-width_of_item == max_marginLeft){
                            $(this).parent().find('.related_control_right').addClass('disabled');
                        }
                        // enable LEFT control if applicable
                        if(parseFloat($(this).parent().find('ul.related_options').css('margin-left')) >= max_marginLeft){
                            $(this).parent().find('.related_control_left').removeClass('disabled');
                        }
                        $(this).parent().find('ul.related_options').fadeTo('fast',0.25);
                        $(this).parent().find('ul.related_options').animate({ 'margin-left':'-='+width_of_item+'px' });
                        $(this).parent().find('ul.related_options').fadeTo('fast',1);            
                    }
                });
                
                   // --- make controls functionable - left ---
                $(this).find('.related_control_left').click(function()
                {
                    if(parseFloat($(this).parent().find('ul.related_options').css('margin-left')) < 0)
                    {            
                        // enable RIGHT control if applicable
                        if(parseFloat($(this).parent().find('ul.related_options').css('margin-left')) <= max_marginLeft){
                            $(this).parent().find('.related_control_right').removeClass('disabled');
                        }
                        // disable LEFT control if applicable
                        if(parseFloat($(this).parent().find('ul.related_options').css('margin-left'))+width_of_item == 0){
                            $(this).parent().find('.related_control_left').addClass('disabled');
                        }
                        
                        // do animation
                        $(this).parent().find('ul.related_options').fadeTo('fast',0.25);
                        $(this).parent().find('ul.related_options').animate({ 'margin-left':'+='+width_of_item+'px' });
                        $(this).parent().find('ul.related_options').fadeTo('fast',1);            
                    }
                });
            });
        }
        
        // --- Preloader ---
        var loadImages = setTimeout(function()
                            {
                                // --- image src array ---
                                var srcArray = new Array();
                                    srcArray[0] = new Array();
                                    srcArray[0][0] = '/images/product/gallery/430px_artdraeneishaman.jpg';
                                    srcArray[0][1] = '/images/product/gallery/2010_11_01_dame_algo_46_leyes.jpg';
                                    srcArray[0][2] = '/images/product/gallery/alan.jpg';
                                    srcArray[0][3] = '/images/product/gallery/1217730720111_f.jpg';
                                    srcArray[0][4] = '/images/product/gallery/alan.jpg';
                                    srcArray[0][5] = '/images/product/gallery/alan.jpg';
                                    srcArray[0][6] = '/images/product/gallery/alan.jpg';
                                    srcArray[0][7] = '/images/product/gallery/alan.jpg';
                                    srcArray[1] = new Array();
                                    srcArray[1][0] = '/images/product/gallery/430px_artdraeneishaman.jpg';
                                    srcArray[1][1] = '/images/product/gallery/2010_11_01_dame_algo_46_leyes.jpg';
                                    srcArray[2] = new Array();
                                    srcArray[2][0] = '/images/product/gallery/2010_11_01_dame_algo_46_leyes.jpg';
                                
                                // --- preload image ---
                                var k=0;
                                $('.related_products').each(function()
                                {
                                    preloadImages(k,srcArray,this);
                                    k++;
                                });
                                
                                // --- set animation to slide ---
                                animateRelatedProducts();
                            
                            }, 1000);*/
    
    // Add to WISHLIST --------------------------------------------------------
    $('.size').click(function(){
        $(this).parents('.product_details').find('.empty_message').fadeOut('slow');
        
        $(this).find('input').attr('checked', 'checked');
        
        if($(this).hasClass('__product_color')){
            $(this).parent().find('input').each(function(){
                $(this).parent().find('img').attr('src','/images/img_content/colorpicker.png');
                if($(this).attr('checked') == true){ $(this).parent().find('img').attr('src','/images/img_content/colorpicker_grey.png'); }
            });
            
            // PRODUCT DETAILS  -----------------------------------------------------------
            var getSmallImage = $(this).attr('smallImage');
            var getBigImage = $(this).attr('bigImage');
            $(this).parent().parent().parent().parent().parent().find('.product_main_image').css({ 'background-image':'url("'+getSmallImage+'")' });
            $(this).parent().parent().parent().parent().parent().find('.product_main_image').attr('href',getBigImage);
        }else{
            $(this).parent().find('input').each(function(){
                $(this).parent().css({ 'background':'none' });
                if($(this).attr('checked') == true){ $(this).parent().css({ 'background':'#ccc' }); }
            });
            
        }
    });    
        
});

function addToWishlist(id_product,slug,lang){
    // retrieve data
    var sentInfo,infos,slugs;
    sentInfo = $('#wishlist_'+id_product).serialize();
    infos = sentInfo.split('&');

    if(sentInfo != ""){
        var slugs = '';
        for(slug_val in infos){
            var theSlug = infos[slug_val].split('=');
            slugs += theSlug[1];
            if(slug_val < infos.length-1){ slugs += '|'; }
        }

        var product = id_product+'|'+slug+'|'+lang+'|'+slugs;
        $.ajax({
            type: 'POST',
            url: '/wishlist/ax_set_wishlist/' + product,
            data: product,
            dataType: "json",
            success: function(wish){
                // scroll up
                $('html, body').animate({ scrollTop: $("#header_afan").offset().top}, 'slow');
                
                // change number
                $('#wishlist_link')
                .delay(300)
                .fadeOut('slow',function(){
                    $(this).attr('href','/wishlist/'+wish['slug']);
                    $(this).find('#number').html(wish['items']);
                    $(this).css({ 'color':'#fff' });
                    $(this).parents('.wishlist').removeClass('hide');
                })
                .fadeIn('slow', function(){
                    $(this).animate({ 'color':'#ffffff' },'slow');
                });
                
                // change button
                $('#wishlist_'+id_product).find('.add_to_wishlist_btn').hide();
                $('#wishlist_'+id_product).find('.in_wishlist_btn').show();
            },
            error: function(){ alert('not working') }
        });
    }else{
        $('#wishlist_'+id_product).find('.empty_message').fadeIn('slow');
    }
}

