var ytplayer;
var counted = false;

$(document).ready(function(){

	var regionName = $('#regionName');
	if (regionName.length) {
		$('area').hover(function(e) {
			e.preventDefault();
			regionName.text($(this).attr('alt'));
		}, function(e) {
			regionName.text('');
		})
	}
  var monCompte = $('#monCompte');
  var monCompteIn = $('#monCompte > div');
  $('h2, h2 a, #hide', monCompte).click(function(e) {
	e.preventDefault();
	e.stopPropagation();
	monCompte.toggleClass('monCompteActive');
  });

  $('.archiveYearLink').click(function(e) {
	 e.preventDefault();
	 $('.archiveYearActive').toggleClass('archiveYearActive');
	 $(this).closest('div').toggleClass('archiveYearActive');
  });

  $('.showReps').click(function(e) {
	 e.preventDefault();
	 $(this).closest('p.comRepsText').next('div.comReps').slideToggle();
  });
  
  $('.communityLog').click(function(e) {
	e.preventDefault();
	monCompte.addClass('monCompteActive');
	window.scrollTo(0, 0);
	$('#login_email', monCompte).focus();
  });

  $('embed').attr('wmode', 'transparent').after('<param name="wmode" value="transparent" />');

  $("#header_message > marquee")
	.marquee()
	.mouseover(function() {
	  $(this).trigger('stop');
	})
	.mouseout(function() {
	  $(this).trigger('start');
	});
  
  $('.menu_elem > a').each(function() {
    $(this).mouseover(function() {
      smenus_hideAll();
      $($($(this).parent().get(0)).parent().get(0)).children('.smenu').css('display', 'block');
    });
  });
  
  $('.smenu').each(function() {
    $(this).mouseleave(function() {
      smenus_hideAll();
    });
  });
  
  $('#newsletter_desabo_link').click(function() {
    var div = $('#newsletter_desabo');
    if(div.css('display') == 'block') {
      $('#newsletter_desabo').hide();
    } else {
      $('#newsletter_desabo').show();
    }
  });
  
  $(".agenda .jour a").tooltip({
      bodyHandler: function() {
        return $($(this).parent().get(0)).children('#event_'+$(this).attr('rel')).html();
      },
      showURL: false
    });
    $('#newsletter_info_link a').tooltip({
      bodyHandler: function() {
        return $($(this).attr("href")).html(); 
      },
      showURL: false
    });
    $('#newsletter_info_link2 a').tooltip({
      bodyHandler: function() {
        return $($(this).attr("href")).html(); 
      },
      showURL: false
    });

});

  $('#ak_modal_iframe').load(function() {
    modalIframeResizer();
  });

function smenus_hideAll() {
  $('.smenu').each(function() {
    $(this).css('display', 'none');
  });
}

function modalIframeResizer() {
	var myiframe = document.getElementById('ak_modal_iframe');
  if(!myiframe) return;
  
  // not IE
  var h;
  if(myiframe.contentDocument) {
    h = parseInt(myiframe.contentDocument.body.scrollHeight);
  } else { // IE
	  if (document.frames["ak_modal_iframe"] && document.frames["ak_modal_iframe"].document)
		h = parseInt(document.frames["ak_modal_iframe"].document.body.scrollHeight);
	  else {
		  setTimeout('modalIframeResizer()', 100);
		  return false;
	  }
  }
  h+= 10;
  myiframe.style.height = h+'px';
}

function onYouTubePlayerReady(playerId) {
  ytplayer = $('embed').get(0);
  ytplayer.addEventListener("onStateChange", "playerStateChange");
}
function playerStateChange(state) {
	if (!counted && state == 1 && ytplayer) {
		counted = true;
		var id = $(ytplayer).closest('span[rel=video]').attr('rev');
		$.ajax({url: '/countVideoPlay/'+id});
	}
}