var CurrentNavigationItem = null;

$(document).ready( function() {		
	if($("a.fancybox").length > 0){
	  $("a.fancybox").fancybox({
		'transitionIn'  :  'none',
		'transitionOut'  :  'none', 
		'overlayShow'  :  true,
		'overlayColor' : '#000',
		'titleShow' : false
	  });
	}
  
	$('.hide').hide();

	$("li.openitem").parent().removeClass('hide').css("display", "block").addClass('show');
	$("#multi-ddm.show").css("display", "block");
	
	/* HOMEPAGE BLOKKEN */
	$('#content-homebloks .blok').hover(function(){
	  $('.text_blok', $(this)).show()
	}, function (){
	  $('.text_blok', $(this)).hide()
	});
	
  if($("#TwitterContainer").length > 0) {
	  $("#TwitterContainer").tweet({ 
		  username: "trianel",
		  avatar_size:20,
		  count: 2,
		  loading_text: "tweets ophalen..."
	  });
  }

  /* cycle sidebar blog */ 
  if(($('#content .sidebar .items').length > 0) || ($('#content .block_col.isnews .items').length > 0)) {
	  $('#content .sidebar .items').cycle({ 
		  timeout: 5000, 
		  cleartype: 1, 
		  speed: 400,
		  pager: '#nav_small',
		  slideExpr: '.sitems_tocycle'   
	  });

	  $('#content .block_col.isnews .items').cycle({ 
		  timeout: 5000, 
		  cleartype: 1, 
		  speed: 400,
		  pager: '#nav_small',
		  slideExpr: '.sitems_tocycle'   
	  });
  }

	/* FADE IN SUBNAV */
	$(".subnav_content").hide().fadeIn(3000); 
	
	/* LANDINGSPAGES */
	$(".block_left").click(function(){ window.location=$(this).find("a").attr("href"); return false; });
	$(".block_nav:not(.blank)").click(function(){ window.location=$(this).find("a").attr("href"); return false; });
	$(".block_col.notnews").click(function(){ window.location=$(this).find("a").attr("href"); return false;});
	$('.block_nav:not(.current), .block_left:not(.current)').hover(
	function() {
		$(this).css({'background' : '#900326'})		
	},
	function() {
		$(this).css({'background' : '#185CA5'})						  									  	
	});

	/* VIDEOPAGINA OVERLAY */ 
	$('.container_video').hover(function(){
		$('.overlay_nametime', $(this)).stop().show().animate({
		  top : '-110px'
		});
	  }, function (){
		$('.overlay_nametime', $(this)).stop().hide().animate({
		  top : '10px'
		});
	  });
  
	/* VIDEOPAGINA OVERLAY - THUMBS */ 
	$('.video-thumb.w110').hover(function(){
		$('.thumb_info', $(this)).stop().animate({
		  bottom : '50px'
		});
	  }, function (){
		$('.thumb_info', $(this)).stop().animate({
		  bottom : '0px'
		});
	  });

	$(".video-thumb.w110").click(function(){
		  window.location=$(this).find("a").attr("href"); return false;
	});


	$(".subnav_content a.subitems").click(function(){
		  window.location=$(this).attr("href"); return false;
	});

	$(".subnav_content li a").click( function() { 
		return ShowSubNav($(this));	
	});
	
	/* VIDEO BLOG 
	$("#blog_lastvideo .video-embed").click(function(){
		  window.location="http://www.trianel.nl/eigen-energiebedrijf/bekijk-video"; return false;
	}); */
});

$(window).resize(function(){
  ResizeHandler();
});


function ShowSubNav(CurrentElement) {		
	if(CurrentElement.next().find('li').length == 0) {
		return true;
	}
	
	  if(CurrentElement.next().css("display") == "none") {
		  	
		  CurrentElement.next().slideDown("slow", function() {
		   var thisPointer = $(this);
		   
		   if(thisPointer.hasClass("subsub")){
			  thisPointer.addClass("active");
		   }
		   if(thisPointer.hasClass("subsub2")){
			  thisPointer.addClass("active");
		   }
		   
		  });
		  
	  } else {
		  if(CurrentElement.next().hasClass("sub")) {
			  CurrentElement.next().slideUp("slow");
		  }

		  if(CurrentElement.next().hasClass("active")) {
			  CurrentElement.next().slideUp("slow", function(){ 
				  $(this).removeClass("active"); 
				  CurrentElement.next().slideUp("slow");
			  });	 
		  }
	  }
	CurrentNavigationItem = CurrentElement;
	return false;
}

function ResizeHandler() { 
	var SiteContainer 				= $("#site");
	var BackgroundContainer 	= $("#bg-container");
	var BackgroundImage				= $("#bg-container img");
	var NavigationContainer		= $("#navigation");
	
	var CurrentWidth 					= 1097;
	var MinWidth							= 979;
	var MinHeight							= 585;
	var WindowWidth 					= $(window).width();
	var WindowHeight 					= $(window).height();
	
	var CurrentMargin 				= parseInt((CurrentWidth / 2) * -1);
	var ResizeMargin					= parseInt(((CurrentWidth - MinWidth)/ 2) * -1);
	var NavigationMargin			= parseInt((MinWidth / 2) * -1);
	
	if(WindowHeight <= MinHeight) { 
		BackgroundContainer.css("height", MinHeight + "px"); 
	} else {
		BackgroundContainer.css("height", "100%"); 
	}

	if(WindowWidth < (CurrentWidth + 23))	{
		if(WindowWidth <= MinWidth) {
			SiteContainer.css("overflow", "hidden");
			
			BackgroundContainer.css("left", "0px");	
			BackgroundContainer.css("margin-left", "0px");
			BackgroundContainer.css("width", MinWidth + "px");	
			
			BackgroundImage.css("margin-left", ResizeMargin + "px");
			
			NavigationContainer.css("left", "0px");	
			NavigationContainer.css("margin-left", "0px");	
		} else {
			SiteContainer.css("overflow", "hidden");

			BackgroundContainer.css("left", "0px");	
			BackgroundContainer.css("margin-left", "0px");	
			BackgroundContainer.css("width", WindowWidth + "px");	
			
			var VariableResizeMargin = parseInt(((CurrentWidth - WindowWidth)/ 2) * -1)
			BackgroundImage.css("margin-left", VariableResizeMargin + "px");
			
			NavigationContainer.css("left", "50%");	
			NavigationContainer.css("margin-left", NavigationMargin + "px");	
		}
	} else {
		SiteContainer.css("overflow", "visible");
		
		BackgroundContainer.css("left", "50%");	
		BackgroundContainer.css("margin-left", CurrentMargin + "px");	
		BackgroundContainer.css("width", CurrentWidth + "px");	
		
		BackgroundImage.css("margin-left", "0px");
		NavigationContainer.css("left", "50%");	
		NavigationContainer.css("margin-left", NavigationMargin + "px");	
	}	
	
	var FadeContainer = $("#fade");
	if(FadeContainer.width() < MinWidth) {
		FadeContainer.css("width", MinWidth + "px");		
	} else {
		FadeContainer.css("width", "100%");
	}

	if(FadeContainer.innerHeight() < MinHeight) {
		FadeContainer.css("height", MinHeight + "px");		
	} else {
		if(BackgroundContainer.height() < SiteContainer.height()) {
			FadeContainer.css("height", SiteContainer.height() + "px");
			BackgroundContainer.css("height", SiteContainer.height() + "px");	
		} else {
			FadeContainer.css("height", BackgroundContainer.height() + "px");
		}
	}

}

