/**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 */
 
;(function($) {
	/**
	 * equalizes the heights of all elements in a jQuery collection
	 * thanks to John Resig for optimizing this!
	 * usage: $("#col1, #col2, #col3").equalizeCols();
	 */
	 
	$.fn.equalizeCols = function(){
		var height = 0,
			_reset = $.browser.msie ? "1%" : "auto";
  
		return this
			.css("height", _reset)
			.each(function() {
				height = Math.max(height, this.offsetHeight);
			})
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					$(this).css("height", height - (h - height));
				};
			});
			
	};
	
})(jQuery);

/**
 * Galleria (http://monc.se/kitchen)
 *
 * Galleria is a javascript image gallery written in jQuery. 
 * It loads the images one by one from an unordered list and displays thumbnails when each image is loaded. 
 * It will create thumbnails for you if you choose so, scaled or unscaled, 
 * centered and cropped inside a fixed thumbnail box defined by CSS.
 * 
 * The core of Galleria lies in it's smart preloading behaviour, snappiness and the fresh absence 
 * of obtrusive design elements. Use it as a foundation for your custom styled image gallery.
 *
 * MAJOR CHANGES v.FROM 0.9
 * Galleria now features a useful history extension, enabling back button and bookmarking for each image.
 * The main image is no longer stored inside each list item, instead it is placed inside a container
 * onImage and onThumb functions lets you customize the behaviours of the images on the site
 *
 * Tested in Safari 3, Firefox 2, MSIE 6, MSIE 7, Opera 9
 * 
 * Version 1.0
 * Februari 21, 2008
 *
 * Copyright (c) 2008 David Hellsing (http://monc.se)
 * Licensed under the GPL licenses.
 * http://www.gnu.org/licenses/gpl.txt
 **/
