$(document).ready(function() 
{	
	//Extra backup om menu te sluiten als er over ander menu gehoverd wordt.
	$('.menu > li .close-backstage').mouseover( function() {
		$('ul.presspack').css('visibility', 'hidden');	
	});
	
	$('.menu > li .close-contact').mouseover( function() {
		$('ul.presspack').css('visibility', 'hidden');	
	});

	// Scrollbar toevoegen
	$('.scrolling').jScrollPane();

	//Filter effects
	filter_menu();
	
	$(".filterjaar").click( function()
	{	
		//Jaar, Land, Show all
		filterPress( this.name, 0, false);
		
		//Jaar, Land, Show all en Publication
		getSelectedItem( this.name, 0, false );
		closeMenu();
		return false;
	});

	$(".filterland").click( function()
	{	
		//Jaar, Land, Show all
		filterPress( 0, this.name, false ); 
		
		//Jaar, Land, Show all
		getSelectedItem( 0, this.name, false  );
		closeMenu();
		return false;
	});
	
	$(".showAll").click( function()
	{	
		//Jaar, Land, Show all 
		filterPress( 0, 0, true ); 
		
		//Jaar, Land, Show all
		getSelectedItem( 0, 0, true );
		closeMenu();
	});
	
	$(".filterPublication").click( function()
	{	
		getSelectedItem( 0, 0, false);
		closeMenu();
	});
	
	//Table effects
	dynamic_table();
	
	//Eerste item tonen
	triggerRightBook();
	
	//Global var gebruikt om presspack id in op te slaan
	$.g_iActive = 0;
});

function dynamic_table()
{	
	//Huidige selectie highlighten
	$('.rowHover').click(
		
		function() 
		{	
			var clicked = $(this);				
			$.g_iActive = $(this).attr('id');
			
			$('.rowHover').removeClass('invertColor');
			
			$(clicked).addClass('invertColor');
			
			// Scrollbar toevoegen
			$('.scrolling').jScrollPane();
		}, 

		function() 
		{	
			var clicked = $(this); 
			$(clicked).removeClass('invertColor');
		}
	);	
	
	//Hoveren op tr
	$('.rowHover').mouseover(
	
		function()
		{	
			var clicked = $(this); // Huidige element
			$(clicked).addClass('invertColor');
		}
	);
	
	//Hoveren op tr
	$('.rowHover').mouseout(
		
		function()
		{	
			var pack = $(this).attr('id');
			var clicked = $(this); // Huidige element
			
			if( $.g_iActive != pack )
			{	
				$(clicked).removeClass('invertColor');
			}
		}
	);
	
	//Functie voor content swap
	imageSwap();
	
	//Functie voor fancybox flipbook aanroep
	fancyboxTriggerFlip();
	
	//Functie voor fancybox photo aanroep
	fancyboxTriggerPhoto();
}

//Filter evenementen
function filterPress( p_iYear, p_iCountry, p_bShowAll )
{
	$.ajax({
		url: "templates/pages/presspack/ajax/filter.php?jaar="+p_iYear+"&land="+p_iCountry+"&show="+p_bShowAll,
		cache: true,
		dataType: 'json', 
		success: function( result )
		{	
			//String opbouwen
			var l_sHTML = '<tr id="tableHead"><td class="eventDate">DATE</td><td class="td-country">COUNTRY</td><td>PUBLICATION</td></tr>';
				l_sHTML += '<tr class="tableHighSpacer"><td colspan="3">&nbsp;</td></tr>';
			
			if(result.length > 0 )
			{
				$.each(result, function(key, value) 
				{ 	
					//Flipbook
					if(value.old_photos.length == 0)
					{	
						l_sHTML += '<tr class="rowHover flipbookRow" id="'+value.id+'" name="'+value.photo+'">';
							l_sHTML += '<td class="eventDate">'+value.date;
							l_sHTML += '<a href="uploads/presspack/flipbook/fb-'+value.id+'/index.html" class="pp_item'+value.id+'"></a></td>';
							l_sHTML += '<td class="td-country">'+value.country+'</td>';
							l_sHTML += '<td class="td-publication">'+value.publication+'</td>';
						l_sHTML += '</tr>';
					}//Fancybox
					else if(value.old_photos.length > 0)
					{	
						l_sHTML += '<tr class="rowHover fancyRow" id="'+value.id+'">';
							l_sHTML += '<td class="eventDate">'+value.date;
								
							$.each(value.old_photos , function(key2, item)
							{
								l_sHTML += '<a href="uploads/presspack/photos/'+item.photo+'" name="'+item.photo+'" class="oldPhoto'+value.id+'" rel="press'+value.id+'"></a>';
							});
								
							l_sHTML += '</td>';
							l_sHTML += '<td class="td-country">'+value.country+'</td>';	
							l_sHTML += '<td class="td-publication">'+value.publication+'</td>';
						l_sHTML += '</tr>';
					}
				});
			}
			else
			{
				l_sHTML += '<tr class="tableSpacer">';
					l_sHTML += '<td colspan="3"><span style="margin-left: 10px;">Currently there are no presspacks in this selection.</span></td>';
				l_sHTML += '</tr>';
			}
			
			//String plaatsen
			$('#presspackTable').html(l_sHTML);

			// Scrollbar
			$('.scrolling').jScrollPane();
			
			//Table effects
			dynamic_table();
			
			//Functie voor content swap
			imageSwap();
		}
	});
}

