/*
Function does the following:
+ Sets the container to be positioned relative & overflow hidden
+ Counts the images within the container
+ Get's the container's width and height
+ Divides the width and height by the number of images
+ Creates a div for each image at the beginning of the object
+ positions them side by side within the container
+ assigns the mouseover function to them

- Options should include:
	+ change transition length
	- set to hold current image onclick or after mouseout
	- Styleable image counter: x of x
	- delay display instructions
	- slideshow on mouseout
*/
(function($){$.fn.extend({scrubber:function(options){var defaults={mode:'horizontal',scrubName:'scrub',showClass:false,speed:0}
var options=$.extend(defaults,options);return this.each(function(){var o=options;container=new Object;container.width=$(this).width();container.height=$(this).height();container.total=$(this).children("a").children("img").size();if(defaults.mode=='customH'){customHobject=$(this).parent().children('.container').children('.scrubcontrol');customH=new Object;customH.width=customHobject.width();customH.height=customHobject.height();}
$(this).css({'position':'relative','overflow':'hidden'});$(this).children("a").children("img").css({'position':'absolute','top':0,'display':'none'});var scrubberTitle=$(this).children("a").children("img").attr('title');scrubbox=new Object;var icount=1;var num=Math.random();var rnd=Math.round(num*100000);var showClasses='';switch(o.mode){case'horizontal':scrubbox.width=Math.ceil(container.width/container.total);scrubbox.height=container.height;var wtotal=0;while(icount<=container.total){var scrubid='#'+o.scrubName+'_'+rnd+'_'+icount;if(o.showClass==true){$(this).append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'" title="'+scrubberTitle+'" class="scrub scrub_'+icount+'"></div>');}
else{$(this).append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'" title="'+scrubberTitle+'" class="scrub"></div>');}
$(scrubid).css({'position':'absolute','left':wtotal,'top':0,'width':scrubbox.width,'height':scrubbox.height});wtotal+=scrubbox.width;icount++;}
break;case'customH':scrubbox.width=Math.ceil(customH.width/container.total);scrubbox.height=customH.height;var wtotal=0;while(icount<=container.total){var scrubid='#'+o.scrubName+'_'+rnd+'_'+icount;if(o.showClass==true){customHobject.append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'" title="'+scrubberTitle+'" class="scrub scrub_'+icount+'"></div>');}
else{customHobject.append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'" title="'+scrubberTitle+'" class="scrub"></div>');}
$(scrubid).css({'position':'absolute','left':wtotal,'top':0,'width':scrubbox.width,'height':scrubbox.height});wtotal+=scrubbox.width;icount++;}
break;case'vertical':scrubbox.width=$(this).width();scrubbox.height=Math.ceil(container.height/container.total);var htotal=0;while(icount<=container.total){var scrubid='#'+o.scrubName+'_'+rnd+'_'+icount;if(o.showClass==true){$(this).append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'" title="'+scrubberTitle+'" class="scrub scrub_'+icount+'"></div>');}
else{$(this).append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'" title="'+scrubberTitle+'" class="scrub"></div>');}
$(scrubid).css({'position':'absolute','left':0,'top':htotal,'width':scrubbox.width,'height':scrubbox.height});htotal+=scrubbox.height;icount++;}
case'box':gridsize=new Object;gridsize.width=parseInt(container.width);gridsize.height=parseInt(container.height);gridsize.tnCnt=container.total;var tArea=(gridsize.width*gridsize.height)/gridsize.tnCnt;gridsize.tnWidth=Math.sqrt(tArea);gridsize.tnHeight=gridsize.tnWidth;if(gridsize.width<gridsize.height){gridsize.tXCnt=parseInt(gridsize.width/gridsize.tnWidth)+1;gridsize.tYCnt=parseInt(gridsize.tnCnt/gridsize.tXCnt);if((gridsize.tnCnt%gridsize.tXCnt)){gridsize.tYCnt+=1;}
gridsize.tnWidth=parseInt(gridsize.width/gridsize.tXCnt);gridsize.tnHeight=parseInt(gridsize.height/gridsize.tYCnt);if(gridsize.tnWidth>gridsize.tnHeight){gridsize.tnWidth=gridsize.tnHeight;}
else{gridsize.tnHeight=gridsize.tnWidth;}}
else{gridsize.tYCnt=parseInt(gridsize.height/gridsize.tnHeight)+1;gridsize.tXCnt=parseInt(gridsize.tnCnt/gridsize.tYCnt);if((gridsize.tnCnt%gridsize.tYCnt)){gridsize.tXCnt+=1;}
gridsize.tnWidth=parseInt(gridsize.width/gridsize.tXCnt);gridsize.tnHeight=parseInt(gridsize.height/gridsize.tYCnt);if(gridsize.tnWidth>gridsize.tnHeight){gridsize.tnWidth=gridsize.tnHeight;}
else{gridsize.tnHeight=gridsize.tnWidth;}}
scrubbox.width=gridsize.tnWidth;scrubbox.height=gridsize.tnHeight;while(icount<=container.total){var scrubid='#'+o.scrubName+'_'+rnd+'_'+icount;if(o.showClass==true){$(this).append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'" class="scrub_'+icount+'"></div>');}
else{$(this).append('<div id="'+o.scrubName+'_'+rnd+'_'+icount+'"></div>');}
$(scrubid).css({'float':'left','width':scrubbox.width,'height':scrubbox.height});icount++;}
break;}
if(defaults.mode=='customH'){var theScrubber=customHobject.children("div");}
else{var theScrubber=$(this).children("div");}
var theParent=$(this);theScrubber.each(function(i){theParent.children("a").children("img").hide();var firstOb=theParent.children("a").children("img").filter(function(index){return index==0;});firstOb.show();$(this).hover(function(){var currentObidx='';var currentOb=theParent.children("a").children("img").filter(function(index){currentObidx=i;return index==i;});currentOb.fadeIn(o.speed);if(currentObidx!=0){firstOb.fadeOut(o.speed);}},function(){var currentObidx='';var currentOb=theParent.children("a").children("img").filter(function(index){currentObidx=i;return index==i;});if(currentObidx!=0){currentOb.fadeOut(o.speed);firstOb.fadeIn(o.speed);}});});});}});})(jQuery);

$(function() {
	/* --- SETUP THUMBNAIL SCRUBBERS & GALLERY CLICKTHROUGH ---------------- */
	
	$('#gallery .thumbnail').scrubber();	
	$('#gallery .thumbnail .scrub').each(function(i){
		$(this).click(function(){
			$(this).parent().children('a:nth-child('+i+')').click();
		});
	});		
});

