tar_gallery={
	defaults:{
		"path":"/imgs/", //String, path to the top level image folder
		"category":"", //String, subfolder structure to the category of images
		"fullsize":"400", //String, name of the folder that contains the images you want to use for fullsize images
		"padoffset":0, //Integer for how much padding room at beginning and end of list for animation snap-back
		"autosizethumbs":false, //Boolean, "true" automatically resizes thumbnail containers so an equal number fit on all full pages
		"scrolltype":"continuous", //String, continuous or page
		"scrollstep":10, //Integer, milliseconds between continuous scroll steps
		"scrolloffset":8, //Integer, the number of pixels the container should move in a single continuous scroll step
		"quickload":false, //Boolean, "true" stops images beyond the first page from loading until the user scrolls them into view
		"qloffset":100 //Integer, number of pixels before a thumbnail is scrolled into view that loading should start
	},
	fetch:function(get){
		return this[get];
	},
	urlhash:"",
	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;
		}}}
		$(seek).each(function(){
			var vars=tar_gallery.buildvars(this,"var","title","text",overrides);
			$(this).removeClass("not-loaded").data(vars);
			if(vars.quickload) tar_gallery.quickload.init(this);
			tar_gallery.setup(this,vars,seek);
		});
		tar_gallery.events(seek);
	},
	quickload:{
		init:function(elem){
			$(elem).find(".thumb img").each(function(){
				$(this).data("src",$(this).attr("src")).removeAttr("src");
				$(this).parent().bind("mouseenter",function(){
					tar_gallery.quickload.show($(this).children("img"));
				});
			});
		},
		show:function(elem){
			$(elem).attr("src",$(elem).data("src"));
		}
	},
	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.hash.test(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.hash.set($(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:vars.scrolloffset;
			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,"scrollstep":vars.scrollstep,"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+')',posinfo.scrollstep);
			},
			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);
	},
	hash:{
		set:function(elem,seek){
			var hash=document.location.hash.replace("#","");
			var hasharr=hash.split("|");
			var myid=$(elem).closest(seek).attr("id");
			var mypos=tar_gallery.hash.test(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;
			}
		},
		test: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;
		},
		nav:function(){
			alert();
		}
	}
}

$(document).ready(function(){
	tar_gallery.init();
});
