$(document).ready(function()
{
	// alert($(window).width() + " " + $(window).height());
	var id = document.getElementById('#content_filler');	
	
	var topHeight = $('#menu_wrapper').height() + $('#topbar').height();
	var newHeight = $(window).height() - topHeight;
	
	var newWidth = $(window).width();
	$('#content_filler').height(newHeight);
	
	// Set backround
	var section = $('#container').attr('class');
	
	$('#bg').append('<div id="bgoverlay"><img src="/img/bg/'+Math.ceil(newWidth/2)+'x'+Math.ceil(newHeight/2)+'/'+section+'.jpg" width="'+newWidth+'" height="'+newHeight+'" alt="" /></div>');
 	$('#bgoverlay').css('height', newHeight);
	
	// direct links
	externalLink();
});

// On risizing browser window
	$(window).resize(function()
	{
		var newHeight = $(window).height() - 52;
		var newWidth = $(window).width();
		$('#content_filler').height(newHeight);
		
		// Set backround
		var section = $('#container').attr('class');
		
		$('#bg').replaceWith('<div id="bg"><div id="bgoverlay"><img src="/img/bg/'+Math.ceil(newWidth/2)+'x'+Math.ceil(newHeight/2)+'/'+section+'.jpg" width="'+newWidth+'" height="'+newHeight+'" alt="" /></div></div>');
	 	$('#bgoverlay').css('height', newHeight);
	});

function externalLink()
{
	$('a[href^="http://"]').click(function(event)
 	{
 		var link = $(this).attr("href");
		lnk = link.replace('http://', '')
		
		if(lnk.substr(0,12) != 'primitivi.nl' && lnk.substr(0,17) != 'blog.primitivi.nl')
		{
			link = link.replace('http://', '');
			$(this).attr('href', '/g/' + link);
		}
	});
}

function removeLeftIcon()
{
	var leftIcons = $('#icons_left > a');
	
	// only remove if there are more than two icons
	if(leftIcons.length > 2)
	{
		var toDelete = $(leftIcons[2]).attr('id');
		$('#' + toDelete).remove();
	}
}

function removeFirstLeft()
{
	var leftIcons = $('#icons_left > a');
	var toDelete = $(leftIcons[0]).attr('id');
	$('#' + toDelete).remove();
}

function removeSecondLeft()
{
	var leftIcons = $('#icons_left > a');
	var toDelete = $(leftIcons[1]).attr('id');
	$('#' + toDelete).remove();
}

function removeRightIcon()
{
	var rightIcons = $('#icons_right > a');
	
	// only remove if there are more than two icons
	if(rightIcons.length > 2)
	{
		var toDelete = $(rightIcons[2]).attr('id');
		$('#' + toDelete).remove();
	}
}

function removeFirstRight()
{
	var rightIcons = $('#icons_right > a');
	var toDelete = $(rightIcons[0]).attr('id');
	$('#' + toDelete).remove();
}

function resetBlock()
{
	// reset everything to how it used to be
	$('#mainblocklarge .timeline_item_text').remove();
	$('#animate_content').attr('id', 'animate_content_old');
	$('#mainblocklarge .timeline_item_text_new').attr('class', 'timeline_item_text');
	externalLink();
}

// new event functions
function addLeftArrowEvent(current_section, current_item_pd, current_timeline_id)
{
	// alert(current_section + ' - ' + current_item_pd + ' - ' + current_timeline_id);
	var link = $('#' + current_item_pd).attr('href');
	var $icons = $('#icons_left > a');
	
	$('.arrow_left').unbind('click');
	$('.arrow_left').attr('href', link);
	$('.arrow_left').click( function()
	{
		// filter id of new main_id
		var $icons = $('#icons_left > a');
		if($icons.size() == 2)
		{
			var link = $($icons[1]).attr('href')
		}
		else
		{
			var link = $($icons[0]).attr('href')
		}
		
		// strip http:// for IE
		var host = 'http://' + window.location.host;
		if(link.slice(0,host.length) == host)
		{
			link = link.replace('http://' + window.location.host, '');
		}
		
		link = link.split('/');
		var mainId = link[2].split('-');
		
		$.post('/timeline_items/previous/', {section: current_section, item_pd: current_item_pd, timeline_id: current_timeline_id, main_id: mainId[0]},
		
		function(publish_date)
		{	
			$('#icons_left').prepend(publish_date);
			var $newIcon = $(publish_date).attr('id');
			
			if($newIcon != '')
			{
				$('#' + $newIcon).animate({
	                marginLeft : '+=' + 76
	        	}, 'normal', 'linear', function () { removeLeftIcon(); });
			}
			else
			{
				// last two icons so check if there's one or two icons left
				if($icons.size() == 2)
				{
					var $animateIcon = $($icons[0]).attr('id');
					$('#' + $animateIcon).animate({
		                marginLeft : '+=' + 76
		        	}, 'normal', 'linear', function () { $(this).removeAttr('style'); $(this).removeClass('icon'); $(this).addClass('iconright'); removeSecondLeft(); });
				}
				else
				{
					var $animateIcon = $($icons[0]).attr('id');
					$('#' + $animateIcon).animate({
		                marginRight : '-=' + 80
		        	}, 'normal', 'linear', function () { removeFirstLeft(); $('.arrow_left').fadeOut('def'); });
				}
			}
			
		});
		
		return false;
	});
	
	// fade arrow in if it's hidden
	if($('.arrow_left').css('display') != 'block')
	{
		$('.arrow_left').fadeIn('def');
	}
}

function addRightArrowEvent(current_section, current_item_pd, current_timeline_id)
{
	var link = $('#' + current_item_pd).attr('href');
	var $icons = $('#icons_right > a');
	
	$('.arrow_right').unbind('click');
	$('.arrow_right').attr('href', link);
	$('.arrow_right').click( function()
	{
		// filter id of new main_id
		var $icons = $('#icons_right > a');
		var link = $($icons[0]).attr('href')
		
		// strip http:// for IE
		var host = 'http://' + window.location.host;
		if(link.slice(0,host.length) == host)
		{
			link = link.replace('http://' + window.location.host, '');
		}
		
		link = link.split('/');
		var mainId = link[2].split('-');
		
		$.post('/timeline_items/next/', {section: current_section, item_pd: current_item_pd, timeline_id: current_timeline_id, main_id: mainId[0]},
		
		function(publish_date)
		{
			$('#icons_right').append(publish_date);
			var move = $($icons[0]).attr('id', 'old_item_id');
			
			
			if($icons.size() == 2)
			{
				$('#old_item_id').animate({
	                marginLeft : '-=' + 76
	        	}, 'normal', 'linear', function() { removeFirstRight(); });
			}
			else
			{
				$('#old_item_id').animate({
	                marginLeft : '-=' + 76
	        	}, 'normal', 'linear', function() { removeFirstRight(); $('.arrow_right').fadeOut('def'); });
			}
		});
		
		return false;
	});
	
	// fade arrow in if it's hidden
	if($('.arrow_right').css('display') != 'block')
	{
		$('.arrow_right').fadeIn('def');
	}
}