/*
* Portfolio Script
* Christian L. Schultz -- SCHULTZconcepts
* October 30, 2008
* 
* This script contains code to perform two seperate functinons
* (1) Create the side bar accordian menu
* (2) Create the gallery displayed to the right - uses gallaria plugin
* 
*/

// Helper function to extract parameters from an anchor tag
jQuery.query = function(s) {
     var r = {};
     if (s) {
         var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
         q = q.replace(/\&$/, ''); // remove the trailing &
         jQuery.each(q.split('&'), function() {
             var splitted = this.split('=');
             var key = splitted[0];
             var val = splitted[1];
             // convert numbers
             if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
             // convert booleans
             if (val == 'true') val = true;
             if (val == 'false') val = false;
             // ignore empty values
             if (typeof val == 'number' || typeof val == 'boolean' || 
val.length > 0) r[key] = val;
         });
     }
     return r;
};


$(document).ready(function() {
	// Set height of all drawer content to be the same
	// var $height = $('ul.drawer_content').height();
	// $('ul.drawer_content').css('height', $height - 1);
	
	if (window.location.hash) { // if there is a hash in the address open that drawer
		var $elHash = window.location.hash.substr(1);
		$('ul.drawer_content').hide();
		$('h2#' + $elHash).addClass('open').parent().find('ul:first').show();
	} else { // open the first drawer
		// $('ul.drawer_content:not(:first)').hide();
		// $('ul.drawer_content:first').parent().find('h2:first').addClass('open');
		$('ul.drawer_content').hide();
	}
	
	// Upon click perform
	
	$('h2.drawer_handle').click(function() {
		// slide up visible content
		$('ul.drawer_content:visible').slideUp('slow').parent().find('h2:first').removeClass('open');
		// slide down content that was opened and apply open class to h2 tag
		$(this).parent().find('ul:first').slideDown('slow').parent().find('h2:first').addClass('open');
		return false;
	});
	
	
	// (2) Gallaria
	// Setup loading indication
	$('#loading').ajaxStart(function() {
		// $('#gallery').css('display' , 'none');
		// $(this).show();
	}).ajaxStop(function() {
		// $(this).hide();
		$(function($) { $('ul.galleryList').galleria(gOptions); });
		// $('#gallery').show();
	});
	
	// Send parameters to portfolio_process.php
	$('ul.drawer_content li a').click(function() {
		// Remove class selected from all a links
		$(this)
			.parents('ul.drawers')
				.find('a')
					.removeClass('selected')
				.end()
			.end()
			// Add class selected
			.addClass('selected');
		// Get parameters from link
		var params = $.query(this.href);
		// Process AJAX request
		$('#gallery').load('portfolio_process.php', {
							'cat' : params.cat,
							'name' : params.name
						});
		return false;
	});
	
	// Set Gallaria Options
	gOptions = {
			history : false,
			clickNext : false,
			insert : '#mainImage',
			onImage   : function(image,caption,thumb) { 
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					image.css('display','none').fadeIn(1000);
				}
				caption.css('display','none').fadeIn(1000);
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		};
	
	
	

});