var sivusto_url = 'http://www.kanttia2.fi/';

function dropAnim(elem,direction,callbackfunc)
{
	// console.log(callbackfunc);
	if(typeof(callbackfunc) == 'undefined')
	{
		callbackfunc = function(){ };	
	}
	elem.show('drop', { 'direction': direction}, 400, 
		function() 
		{
			$(this).removeAttr('style'); 
			callbackfunc();
		});
}


function startActions()
{
	
	// MAIN NAVIGATION ACTIVATION
	activateMainNav();
	
	// SUB NAVIGATION ACTIVATION
	activateSubNav();	
	
	// BACK NAVIGATION ACTIVATION
	activateBackNav();	
	
	
	// CYCLE PLUGIN ACTIVATION
	$('#large_image_cycle').cycle(
	{ 
	    timeout:  3000
	});
	

	// GALLERY ACTIVATION
	$("a[rel='gallery1']").colorbox(
		{
			opacity:0.6,
			current: "kuva {current} / {total}",
			previous: "edellinen",
			next: "seuraava",
			close: "sulje"
		}
	);
	
}


function activateMainNav()
{
	$('#navigator a').unbind('click').bind('click',
	function()
	{
		var sectionid = $(this).attr('pageid');
		viewSection(sectionid);
		return false;

	});
}

function activateSubNav()
{
	$('#content_sublinks a').unbind('click').bind('click',
	function()
	{
		var sectionid = $(this).attr('pageid');
		viewSection(sectionid);
		return false;

	});
}

function activateBackNav()
{
	$('#backlink').unbind('click').bind('click',
	function()
	{
		var sectionid = $(this).attr('pageid');
		viewSection(sectionid);
		return false;

	});
}





function showSection(sectionid)
{

	$('#main_box').html('<div id="loader"><img src="'+sivusto_url+'img/ajax-loader.gif" border="0" /><br />LADATAAN</div>');

	$('#main_box').load(sivusto_url + 'sections/view/' + sectionid, function() 
		{
			$('#large_image').hide();
			dropAnim($('#content_box'),'left',
				function()
				{
					$('#large_image').fadeIn('fast');
				});
			
			// dropAnim($('#large_image'),'right');
			startActions();
		});
}


function viewSection(sectionid)
{
	
	// Hide sections if they exist, otherwise skip to showing directly
	if($('#content_box').length)
	{
		$('#content_box').hide('drop', { 'direction': 'left'}, 400, 
			function()
			{ 
				showSection(sectionid);
			});
	}
	
	if($('#large_image').length)
	{
		$('#large_image').hide();
		/*
		$('#large_image').hide('drop', { 'direction': 'right'}, 400, 
		function() 
		{
			$(this).removeAttr('style'); 
			showSection(sectionid);
		});
		*/
		
	}
	else
	{
		showSection(sectionid);	
	}

}




$(document).ready(function()
{
	viewSection(default_section_id);
});