$(document).ready(function(){
  $('#multi-ddm').dropDownMenu({timer: 200, parentMO: 'multi-ddm-hover', childMO: 'multi-ddm-child-hover'});
  
  $('a.button.reply').click(function(){
    $('#texteditor').focus();
    $('html, body').animate({scrollTop: $('#site').height()}, 1000);
    var FCKeditorInstance = FCKeditorAPI.GetInstance('texteditor') ;
    FCKeditorInstance.Focus();
  });

  if($.browser.version < 7 && $.browser.msie){
    $(document).pngFix();
  } else {
    Custom.init();
  }
  
  $('#content a img.fancybox').each(function(){
      $(this).attr('rel','gallery');
      $(this).parent().fancybox({
        'transitionIn'  : 'none',
        'transitionOut'  : 'none',
        'overlayOpacity' : 0.8,
        'overlayColor' : '#000'
      });
  });
  
  if($('a.youtube-link').length > 0){
    $("a.youtube-link").click(function() {
      $.fancybox({
        'padding'    : 0,
        'autoScale'    : false,
        'transitionIn'  : 'none',
        'transitionOut'  : 'none',
        'title'      : this.title,
        'width'    : 680,
        'height'    : 495,
        'href'      : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
        'type'      : 'swf',
        'overlayOpacity' : 0.8,
        'overlayColor' : '#000',
        'swf'      : {
          'wmode'    : 'transparent',
          'allowfullscreen'  : 'true'
        }
      });


      return false;
    });
  }
  
  $('#content-portal .block .head').click(function(){
    FoldDiv($(this));
  });
  
  /* Country Fix 
  
  if($("#ac_country").length > 0) {
    //Change this to the ID of the country input you want to be autocompleted
  //make sure to update the CSS for this ID as well
  var ac_country = $("#ac_country");
  

  //options are the same as the JQuery Autocomplete plugin
  ac_country.autocomplete(countries, {
    minChars: 2,
    width: 320,
    matchContains: true,
    scroll: true,
    max:0,
    formatItem: function(row, i, max, term) {
      return "<img src='/js/countries/images/flags/" + row.code.toLowerCase() + ".gif'/> " + row.name;
    },
    formatResult: function(row) {
      return row.name;
    },
    formatMatch: function(row, i, max) {
      return row.name;
    }
  });

  ac_country.after(ac_country.clone().attr('value','').attr({'id': ac_country.attr('id') + '_hidden'}));
  
  var ac_country_hidden = $("#ac_country_hidden");
  ac_country_hidden.removeAttr('name', '').clearField();
  ac_country.result(function(event, data, formatted) {
    if(data) {
      ac_country_hidden.val(data.code.toLowerCase());
    }
    var src = '/js/countries/images/flags/' + data.code.toLowerCase() + '.gif';
    ac_country.css('backgroundImage', 'url(' + src + ')');
  });
  }
  */
  
  
});

function FoldDiv(ClickedElement) {
  var DivToAnimate = ClickedElement.parent().find('.text');
  
  if(parseInt(DivToAnimate.innerHeight()) > 1) {
    ClickedElement.css('background','url(/pics/arrow-down.png) top left no-repeat');
    DivToAnimate.animate({ 'height' :'0px' });
  } else {
    ClickedElement.css('background','url(/pics/arrow-right.png) top left no-repeat');
    DivToAnimate.animate({ 'height' : parseInt(DivToAnimate[0].scrollHeight) + 'px' });
  }
}

function GetYouTubeScreen( Url ) {
  if(Url === null){ 
    return ""; 
  }
  
  var VideoId;
  var Results;

  Results = Url.match("[\\?&]v=([^&#]*)");

  VideoId = ( Results === null ) ? Url : Results[1];
  
  return "http://img.youtube.com/vi/"+VideoId+"/0.jpg";
}

function ValidateNewsletter() {
  if (!RequiredTextField(document.getElementById("_firstname"), "Please fill in your firstname")) {
    return false;
  }
  
  if (!RequiredTextField(document.getElementById("_lastname"), "Please fill in your lastname")) {
    return false;
  }
  
  if (!ValidateEmail(document.getElementById("_email"), "Please fill in your E-mail Address")) {
    return false;
  }  

  return true;
}

function ClearTextBox(TextBox, DefaultVal){
  if(TextBox.val() == DefaultVal){
    TextBox.val('');
  }
  return false;
}

function CheckTextBox(TextBox, DefaultVal){
  if(TextBox.val() == ""){
    TextBox.val(DefaultVal);
  }
  return false;
}

