var wait_before_hide;

$(function(){
  
  var url     = String(document.location);
  var movieId = url.substring(url.indexOf("#movie")+1);
  
  $('.content_movies .middle a').each(function(){
    var id       = $(this).attr('id');
    var href     = $(this).attr('href');
    var new_href = href + '&p_share_url=' + escape(url + '#' + id);
    $(this).attr('href',new_href);
  });
  
  $('.share_media a').each(function(){
    var href = $(this).attr('href') + escape(url);
    $(this).attr('href',href);
  });

  $('a.fancy_popup').fancybox({
    'padding' : 0,
    'hideOnContentClick' : false,
    'hideOnOverlayClick' : false,
    'overlayOpacity' : 0.6,
    'centerOnScroll' : true,
    'transitionOut' : 'elastic',
    'transitionIn' : 'elastic',
    'speedIn' : 100,
    'speedOut' : 100,
    'showNavArrows' : false,
    'cyclic' : true
  });
  
  if($('.open_on_page_load').size() > 0) {
    $('a.popup_opleidingen').trigger('click');
  }
  
  if (movieId != ''){
    $('a#'+movieId).trigger('click');
  }
  
  if( $('.img_wrapper div') ) {
    $('.img_wrapper div').each(function () {
      moveText(this);
    });  
  }
  
  $('.img_wrapper a img').hover(function () {
    var current_position = $(this).position();
    
    $(this).css({
      'position' : 'absolute',
      'height' : '124px',
      'width' : '96px',
      'left' : (current_position.left - 12) + 'px',
      'top' : '8px'
    });
    $(this).parent().next().css({
      'position' : 'absolute',
      'top' : '132px',
      'color' : 'white',
      'font-size' : '11px'
    });
    
    moveText($(this).parent().next());
  });
  
  $('.img_wrapper a img').mouseout(function () {
    $(this).css({
      'position' : 'static',
      'height' : '94px',
      'width' : '71px'
    });
    
    $(this).parent().next().css({
      'top' : '126px',
      'color' : '#4d4d4d',
      'font-size' : '8.25px'
    });
    
    moveText($(this).parent().next());
  });
  
  if ($('.frontpage').size() > 0 ) {
    
    $('.sections a').mouseover(function(){
      showMenu(this.id);
    }).mouseout(function(){
      wait_before_hide = setTimeout('hideMenu("'+this.id+'")',400);
    });
    
    $('.center_menu').mouseover(function(){
      clearTimeout(wait_before_hide);
    }).mouseout(function(){
      var object_id = this.id.replace('_menu','');
      wait_before_hide = setTimeout('hideMenu("'+object_id+'")',400);
    });
  
  }
  
});


function showMenu(object_id){
  clearTimeout(wait_before_hide);
  $('.sections a img').css('opacity','0.4');
  $('.center_menu').hide();
  $('.content_center').css('background-image','url(/TXP/deltion/images/white_transparent_80.png)');
  $('a#'+object_id).children('img').css('opacity','1');
  $('ul#'+object_id+'_menu').show();
}


function hideMenu(object_id){
  $('.content_center').css('background-image','none');
  $('a#'+object_id).children('img').css('opacity','0.4');
  $('ul#'+object_id+'_menu').hide();
}
  
  
function moveText(element) {
  var img_width = $(element).prev().children().width();
  var img_pos   = $(element).prev().children().position();
  
  var width = $(element).width();
  
  left = img_pos.left + (img_width - width) / 2 + 'px';        
  $(element).css({'left' : left});
}


function resizeBg() { 
  var img_height = 1094;
  var img_width = 1301;
  
  var h = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  var w = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  var elem = $('#background');
  
  if ( w > h ) {
    var tmp = w / img_width;
    elem.css({'height' : img_width * tmp + 'px'});
    elem.css({'width' : w + 'px'});
    
  } else {
    var tmp = h / img_height;
    elem.css({'width' : img_height * tmp + 'px'});
    elem.css({'height' : h + 'px'});
  }
}
