var activeMenu = false;										// Submenu actually shown
var sl = false;												// Placeholder for the SlideShow object
var menuLinks = new Array();

if (!Prototype.Browser.IE)
{
	window.dhtmlHistory.create({
	        toJSON: function(o) {
	                return Object.toJSON(o);
	        }
	        , fromJSON: function(s) {
	                return s.evalJSON();
	        }
	});
	
	var historyListener = function(newLocation, historyData) {
			var hash = window.location.hash.substr(1);
	        if (hash)
	        {
				var link = $(hash);
				var parent = $(hash).up('ul').previous().className + 'Nav';
				initSlideshow(hash);
				showNav(parent);
				changeCategory(link);
	        }
	};


	Event.observe(window, 'load', function() {
	        dhtmlHistory.initialize();
	        dhtmlHistory.addListener(historyListener);
	});
}

document.observe("dom:loaded", function(){					// createing a SlideShow instance
	sl = new SlideShow($('slider'));
	var deepLink = window.location.hash.substr(1);
	
	$$('ul.subMenu').each(function(ul)						// Creating an array of all navigation links
	{
		ul.select('a').each(function(a)
		{
			a.observe('click', categoryHandler, a);
			menuLinks.push(a);
		});
	});

	menuLinks.each(function(elmt){							// normalize the menu link id's (ppreventig scroll when entering with a hash tag)
		elmt.id = elmt.id.substr(2);
	});
	
	if (deepLink)
	{
		var parent = $(deepLink).up('ul').previous().className + 'Nav';
		showNav(parent);
		initSlideshow(deepLink);
		$(deepLink).addClassName('active');
	}
	
	Cufon.replace('#navigationWrapper ul a', {
		hover: true, hoverables: { a: true }
	});
	Cufon.replace('.sizes span');
	Cufon.replace('.index');
	Cufon.replace('#credits');	
});


Event.observe(document.onresize ? document : window, "resize", function() {  // Check if there are images to load after window resize
	if (sl.Data)
	{
		sl.fillSlides()
	}
});


function initSlideshow(category)							// Initialize a new slideshow
{	
	sl.Data = eval(category);
	sl.Folder = category;
	sl.createSlides();
	var credit = credits[category];
	if (credit != undefined)
	{
		$('credits').innerHTML = '<span>credits</span>' + credit;
	}
	else
	{
		$('credits').update('');
	}
	Cufon.refresh();
}



function showNav(nav)										// show the navigaton links for the cklicked category
{
	if (nav != activeMenu)
	{
		if (activeMenu)
		{
			Effect.BlindUp(activeMenu, { duration: 0.5, afterFinish: function()
				{
						activeMenu = nav;
						Effect.BlindDown(nav, { duration: 0.5 });
				}
			});
		}
		else
		{
			activeMenu = nav;
			Effect.BlindDown(activeMenu, { duration: 0.5 });
		}
	}
}



function categoryHandler(e)
{
	var link = e.element().up('a');
	changeCategory(link);
}

function changeCategory(link)								// give the click menu link the active state (CSS class)
{
	
	menuLinks.each(function(elmt)
	{
		elmt.removeClassName('active');
	});
	
	link.addClassName('active');
	
	var id = link.id;										// update the location hash for current content
	link.id = id + '-temp';
	window.location.hash = id; 
	link.id = id;
	
	Cufon.refresh('#navigationWrapper a');
	
	var navWrapper = $('navigationWrapper');
	if (!navWrapper.hasClassName('shaded'))
	{
		navWrapper.addClassName('shaded');
	}
}


















/*
document.observe("dom:loaded", getSlideshowWidth);
Event.observe(document.onresize ? document : window, "resize", getSlideshowWidth);
function getSlideshowWidth()
{
	//console.log('Slideshow width: ' + $('slideshowWrapper').getWidth() + ' pixels');
	var totalWidth = $('slide1').getWidth() + $('slide2').getWidth() + $('slide3').getWidth() + $('slide4').getWidth() + $('slide5').getWidth() + $('slide6').getWidth();
	console.log(totalWidth);
}*/