function getSelectedItem( p_iYear, p_sCountry, p_bShowAll )
{	
	var l_sHTMLYear = 'CHOOSE YEAR';
	var l_sHTMLCountry = 'CHOOSE COUNTRY';

	//Als er op show all geklikt selectie resetten
	if( p_bShowAll ) 
	{	
		$('div.selectYear').html(l_sHTMLYear).css('background-color', '#FFFFFF');
		$('div.selectCountry').html(l_sHTMLCountry).css('background-color', '#FFFFFF');
		$('div.showAll').css('background-color', '#BE3100');
		triggerRightBook();
	}
	else
	{
		l_sHTMLYear = '<a href="#" onclick="return false;" class="filterjaar selected {if $smarty.section.years.last}lastItem{/if}" name="'+p_iYear+'">'+p_iYear+'</a>';
		l_sHTMLCountry = '<a href="#" onclick="return false;" class="filterland selected {if $smarty.section.country.last}lastItem{/if}" name="'+p_sCountry+'">'+p_sCountry+'</a>';
	
		//Highlight resetten
		$('div.showAll').css('background-color', '#FFFFFF');
		$('div.showAll a').css('color', '#000000');
				
		//Geselecteerde jaar tonen
		if( p_iYear.length > 0 )
		{ 
			$('div.selectYear').html(l_sHTMLYear).css('background-color', '#BE3100'); 
			triggerRightBook();
		}
			
		//Geselecteerde land tonen
		if( p_sCountry.length > 0 )
		{ 
			$('div.selectCountry').html(l_sHTMLCountry).css('background-color', '#BE3100'); 
			triggerRightBook();
		}
	}
}

function imageSwap()
{
	//Image swap flipbook
	$('.flipbookRow').click( function()
	{	
		$('#fancy-show').fadeOut( 500 );
		$('#flipbook-show').fadeIn( 500 );
		
		var pack = $(this).attr('id');
		var photo = $(this).attr('name');
		var title = $(this).find('.td-publication').html();
		
		//String opbouwen
		var l_sHTMLImage = '<div class="press-image" id="flip-'+pack+'">';
		
		l_sHTMLImage += '<a href="uploads/presspack/flipbook/fb-'+pack+'/index.html" class="pp_itemflip-'+pack+'">';
		l_sHTMLImage += '<img src="uploads/presspack/photos/'+photo+'" alt="'+title+'" title="'+title+'" height="220" class="" /></a>';
		l_sHTMLImage += '<a href="#" onclick="return false;" class="press-button"><img src="templates/pages/presspack/images/view.gif" alt="View publication" title="View publication" /></a>';
		l_sHTMLImage += '</div>';
		
		//String plaatsen in de html
		$('#flipbook-show').html(l_sHTMLImage);
		
		//Functie voor fancybox flipbook aanroep
		fancyboxTriggerFlip();

		$().width();
	});
	
	//Image swap fancybox gallery
	$('.fancyRow').click( function()
	{	
		$('#flipbook-show').fadeOut( 500 );
		$('#fancy-show').fadeIn( 500 );
		
		var pack = $(this).attr('id');
		var photo = $(this).find('.eventDate a').first().attr('name');
		var title = $(this).find('.td-publication').html();
		
		//String opbouwen
		var l_sHTMLFoto = '<div class="press-image fancy-trigger" name="'+pack+'">';
			
		l_sHTMLFoto += '<img src="uploads/presspack/photos/'+photo+'" alt="'+title+'" title="'+title+'" height="220" />';
		l_sHTMLFoto += '<a href="#" onclick="return false;" class="press-button"><img src="templates/pages/presspack/images/view.gif" alt="View publication" title="View publication" /></a>';
		l_sHTMLFoto += '</div>';
		
		//String plaatsen in de html
		$('#fancy-show').html(l_sHTMLFoto);
		
		//Functie voor fancybox photo aanroep
		fancyboxTriggerPhoto();
	});

}

// Fancybox aanroepen
function fancyboxTriggerFlip()
{
	$('.press-image').click( function()
	{	
		var pack = $(this).attr('id');
		$('.pp_item'+pack).fancybox({
			'hideOnContentClick'	: false,
			'autoDimensions'		: true,
			'width'					: 750,
			'height'				: 625,
			'type'					: 'iframe',
			'showCloseButton'       : false,
			'titlePosition' 		: 'inside',
			'titleFormat'			: formatTitle
		});
			
		$('.pp_item'+pack).trigger("click");
			
		return false;
	});
}

function fancyboxTriggerPhoto()
{
	$('.fancy-trigger').click( function()
	{	
		var pack = $(this).attr('name');
				
		$('.oldPhoto'+pack).fancybox({
			'type'       		: 'image',
			'showCloseButton'   : false,
			'titlePosition' 	: 'inside',
			'titleFormat'		: formatTitle
		});
		
		$('.oldPhoto'+pack).first().trigger("click");
		
		return false;
	});
}

//Custom fancybox title
function formatTitle( title, currentArray, currentIndex, currentOpts ) {
    return '<div id="closeBox"><a href="javascript:;" onclick="$.fancybox.close();"><img src="images/close.gif" /></a></div>';
}

function triggerRightBook()
{
	//Afvangen welke soort presspack we gelijk tonen bij openen pagina
	var firstRow = $('#presspackTable tr.rowHover').first().attr('rel');
	
	if( firstRow == 'flipbookRow')
	{
		$('.flipbookRow').first().trigger("click");
	}
	else
	{
		$('.fancyRow').first().trigger("click");
	}
}
