// JQUERY CUSTOM COMMANDS
$(document).ready(function () {  // start javascript when document is loaded

    /* FOCUS CLASS ON :FOCUS */
    $('.textfield').focus(function () { // IE6 does not support :focus on elements other than <a>
        $(this).addClass('focus');
    }, function () {
        $(this).removeClass('focus');
    });

    /* MENU HOVERS */
    $('#mainMenu li, .imgZoom, .combiImage, .matching .image, .also .image').hover(function () { // IE6 does not support :hover on elements other than <a>
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });
    $('#mainMenu li.active:has(ul)').addClass('hasSub'); // give class to mainmenu parent li's.

    /* in IE6/7 de li's van de bovenstaande hovers voor matching fabrics & also bought over elkaar laten vallen */
    $('.matching .image, .also .image').hover(function () {
        $(this).parents('li').addClass('zindex');
    }, function () {
        $(this).parents('li').removeClass('zindex');
    });

    // FORM FIELDS ERROR HIDE tb_mailafriend
    $('ul.formfields div.error:empty').hide(); // Hide empty error fields
    // Input focus / blur / error
    $('input.textfield, textarea.textarea, select.select').blur(function () {
        $(this).addClass("focus");
        $(this).removeClass("error");
        $(this).parents("li").find("div.error").empty().hide();
    });
    $('input.textfield, textarea.textarea, select.select').blur(function () {
        $(this).removeClass("focus");
    });

    // FORM ERRORS HIDE ON BLUR/CLICK
    $('.checkout input, .contact input, .newsletter input, textarea, .coupon input').blur(function () { // ,#columnRight .boxAccount input < toevoegen als error hint moet verdwijnen bij algemene login error
        $(this).parents("div").removeClass("error");
    });
    $('.radio').click(function () { // bovenstaande begrijpt CHROME niet, dus voor chrome on click ipv blur
        $(this).parents("div").removeClass("error");
    });

    // Cursor hand on hover language selection
    $(".customSelect").hover(function () {
        $(this).css({ 'cursor': 'pointer' });
    });
    
    // Account toggle //
    $("#headerAccount .account").click(function () {
        $("#headerAccount").toggleClass("dropped");
        $("#headerAccount").find('.accountInfo').toggle();
    });

    if ($('div#headerAccount.dropped')) {
        $(document).click(function (event) {
            var $target = $(event.target);
            if ($target.is(":not('div#miniBasket, div#headerAccount *')")) {
                $('div#headerAccount').removeClass('dropped');
                $("#headerAccount").find('.accountInfo').hide();
            }
        });
    }

    // Fully clickable inputs
    $('.collection li').click(function (event) {
        if ($(event.target).is(":not('a, input, label')")) {
            window.location = $(this).find('a').attr("href");
        }
    });

    // CUSTOM SELECT
    // Show on click
    $(".customSelect a.selected").click(function () {
        if ($('div#headerAccount.dropped')) {
            $('div#headerAccount').removeClass('dropped');
            $("#headerAccount").find('.accountInfo').hide();
        }
        $(this).parents(".customSelect").css({ position: "relative" });
        $(".customSelect").addClass("customSelectActive");
        $(this).parents(".customSelect").find('ul').show();
        return false;
    });
    // Hide on mouseout
    $(".customSelect").mouseleave(function () {
        $(this).find('ul').hide();
        $(this).css({ position: "static" });
        $(this).removeClass("customSelectActive");
    });
    // Hide on mouseout of header
    $("#header .customSelect").mouseleave(function () {
        $(this).find('ul').hide();
        $(this).css({ position: "absolute" });
        $(this).removeClass("active");
    });
    // Set width from parent to child
    $(".customSelect").each(function (i) {
        var $customSelectWidth = $(this).width();
        $(this).find('ul').width($customSelectWidth);
    });

   
    //CHECKBOXES DELIVERYADDRESS checkout_02      
    //Hide div w/id extra
    $("input#fc_differ[@checked]").parents('.subCol').find('.delivery').show();
    $("input#fc_differ").not(":checked").parents('.subCol').find('.delivery').hide();
    $("input#fc_differ").click(function () {
        if ($("input#fc_differ").is(":checked")) {
            $(".delivery").show();
        } else {
            $(".delivery").hide();
        }
    });


    // SELECT RADIO WHEN CLICKED ON PARENT DIV checkout_03 & TR in checkout_05
    $('.shipping .method, .payment .method tr').click(function () {
        $(this).find('input').attr('checked', 'checked');
    });


    // CHANGE DIV COLOR ON HOVER checkout_03
    $('.shipping .method').hover(function () {
        $(this).css({ 'background-color': '#f1eee6', 'cursor': 'pointer' });
    }, function () {
        $(this).css({ 'background-color': 'transparent', 'cursor': 'default' });
    });
    // CHANGE TR TDs COLOR ON HOVER checkout_05
    $(' .payment .method tr').hover(function () {
        $(this).find('td').css({ 'background-color': '#f1eee6', 'cursor': 'pointer' });
    }, function () {
        $(this).find('td').css({ 'background-color': 'transparent', 'cursor': 'default' });
    });

    /* MORE INFO TOGGLE */
    $('.moreContainer').find('.moreInfo').hide().end().find('.lessLink').hide().end().find('.moreLink a').click(function () {
        $(this).parents('.moreContainer').find('.moreInfo').show();
        $(this).parents('.moreContainer').find('.moreLink').hide();
        /* $(this).parents('.moreContainer').find('.lessLink').show(); */ //dit gedeelte kan ook merged met .moreinfo show
        return false;
    });
    /* $('.moreContainer .lessLink a').click(function() {         // kan toegevoegd wordne als less info moet
    $(this).parents('.moreContainer').find('.moreInfo').hide();
    $(this).parents('.moreContainer').find('.moreLink').show();
    return false;
    }); */


    /* FIRST/LAST LIST_ITEM OTHER LIST-STYLE sitemap */
    $('.sitemap ul li ul li:last-child').addClass("last");
    $('.sitemap ul li ul li:first-child').addClass("first");


    // SELECT CHECKBOX WHEN OTHER CHECKBOX IS SELECTED checkout_04 of 03
    $("#fc_agree_top").click(function () {
        if ($("#fc_agree_top").is(":checked")) {
            $("#fc_agree").attr('checked', 'checked');
        } else {
            $("#fc_agree").attr('checked', '');
        }
    });
    $("#fc_agree").click(function () {
        if ($("#fc_agree").is(":checked")) {
            $("#fc_agree_top").attr('checked', 'checked');
        } else {
            $("#fc_agree_top").attr('checked', '');
        }
    });

    // ADD MARGIN IF IMG IS NOT 1ST IMG (combination discounts) FCK-parts
    $(".newsletter, .sitemap").find(".box:last").css('margin-bottom', '0px');

    // ADD MARGIN IF NOT 1ST PRODUCT (combination discounts) checkout_01, 04 en 06
    $(".checkoutCart tr").find(".imgZoom:gt(0)").css('margin-top', '7px');

    // ADD MARGIN IF IMG IS NOT 1ST IMG (combination discounts) FCK-parts
    $(".imgLeft, .imgRight").find("img:gt(0)").css('margin-top', '5px');

    // ADD MARGIN IF IMG IS NOT 1ST IMG (combination discounts) FCK-parts
    $(".checkoutCart td table").find("tr:gt(0) td").css({ 'padding-top': '7px', 'height': '40px' });

    // ADD PLUS BETWEEN ITEMS IF MORE THAN 1 IN COMBI (combination discounts) checkout_01
    $(".checkoutCart td table").find("tr:gt(0) td div").append('<img src="images/plus_co.gif" class="plus" alt="plus" />'); //<img src="images/plus_co2.gif" class="plus2" alt="plus" />     ook toevoegenals plus tussne teksten ook wenselijk

    // ADD MARGIN IF NOT 1ST MINICART ITEM (tb_addtobag & tb_combi) tb_addtobag & tb_combi
    $(".miniCart:gt(0)").css({ 'margin-top': '20px', 'border-top': '1px solid #DCD5BF', 'padding-top': '14px' });

    // ADD MARGIN IF LAST MINICART ITEM (tb_addtobag & tb_combi) tb_addtobag & tb_combi
    if ($(".miniCart").length > 1) {
        $(".miniCart:last").css('margin-bottom', '35px');
    }

    // FF only because these cellpaddings work different in FF
    if ($.browser.mozilla) {
        /* HIDE SELECTS ON ABSOLUTE HOVERS in lister pagina van toepassing*/
        $(".checkoutCart td table").find("tr:gt(0) td").css({ 'padding-top': '7px', 'height': '47px' });
    }


    $(".items li .price:has(.discount)").addClass('containing');


    // ADJUST THICKBOX HEIGHT WITH LARGE COMBI OR INCLUDED ARTICLES
    function thickboxResize() {

        var boundHeight = 130; // minimum height
        var viewportHeight = (self.innerHeight || (document.documentElement.clientHeight || (document.body.clientHeight || 0)))

        $('.combibag').hover(function () {
            var text = $(this).attr("alt");  // alt atr van ... met class addtobag
            var chex = $('.detailList input[type=checkbox], .suggest input[type=checkbox]');
            var included = chex.filter(':checked').length; // aantal geselecteerde checkboxes
            var combisize = $(".combiImage").length; // aantal combi artikelen

            if (included >= 1) {
                var btnz = 35;
            }
            else {
                var btnz = 0;
            }

            if (Math.round(boundHeight + (Math.floor(combisize / 6) * 50) + (Math.floor(included) * 100)) <= (document.documentElement.clientHeight * 0.9)) { //als meer dan 6 combi artikelen
                text = text.replace(/height=[0-9]*/, 'height=' + Math.round(boundHeight + (Math.floor(combisize / 6) * 50) + (Math.floor(included) * 100) + btnz));
            }
            else {
                text = text.replace(/height=[0-9]*/, 'height=' + (document.documentElement.clientHeight * 0.9) + btnz);
            }

            $(this).attr("alt", text);
        });

        $('.addtobag').hover(function () {
            var text = $(this).attr("alt");  // alt atr van ... met class addtobag
            var chex = $('.detailList input[type=checkbox], .suggest input[type=checkbox]');
            var included = chex.filter(':checked').length; // aantal geselecteerde checkboxes

            if (included >= 1) {
                var btnz = 35;
            }
            else {
                var btnz = 0;
            }

            if (Math.round(boundHeight + (Math.floor(included) * 100)) <= (document.documentElement.clientHeight * 0.9)) {
                text = text.replace(/height=[0-9]*/, 'height=' + Math.round(boundHeight + (Math.floor(included) * 100) + btnz));
            }
            else {
                text = text.replace(/height=[0-9]*/, 'height=' + (document.documentElement.clientHeight * 0.9) + btnz);
            }

            $(this).attr("alt", text);
        });
    }



    $(window).bind('load', thickboxResize);
    $(window).bind('resize', thickboxResize);



    // ROLLOVER IMAGES (1/2)
    DNZ.rollover.init();

});  // end ready function


// ROLLOVER IMAGES (2/2)
// Rollover adds '_on' to image source name.
DNZ = {};
DNZ.rollover = {
   init: function() {
      this.preload();
      $(".rollover").hover(
         function () { $(this).attr( 'src', DNZ.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', DNZ.rollover.oldimage($(this).attr('src')) ); }
      );
   },
   preload: function() {
         $('.rollover').each( function( key, elm ) { $('<img>').attr( 'src', DNZ.rollover.newimage( $(this).attr('src') ) ); });
      $(window).bind('load', function() {
      });
   },
   newimage: function( src ) {
      return src.substring( 0, src.search(/(\.[a-z]+)/) ) + '_on' + src.match(/(\.[a-z]+)/)[0];
   },
   oldimage: function( src ) {
      return src.replace(/_on/, '');
   }
};



