tar_gallery={
	defaults:{
		"path":"/imgs/", //The path to the top level image folder
		"category":"", //The subfolder structure to the category of images
		"fullsize":"400", //The name of the folder that contains the images you want to use for fullsize images
		"padoffset":0, //Integer for how much padding room
		"autosizethumbs":false, //Set to true to automatically resize thumbnail containers so an equal number fit on all full pages
		"scrolltype":"continuous" //continuous or page
	},
	fetch:function(get){
		return this[get];
	},
	timers:{},
	init:function(){
		var seek=".tar_gallery";
		var overrides={};
		if(arguments){for(i=0;i<arguments.length;i++){switch(typeof arguments[i]){
			case "string": seek=arguments[i]; break;
			case "object": overrides=arguments[i]; break;
		}}}
		/*var loadnum=$(seek).find(".tar_gallery_thumb_list img").length;
		tar_gallery.timers["loadcount"]=0;
		$(seek).find(".tar_gallery_thumb_list a").each(function(){
			var mysrc=$(this).attr("src");
			$(this).attr("src","");
			$(this).attr("src",mysrc);
		}).bind("load readyStateChange",function(){
			tar_gallery.timers.loadcount=tar_gallery.timers.loadcount+1;
			if(tar_gallery.timers.loadcount==loadnum){
				tar_gallery.loaded(seek,overrides);
			}
		});*/
		$(seek).each(function(){
			var vars=tar_gallery.buildvars(this,"var","title","text",overrides);
			$(this).removeClass("not-loaded").data(vars);
			tar_gallery.setup(this,vars,seek);
		});
		tar_gallery.events(seek);
	},
	setup:function(elem,vars,seek){
		var myid=$(elem).attr("id");
		var thumbnum=$(elem).find(".tar_gallery_thumb_list .thumb").length;
		var hash=[];
		var setme;
		if(myid==""){ tar_gallery.addid(elem); }
		for(i=0;i<thumbnum;i++){ $(elem).find(".tar_gallery_thumb_list .thumb:eq("+i+")").attr("rel",i); }
		if(document.location.hash!=""){
			hash=document.location.hash.replace("#","").split("|");
			setme=tar_gallery.hashtest(hash,$(elem).attr("id"));
		}
		if(setme&&hash[setme]!=0){
			tar_gallery.data.ping($(elem).find(".thumb:eq("+hash[setme]+") a"),vars,seek);
		}else{
			tar_gallery.data.bringin(elem,$(elem).find(".tar_gallery_full_image img"));
		}
		tar_gallery.list.setup($(elem).find(".tar_gallery_thumb_list"),vars);
	},
	events:function(seek){
		$(seek).find(".tar_gallery_thumb_list a").live("click",function(){
			var vars=$(this).closest(seek).data();
			tar_gallery.hashset($(this).closest(".thumb"),seek);
			tar_gallery.data.ping(this,vars,seek);
			return false;
		});
		$(seek).find(".scroll-button").live("mousedown",function(){
			$(this).parent().find(".scroll-container").addClass("scrolling");
			var type=$(this).parent().find(".scroll-container").data("scrolltype");
			tar_gallery.list.scroll[type]($(this).parent().find(".scroll-container"),$(this).hasClass("next"),seek);
		}).live("mouseup",function(){
			tar_gallery.list.endscroll($(this).parent().find(".scroll-container"));
		});
	},
	list:{
		setup:function(elem,vars){
			var availablewidth=parseInt($(elem).width());
			var dim=tar_gallery.list.size(elem,vars.autosizethumbs);
			var scrollsize=(vars.scrolltype=="page")?availablewidth:10;
			var scrollwidth=(vars.scrolltype=="page")?dim[1]:dim[3];
			$(elem).wrapInner('<div class="scroll-container"><div class="scroller" style="padding:0 '+vars.padoffset+'px;width:'+scrollwidth+'px">');
			$(elem).find(".scroll-container").scrollLeft(vars.padoffset).data({"scrolltype":vars.scrolltype,"scrollsize":scrollsize,"totalpage":dim[2]-1,"curpage":0,"padoffset":vars.padoffset});
			if(dim[2]>1){
				$(elem).append('<div class="scroll-button prev"></div><div class="scroll-button next"></div>');
			}
		},
		size:function(elem,autosize){
			var dim=[0,0,0];
			var childtotal=$(elem).children().length;
			var childmaxwidth=0;
			var childmaxheight=0;
			var availablewidth=parseInt($(elem).width());
			var childtotalwidth=0;
			for(i=0;i<childtotal;i++){
				var dimwidth=parseInt($(elem).children(":eq("+i+")").outerWidth());
				var dimheight=parseInt($(elem).children(":eq("+i+")").outerHeight());
				childmaxwidth=(dimwidth>childmaxwidth)?dimwidth:childmaxwidth;
				childmaxheight=(dimheight>childmaxheight)?dimheight:childmaxheight;
				childtotalwidth+=dimwidth;
			}
			if(autosize){
				dim=tar_gallery.list.limit(childtotal,childmaxwidth,availablewidth,childtotalwidth);
				$(elem).find("a").css({"width":dim[0],"padding":0});
			}else{
				dim=["auto",(Math.ceil(childtotalwidth/availablewidth)*availablewidth),Math.ceil(childtotalwidth/availablewidth),childtotalwidth];
				$(elem).find("a").css({"width":dim[0],"padding-top":0,"padding-bottom":0});
			}
			$(elem).find("a").css({"height":childmaxheight+"px","line-height":childmaxheight+"px"});
			return dim;
		},
		limit:function(total,cwidth,twidth){
			var perpage=0;
			var wtest=twidth;
			var totalpage=1;
			var ptest=0;
			while(wtest>0){
				wtest=wtest-cwidth;
				if(wtest>0) perpage++;
			}
			if(total>perpage&&perpage!=0){
				while(ptest<total){
					ptest+=perpage;
					if(ptest<total) totalpage++;
				}
			}
			return [(twidth/perpage),(twidth*totalpage),totalpage];
		},
		scroll:{
			page:function(elem,direc,seek){
				var posinfo=$(elem).data();
				var offset=(direc)?1:-1;
				var reset=false;
				var delay=0;
				var gopage=posinfo.curpage+offset;
				if(gopage>posinfo.totalpage){
					if(posinfo.padoffset>0) var reset=(posinfo.curpage*posinfo.scrollsize)+(posinfo.padoffset*2);
					delay=400;
					gopage=0;
				}
				if(gopage<0){
					if(posinfo.padoffset>0) var reset=0;
					delay=400;
					gopage=posinfo.totalpage;
				}
				var goscroll=gopage*posinfo.scrollsize+posinfo.padoffset;
				if(reset!==false) $(elem).animate({scrollLeft:reset},delay);
				tar_gallery.list.scroll.scroll(elem,"animate",goscroll,delay);
				$(elem).data("curpage",gopage).removeClass("scrolling");
			},
			continuous:function(elem,direc,seek){
				var posinfo=$(elem).data();
				var offset=(direc)?1:-1;
				var myid=$(elem).closest(seek).attr("id");
				
				tar_gallery.timers[myid]=setInterval('tar_gallery.list.scroll.cdelegate("'+myid+'","step",'+posinfo.scrollsize+','+offset+','+0+')',50);
			},
			cdelegate:function(myid,method,scrollsize,offset,delay){
				if($("#"+myid).find(".scroll-container").hasClass("scrolling")){
					var goscroll=parseInt($("#"+myid).find(".scroll-container").scrollLeft())+(scrollsize*offset);
					tar_gallery.list.scroll.scroll($("#"+myid).find(".scroll-container"),method,goscroll,delay);
				}else{
					clearInterval(tar_gallery.timers[myid]);
				}
			},
			scroll:function(elem,method,goscroll,delay){
				if(method=="animate"){
					$(elem).animate({scrollLeft:goscroll}).delay(delay);
				}else{
					$(elem).scrollLeft(goscroll);
				}
			}
		},
		endscroll:function(elem){
			var posinfo=$(elem).data();
			if(posinfo.scrolltype=="continuous"){
				$(elem).removeClass("scrolling");
			}
		}
	},
	data:{
		size:function(elem){
			var curheight=$(elem).height();
			$(elem).css({"height":curheight+"px"});
			$(elem).children().fadeOut(300,function(){ $(this).remove(); });
		},
		ping:function(elem,vars,seek){
			tar_gallery.data.size($(elem).closest(seek).find(".tar_gallery_full_image .image"));
			$(elem).closest(seek).find(".tar_gallery_full_image").addClass("loading");
			var mykey=$(elem).attr("rel");
			var getpage=vars.ping+":r=dinfo&m="+vars.m+"&o="+vars.o+"&f="+vars.f+"&k="+mykey+"/";
			$.ajax({
				url:getpage,
				dataType:"json",
				success:function(json) {
					tar_gallery.data.append($(elem).closest(seek),vars,json);
				},
				error:function(){
					tar_gallery.data.error($(elem).closest(seek),vars);
				}
			});
		},
		schemas:{
			"tags":function(elem,vars,node,k){
				$(elem).find("."+k).empty().append("<p>"+node[k]+"</p>");
			},
			"default":function(elem,vars,node,k){
				$(elem).find("."+k).empty().append(node[k]);
				//$(elem).find("."+k).wrapInner("<span>");
				//$(elem).find("."+k).children("span").fadeOut(function(){ $(this).before(node[k]);$(this).remove(); });
			}
		},
		append:function(elem,vars,node){
			var fullimgnode='<img src="'+vars.path+'/'+vars.category+'/'+vars.fullsize+'/'+node.image+'" class="loading-image" style="position:absolute;top:-9001px"/>';
			$(elem).find(".tar_gallery_full_image .image").append(fullimgnode);
			$(elem).find(".loading-image").bind('load readystatechange',function(){
				delete node.image;
				tar_gallery.data.loop(elem,vars,node);
				tar_gallery.data.bringin(elem,$(this));
			});
		},
		bringin:function(elem,img){
			var myheight=$(img).outerHeight();
			$(elem).find(".tar_gallery_full_image").removeClass("loading");
			$(elem).find(".tar_gallery_full_image .image").animate({"height":myheight+"px"},400,function(){					
				$(img).removeClass("loading-image").removeAttr("style").hide().fadeIn(300);
			});
		},
		loop:function(elem,vars,node){
			for(k in node){
				var go=(k in tar_gallery.data.schemas)?k:"default";
				tar_gallery.data.schemas[go](elem,vars,node,k);
			}
		},
		error:function(elem,vars){
			$(elem).find(".tar_gallery_full_image").removeClass("loading");
			$(elem).find(".tar_gallery_full_image .image").append("<p>We're sorry. The image could not be loaded.</p>");
		}
	},
	buildvars:function(elem,node,k,v,overrides){
		var vars=this.fetch("defaults");
		for (i=0;i<$(elem).children(node).length;i++){
			var key=$(elem).children(node+":eq("+i+")").attr(k);
			var val=(v=="text")?$(elem).children(node+":eq("+i+")").text():$(elem).children(node+":eq("+i+")").attr(v);
			if(val=="true"||val=="false"){ val=this.bool(val); }
			vars[key]=val;
		}
		if(overrides){
			for(k in overrides){ vars[k]=overrides[k]; }
		}
		return vars;
	},
	bool:function(val){
		switch(val){
			case "true": return true; break;
			case "false": return false; break;
			case true: return true; break;
			case false: return false; break;
			default: return parseInt(val);
		}
	},
	addid:function(elem){
		var galid="tar_gallery_"
		var increm=0;
		while($("#"+galid+increm).length>0){
			increm++;
		}
		$(elem).attr("id",galid+increm);
	},
	hashset:function(elem,seek){
		var hash=document.location.hash.replace("#","");
		var hasharr=hash.split("|");
		var myid=$(elem).closest(seek).attr("id");
		var mypos=tar_gallery.hashtest(hasharr,myid);
		var mynum=$(elem).attr("rel");
		if(mypos){
			hasharr[mypos]=mynum;
			document.location.hash=hasharr.join("|");
		}else{
			var leadin=(hash)?hash+"|":"#";
			document.location.hash=leadin+myid+"|"+mynum;
		}
	},
	hashtest:function(hash,seek){
		var testor=hash;
		var tosender=false;
		for(i=0;i<testor.length;i++){
			if(testor[i]==seek){
				tosender=i+1;
			}
		}
		return tosender;
	}
}

