jQuery.GAL = {
	$thumbs : null,
	
	switchThumb : function($thumb, options){
		var defaults = {speedIn : 300};
		var options = jQuery.extend({}, defaults, options);
		var $target = $thumb.data('$target');
		
		if(!$target.length){return false;}
		if($thumb.data('id') == $target.data('currentThumb')){return false;}
		
		$target.data('currentThumb', $thumb.data('id'));
		
		$target.empty();
		
		var $img = jQuery(new Image());
		$img.load(function () {   
			$target.hide().empty().append($img);
			$target.fadeIn(options.speedIn);
		}).attr('src', $thumb.attr('href'));
		
		return false;
	},
	
	switchSet : function($setSwitch, options){
		var defaults = {speedIn : 300};
		var options = jQuery.extend({}, defaults, options);
		
		$setSwitch.get(0).checked = true;
		var $galleryThumbs = jQuery('.GALTHUMB.'+$setSwitch.data('GALID'));
		
		$galleryThumbs.filter(':visible').hide();
		$galleryThumbs.filter('.'+$setSwitch.data('GALSET')).fadeIn(options.speedIn);
		jQuery.GAL.switchThumb($galleryThumbs.filter('.'+$setSwitch.data('GALSET')+':first'), options);
	},
	
	getGALID : function($jQueryObject){var $return = $jQueryObject.attr('class').match(/\bGALID[^ ]+/); if(!$return){return '';} else {return $return[0];}},
	getGALSET : function($jQueryObject){var $return = $jQueryObject.attr('class').match(/\bGALSET[^ ]+/); if(!$return){return '';} else {return $return[0];}},
	getGALTARGET : function($jQueryObject){return 'GALTARGET'+jQuery.GAL.getGALID($jQueryObject).replace('GALID', '');},
	get$target : function($jQueryObject){return jQuery('#'+jQuery.GAL.getGALTARGET($jQueryObject) + ' .GALTARGETINNER');},
	
	init : function(){
		jQuery.GAL.$thumbs = jQuery('.GALTARGET').addClass('loading').wrapInner(jQuery('<div class="GALTARGETINNER"></div>'));
		jQuery.GAL.$thumbs = jQuery('a.GALTHUMB').each(function(i){
			var $thumb = jQuery(this);
			
			$thumb.data('id', i);
			$thumb.data('$target', jQuery.GAL.get$target($thumb));
			$thumb.data('GALSET', jQuery.GAL.getGALSET($thumb));	
			$thumb.data('GALID', jQuery.GAL.getGALID($thumb));	
			
			if(!$thumb.data('GALID')){$thumb.data('GALID', 'GALIDDEFAULT').addClass('GALIDDEFAULT');}
			if(!$thumb.data('GALSET')){$thumb.data('GALSET', 'GALSETDEFAULT').addClass('GALSETDEFAULT');}
			
			$thumb.mouseenter(function(){jQuery.GAL.switchThumb($thumb);});	
		});
		
		var gallerySwitches = new Array();//a list of all of the galleries that have set switches
		jQuery('.GALSWITCH').each(function(){
			var $setSwitch = jQuery(this);
			$setSwitch.data('GALID', jQuery.GAL.getGALID($setSwitch));
			$setSwitch.data('GALSET', jQuery.GAL.getGALSET($setSwitch));
			gallerySwitches.push($setSwitch.data('GALID'));
			
			if($setSwitch.is('input:radio')){
				$setSwitch.change(function(){jQuery.GAL.switchSet($setSwitch);});	
			} else {
				$setSwitch.click(function(){jQuery.GAL.switchSet($setSwitch);});	
			}
		});
		
		gallerySwitches = gallerySwitches.unique();//make the list unique
		for(var x = 0, y = gallerySwitches.length; x < y; x++){//for every gallery that has switches
			jQuery('.'+gallerySwitches[x]+'.GALSWITCH:eq(0)').each(function(){//choose the first switch
				jQuery.GAL.switchSet(jQuery(this), {speedIn : 0});//switch to it														 
			});
		}
	}
}
jQuery(document).ready(function(){
	jQuery.GAL.init();
});

Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++){
		for(var x = 0, y = r.length; x < y; x++){
			if(r[x]==this[i]){continue o;}
		}
		r[r.length] = this[i];
	}
	return r;
}
