var $j = jQuery.noConflict();
$j(document).ready(function()
{
	function trace(o){console.log(o);}
	//
	var namespace = 'de.dmoch-segel';
	// initialize cookie
	$j.Jookie.Initialise(namespace, 60*24*31);
	var cookie = $j.Jookie.Get(namespace);
	var file = window.location.pathname.substring(window.location.pathname.lastIndexOf("/")+1, window.location.pathname.length);
	//
	function coreNavi()
	{
		var navi_items = $j('#nav a');
		var initCoreNavi = function()
		{
			// traverse navi items:
			$j.each(navi_items, function(i, val)
			{
				var href = $j(this).attr('href');
				$j(this).data('href', href);
				// catch it's links
				$j(this).bind('click', function()
				{
					// set cookie on click ("which item has been clicked")
					$j.Jookie.Set(namespace, 'current_page', href);
					// and route to normal behaviour ...
				});
				// get "current page":
				if (file === $j(this).attr('href'))
				{
					var html = '<span class="current' + (file === 'index.html' ? ' home' : '') + '">' + $j(this).html() + '</span>';
					var current = $j(this).after(html);
					current.data('href', href)
					$j(this).hide();
				}
				
			});
		}
		initCoreNavi();
	}
	coreNavi();
	//
	function toggleNavi ()
	{
		var saveState = function ()
		{
			$j.Jookie.Set(namespace, 'toggle_state', $j('.collectionshead').data('visible')); 
		}
		//
		var initToggleNavi = function()
		{
			// init state read from cookie:
			// first time, cookie is set but has no entries:
			var url = window.location.href;
			var cur_page = url.substr(url.lastIndexOf('/')+1);
			var deeplinks = $j.makeArray($j('#nav .sails a'));
			var is_deeplink = false;
			$j.each(deeplinks, function(i, val)
			{
				deeplinks[i] = $j(this).data('href');
			});
			if ($j.inArray(cur_page, deeplinks) != -1)
			{
				var is_deeplink = true;
			}
			//
			if (cookie.toggle_state == undefined && !is_deeplink ) 
			{
				$j.Jookie.Set(namespace, 'current_page', cur_page);
				$j.Jookie.Set(namespace, 'toggle_state', false);
				$j('#nav ul.sails').hide();
				$j('.collectionshead').data('visible', false);
			} 
			else if(!is_deeplink){
				// check for deeplink:
				// toggle object initially:
				cookie.toggle_state ? null : $j('#nav ul.sails').hide();
				// write data to the object:
				$j('.collectionshead').data('visible', cookie.toggle_state);
			}
			else
			{
				$j.Jookie.Set(namespace, 'current_page', cur_page);
				$j('.collectionshead').data('visible', true);
				$j('#nav ul.sails').show();
			}
			// save to cookie:
			$j.Jookie.Set(namespace, 'toggle_state', $j('.collectionshead').data('visible'));
			//
			// bind taggle:
			$j('.collectionshead').bind('click', function()
			{
				// write data to object:
				$j(this).data('visible', !$j(this).data('visible'));
				// toggle object:
				$j(this).data('visible') == true ? $j('#nav ul.sails').fadeIn(450) : $j('#nav ul.sails').fadeOut(200);
				// hmmm ;-)
				saveState();
			});
		}
		initToggleNavi();
	}
	toggleNavi ();
	//
	function addCSS()
	{
		$j('#content').height() < $j('#navi').height() ? $j('#content').height($j('#navi').height())  : null;
	}
	addCSS();
	//
	function addFancyZoom()
	{
		$j('#photos a').fancyzoom({overlay:0.75, overlayColor:"#000", imgDir:'pic/', Speed:250}); 
	}
	addFancyZoom ();
	//
	$j('.png32').ifixpng();
});

/*
return;
		
*/