$(document).ready(function() {


  // TEST LOGGIN CONSOLE
  jQuery.logThis = function( text ){
	  if( (window['console'] !== undefined) ){
		  console.log( text );
	  }
  }



  // FIX DROPDOWN DISABLED <IE8
  try {
    if ($.browser.msie && parseFloat($.browser.version) < 8) {
      $('option[disabled]').css({'color': '#ccc'});
      $('select').change(function() {
        if(this.options[this.selectedIndex].disabled) {
          if(this.options.length == 0) {
            this.selectedIndex = -1;
          } else {
            this.selectedIndex--;
          }
          $(this).trigger('change');
        }
      });
      $('select').each(function(it) {
        if(this.options[this.selectedIndex].disabled)
          this.onchange();
      });
    }
  } catch (ex) {
    $.logThis('FIX DROPDOWN DISABLED: ' + ex)
  }



  

  // ********** TABS ************
  try {
	  if ($('.divTabs').length) {
	  	  $(".divTabs").tabs().show();
	  	  // set active tab
	  	  var param = $.getURLParam('selectedTab');
	  	  if (param != '') {
	  	    $('.divTabs').tabs('select', param); 
	  	  }
	  	  param = $.getURLParam('selectedtab');
	  	  if (param != '') {
	  	    $('.divTabs').tabs('select', param); 
	  	  }
    }
  } catch (ex) {$.logThis('divTabs: ' + ex)}



  try {
	  $("a.zoom").fancybox({
		  overlayShow					: true,
		  hideOnContentClick	: false,
		  zoomSpeedIn					: 0,
		  zoomSpeedOut				: 0,
		  zoomSpeedChange			: 0,
		  width					      : 700,
		  height					    : 480
	  });

	  $("a.youtube").fancybox({
		  width					      : 640,
		  height					    : 360,
	    frameWidth					: 640,
		  frameHeight					: 360,
		  overlayShow					: true,
		  hideOnContentClick	: false,
		  zoomSpeedIn					: 0,
		  zoomSpeedOut				: 0,
		  zoomSpeedChange			: 0
	  });

	  $("a.zoom480").fancybox({
		  width					      : 480,
		  height					    : 400,
		  frameWidth					: 480,
		  frameHeight					: 400,
		  hideOnContentClick	: false,
		  zoomSpeedIn					: 0,
		  zoomSpeedOut				: 0,
		  zoomSpeedChange			: 0
	  });

	  $("a.zoom_product_swf").fancybox({
		  width					      : 530,
		  height					    : 640,
		  frameWidth					: 530,
		  frameHeight					: 640,
		  overlayShow					: true,
		  hideOnContentClick	: false,
		  zoomSpeedIn					: 0,
		  zoomSpeedOut				: 0,
		  zoomSpeedChange			: 0
	  });

	  $("a.zoomdyn").fancybox({
		  width					      : 640,
		  height					    : 560,
		  frameWidth					: 640,
		  frameHeight					: 560,
		  overlayShow					: true,
		  hideOnContentClick	: false,
		  zoomSpeedIn					: 0,
		  zoomSpeedOut				: 0,
		  zoomSpeedChange			: 0
	  });
  	
	  $("a.zoom800").fancybox({
		  width					      : 800,
		  height					    : 600,
		  frameWidth					: 800,
		  frameHeight					: 600,
		  overlayShow					: true,
		  hideOnContentClick	: false,
		  zoomSpeedIn					: 0,
		  zoomSpeedOut				: 0,
		  zoomSpeedChange			: 0
	  });

	  $("a.image_zoom_iframe").fancybox({
		  width					      : 640,
		  height					    : 640,
		  frameWidth					: 640,
		  frameHeight					: 560,
		  overlayShow					: true,
		  hideOnContentClick	: false,
		  zoomSpeedIn					: 0,
		  zoomSpeedOut				: 0,
		  zoomSpeedChange			: 0
	  });
  	
	  $("a.image_popup").fancybox({
		  overlayShow		: true,
		  zoomSpeedIn		: 10,
		  zoomSpeedOut	: 10,
		  zoomSpeedChange	: 10,
		  titlePosition	: 'inside'
	  });
  } catch (ex) {$.logThis('Fancybox: ' + ex)}


  try {
    $('a.zoomdyn').each(function(){ 
			var fWidth = parseInt($(this).attr('href').match(/width=[0-9]+/i)[0].replace('width=','')); 
			var fHeight =  parseInt($(this).attr('href').match(/height=[0-9]+/i)[0].replace('height=','')); 
			$(this).fancybox({
			  'width':fWidth,
			  'height': fHeight,
        'onStart' : function() { 
          $("body").css({'overflow-y':'hidden'}); 
        }, 
        'onClosed' : function() { 
          $("body").css({"overflow-y":"scroll"}); 
        } 
      });
    });
  } catch (ex) {
    $.logThis('zoomdyn: ' + ex)
  }
  
  
  // ********** IMAGE POPUP ************
  try {
    $('a.prod_zoom').fancybox({
      'hideOnContentClick' : true,
      'type' : 'iframe', 
      'centerOnScroll' : true, 
      'width' : 970,
      'height' : 1280,
      'onStart' : function() { 
        $("body").css({'overflow-y':'hidden'}); 
      }, 
      'onClosed' : function() { 
        $("body").css({"overflow-y":"scroll"}); 
      } 
    }); 
  } catch (ex) {
    $.logThis('a.prod_zoom: ' + ex)
  }
 


  // ********** AUTO POPUP ON LOAD ************
	if ($('#popup_auto').length) {
		$('#popup_auto').fancybox({
		  overlayShow		: true,
		  zoomSpeedIn		: 10,
		  zoomSpeedOut	: 10,
		  zoomSpeedChange	: 10,
		  titlePosition	: 'inside'
		}).trigger('click');
	}

  
  // ********** TOOLTIP ************
  try {
    if ($(".tooltip").length) {
      $(".tooltip").tooltip({ 
          track: false, 
          delay: 100, 
          showURL: false, 
          opacity: 1, 
          fixPNG: false, 
          showBody: " - ", 
          extraClass: "tooltip_custom", 
          top: -15, 
          left: 30
      }); 
    }
  } catch (ex) {
    $.logThis('Tooltip: ' + ex)
  }




  // ************************************ CMS EDIT ************************************
  try {
      if ($(".divEdit img").length) {
        $(".divEdit img").css('border', '1px solid transparent');
        $(".divEdit img").hover( function(){
          $(this).css('border', '1px solid #333');
        },function(){
          $(this).css('border', '1px solid transparent');
        });
        
        // CHECK JS LOADED
        if (!$.cookie) {
          //alert('add javascript for jquery.cookie.js in header');
          return;
        }
        
        // If FrontEdit in Url-Params = ON, show message
        if ($.getURLParam("FrontEdit") == 'true' || $.getURLParam("FrontEdit") == '1' || $.getURLParam("FrontEdit") == 'True' || 
            $.getURLParam("frontedit") == 'true' || $.getURLParam("frontedit") == '1' || $.getURLParam("frontedit") == 'True' || 
            $.getURLParam("Frontedit") == 'true' || $.getURLParam("Frontedit") == '1' || $.getURLParam("Frontedit") == 'True') {
            $('body').showMessage({	
              thisMessage       : ['Use SHIFT+E to enable/disable FRONT-EDIT'],
              className         : 'showMessage_Green',
              displayNavigation : false,
              delayTime         : 10000,
              position          : 'top',
              autoClose         : true});
        }
        
        // HIDE Edit using Cookie
        if ($.cookie("FrontEdit") == '0') {
          $(".divEdit").hide();
        }
        
        // Shift+E pressed, enable/disable FrontEdit
        $(document).bind('keypress', function(event) {
          var keycode = (event.keyCode ? event.keyCode : event.which);

          // Shift+E pressed, enable/disable FrontEdit
          if ((keycode == 69) && (event.shiftKey)) {

            // GET VALUE FROM COOKIE
            var FrontEdit = $.cookie("FrontEdit");
            
            // TOGGLE ON | OFF
            if (FrontEdit=='0') {
              FrontEdit = '1';
              $(".divEdit").show();
            } else {
              FrontEdit = '0';
              $(".divEdit").hide();
            }
            
            // SAVE COOKIE
            $.cookie("FrontEdit", FrontEdit);
          }
          
        });
      }
  } catch (ex) {
    $.logThis('FrontEdit: ' + ex)
  }






  // ************************************ WINKELWAGEN ************************************
  try {
	    $("#divCart").hover(
	      function(){
          $("#divCart_pullout").css('visibility', 'visible');
          $("#divCart_pullout").fadeIn(50);
          $(".navSub").fadeOut();
        },
		    function(){
			    $('#divCart_pullout').delay(50).fadeOut(100, function(){
			    });
		    }
	    );
  } catch (ex) {
    $.logThis('#divCart: ' + ex)
  }

	
	
	
	
	
	
  // ************************************ LAATST BEKEKEN PRODUCTEN ************************************
  try {
	    if ($('#divLastViewed_NL').length) {
	      $('#divLastViewed_NL').load('UcLastViewed.aspx?r=NL-' + $.cookie("LaatstBekeken"), function() {
		    });
	    }
    	
	    if ($('#divLastViewed_EN').length) {
	      $('#divLastViewed_EN').load('UcLastViewed.aspx?r=EN-' + $.cookie("LaatstBekeken"), function() {
		    });
	    }
  } catch (ex) {
    $.logThis('#divLastViewed_NL: ' + ex)
  }




  // ************************************ LAATSTE OVERZICHT ************************************
  try {
    var LastViewedUrl = $.cookie("LastViewedUrl");
    if (LastViewedUrl != null && LastViewedUrl != '') {
      $('#tdBackLinkNL').html("<a href='" + LastViewedUrl + "'>Terug naar overzicht &raquo;</a>");
      $('#tdBackLinkEN').html("<a href='" + LastViewedUrl + "'>Back to overview &raquo;</a>");
    }
  } catch (ex) {
    $.logThis('#LastViewedUrl: ' + ex)
  }




  // zet in de css de cycleshows op display:none
  // op deze manier:
  //  .cycleshow {display:none;}
  try {
	    if ($('.cycleshow').length) {
	      $('.cycleshow').show();
        $('.cycleshow').cycle({
          fx:     'fade', 
          timeout:  4000,
          delay:		500,
          speed:		1000,
          pause:		false   // pause on mouseover
        });
      }
  } catch (ex) {
    $.logThis('.cylcleshow: ' + ex)
  }
	
	
	
	
	
  // ********** AUTO COMPLETE ************
  try {
    if ($('#autocomplete_search').length) {
    
      function formatItem(row) {
        return row[0].split("#")[1] + row[0].split("#")[2];
      }

      $("#autocomplete_search").autocomplete(ac_list, {
	      minChars: 1,
	      width: 500,
	      max: 30,
	      matchContains: true, 
	      scroll: false,
	      scrollHeight: 400,
        formatItem: formatItem
      });
  	  
      $("#autocomplete_search").result(function () {
        // find name in list
        for(var i in ac_list) {
	        if (ac_list[i].split("#")[1] + ac_list[i].split("#")[2] == this.value) {
	          top.location.href = ac_list[i].split("#")[0];
	          this.value = 'loading...';
	          return;
	        }
        }
        // Het idee: Niet gevonden in de lijst, dan naar de search-pagina.
        // deze redirect wordt niet gestart in deze result-functie.
        // vandaar dat hieronder een keypress-event wordt gezet.
        location.href = 'product_search.aspx?redirect_1_product=1&q=' + escape(this.value);
      });
  		
  		
      //  Dit werkt nog niet goed, omdat bij ENTER zowel de Result-functie, als de keypress-functie wordt gestart.
  		
      $("#autocomplete_search").keypress(function(e){ 
	      if(e.which == 13) {
		      // location.href = 'product_search.aspx?redirect_1_product=1&q=' + escape(this.value);
		      return false;
	      }
      });
      
    }  // if found #autocomplete_product
  } catch (ex) {
    $.logThis('Autocomplete search: ' + ex)
  }

	
	
	
	
});  // End Document-Ready





