
//window.addEvent('load', function() { - from MooTools to jQuery
$(document).load(function() {

  // Transparency hack for IE 6 (MooTools)
  //if (Browser.Engine.trident4) {
  //[$$('img[src$=.png]'), $('nav')].each(function(item) {
  //  item.setStyle('behavior', 'url(/_scripts/pngfix.htc)');
  //});
  //}

  // Transparency hack for IE6
  if ($.browser.msie) {
    if ($.browser.version <= 6) {
      [$('img[src$=.png]'), $('nav')].each(function(item) {
        item.css('behavior', 'url(/_scripts/pngfix.htc)');
      });
    }
  }

  // CSS3 hack for IE6+IE7
  //if (Browser.Engine.trident4) $$('div.AspNet-Menu-Horizontal li:first-child').setStyle('border-left', 'none');
  //if (Browser.Engine.trident) $$('div.AspNet-Menu-Horizontal ul ul').setStyle('opacity', 0.7);

  // CSS3 hack for IE6+IE7
  if ($.browser.msie) {
    if ($.browser.version <= 6) {
      $('div.AspNet-Menu-Horizontal li:first-child').css('border-left', 'none');
    }
    $('div.AspNet-Menu-Horizontal ul ul').css('opacity', 0.7);
  }
});


//window.addEvent('domready', function() { - from MooTools to jQuery
$(document).ready(function() {

  // replace all rel=external links with target=_blank
  $('a[rel=external]').attr('target', '_blank');
  
  // replace all rel=mediaitem links (media related to page) with target=blank
  $('a[rel=mediaitem]').attr('target', '_blank');

  getProductColors();

  $(window).load(SWT20_fadeIn);

  $('ul.AspNet-Menu2').shopMenu();
  //$.fn.shopMenu.setupLayout($('ul.AspNet-Menu2'));        // DEBUGGING
  //$.fn.shopMenu.show($('ul.AspNet-Menu2').find('li')[0]); // DEBUGGING

});

function SWT20_fadeOut(){
  $('#blinky').delay(5000).fadeOut("slow",SWT20_fadeIn);
}
function SWT20_fadeIn(){
  $('#blinky').delay(1000).fadeIn("slow",SWT20_fadeOut);
}

//2011-07-15: JM: Tijdelijk? uitgezet en vervangen door wat hopelijk een betere functie is
//function getProductColors() {

//    if ($('#Products')) {

//        $('#Products input[type=hidden]').each(function(index) {
//            var hfID = $(this).attr('id');
//            var hfVal = $(this).val();
//            // mp_cphArticle_ProductControl_Repeater3_ctl01_hfProdCode    (input:hiddenfield {input})
//            // mp_cphArticle_ProductControl_Repeater3_ctl01_pnlProdColors (div              {output})
//            //var divTarget = hfID.replace('hfProdCode', 'pnlProdColors');
//            var divTarget = hfID.replace('$', '_').replace('hfProdCode', 'pnlProdColors');
//            $.ajax({ url: '/_components/productcolors.ashx?c=' + hfVal + '&url=' + document.location,
//                context: $('#' + divTarget),
//                success: function(data) {
//                    $(this).html(data).find('a.productColor').colorSwapThumbnailAndPrice();
//                }
//            });
//        });

//    }
//}

function getProductColors() {

    if ($('#Products')) {

        $('#Products input[type=hidden]').each(function(index) {
            var hfID = $(this).attr('id');
            var hfVal = $(this).val();
            // mp_cphArticle_ProductControl_Repeater3_ctl01_hfProdCode    (input:hiddenfield {input})
            // mp_cphArticle_ProductControl_Repeater3_ctl01_pnlProdColors (div              {output})
            //var divTarget = hfID.replace('hfProdCode', 'pnlProdColors');
            var divTarget = hfID.replace('$', '_').replace('hfProdCode', 'pnlProdColors');
            var querystringseries = '';
            if ($('#querystringseries')) {
                if (($('#querystringseries').attr('series')) != undefined) {
                    querystringseries = '&series=' + ($('#querystringseries').attr('series'));
                }
            }
            $.ajax({ url: '/_components/productcolors.ashx?c=' + hfVal + '&url=' + document.location + querystringseries,
                context: $('#' + divTarget),
                success: function(data) {
                    $(this).html(data).find('a.productColor').colorSwapThumbnailAndPrice();
                }
            });
        });

    }
}

