//Cookie plugin -- Copyright (c) 2006 Klaus Hartl (stilbuero.de)
jQuery.cookie=function(a,b,c){if(typeof b!='undefined'){c=c||{};if(b===null){b='';c.expires=-1};var d='';if(c.expires&&(typeof c.expires=='number'||c.expires.toUTCString)){var e;if(typeof c.expires=='number'){e=new Date();e.setTime(e.getTime()+(c.expires*24*60*60*1000))}else{e=c.expires};d='; expires='+e.toUTCString()};var f=c.path?'; path='+(c.path):'';var g=c.domain?'; domain='+(c.domain):'';var h=c.secure?'; secure':'';document.cookie=[a,'=',encodeURIComponent(b),d,f,g,h].join('')}else{var j=null;if(document.cookie&&document.cookie!=''){var k=document.cookie.split(';');for(var i=0;i<k.length;i++){var l=jQuery.trim(k[i]);if(l.substring(0,a.length+1)==(a+'=')){j=decodeURIComponent(l.substring(a.length+1));break}}};return j}};

// Galery Plugin -- Copyright (c) 2009 Adam Kizer
(function($) {
	var gallery = new Array();
	var current = {};
	var xmlLoc = '/gallery.xml';
	var imgDir = '/images/gallery/';
	var isReady = false;
	var cookieDomain = String(window.location).split('/')[2].replace('www.','');	
	
	function getGallery() { $.ajax({ type:"GET", url:xmlLoc+"?r="+Math.random()*1000, dataType:"xml", success:galleryHandler }); };
	
	function galleryHandler(xml) {
		var activeTab = 0;
		$('#accordion').html('');
		
		$(xml).find('category').each(function(i) {
			var path = SWFAddress.getPathNames();
			var currentTab = false;
			
			if(i==0) {
				current.category = $(this).attr('name').toLowerCase().replace(/ /g, '-');
			} else if(path[0] && path[0]==$(this).attr('name').toLowerCase().replace(/ /g, '-')) {
				current.category = $(this).attr('name').toLowerCase().replace(/ /g, '-');
				currentTab = true;
				activeTab = i;
			};
											  
			var category = { index:i, images:new Array() };
			var html = new Array();
			
			html.push('<h3><a href="#" class="',$(this).attr('class'),'">',$(this).attr('name'),'</a></h3>');
			html.push('<div><ul>');
			
			$(this).find('image').each(function(j) {
				html.push('<li><a href="#"><img src="'+imgDir+'thumbs/',$(this).attr('src'),'" title="',($(this).attr('title') ? $(this).attr('title') : ''),'" rel="',parseInt(j+1),'" width="79" height="79" /></a></li>');
				category.images.push({ src:$(this).attr('src'), title:$(this).attr('title') });
				if(path[1] && path[1]==j+1 && currentTab) {
					$('<img src="'+imgDir+$(this).attr('src')+'" alt="'+($(this).attr('title') ? $(this).attr('title') : '')+'" />').appendTo('#contentColumn');
				};
			});
			
			html.push('</ul><div class="clear"></div></div>');
			$(html.join('')).appendTo('#accordion');
			
			gallery[$(this).attr('name').toLowerCase().replace(/ /g, '-')] = category;
			
			if(i==0 && document.getElementById('contentGalleryImage')) {
				throwInNewImage(true);
			};
			isReady = true;
		});
		
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
		$("#accordion").accordion({ autoHeight: false, collapsible: false, active:activeTab }).find('h3 a').click(function() {
			current.category = $(this).html().toLowerCase().replace(/ /g, '-');
			SWFAddress.setValue(current.category+'/');
		}).end().find('li a').click(function(e) {
			e.preventDefault();
			current.image = $(this).find('img').attr('rel');
			SWFAddress.setValue(current.category+'/'+current.image+'/');
		});	
		
	};
	
	function handleChange(e) {
		if(!isReady) {
			setTimeout(handleChange,1000);
			return;
		};
		
		var path = SWFAddress.getPathNames();
		if(!path[0]) {
			$("#accordion").accordion('activate', 0);
			return;
		};
		current.category = path[0];
		$.cookie('category', current.category, { path:'/', domain:cookieDomain });
		
		$("#accordion").accordion('activate', gallery[current.category].index);
		if(path[1]) {
			if(document.getElementById('contentGalleryImage')) {
				$('#contentGalleryImage').stop().fadeTo(150,0,function() { 
					throwInNewImage();
				});
			} else {
				$('#contentColumn').children().fadeTo(150,0,function() {
					throwInNewImage();
				});
				var c = $('#contentColumn').children();
				if(c.length<1) {
					throwInNewImage();
				};
			};
		};	
	};
	
	function throwInNewImage(forceDefault) {
		var path = SWFAddress.getPathNames();
		current.image = (forceDefault ? 1 : path[1]);
		var img = gallery[current.category].images[current.image-1].src;
		var title = gallery[current.category].images[current.image-1].title;
		
		$('#contentColumn').css({ position:'relative' }).html('<div id="contentLoading"></div>');
		$('<img id="contentGalleryImage" src="'+imgDir+img+'" alt="'+(title ? title : '')+'" />').appendTo('#contentColumn').hide().css({ position:'absolute', top:0, left:0, zIndex:100 }).load(function() {
			$(this).show().fadeTo(150,1);
		});
	};
			  
	SWFAddress.addEventListener(SWFAddressEvent.INIT, handleChange);
	$(document).ready(function() {
		try{
			var path = SWFAddress.getPathNames();
			if(!path[0] && $.cookie('category')) SWFAddress.setValue($.cookie('category')+'/');
			if(path[1]) { $('#contentGalleryImage').remove(); };
		} catch(err) { };
		getGallery();						   
	});
})(jQuery);