function BuildRating(FormId, VoteUrl, bCanVote) {
  FormId.children().not(":radio").hide();
  
  // Create stars
  
  if(bCanVote){
    FormId.stars({
      cancelShow: false,
      oneVoteOnly: true,
      callback: function(ui, type, value)
      {
        var StarLinks = ui.$stars.find("a");
        var AjaxUrl = VoteUrl + value;
        
        StarLinks.slowEach(100, 
          function(){ 
            $(this).animate({top: "28px"}, 300) }, function(){
              $("#loader").fadeIn(function(){
                $.ajax({ url: AjaxUrl, success: function(){
                  $("#loader").fadeOut(function(){ 
                    StarLinks.slowEach(100, function(){ 
                      $(this).animate({top: 0}, 300);
                    });
                  });
                }});
              });
          });
      }
    });
  } else {
    FormId.stars({
      cancelShow: false,
      disabled: true
    });
  }
}

var PreviousTag = 0;

function AddTag(){
  var TagDiv = $('.tags');  
  
  if(PreviousTag == 0){
    var TagCount = -1;
    $('.tag').each(function(){
      TagCount++;
      if($(this).css('display') == 'none'){
        PreviousTag = TagCount;
        return false;
      }
      
    });
  }
  
  if(PreviousTag >= 9){
    alert('Het maximaal aantal tags is bereikt.')
    return false;
  }
  $('.tag',TagDiv).eq(PreviousTag).css('display','block');
  $('.tag',TagDiv).eq(PreviousTag).find('input').focus();
  PreviousTag = PreviousTag + 1;    
}

function DeleteTag(){
  var TagDiv = $('.tags');
  
  if(PreviousTag == 0){
    var TagCount = -1;
    $('.tag').each(function(){
      TagCount++;
      if($(this).css('display') == 'none'){
        PreviousTag = TagCount;
        return false;
      }
      
    });
  }
  
  $('.tag',TagDiv).eq(PreviousTag - 1).css('display','none');
  $('.tag',TagDiv).eq(PreviousTag - 1).find('input').val('');
  $('.tag',TagDiv).eq(PreviousTag - 2).find('input').focus();
  PreviousTag = PreviousTag - 1;    
}

jQuery.fn.slowEach = function(interval, callback, callback2) {
  var items = this, i = 0;
  if(!items.length) return;
  function next() {
    (callback.call(items[i], i, items[i]) !== false && ++i < items.length) ? setTimeout(next, interval) : callback2 && callback2.call(items, i, items);
  }
  next();
};

function CheckImageSizes(){
  var $BackgroundDiv = $('#background');
  var $BackgroundImage = $BackgroundDiv.find('img');
  var isiPad = navigator.userAgent.match(/iPad/i) != null;
  var BrowserHeight = parseInt($(window).height());
  var BrowserWidth = parseInt($(window).width());
  
  var BgImageHeight = parseInt($BackgroundImage.height());  
  var BgImageWidth = parseInt($BackgroundImage.width());

  //console.debug("BgImageHeight: " + BgImageHeight + " BrowserHeight: " + BrowserHeight + " BgImageWidth: " + BgImageWidth + " BrowserWidth" + BrowserWidth );
  if(!isiPad) {
    if(BrowserHeight > BgImageHeight) {  
      $BackgroundImage.css({ height : '100%', width : 'auto' })
    }
    
    if(BgImageWidth < BrowserWidth) {
      $BackgroundImage.css({ height : 'auto', width : '100%' })
    }
    
    if(BrowserHeight > BgImageHeight && BgImageWidth < BrowserWidth) {  
      $BackgroundImage.css({ height : '100%', width : '100%' })
    }
      
    if(bFirst) {
      $BackgroundDiv.css({ 'display':'none', 'visibility':'visible'});
      $BackgroundDiv.fadeIn('slow');
      bFirst = false;
    }
  
  } else {
    if(BgImageWidth < BrowserWidth) {
        $BackgroundImage.css({ height : 'auto', width : '100%' })
    }
    
    if(BrowserHeight > BgImageHeight) {
      $BackgroundImage.css({ height : '100%', width : 'auto' })
    }
  }
}

/*
$(document).ready(function() {
	$(".list-toggle .item").each(function() {
		
		var Item = $(this);
		
	  $(this).find(".category_name").click(function() { 	  	   	
			var CategoryName = $(this);			

			var DetailContentUrl = CategoryName.find("a").attr("href");		
			
			if(Item.find(".txt").html() == "") {	
				Item.find(".txt").load(DetailContentUrl, function() {
					showELearningItem(CategoryName);	
			  });
			} else {
				showELearningItem(CategoryName);
			}
	  });
	});
	
	$(".list-toggle .item .hide").hide();
});
*/


function loadPubmedContent(SearchQuery) {
  $('#pubmed').html("loading items...");  
  $('#pubmed').load('/lastpubmed.php?_query=' + encodeURI(SearchQuery), function() {
	
	  $("a.fancybox_pubmed").fancybox({
		padding: 0,
	  	titleShow: false,
	  	type: 'iframe',
	  	autoDimensions: false,
	  	width: 900,
	  	height: 550
	  });

  });  
    
}
