jQuery(document).ready(function($) {
				
    $.history.init(function(url) {
    	var loc = url.split('/');
    	if(loc[0] == 'about'){
        	$('#a3,#b3').addClass('active').removeAttr('style');
        	$('#a2,#a4,#b1,#b2,#b4').removeClass('active activecloud').removeAttr('style');
        	$('#d').load(loc[0] + '.php').addClass('d-about');
        }
        if(loc[0] == 'work'){
        	$('#a2,#b2').addClass('active').removeAttr('style');
        	$('#a3,#a4,#b1,#b3,#b4').removeClass('active activecloud').removeAttr('style');
        	if(loc[1] == 'web'){var cat = 'w_';}
        	if(loc[1] == 'print'){var cat = 'p_';}
        	if(loc[1] == 'id'){var cat = 'id_';}
        	else {}
        	$('#d').load(loc[0] + '.php', function() { 
        	if(loc[1] !== undefined || loc[2] !== undefined) {
	        	$('#aproject').load("./work/"+ cat + loc[2] +".php");
	        	$('#worknavcontainer').load("./work/_"+ loc[1] +"nav.php", function() {$('#worknav li').removeClass('activenav');$("a[rel='"+ cat + loc[2] +"']").parent().addClass('activenav');});
	        } else {}
        	});
        	
        }
        if(loc[0] == 'contact' || loc[0] == 'contact-thanks'){
        	$('#a4,#b4').addClass('active').removeAttr('style');
        	$('#a2,#a3,#b1,#b2,#b3').removeClass('active activecloud').removeAttr('style');
        	$('#d').load(loc[0] + '.php')
        }
        if(loc[0] == 'main' || url == ''){
        	$('#b1').addClass('activecloud').removeAttr('style');
        	$('#a2,#a3,#a4,#b2,#b3,#b4').removeClass('active').removeAttr('style');
        	$('#d').css({'display':'none'});
        }
    });

    $('#nav a, #workmenu a, #worknav a, #workintro a').live('click', function(e) {
            var url = $(this).attr('href');
            url = url.replace(/^.*#/, '');
            $.history.load(url);
            return false;
        });		

	function hideFront(link){
			$('#front .active,div.activecloud').stop(false,false).css({'z-index':'2'}).animate({left:'-225%'}, {duration: 4000, queue: true, easing: 'easeInOutQuad', complete: function(){
			if(link == '#main_link'){$('#b2,#b3,#b4').removeClass('active').removeAttr('style');}
			if(link == '#work_link'){$('#b3,#b4').removeClass('active').removeAttr('style');$('#b1').removeClass('activecloud').removeAttr('style');}
			if(link == '#about_link'){$('#b2,#b4').removeClass('active').removeAttr('style');$('#b1').removeClass('activecloud').removeAttr('style');}
			if(link == '#contact_link'){$('#b2,#b3').removeClass('active').removeAttr('style');$('#b1').removeClass('activecloud').removeAttr('style');}
			else {}
		}});
	}
        
    function hideBack(link){
    	$('#back .active').stop(false,false).animate({left:'-200%'}, {duration: 4000, queue: true, easing: 'easeInOutQuad', complete: function(){
    	if(link == '#main_link'){$('#a2,#a3,#a4').removeClass('active').removeAttr('style');}
    	if(link == '#work_link'){$('#a3,#a4').removeClass('active').removeAttr('style');}
		if(link == '#about_link'){$('#a2,#a4').removeClass('active').removeAttr('style');}
		if(link == '#contact_link'){$('#a2,#a3').removeClass('active').removeAttr('style');}
		else {}
		}});
    }
        
    function showFront(id, section){
    	if(id == '#b1'){$(id).stop(false,false).animate({left:'50%'}, 4500, 'easeInOutQuad');}
    	else{$(id).stop(false,false).animate({left:'-50%'}, 4000, 'easeInOutQuad', function(){
    			if(id == '#b3'){$('#d').addClass('d-about');}
    			else{$('#d').removeClass('d-about');}
    			$('#d').load(section + '.php').fadeIn(1000)
    		});}
    }
        
    function showBack(id){$(id).stop(false,false).animate({left:'-50%'}, 4000, 'easeInOutQuad');}
    

	$('#main_link').click(function() {
	 if ($("#b1").hasClass('activecloud')) {}
	 else {
	  showFront('#b1', 'main');
	  showBack('#a1');
	 $('#d').fadeOut(1000, function(){
	  $('#d').html('&nbsp;');
	  hideFront('#main_link');
	  hideBack('#main_link');
	  $('#b1').addClass('activecloud');
	  })
	  }
	});
	
	$('#work_link').click(function() {
	 if ($("#a2").hasClass('active')) {$('#d').load('work.php');}
	 else {
	  showFront('#b2', 'work');
	  showBack('#a2');
	 $('#d').fadeOut(500, function(){
	  $('#d').html('&nbsp;');
	  hideFront('#work_link');
	  hideBack('#work_link');
	  $('#b2,#a2').addClass('active');
	  })
	 }
	});
	
	$('#about_link').click(function() {
	 if ($("#a3").hasClass('active')) {}
	 else {
	  showFront('#b3', 'about');
	  showBack('#a3');
	 $('#d').fadeOut(500, function(){
	  $('#d').html('&nbsp;');
	  hideFront('#about_link');
	  hideBack('#about_link');
	  $('#b3,#a3').addClass('active');
	  })
	  }
	});
	
	$('#contact_link').click(function() {
	 if ($("#a4").hasClass('active')) {}
	 else {
	  showFront('#b4', 'contact');
	  showBack('#a4');
	 $('#d').fadeOut(500, function(){
	  $('#d').html('&nbsp;');
	  hideFront('#contact_link');
	  hideBack('#contact_link');
	  $('#b4,#a4').addClass('active');
	  })
	  }
	});

/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);
			
			var imgno = 1;
			var circles = '<ul id="count">';
			while (imgno <= s) { 
				if (imgno == 1) {circles += '<li class="active">'; }
				else {circles += '<li>'; }
				circles += '<img src="./_img/work_imgno.png" alt="'+imgno+'" /></li>';
				imgno ++;
			}
			circles += '</ul>';
			
			$('#slider').before(circles);
			
			if(!options.vertical) $("li", obj).css('float','left');
			
			if(options.controlsShow){
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;						
				$(obj).after(html);	};
	
			$("a","#"+options.nextId).click(function(){	animate("next",true);});
			$("a","#"+options.prevId).click(function(){	animate("prev",true);});	
			$("a","#"+options.firstId).click(function(){ animate("first",true);});				
			$("a","#"+options.lastId).click(function(){	animate("last",true);});		
			
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "next": t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1; break; 
					case "prev": t = (t<=0) ? (options.continuous ? ts : 0) : t-1; break; 
					case "first": t = 0; break; 
					case "last": t = ts; break; 
					default: break; };	
					
				if(dir == 'next') { $('#count li.active').removeClass('active').next().addClass('active'); }
				if(dir == 'prev') { $('#count li.active').removeClass('active').prev().addClass('active'); }
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, speed);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, speed);};
				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$("a","#"+options.nextId).hide();
						$("a","#"+options.lastId).hide();
					} else {
						$("a","#"+options.nextId).show();
						$("a","#"+options.lastId).show();  };
					if(t==0){
						$("a","#"+options.prevId).hide();
						$("a","#"+options.firstId).hide();
					} else {
						$("a","#"+options.prevId).show();
						$("a","#"+options.firstId).show(); };					
				};				
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);};
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();	
				if(t==ts){$("a","#"+options.nextId).hide();}
				else {}			
			};				
			
		});
	  
	};

})