function InitSlider(price_slider_id, price_amount_id, hid_prijsmin_id, hid_prijsmax_id, prijsmin_param, prijsmax_param) {
    $slider = $(price_slider_id); //Caching slider object
    $amount = $(price_amount_id); //Caching amount object

    // get min prijs van de params
    var req_prijsmin;
    if ($.getURLParam(prijsmin_param) != '') {
      req_prijsmin = $.getURLParam(prijsmin_param);
    }
    if (!req_prijsmin) {req_prijsmin='1'}

    // get max prijs van de params
    var req_prijsmax;
    if ($.getURLParam(prijsmax_param) != '') {
      req_prijsmax = $.getURLParam(prijsmax_param);
    }
    if (!req_prijsmax) {req_prijsmax='795'}

    $slider.slider({
        range: true, // necessary for creating a range slider
        min: 1, // minimum range of slider
        max: 1000, //maximimum range of slider
        values: [(req_prijsmin>0) ? req_prijsmin : 1, (req_prijsmax>0) ? req_prijsmax : 795], // initial range of slider
        slide: function(event, ui) { // This event is triggered on every mouse move during slide.
          $amount.html('&euro; ' + ui.values[0] + ' - &euro; ' + ui.values[1]);//set value of  amount span to current slider values
        },
        stop: function(event, ui){ // This event is triggered when the user stops sliding.
          $(hid_prijsmin_id).val(ui.values[0]);
          $(hid_prijsmax_id).val(ui.values[1]);
        }
    });

    // Set init-values into hidden fields    
    $(hid_prijsmin_id).val(req_prijsmin);
    $(hid_prijsmax_id).val(req_prijsmax);
  
    // Set init-values into amount-field
    $amount.html('&euro; ' + $slider.slider('values',0) + ' - &euro; ' + $slider.slider('values',1));

}



// Init Countdown Counter
function InitCountdown(divId, year, month, day, hours, minutes, seconds) {
  try {
    if ($(divId).length) {
      var countdownDateTime = new Date(year, month-1, day, hours, minutes, seconds);
      
      $(divId).countdown(
        $.extend({
          until: countdownDateTime,
	        format: 'H:M:S',
	        compact: true
	      }, 
        $.countdown.regional[''])
      );
      
    }
  } catch (ex) {$.logThis('InitCountdown: ' + ex)}
}	


