$(document).ready( function() {

$('object, embed, iframe').css({"width":"605px","height":"360px"}).attr({"width":"605px","height":"360px"});
$('.rennerimage a').css({"margin":"0px", "padding":"0px"   });

$('#languages ul').append($('.selectedland').parent());

$('a.subsub').click(function(e) {
	e.preventDefault();
});

//////////    LANGUAGES MENU OPENER    /////////////	
	$('#languageopener').click(function() {
		if($('#languageopener').hasClass('open'))
		{
			removelanguages();
		}
		else
		{
			$('#languages').animate({"top":"+=310" }, { queue:false, duration:1250, easing:"bounceEaseOut"});
			$('#languageopener').css({"background-position": "0 -1px"});
			$('#languageopener').addClass('open');
		}
	});
	$('#languages').mouseleave(function(){
		if($('#languageopener').hasClass("open"))
		{
			ltm = setTimeout('removelanguages()', 2500);
		}
	});
	$('#languages').mouseenter(function() {
		clearTimeout(ltm);
	});
/***************    LANGUAGES MENU OPENER END   ******************/
	
//////////    SUBMENU OPENER    /////////////	
	var tm;
	$('#menu a').click(
		function(){
			$('#submenu').mouseleave(
				function(){
					// Alleen verbergen indien submenu al zichtbaar is
					if($('#submenu').css('top')=='0px')
					{
						tm = setTimeout(function() {
							$('#submenu').animate({'top': '-30', 'opacity':'0'}, { queue:false, duration:450, easing:'backEaseOut', complete: function() { $(this).css({"display":"none","height":"30px"}) } });
							$('.subsubmenu').css({"display":"none"}); 	//Verberg alle subsubmenu's
						}, 250);
					 }
				}
			)
			$('#submenu').css({"height":"30px","display":"block"});
			if($('#submenu').css("top")=="-30px" || $('#submenu').css("top")=="0px")
			{
				clearTimeout(tm);
				var elem = '#submenu .'+$(this).attr('class');
				if($('#submenu').css("top")!="0px")
				{
					$('#submenu').css("top", "-30px");
					$('#submenu').animate({"top": "0"}, { queue:false, duration:250, easing:"backEaseOut"});
					$('#submenu').css("display", "block");
					$('#submenu').animate({"opacity": "1"}, { queue:false, duration:650, easing:"backEaseOut"});
				}					
				$('#submenu div').css({"display":"none"});
				$(elem).animate({"opacity": "1"}, { queue:false, duration:650, easing:"backEaseOut"});
				$(elem).css({"display":"block"});
			}
		}
	);
	$('#submenu').mouseenter(function() {clearTimeout(tm)} );
/***************    SUBMENU OPENER END   ******************/


//////////    SUBSUBMENU RENNERS    /////////////	
	var elem = ' .subsubmenu .Renners, .subsubmenu .Cyclists, .subsubmenu .Versenyzok, .subsubmenu .Fahrer, .subsubmenu .Kolarze, .subsubmenu .Ciclisti, .subsubmenu .Rytterprofiler, .subsubmenu .Cyclistes';
	var elem1= '.subsubmenu .Teamstaf, .subsubmenu .Teamstaff';
	var bothElem = '.subsubmenu .Renners, .subsubmenu .Teamstaf, .subsubmenu .Cyclists, .subsubmenu .Versenyzok, .subsubmenu .Fahrer, .subsubmenu .Kolarze, .subsubmenu .Ciclisti, .subsubmenu .Rytterprofiler, .subsubmenu .Cyclistes';
	var rtm;

	//entering other submenus will make the big subsubmenu disappear
	$('.subsub, #submenu a span').mouseenter(function() {
		if($(elem).css('height')!='0px' || $(elem1).css('height')!='0px')
		{
			$('#submenu').css({"height":"30px"});
			$('.subsubmenu .Renners, .subsubmenu .Teamstaf').css({"height":"0px","display":"none"});
		}
	});
	//leaving the big subsubmenu will make it disappear
	$(elem1).mouseleave( function() {
		if($(elem1).css('height')!='0px')
		{
			$(bothElem).animate({"height":"0px"}, { queue:false, duration:850, easing:"cubicEaseOut", complete: function () {$(this).css({'display':'none',"z-index":"1"}) }});
		}
	});
	//leaving the big subsubmenu will make it disappear	
	$(elem).mouseleave( function() {
		if($(elem).css('height')!='0px')
		{
			$(bothElem).animate({"height":"0px"}, { queue:false, duration:850, easing:"cubicEaseOut", complete: function () {$(this).css({'display':'none',"z-index":"1"}) }});
		}
	});
	
	//chosing Renners or Teamstaf will make the big subsubmenu appear
	var renners = '.subsub .Renners, .subsub .Cyclists, .subsub .Versenyzok, .subsub .Fahrer, .subsub .Kolarze, .subsub .Ciclisti, .subsub .Rytterprofiler, .subsub .Cyclistes'
	$(renners).mouseenter(function() {
		$('.subsubmenu').css({"display":"block", "height":"0px"});
		$(elem1).css({"z-index":"1"});
		$(elem).css({"z-index":"100", "display":"block","float":"left","position":"absolute", "top":"30px", "height":"0px","opacity":"1", "width":"100%", "background-color":"#039"});
		$('.rennerimage').css({"display":"block","opacity":"1"});
		$('#submenu').animate({"height":"480px"}, { queue:false, duration:750, easing:"cubicEaseOut"});
		$(elem).animate({"height":"480px"}, { queue:false, duration:850, easing:"cubicEaseOut"});
	});
	var staf = '.subsub .Teamstaf, .subsub .Teamstaff';
	$(staf).mouseenter(function() {
		$('.subsubmenu').css({"display":"block","height":"0px"}); 
		$(elem).css({"z-index":"1"});
		$(elem1).css({"z-index":"100", "display":"block","position":"absolute", "top":"30px", "height":"0px","opacity":"1", "width":"100%", "background-color":"#039"});
		$('.Teamstaf div').css({"display":"block", "opacity":"1"});
		$(elem1).animate({"height":"390px"}, { queue:false, duration:850, easing:"cubicEaseOut"});
		$('#submenu').animate({"height":"420px"}, { queue:false, duration:850, easing:"cubicEaseOut"});
	});


/***************    SUBSUBMENU RENNERS END   ******************/

//////////    SPONSOR BANNER   /////////////	
	
	$('.sponsoren .sponsor:first-child img').css({'height':'185px','width':'270px'});
	$('.sponsoren .sponsor:first-child').css({'height':'218px'});
	
/*********    SPONSOR BANNER END   ********/


//////////    TWITTER       ///////////
    $(function(){
      $("#twitter").tweet({
        avatar_size: 48,
        count: 3,
        username: ["vacansoleildcm","krisboeckmans", "stefandenifl", "zeeuwseleeuw", "kennyvanhummel", "GustavVeronica", "pimligthart", "BertjanLindeman", "Tmarczynski", "woutermolll", "M__Mortensen", "woutpoels", "robruijgh", "mirkoselvaggi", "lieuwewestra", "MarcatoMarco", "KeizerMartijn", "CornelisseM"],
        loading_text: "searching...",
        refresh_interval: 90
      });
    });

/*********    TWITTER END   **********/


//////////    KALENDER       ///////////
	$('#jaarkalender span').css({"display":"none", "opacity":"0"});
	var currentmonth = $('#currentmonth').html();
	$('#jaarkalender .maand'+currentmonth).css({"display":"block", "opacity":"1"});
	
	currentmonth = ".kalender ul li:nth-child("+currentmonth+") a";
	left = $(currentmonth).offset().left + ($(currentmonth).width()/2)-  $('.kalender').offset().left;
	$('.kalender img').css({"left":left});
	
	$('.kalender li a').click( function() {
		$('.wedstrijdinfo').remove();
		var left = ($(this).offset().left + ($(this).width()/2)  ) -  $('.kalender').offset().left;
		$('.kalender img').animate({"left":left}, { queue:false, duration:750, easing:"backEaseOut" });
		
		$('#jaarkalender span').css({"display":"none", "opacity":"0"});
		maand = '#jaarkalender .maand'+($('.kalender li a').index(this )+1);
		$(maand).css({"display":"block"});
		$(maand).animate({"opacity":"1"}, { queue:false, duration:1000, easing:"backEaseOut" });
	
	
	});
	
	
	var thumbcount =1;
	if($('.wedstrijdgallery .fotothumbs').css('width') == '840px')
	{
		var maxcount=5;
	}
	else
	{
		var maxcount=3;
	}
	var maxthumbcount = $('.wedstrijdgallery .thumbscontainer li').length-maxcount;
	$('.wedstrijdgallery .rightarrow').click(function() { 
		thumbcount++;
		if(thumbcount>maxthumbcount)
		{
			thumbcount = maxthumbcount;
		}
		else
		{
			$('.wedstrijdgallery .thumbscontainer').animate({"left":"-=125"}, { queue:true, duration:350, easing:"backEaseOut" });
		}
	});
	
	$('.wedstrijdgallery .leftarrow').click(function() {
		thumbcount--;
		if(thumbcount<1)
		{
			thumbcount=1;
		}
		else
		{
			$('.wedstrijdgallery .thumbscontainer').animate({"left":"+=125"}, { queue:true, duration:350, easing:"backEaseOut" });
		}
	});
	
	$('.thumbscontainer img').click(function () {
		$('.wedstrijdgallery #bigfoto').attr('src', $(this).attr('url'));
		$('.wedstrijdgallery .currentfoto').html($('.thumbscontainer img').index(this)+1);
		$('.thumbscontainer img').css({"border":"1px solid #002f66"});
		$(this).css({"border":"1px solid #f00"});
	});
	
/*********    KALENDER END   **********/


/************  TEAMSTAFMENU   *************/
	$.post("/paginas/teamstafmenu.php", function(data) {
		//haal data op van alle teamstaf leden
		$('div.Teamstaf').html(data);
		$('.Teamstaf a').css({		"border":"1px solid #003066",
									"display":"block",
									"position":"relative",
									"width":"80px",
									"height":"100px",
									"padding":"0",
									"margin":"0 5px 5px 0",
									"font-weight":"200",
									"font-size":"9px",
									"background-color":"#003066"
								});
	
		$('.Teamstaf a').click(function (event) {
			event.preventDefault();
			var stafid = $(this).attr('stafid');
			// haal data op van een bepaald staflid
			$.post("/paginas/teamstafmenu.php?stafid="+stafid, function(data) {
		 		$('div.Teamstaf').prepend(data);
				$('.Teamstaf table').css({'display':'none'});
				$('#staflidinfo').animate({"left":"0"}, { queue:false, duration:750, easing:"backEaseOut" });
				$('#staflidinfo').click(function () {$('.Teamstaf table').css({'display':'block'});$('#staflidinfo').animate({"left":"+=960"}, { queue:false, duration:500, easing:"backEaseOut", complete: function () {$(this).remove();} }); });
				
			});

		});
	
	});
/*********    TEAMSTAFMENU END   **********/


/*********      VIDEOLIJST     **********/
$('.videolijst').click(function () {
	var titel = $(this).attr('title');
	var object = $(this).find('.videourl').html();
	$('#mainvideo #titel').html(titel);
	$('#mainvideo object, #mainvideo embed, #mainvideo iframe').remove();
	$('#mainvideo').append(object);
	$('.videolijst').css({"border":"1px solid #fff"});
	$(this).css({"border":"1px solid #f00"});
})

/*********    VIDEOLIJST END   **********/

////////END///////
});


//// Helper functions
var ltm;
function removelanguages()
{
	clearTimeout(ltm);
	$('#languages').animate({"top":"-=310" }, { queue:false, duration:750, easing:"backEaseOut"});
	$('#languageopener').removeClass('open');
	$('#languageopener').css({"background-position": "14px -1px"});
}

function changeNewsflash() 
{
	newsflashpos++;
	if(newsflashpos>3)
	{
		newsflashpos=1;
		$('#newsflash1ul').css({"top":"-10px"});
		$('#newsflash2ul').css({"top":"-3px"});
	}
	$('#newsflash1ul').animate({"top":"-=86"}, { queue:false, duration:450, easing:"backEaseOut"});
	$('#newsflash2ul').animate({"top":"-=86"}, { queue:false, duration:450, easing:"backEaseOut"});
}