// jQuery Captify Plugin
// Copyright (C) 2009 Brian Reavis
// Licenced under the MIT license
// $Date: 2009-11-30 [Mon, 30 Nov 2009] $
jQuery.fn.extend({
	captify: function(uo) {
		var o = $.extend({
			speedOver: 'fast',				// speed of the mouseover effect
			speedOut: 'normal',				// speed of the mouseout effect
			hideDelay: 500,					// how long to delay the hiding of the caption after mouseout (ms)
			animation: 'slide',				// 'fade', 'slide', 'always-on'
			prefix: '',						// text/html to be placed at the beginning of every caption
			opacity: '.95',				// opacity of the caption on mouse over
			className: 'caption-bottom',	// the name of the CSS class to apply to the caption box         
			position: 'bottom',				// position of the caption (top or bottom)         
			spanWidth: '100%'				// caption span % of the image
		}, uo);
		$(this).each(function() {
			var img = this;
			$(this).load(function() {
				if (img.hasInit){ return false; }
				img.hasInit = true;
				var over_caption = false;
				var over_img = false;

				//pull the label from another element if there is a 
				//valid element id inside the rel="..." attribute, otherwise,
				//just use the text in the alt="..." attribute.
				var captionLabelSrc = $('#' + $(this).attr('rel'));
				var captionLabelHTML = !captionLabelSrc.length ? $(this).attr('alt') : captionLabelSrc.html();
				//captionLabelSrc.remove();
				var toWrap = this.parent && this.parent.tagName == 'a' ? this.parent : $(this);
				
				var wrapper = 
					toWrap.wrap('<div></div>').parent()
					.css({
						overflow: 'hidden',
						padding: 0,
						fontSize: 0.1
					})
					.addClass('caption-wrapper')
					.width($(this).width())
					.height($(this).height());

				//transfer the margin and border properties from the image to the wrapper
				$.map(['top', 'right', 'bottom', 'left'], function(i) {
					wrapper.css('margin-' + i, $(img).css('margin-' + i));
					$.map(['style', 'width', 'color'], function(j) {
						var key = 'border-' + i + '-' + j;
						wrapper.css(key, $(img).css(key));
					});
				});
				$(img).css({ border: '0 none' });

				//create two consecutive divs, one for the semi-transparent background,
				//and other other for the fully-opaque label
				var caption = $('div:last', wrapper.append('<div></div>'))
					.addClass(o.className);
					
				var captionContent = $('div:last', wrapper.append('<div></div>'))
					.addClass(o.className)
					.append(o.prefix)
					.append(captionLabelHTML);

				//override hiding from CSS, and reset all margins (which could have been inherited)
				$('*', wrapper).css({ margin: 0 }).show();

				//ensure the background is on bottom
				var captionPositioning = jQuery.browser.msie ? 'static' : 'relative';
				caption.css({
					zIndex: 1,
					position: captionPositioning,
					opacity: o.animation == 'fade' ? 0 : o.opacity,
					width: o.spanWidth
				});

				if (o.position == 'bottom'){
					var vLabelOffset = 
						parseInt(caption.css('border-top-width').replace('px', '')) + 
						parseInt(captionContent.css('padding-top').replace('px', '')) - 1;
					captionContent.css('paddingTop', vLabelOffset);
				}
				//clear the backgrounds/borders from the label, and make it fully-opaque
				captionContent.css({
					position: captionPositioning,
					zIndex: 2,
					background: 'none',
					border: '0 none',
					opacity: o.animation == 'fade' ? 0 : 1,
					width: o.spanWidth
				});
				caption.width(captionContent.outerWidth());
				caption.height(captionContent.height());

				// represents caption margin positioning for hide and show states
				var topBorderAdj = o.position == 'bottom' && jQuery.browser.msie ? - 0 : 0;
				var captionPosition = o.position == 'top'
				   ? { hide: -$(img).height() - caption.outerHeight() - 1, show: -$(img).height() }
				   : { hide: 0, show: -caption.outerHeight() + topBorderAdj };
				
				//pull the label up on top of the background
				captionContent.css('marginTop', -caption.outerHeight());
				caption.css('marginTop', captionPosition[o.animation == 'fade' || o.animation == 'always-on' ? 'show' : 'hide']);
				
				//function to push the caption out of view
				var cHide = function() {
					if (!over_caption && !over_img){
						var props = o.animation == 'fade'
							? { opacity: 0 }
							: { marginTop: captionPosition.hide };
						caption.animate(props, o.speedOut);
						if (o.animation == 'fade'){
							captionContent.animate({opacity: 0}, o.speedOver);
						}
					}
				};

				if (o.animation != 'always-on'){
					//when the mouse is over the image
					$(this).hover(
						function() {
							over_img = true;
							if (!over_caption) {
								var props = o.animation == 'fade'
									? { opacity: o.opacity }
									: { marginTop: captionPosition.show };
								caption.animate(props, o.speedOver);
								if (o.animation == 'fade'){
									captionContent.animate({opacity: 1}, o.speedOver/2);
								}
							}
						},
						function() {
							over_img = false;
							window.setTimeout(cHide, o.hideDelay);
						}
					);
					//when the mouse is over the caption on top of the image (the caption is a sibling of the image)
					$('div', wrapper).hover(
						function() { over_caption = true; },
						function() { over_caption = false; window.setTimeout(cHide, o.hideDelay); }
					);
				}
			});
			//if the image has already loaded (due to being cached), force the load function to be called
			if (this.complete || this.naturalWidth > 0) {
				$(img).trigger('load');
			}
		});
	}
});