$.fn.colorSwapThumbnailAndPrice = function() {
    return this.each(function() {
        $(this).hover(function() {
            $(this).parent().parent()
                .find('.currentColor').removeClass('currentColor');
            $(this).parent().addClass('currentColor');
            $(this).parent().parent().parent().parent()
                .find('.prodImg img').attr('src', '/_images/product/thumbs/' + this.className.replace('productColor ', '') + '.jpg')
                .parent().attr('href', $(this).attr('href'))
                .parent().nextAll('a').attr('href', $(this).attr('href'));
            // 2011-06-14 - GG - Aangepast om Sales prijs zichtbaar te maken
            //$(this).parent().parent().parent().parent().find('.prodPrice .prodactualprice').html('&euro;&nbsp;' + $(this).attr('rel'));
            var price = $(this).attr('rel');
            $(this).parent().parent().parent().parent().find('.prodPrice').html(price.replace('&#060;', '<').replace('&#062;', '>').replace('&#034;', '"'));
            if ($(this).attr('-data-sales') === 'sale') {
                $(this).parent().parent().find('.salesimagedisplaynone').removeClass('salesimagedisplaynone').addClass('salesimagedisplayblock');
            } else {
                $(this).parent().parent().find('.salesimagedisplayblock').removeClass('salesimagedisplayblock').addClass('salesimagedisplaynone');
            }
        });
    });
}

function swapProductImage(currProdImgThumb, prodImgBigLink, prodImgBig, strThumbImg, strImgBgColor, strProductTitle) {
  //1
  $('.divProdImgThumb img').removeClass('thumbSelected');
  $('#' + currProdImgThumb).addClass('thumbSelected');
  //2
  $('#divProdImgBig').html('<a href="/imghandler.ashx?img=/_images/product/' + strThumbImg + '&amp;w=600&amp;fw=1' + strImgBgColor + '" rel="zoom-color:#4D4D4D; size:200px;" title="' + strProductTitle + '" id="prodImgBigLink" class="MagicMagnifyPlus"><img src="/imghandler.ashx?img=/_images/product/' + strThumbImg + '&amp;w=350&amp;fw=1' + strImgBgColor + '" id="prodImgBig" title="' + strProductTitle + '" /></a>');
  //3
  MagicMagnify_findMagnifiers()
}


function swapProdImage(currProdImgThumb) {
  $('.divProdImgThumb img').removeClass('thumbSelected');
  $('#' + currProdImgThumb).addClass('thumbSelected');
  return false;
}


(function($) {
    
    /**
     * Shop Menu
     */
    $.fn.shopMenu = function() {
        return this.each(function() {
            $.fn.shopMenu.setupLayout(this);
            $(this).find('li').each(function() {
                $(this).bind({
                    'mouseenter' : $.fn.shopMenu.onShow,
                    'mouseleave' : $.fn.shopMenu.onHide
                });
            });
        });
    }

    $.fn.shopMenu.onShow = function(e) {
        $.fn.shopMenu.show(this);    
    }

    $.fn.shopMenu.show = function(item) {
        $(item).find('.wrapper').css('z-index', '999').stop(true, true).slideDown(400, 'easeOutQuad');
    }

    $.fn.shopMenu.onHide = function(e) {
        $.fn.shopMenu.hide(this);    
    }

    $.fn.shopMenu.hide = function(item) {
        $(item).find('.wrapper').css('z-index', '901').stop(true, true).slideUp(400, 'easeOutQuad');
    }

    $.fn.shopMenu.setupLayout = function(menu) {
        var wrapper, lists, columns, width, height;

        $(menu).find('li.AspNet-Menu-Leaf .wrapper').each(function() {
            wrapper = $(this);
            
            wrapper.find('.bg').css('opacity', 0.9);
            wrapper.css('display', 'block');
            
            width = wrapper.width() + 20;
            wrapper.css('width', '10000px');
            height = wrapper.height() - 40;

            lists = wrapper.find('ul');
            columns = lists.length;

            lists.each(function() {
                $(this).css({
                    'width'  : width + 'px',
                    'height' : height + 'px'
                });
            });

            wrapper.css({
                'width'   : (width * columns + columns * 40) + 'px',
                'display' : 'none'
            });
        })
    }

})(jQuery);
