// JQUERY CUSTOM COMMANDS
$(function() {   // start javascript when document is loaded


    // homeCategories
    $('.homeCategories li').hover(function() {
        $(this).addClass('hover').css({ position: "relative" });
        $(this).parents('.homeCategories').addClass('zindex');
        Cufon.replace('.homeCategories h2');
    }, function() {
        $(this).removeClass('hover').css({ position: "static" });
        $(this).parents('.homeCategories').removeClass('zindex');
        Cufon.replace('.homeCategories h2');
    }
	);

    // HeaderCart SlideUp and SlideDown incl TextAdjust
    $('#headerCart .head .actions .left a').click(function() {
        if ($('.list').is(':hidden')) {
            $(this).parents('.head').next('.list').slideDown("10");
        }
        else {
            $(this).parents('.head').next('.list').slideUp("10");
        }
        $(this).text($(this).text() == 'Bekijken' ? 'Verbergen' : 'Bekijken');
    });

    $('#headerCart .list .actions .left a').click(function() {
        $(this).parents('.list').slideUp("10");
        $(this).parents('.list').prev('.head').find('.left').find('a').text($(this).text() == 'Bekijken' ? 'Verbergen' : 'Bekijken');
    });
    /*		
    $('#headerCart').mouseleave(function() {
    $(this).find('.list').slideUp("10");
    $(this).find('.list').prev('.head').find('.left').find('a').text($(this).text() == 'Bekijken' ? 'Verbergen' : 'Bekijken');
    });
    */

    /*
    $('.homeCategories li').hover(function () {
    $(this).next().find('.content').css("background-image", "none").end().prev().find('.content').css("background-image", "none"); 
    });
    */

    // First, Last & odd, even
    $('.homeCategories li:first, #headerCart li:first').addClass('first').find('.box').addClass('first');
    $('.homeCategories li:last, #headerCart li:last').addClass('last').find('.box').addClass('last');
    $('.customSelect').find('li:last').addClass('last');
    $('.compareList li:even').addClass('even');
    $('.similar li:even').addClass('even');
    $('.collateList .contains ul li:even').addClass('even');
    $('tr th:first').addClass('first').next().next().addClass('time').next().addClass('amount');
    $('tr th:last').addClass('last');
    $('tr:even').addClass('even');
    $('table tr:last').addClass('last');
    $('table tr').find('td:first').addClass('first').next().addClass('second');
    $('table tbody tr').find('.second').next().addClass('time').next().addClass('amount');
    $('table tr').find('td:last').addClass('last');
    $('table tfoot tr').find('td:last').prev().addClass('secondlast');
    $('table tfoot tr:first').addClass('first').find('td:first').next().next().addClass('third').next().addClass('fourth').parents('tfoot').find('tr:gt(0)').addClass('gt0').parent().find('tr:last').addClass('footlast');

    // In case of old price
    $('.lister .price:has(.old)').addClass('inclOld');
    $('.lister .inclOld').parent().find('.items').addClass('withOld');


    // Other input image for ie6 on lister & detail & vergelijkpagina
    if ($.browser.msie && $.browser.version == 6.0) {
        $('.lister li .submit input.rollover').attr('src', '/images/btn_order.gif');
        //alert($('.lister .submit input.rollover').attr('src')); 
        $('.lister li.even .submit input.rollover').attr('src', '/images/btn_order_even.gif');
        $('#detail .buy .submit input.rollover').attr('src', '/images/btn_order_2.gif');
        $('#detail .similar .actions input.rollover').attr('src', '/images/btn_order.gif');
        $('.collateListBox .submit input').attr('src', '/images/btn_order_even.gif');
    }


    // CUSTOM SELECT
    // Show on click
    $(".customSelect a.selected").hover(function() {
        $(this).parents("#filterBox").addClass('zindex');
        $(this).parents(".customSelect").css({ position: "relative" });
        $(this).parents(".customSelect").find('ul').show();
        //$(this).parents(".customSelect").find('li').show();
        return false;
    });
    // Hide on mouseout
    $(".customSelect").mouseleave(function() {
        $(this).parents("#filterBox").removeClass('zindex');
        $(this).find('ul').hide();
        //$(this).find('li').hide();
    });
    // Set width from parent to child
    $(".customSelect").each(function(i) {
        var $customSelectWidth = $(this).width();
        $(this).find('ul').width($customSelectWidth);
    });
    // Selected backgrounds changes with ul hover or leave
    $('.customSelect ul').hover(function() {
        $(this).parent().addClass('hover');
    });
    $('.customSelect ul').mouseleave(function() {
        $(this).parent().removeClass('hover');
    });


    /* EQUAL HEIGHTS Vergelijkpagina*/
    equalHeight($(".articleLi .contains"));
    equalHeight($(".articleLi .price"));
    equalHeight($(".shoppingInfo .address"));

    function equalHeight(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }

    if ($.browser.msie && $.browser.version == 6.0) {
        // NODIG VOOR IE6!!! ANDERS KRIJG JE GEEN BACKGROUND IN DE EXTRA HEIGHT 
        $(".articleLi .content").css({ background: "url(/images/compare_back.png) repeat-y 0 0" });
        // NODIG VOOR IE6!!! ANDERS KRIJG JE GEEN BACKGROUND IN DE EXTRA HEIGHT
        var x = $(".articleLi").height();
        //$(".collateList").height(x + 20);
        //$("#collateBox .content").height(x - 65);
        $(".collateListBox").height(x);
        // NODIG VOOR IE6!!! ANDERS KRIJG JE GEEN BACKGROUND IN DE EXTRA HEIGHT
        $("#collateBox > .content").css({ background: "url(/images/content_back.png) repeat-y 0 0" });

        var t = $(".shoppingTable table").height();
        $(".shoppingTable").height(t);
        // NODIG VOOR IE6!!! ANDERS KRIJG JE GEEN BACKGROUND IN DE EXTRA HEIGHT
        $("#shoppingBasket > .content").css({ background: "url(/images/content_back.png) repeat-y 0 0" });
    }

    // Slice shoppingbasket text
    var slicePoint = 180;
    var widow = 4;
    $('#shoppingBasket .description').each(function() {
        var allText = $(this).html();
        var startText = allText.slice(0, slicePoint).replace(/\w+$/, '');
        var endText = allText.slice(startText.length);
        if (endText.replace(/\s+$/, '').split(' ').length > widow) {
            $(this).html([
				 startText,
				 '<span style="display:none;">',
				   endText,
				 '</span>'
				 ].join('')
			   );
        }
    });


    /* DELIVERY ADDRESS show/hide checkout*/
    $("input.cbDeliveryAdress[@checked]").parents('#shoppingBasket').find('.delivery').show();
    $("input.cbDeliveryAdress").not(":checked").parents('#shoppingBasket').find('.delivery').hide();
    $(".cbDeliveryAdress").click(function() {
        if ($(".cbDeliveryAdress").is(":checked")) {
            $(".delivery").show();
        } else {
            $(".delivery").hide();
        }
    });

    /* DELIVERY ADDRESS show/hide fields checkout*/
    showhidePS()
    $(".fc_s, .fc_p").click(function() {
        showhidePS()
    });
    function showhidePS() {
        if ($(".fc_s").is(":checked")) {
            $('.street').show();
            $('.bus').hide();
        } else {
            $('.street').hide();
            $('.bus').show();
        }
    }
    showhidePS2()
    $(".fc_s2, .fc_p2").click(function() {
        showhidePS2()
    });
    function showhidePS2() {
        if ($(".fc_s2").is(":checked")) {
            $('.street2').show();
            $('.bus2').hide();
        } else {
            $('.street2').hide();
            $('.bus2').show();
        }
    }




    // SELECT RADIO WHEN CLICKED ON PARENT DIV checkout
    $('.methods .method').click(function() {
        $(this).find('input').attr('checked', 'checked');
    });
    // Different for checkbox
    $('.control .method').click(function(event) {
        var $target = $(event.target);
        if ($target.is(":not('input, label, a, select, option')")) {
            $(':checkbox', this).trigger('click');
        }
    });
    $('.method').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    // Active classes for payment methods
    $(".method input#id[@checked]").parent('.method').addClass('active');
    $(".method input#id").not(":checked").parents('.method').removeClass('active');
    $(".method input#cc[@checked]").parent('.method').addClass('active');
    $(".method input#cc").not(":checked").parents('.method').removeClass('active');
    $(".method input#ccv[@checked]").parent('.method').addClass('active');
    $(".method input#ccv").not(":checked").parents('.method').removeClass('active');
    $(".method input#ab[@checked]").parent('.method').addClass('active');
    $(".method input#ab").not(":checked").parents('.method').removeClass('active');
    $('.methods .method').click(function(event) {
        var $target2 = $(event.target);
        if ($target2.is(":not('.input input, .input2 input, .input3 input, a, select, option')"))
            $(this).parent().find('.method').removeClass('active').end().end().addClass('active');
    });

    // REMOVING ERROR CLASSES

    $('.error').find('input').blur(function() {
        $(this).parent().parent().removeClass('error');
        $(this).parent().parent().find('.hint').hide();
        equalHeight($(".shoppingInfo .address"));
    });

    $('.iDeal select.select').change(function() {
        $(this).parent().removeClass('error').parent().removeClass('error');
        $(this).parent().find('.hint').hide();
    });

    $('.dates select.select').change(function() {
        var s1 = $('.select1 :selected').text();
        var s2 = $('.select2 :selected').text();
        var s3 = $('.select3 :selected').text();
        var s4 = $('.select4 :selected').text();
        var x1 = "mm";
        var x2 = "jjjj";

        if ((s1 != x1) && (s2 != x2)) {
            $(this).parents('li').removeClass('error');
            $(this).parent().parent().find('.hint').hide();
            //alert('geen defaults');
        }
        if ((s3 != x1) && (s4 != x2)) {
            $(this).parents('li').removeClass('error');
            $(this).parent().parent().find('.hint').hide();
            //alert('geen defaults');
        }
        //alert(s1);alert(s2);alert(s3);alert(s4);		
    });

    $('.method .textfield').blur(function() {
        var merror = $('.method ul').children('li.error').size();
        if (merror == 0) {
            $(this).parents('.method').removeClass("error");
        }
    });
    $('.method select.select').change(function() {
        var merror = $('.method ul').children('li.error').size();
        if (merror == 0) {
            $(this).parents('.method').removeClass("error");
        }
    });
    $('.control .error').click(function() {
        $(this).removeClass('error');
    });

    //REMOVE HEADER ERROR ON FOCUS BLUR & CHANGE OF ELEMENTS
    function removedErrors() {
        var billingerror = $('.billing fieldset').children('.error').size();
        var deliveryerror = $('.delivery fieldset').children('.error').size();
        var methodserror = $('.methods fieldset').children('.error').size();
        var controlerror = $('.control fieldset').children('.error').size();
        if (billingerror + deliveryerror + methodserror + controlerror == 0) {
            $('.info').find('.errorMessage').slideUp("fast").parent().removeClass("error");
        }
    }
    $('input.textfield').blur(function() {
        removedErrors();
    });
    $('.method select.select').change(function() {
        removedErrors();
    });
    $('.control .error').click(function() {
        removedErrors();
    });

    function showHideMethodError() {
        $('#wrapper .methods .error .error').find('.hint').hide();
        $('#wrapper .methods .error.active .error').find('.hint').show();
    }
    showHideMethodError()
    $('.methods').click(function() {
        showHideMethodError()
    });



    /* LISTITEMS vergelijkpagina*/
    $(".collateListBox ul").find(".articleLi:gt(2)").hide();
    $(".collate .prev").hide();

    var n = $(".articleLi").length; // aantal producten
    var teller = 1; // teller begint op 1

    if (n <= 3) {
        $(".collate .next").hide();
    }

    $('.collate .next').click(function() {
        $(".collateListBox ul").find(".articleLi:visible:last").next(".articleLi:hidden").show();
        $(".collateListBox ul").find(".articleLi:visible:first").hide();
        teller++;
        //alert(teller);
        showhideButtons(); // roep showhidefunctie op als je op next klikt
        return false;
    })

    $('.collate .prev').click(function() {
        $(".collateListBox ul").find(".articleLi:visible:first").prev(".articleLi:hidden").show();
        $(".collateListBox ul").find(".articleLi:visible:last").hide();
        teller--;
        // alert(teller);
        showhideButtons(); // roep showhidefunctie op als je op prev klikt
        return false;
    })

    function showhideButtons() {
        if (teller == (n - 2)) { // als teller opgeteld tot totaal aantal th's -2 dan next button hide, anders show
            $(".collate .next").hide();
        } else {
            $(".collate .next").show();
        }

        if (teller == 1) { // als teller beginwaarde prev hide, anders show.
            $(".collate .prev").hide();
        } else {
            $(".collate .prev").show();
        }
    }


    /* FAQ TOGGLE in faq*/
    $('.faqList .faqAnswer').hide();
    $('.faqList .faqQuestion a').click(function() {
        $(this).parents('.faq').find(".faqAnswer").hide(); // Hide all answers
        $(this).parents('.faq').find('li').removeClass("active");
        $(this).parents('li').find(".faqAnswer").show(); // Show current answer
        $(this).parents('li').addClass("active");
        return false;
    });



    // ROLLOVER IMAGES (1/2)
    IMAGE.rollover.init();

}); // end ready function


// ROLLOVER IMAGES (2/2)
IMAGE = {};
IMAGE.rollover = {
    init: function() {
        this.preload();
        $(".rollover").hover(
         function() { $(this).attr('src', IMAGE.rollover.newimage($(this).attr('src'))); },
         function() { $(this).attr('src', IMAGE.rollover.oldimage($(this).attr('src'))); }
      );
    },
    preload: function() {
        $(window).bind('load', function() {
            $('.rollover').each(function(key, elm) { $('<img>').attr('src', IMAGE.rollover.newimage($(this).attr('src'))); });
        });
    },
    newimage: function(src) {
        return src.substring(0, src.search(/(\.[a-z]+)/)) + '_on' + src.match(/(\.[a-z]+)/)[0];
    },
    oldimage: function(src) {
        return src.replace(/_on/, '');
    }
};
