$(document).ready(function(){

  //external window
  $('a[class="external"]').click(function(){
    this.target = "_blank";
  });
  
  //popup window
  $('a[class="popup"]').click(function(){
    window.open(this.href, 'Popup', 'height=580,width=400,scrollTo,resizable=0,scrollbars=0,location=0,left=50,top=50', 'false');
    return false;
  });
  
  //close popup window link
  $('a[class="popup_close"]').click(function(){
    window.close();
    return false;
  });
  
  //print link
  $('a[class="print"]').click(function(){
    window.print();
    return false;
  });
  
  //confirm link
  $('a[class="confirm"]').click(function(){
    return confirm(this.title);
  });
  
  //bookmark link 
  $("a.bookmark").click(function(event){
    event.preventDefault();
    var url = this.href;
    var title = this.title;
    if (window.sidebar) {
      window.sidebar.addPanel(title, url, "");
    }
    else 
      if (window.external) {
        window.external.AddFavorite(url, title);
      }
      else {
        alert('Uw browser ondersteunt helaas niet deze optie.');
      }
  });
  
  //iframe replacement
  $('a[class="iframe"]').each(function(i){
    $(this).replaceWith("<iframe src='" + this.getAttribute("href") + "' frameborder='0' scrolling='no' width='934' height='600'></iframe>");
  });
  
  //image rollover
  $("img.rollover").hover(function(){
    this.src = this.src.replace("_off.", "_on.");
  }, function(){
    this.src = this.src.replace("_on.", "_off.");
  });
  
  //forms - focus
  $("input[type=password], input[type=text], textarea").focus(function(){
    $(this).addClass("focus");
  });
  $("input[type=password], input[type=text], textarea").blur(function(){
    if ($(this).find(".focus")) {
      $(this).removeClass("focus");
    }
  });

  // overLabel
  $("#form_search label").overLabel();

  //call the function for each field with a label
  /*$("#form_search label").each(function(){
    label = $(this).html();
    selector = "#"+$(this).attr("for");
    createValueLabel(selector, label);
    $(this).addClass("offScreen");
  });
 
  function createValueLabel (selector, defaultValue){
    $(selector).data("default", defaultValue);
    $(selector).bind("focus blur", function(){
      value = $(this).val();
      if(value==defaultValue){ $(this)
        .val("")
        .removeClass("value-label");
      }
      if(!value){ $(this)
        .addClass("value-label")
        .val(defaultValue);
      }
    });
    $(selector).val(defaultValue);
  }*/

  //forms - hover
  $("input.submit").hover(function(){
    $(this).addClass("hover");
  }, function(){
    $(this).removeClass("hover");
  });
  
  //forms - select all
  //$("input[@name=select_deselect]").click(function(){
  //  var checked_status = this.checked;
  //  $("input[@class=check_me]").each(function(){
  //    this.checked = checked_status;
  //  });
  //});
  
  //forms - multi select transfer
  $('a#add').click(function(){
    return !$('select#select1 option:selected').remove().appendTo('#select2');
  });
  $('a#remove').click(function(){
    return !$('select#select2 option:selected').remove().appendTo('#select1');
  });
  
  //forms - toggle visibility information
  $('p.more_info').hide();
  $('.toggle_info input[type=radio]').click(function(){
    ($(this).val() == 'Yes') ? $('p.more_info').show() : $('p.more_info').hide();
  });
  
  //forms - auto submit
  $(".auto_submit").change(function(){
    $(this.form).submit();
  });
  
  //forms - no javascript button
  $('input.no_javascript').hide();

  //text - resizer
  /*if($.cookie('TEXT_SIZE')) {
    $('body').addClass($.cookie('TEXT_SIZE'));
  }
  $('ul.options li a').click(function() {
    var textSize = $(this).attr('class');
    $('body').removeClass('normal large').addClass(textSize);
    $.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
    return false;
  });*/
  
  // add JavaScript links
  var fsCookie = $.cookie('TEXT_SIZE');
  if (!fsCookie) {
    // check whether cookies are supported
    $.cookie('TEXT_SIZE', 'normal', { path: '/', expires: 10000 });
    fsCookie = $.cookie('TEXT_SIZE');
  }
  if (fsCookie) { // continue if cookies are supported
    $('body').addClass(fsCookie);
    
    // options for inserting links
    var fsLinkOptions = {
      wrapper: "<li class='fontsize'>[@]</li>",
      insert: "prepend"
    };
    
    // callback function for links
    var fsCallback = function(e) {
    e.preventDefault();
    var textSize = $(this).find("img").attr('class');
    $('body').removeClass('normal large').addClass(textSize);
    $.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
    };
    
    // insert links
    $("#header ul.options")
    .addJsLink('<img src="/images/btn_font2.gif" width="12" height="14" alt="Grote letters" title="Vergroot het lettertype" class="large" />', fsLinkOptions, fsCallback)
    .addJsLink('<img src="/images/btn_font1.gif" width="8" height="14" alt="Standaard lettergrootte" title="Verklein het lettertype" class="normal" />', fsLinkOptions, fsCallback);
  }
 
  //FAQ - toggle
  $("dd.content").hide();
  $("dt.toggle a").click(function(){
    $(this).parent().next("dd").toggle();
    return false;
  });
  
  /*dropdown menu
                   - Hover IE6
                   - Plaatsen over volle breedte ul.mainMenu
                     maar wel de linkjes recht onder de gehoverde li
  */
  
$('div#header ul.mainMenu li').hover(function(){
    $this = $(this);
	$subMenu = $('div#subMenu');
	
	$this.addClass('hover');

	// Posities bepalen: waar staat de gehoverde li?
	var posLeft = $this.position();
	// Hoe breed is het submenu?
	var submenuWidth = parseInt($subMenu.width());
	// Inclusief padding natuurlijk
	var submenuWidth = submenuWidth + parseInt($subMenu.css("padding-right")) + parseInt($subMenu.css("padding-left"));
	// afstand van gehoverde li vanaf rechterkant submenu
	var menuLeft = posLeft.left - submenuWidth;
	// Hoe breed is het hoofdmenu?
	var mainMenuWidth = parseInt($('ul.mainMenu').width());
	
	$this.children('ul').css('left', -(menuLeft));
	if(menuLeft == 0){
		$this.children('ul').css('padding-left', menuLeft); // De uitklapper van de eerste li heeft geen border-left
	}else{
		$this.children('ul').css('padding-left', menuLeft-1); // De uitklapper heeft een border-left van 1px
	}
	$this.children('ul').css('width', (mainMenuWidth - menuLeft));

}, function(){
    $(this).removeClass('hover');
  });


});