$(window).load(function(){
	tar_gallery.init();
});
/*
function swapfullimg(aelem){
	var newimg = $(aelem).attr("href");
	var curimg = $("#prod_full_img").attr("src");
	$("#prod_full_img_wrapper").prepend('<div id="prod_swap_img_wrap" class="centering-wrap"><img src="' + curimg + '" id="prod_swap_img"/></div><div id="prod_img_loader"><img src="/_layouts/default/images/loadinganim.gif"/></div>');
	$("#prod_full_img").attr("src",newimg);
	$("#prod_full_img").load(function(){
		$("#prod_img_loader").remove();
		$("#prod_swap_img_wrap").fadeOut(500,function(){ $(this).remove(); });
	});
	$(aelem).blur();
	return false;
}

function addimgsetup(){
	imgtotal = $(".additional_images img").length;
	imgcount = 0;
	imglengths = 0;
	imgwrap = parseInt($(".additional_images .additional_images_wrap").width());
	$(".additional_images img").each(function(){
		$(this).bind('load readystatechange',function(){
			imgcount++;
			thislength = parseInt($(this).parent().outerWidth())
			imglengths = imglengths + thislength;
			if (imgcount == imgtotal) {
				addimgevents(imglengths,imgwrap);
			}
		});
		var src = this.src;
        this.src = '#';
        this.src = src;
	});
}

function addimgevents(finalwidth,criticalwidth) {
	if (finalwidth > criticalwidth) {
		$(".additional_images").append('<div class="additional_list_scroller scrollleft"></div><div class="additional_list_scroller scrollright"></div>');
		$(".additional_images .additional_list_scroller").hover(function(){
			if ($(this).hasClass("scrollleft")) {
				scrollingtimer = setInterval('scrolladdlist("left")',10);
			} else {
				scrollingtimer = setInterval('scrolladdlist("right")',10);
			}
		},function(){
			clearInterval(scrollingtimer);
		});
	}
	$(".additional_images a").click(function(){
		return swapfullimg(this);
	});
}

function scrolladdlist(direc) {
	curoffset = $(".additional_images .additional_images_wrap").scrollLeft();
	modoffset = (direc == "right") ? 10 : -10;
	$(".additional_images .additional_images_wrap").scrollLeft(curoffset + modoffset);
}
*/