(function(B){var A;A=B.fn.galleria=function(E){if(!A.hasCSS()){return false;}B.historyInit(A.onPageLoad);var I={insert:".galleria_container",history:true,clickNext:true,onImage:function(L,K,J){},onThumb:function(J){}};var F=B.extend(I,E);for(var H in F){if(H){B.galleria[H]=F[H];}}var D=(B(F.insert).is(F.insert))?B(F.insert):jQuery(document.createElement("div")).insertAfter(this);var C=B(document.createElement("div")).addClass("galleria_wrapper");var G=B(document.createElement("span")).addClass("caption");D.addClass("galleria_container").append(C).append(G);return this.each(function(){B(this).addClass("galleria");B(this).children("li").each(function(P){var L=B(this);var J=B.meta?B.extend({},F,L.data()):F;J.clickNext=B(this).is(":only-child")?false:J.clickNext;var O=B(this).find("a").is("a")?B(this).find("a"):false;var K=B(this).children("img").css("display","none");var Q=O?O.attr("href"):K.attr("src");var N=O?O.attr("title"):K.attr("title");var M=new Image();if(J.history&&(window.location.hash&&window.location.hash.replace(/\#/,"")==Q)){L.siblings(".active").removeClass("active");L.addClass("active");}B(M).load(function(){B(this).attr("alt",K.attr("alt"));var S=B(this);var R=O?O.find("img").addClass("thumb noscale").css("display","none"):K.clone(true).addClass("thumb").css("display","none");if(O){O.replaceWith(R);}R.attr("rel",Q);R.attr("title",N);R.click(function(){B.galleria.activate(Q);});R.hover(function(){B(this).addClass("hover");},function(){B(this).removeClass("hover");});L.hover(function(){L.addClass("hover");},function(){L.removeClass("hover");});L.prepend(R);R.css("display","block");J.onThumb(jQuery(R));if(L.hasClass("active")){B.galleria.activate(Q);}K.remove();}).error(function(){L.html('<span class="error" style="color:red">Error loading image: '+Q+"</span>");}).attr("src",Q);});});};A.nextSelector=function(C){return B(C).is(":last-child")?B(C).siblings(":first-child"):B(C).next();};A.previousSelector=function(C){return B(C).is(":first-child")?B(C).siblings(":last-child"):B(C).prev();};A.hasCSS=function(){B("body").append(B(document.createElement("div")).attr("id","css_test").css({width:"1px",height:"1px",display:"none"}));var C=(B("#css_test").width()!=1)?false:true;B("#css_test").remove();return C;};A.onPageLoad=function(F){var D=B(".galleria_wrapper");var E=B('.galleria img[@rel="'+F+'"]');if(F){if(B.galleria.history){window.location=window.location.href.replace(/\#.*/,"")+"#"+F;}E.parents("li").siblings(".active").removeClass("active");E.parents("li").addClass("active");var C=B(new Image()).attr("src",F).addClass("replaced");D.empty().append(C);D.siblings(".caption").text(E.attr("title"));B.galleria.onImage(C,D.siblings(".caption"),E);if(B.galleria.clickNext){C.css("cursor","pointer").click(function(){B.galleria.next();});}}else{D.siblings().andSelf().empty();B(".galleria li.active").removeClass("active");}B.galleria.current=F;};B.extend({galleria:{current:"",onImage:function(){},activate:function(C){if(B.galleria.history){B.historyLoad(C);}else{A.onPageLoad(C);}},next:function(){var C=B(A.nextSelector(B('.galleria img[@rel="'+B.galleria.current+'"]').parents("li"))).find("img").attr("rel");B.galleria.activate(C);},prev:function(){var C=B(A.previousSelector(B('.galleria img[@rel="'+B.galleria.current+'"]').parents("li"))).find("img").attr("rel");B.galleria.activate(C);}}});})(jQuery);jQuery.extend({historyCurrentHash:undefined,historyCallback:undefined,historyInit:function(D){jQuery.historyCallback=D;var C=location.hash;jQuery.historyCurrentHash=C;if(jQuery.browser.msie){if(jQuery.historyCurrentHash===""){jQuery.historyCurrentHash="#";}$("body").prepend('<iframe id="jQuery_history" style="display: none;"></iframe>');var A=$("#jQuery_history")[0];var B=A.contentWindow.document;B.open();B.close();B.location.hash=C;}else{if($.browser.safari){jQuery.historyBackStack=[];jQuery.historyBackStack.length=history.length;jQuery.historyForwardStack=[];jQuery.isFirst=true;}}jQuery.historyCallback(C.replace(/^#/,""));setInterval(jQuery.historyCheck,100);},historyAddHistory:function(A){jQuery.historyBackStack.push(A);jQuery.historyForwardStack.length=0;this.isFirst=true;},historyCheck:function(){if(jQuery.browser.msie){var A=$("#jQuery_history")[0];var D=A.contentDocument||A.contentWindow.document;var F=D.location.hash;if(F!=jQuery.historyCurrentHash){location.hash=F;jQuery.historyCurrentHash=F;jQuery.historyCallback(F.replace(/^#/,""));}}else{if($.browser.safari){if(!jQuery.dontCheck){var B=history.length-jQuery.historyBackStack.length;if(B){jQuery.isFirst=false;var C;if(B<0){for(C=0;C<Math.abs(B);C++){jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop());}}else{for(C=0;C<B;C++){jQuery.historyBackStack.push(jQuery.historyForwardStack.shift());}}var E=jQuery.historyBackStack[jQuery.historyBackStack.length-1];if(E!==undefined){jQuery.historyCurrentHash=location.hash;jQuery.historyCallback(E);}}else{if(jQuery.historyBackStack[jQuery.historyBackStack.length-1]===undefined&&!jQuery.isFirst){if(document.URL.indexOf("#")>=0){jQuery.historyCallback(document.URL.split("#")[1]);}else{F=location.hash;jQuery.historyCallback("");}jQuery.isFirst=true;}}}}else{F=location.hash;if(F!=jQuery.historyCurrentHash){jQuery.historyCurrentHash=F;jQuery.historyCallback(F.replace(/^#/,""));}}}},historyLoad:function(D){var E;if(jQuery.browser.safari){E=D;}else{E="#"+D;location.hash=E;}jQuery.historyCurrentHash=E;if(jQuery.browser.msie){var A=$("#jQuery_history")[0];var C=A.contentWindow.document;C.open();C.close();C.location.hash=E;jQuery.historyCallback(D);}else{if(jQuery.browser.safari){jQuery.dontCheck=true;this.historyAddHistory(D);var B=function(){jQuery.dontCheck=false;};window.setTimeout(B,200);jQuery.historyCallback(D);location.hash=E;}else{jQuery.historyCallback(D);}}}});

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Date: 2/19/2008
 * @author Ariel Flesler 
 * @version 1.3.3
 */
;(function($){var o=$.scrollTo=function(a,b,c){o.window().scrollTo(a,b,c)};o.defaults={axis:'y',duration:1};o.window=function(){return $($.browser.safari?'body':'html')};$.fn.scrollTo=function(l,m,n){if(typeof m=='object'){n=m;m=0}n=$.extend({},o.defaults,n);m=m||n.speed||n.duration;n.queue=n.queue&&n.axis.length>1;if(n.queue)m/=2;n.offset=j(n.offset);n.over=j(n.over);return this.each(function(){var a=this,b=$(a),t=l,c,d={},w=b.is('html,body');switch(typeof t){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(t)){t=j(t);break}t=$(t,this);case'object':if(t.is||t.style)c=(t=$(t)).offset()}$.each(n.axis.split(''),function(i,f){var P=f=='x'?'Left':'Top',p=P.toLowerCase(),k='scroll'+P,e=a[k],D=f=='x'?'Width':'Height';if(c){d[k]=c[p]+(w?0:e-b.offset()[p]);if(n.margin){d[k]-=parseInt(t.css('margin'+P))||0;d[k]-=parseInt(t.css('border'+P+'Width'))||0}d[k]+=n.offset[p]||0;if(n.over[p])d[k]+=t[D.toLowerCase()]()*n.over[p]}else d[k]=t[p];if(/^\d+$/.test(d[k]))d[k]=d[k]<=0?0:Math.min(d[k],h(D));if(!i&&n.queue){if(e!=d[k])g(n.onAfterFirst);delete d[k]}});g(n.onAfter);function g(a){b.animate(d,m,n.easing,a&&function(){a.call(this,l)})};function h(D){var b=w?$.browser.opera?document.body:document.documentElement:a;return b['scroll'+D]-b['client'+D]}})};function j(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/**
 * jQuery.SerialScroll - Animated scrolling of series
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 8/3/2008
 * @author Ariel Flesler
 * @version 1.1.2
 *
 * http://flesler.blogspot.com/2008/02/jqueryserialscroll.html
 */
;(function($){var a='serialScroll',b='.'+a,c='bind',u=$[a]=function(b){$.scrollTo.window()[a](b)};u.defaults={duration:1000,axis:'x',event:'click',start:0,step:1,lock:1,cycle:1};$.fn[a]=function(q){q=$.extend({},u.defaults,q);var r=q.event,s=q.step,t=q.duration/s;return this.each(function(){var h=$(this),j=q.lazy?q.items:$(q.items,h),k=q.start,l;if(q.force)n.call(this,{},k);$(q.prev||[])[c](r,-s,m);$(q.next||[])[c](r,s,m);h[c]('prev'+b,-s,m)[c]('next'+b,s,m)[c]('goto'+b,n)[c]('start'+b,function(e,i){if(!q.interval){q.interval=i||1000;p();o()}})[c]('stop'+b,function(){p();q.interval=0});if(!q.lazy&&q.jump)j[c](r,function(e){e.data=j.index(this);n(e,this)});function m(e){e.data+=k;n(e,this)};function n(e,a){if(typeof a=='number'){e.data=a;a=this}var b=e.data,c,d=e.type,f=$(j,h),g=f.length;if(d)e.preventDefault();b%=g;if(b<0)b+=g;c=f[b];if(q.interval){p();l=setTimeout(o,q.interval)}if(isNaN(b)||d&&k==b||q.lock&&h.is(':animated')||!q.cycle&&!f[e.data]||d&&q.onBefore&&q.onBefore.call(a,e,c,h,f,b)===!1)return;if(q.stop)h.queue('fx',[]).stop();q.duration=Math.abs(t*(k-b));h.scrollTo(c,q);k=b};function o(){h.trigger('next'+b)};function p(){clearTimeout(l)}})}})(jQuery);

jQuery.fn.gallery = function(opts){
	// set default options
	var defaults = {
		target      : '.galleria_container',
		caption     : '.galleria_container .caption',
		activeThumbClass : 'active',
		inactiveThumbClass : 'inactive',
		beforeLoad : function(target,caption){
			$('.loadingImage',target).remove();
			target
				.append('<div class="loadingImage">Loading Image</div>')
				.height( (target.innerHeight() > 400) ?target.innerHeight() : 420 );
				
			var image = $(this);
			image.fadeOut(200,function(){  image.remove();window.location.hash = 'gallery' });
			
			caption && caption.html('');
		},
		onLoad : function(target,caption,text){// called when new image is ready to diplay
	/*		
			if (window.location.search.match(/hello/)) {
				alert(target[0])
				alert(caption[0])
				alert(text)
			}
			try {*/
				
			
				target.height('auto');
						
				window.location.hash = 'gallery';
			
				if(window.location.search)
			
				var image = $(this);
				var insert = setInterval(function(){
					if( $('img',target).length == 0) {
						$('ul.gallery').attr('data-gallery-processing','f');
						$('.loadingImage',target).remove();
						
						clearInterval(insert);
						image
							.insertBefore(caption)
							.hide()
							.fadeIn(200);
						caption.html(text);
						//window.location.hash = 'gallery';
					}
				},100);
			/*} catch(e) {
				if (window.location.search.match(/hello/)) { alert(e); }
			}*/
		}
	};
	
	// extend the options
	var settings = $.extend(defaults, opts);
	
	return this.each(function(){
		var $this = $(this),
			target = $(settings.target),
			caption = $(settings.caption),
			activeThumbClass = settings.activeThumbClass,
			beforeLoad = settings.beforeLoad,
			onLoad = settings.onLoad || function(){};
	//	(function() {
		$this
			.unbind('click.gallery')
			.bind('click.gallery',function(e,element){
				var current = current = $(element || e.target).closest('img'),
					gallery = $(this);				
				
				if(!current.length //current.nodeName != 'IMG'
				   || gallery.attr('data-gallery-processing') == 't'  )
					{ e.stopPropagation(); e.preventDefault(); return false; }
				
				current = $(current).parents('a:first');
								
				gallery.attr('data-gallery-processing','t');
				
			// Remove active class from current active image
				$('.'+activeThumbClass,this)
					.removeClass(activeThumbClass);
								
				var	currentLI = current.parents('li:first'),
					nextSrc = $('a', currentLI.next('li:first') ).attr('href'),
					largeSrc = current.attr('href'),
					largeCaption = current.attr('title') || '',
					large = new Image(),
					next = new Image();				
				(function(large) {
				large.onload = function(){
									
					onLoad && onLoad.apply(large,[target,caption,largeCaption]);
				};
				})(large);
				large.src = largeSrc;
				
				next.src = nextSrc;
				
				beforeLoad && beforeLoad.apply($('img',target),[target,caption]);
				
			// Add active class to selected image
				currentLI.addClass(activeThumbClass);
				
				return false;
			})
			.trigger('click.gallery', $('img:first', $this) );
	//	})($this);
	});
};

$(document).ready(function(){
	$('.columns_2 .section').equalizeCols();
	$('.columns_3 .section').equalizeCols();


	$('ul.gallery')
		.gallery({
			target      : '.galleria_container',
			caption     : '.galleria_container .caption'
		})
	
	$('#gallery_thumbs').serialScroll({
		items:'li',
		prev:'#gallery_prev',
		next:'#gallery_next',
		axis:'x',
		offset:-230, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:500,
		force:true,
		stop:false,
		step: 3,
		event:'click',
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:false,//'easeOutQuart', //use this easing equation for a funny effect
		jump: true //click on the images to scroll to them,
	});
});