$(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)
  }


	$("a.zoom").fancybox({
		overlayShow					: true,
		hideOnContentClick	: false,
		zoomSpeedIn					: 10,
		zoomSpeedOut				: 10,
		zoomSpeedChange			: 10,
		titlePosition	: 'inside'
	});

	$("a.youtube").fancybox({
		frameWidth					: 640,
		frameHeight					: 360,
		overlayShow					: true,
		hideOnContentClick	: false,
		zoomSpeedIn					: 0,
		zoomSpeedOut				: 0,
		zoomSpeedChange			: 0,
		titlePosition	: 'inside'
	});

	$("a.zoomdyn").fancybox({
		frameWidth					: 640,
		frameHeight					: 560,
		overlayShow					: true,
		hideOnContentClick	: false,
		zoomSpeedIn					: 0,
		zoomSpeedOut				: 0,
		zoomSpeedChange			: 0,
		titlePosition	: 'inside'
	});
	

	$("a.image_popup").fancybox({
		overlayShow		: true,
		zoomSpeedIn		: 10,
		zoomSpeedOut	: 10,
		zoomSpeedChange	: 10,
		titlePosition	: 'inside'
	});


  $('a.zoomdyn').each(function(){ 
		try {
			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({'frameWidth':fWidth,'frameHeight':fHeight,'width':fWidth,'height':fHeight});
		} catch(e) {}
  }); 


	if ($('#popup_auto').length) {
		$('#popup_auto').fancybox().trigger('click');
	}


	if ($('#divTabs').length) {
	  	$("#divTabs").tabs();
	}


	if ($('#tabs').length) {
		$("#divTabs").tabs();
	}





  // ************************************ CMS EDIT ************************************
  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);
      }
      
    });
  }







//  // ********** EXTERNAL LINKS ************
//  $("a[target='_blank'").not("[href*='beheer/']").each(function() {
//      $(this).addClass('external_link');
//  });



	
	
  // ********** AUTO COMPLETE ************
  try {
    if ($('#autocomplete_search').length) {
    
      function formatItem(row) {
        return row[0].split("#")[1] + row[0].split("#")[2];
      }

      $("#autocomplete_search").autocomplete(ac_list, {
	      autoFill: false, 
        formatItem: formatItem,
	      matchContains: true, 
	      matchSubset: false, 
	      max: 30,
	      minChars: 1,
      	selectFirst: false,
	      scroll: false,
	      scrollHeight: 400,
	      width: 500
      });
  	  
      $("#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.
        if (this.value != '') {
          location.href = 'search.aspx?q=' + escape(this.value);
        } else {
		      return false;
        }
      });
  		
  		
      //  Dit werkt nog niet goed, omdat bij ENTER zowel de Result-functie, als de keypress-functie wordt gestart.
      //  Dit werkt nu wel goed, omdat
      $("#autocomplete_search").keypress(function(e){ 
	      if(e.which == 13) {
	        e.which = -1;
		      location.href = 'search.aspx?q=' + escape(this.value);
		      return false;
	      }
      });
      
    }  // if found #autocomplete_product
  } catch (ex) {
    $.logThis('Autocomplete search: ' + ex)
  }


	


});

