var stepcarousel={
	ajaxloadingmsg: '<div style="margin: 1em; font-weight: bold"> Fetching Content. Please wait...</div>', 
	configholder: {},
	getCSSValue:function(val){ 
		return (val=="auto")? 0 : parseInt(val)
	},
	getremotepanels:function($, config){ 
		config.$belt.html(this.ajaxloadingmsg)
		$.ajax({
			url: config.contenttype[1], 
			async: true,
			error:function(ajaxrequest){
				config.$belt.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config.$belt.html(content)
				config.$panels=config.$gallery.find('.'+config.panelclass)
				stepcarousel.alignpanels($, config)
			}
		})
	},
	alignpanels:function($, config){
		var paneloffset=0
		config.paneloffsets=[paneloffset] 
		config.panelwidths=[] 
		config.$panels.each(function(index){ 
			var $currentpanel=$(this)
				$currentpanel.bind('click', function(e){return config.onpanelclick(e.target)}) 
				paneloffset+=340;  
			config.paneloffsets.push(paneloffset) 
			config.panelwidths.push(paneloffset-config.paneloffsets[config.paneloffsets.length-2]) 
		})
		config.paneloffsets.pop() 
		config.$belt.css({width: paneloffset+'px'}) 
		this.statusreport(config.galleryid)
		config.oninit()
		config.onslideaction(this)
	},
	stepTo:function(galleryid, pindex){ /*User entered pindex starts at 1 for intuitiveness. Internally pindex still starts at 0 */
		var config=stepcarousel.configholder[galleryid]
		if (typeof config=="undefined"){
			alert("There's an error with your set up of Carousel Viewer \""+galleryid+ "\"!")
			return
		}
		var pindex=Math.min(pindex-1, config.paneloffsets.length-1)
		var endpoint=config.paneloffsets[pindex]+(pindex==0? 0 : config.beltoffset)
		config.$belt.animate({left: -endpoint+'px'}, 'slow', function(){config.onslideaction(this)})
		config.currentpanel=pindex
		this.statusreport(galleryid)
	},
	stepBy:function(galleryid, steps){
		var config=stepcarousel.configholder[galleryid]
		if (typeof config=="undefined"){
			alert("There's an error with your set up of Carousel Viewer \""+galleryid+ "\"!")
			return
		}
		var direction=(steps>0)? 'forward' : 'back' 
		var pindex=config.currentpanel+steps 
		pindex=(pindex>config.paneloffsets.length-1 || pindex<0 && pindex-steps>0)? 0 : (pindex<0)? config.paneloffsets.length+steps : pindex 
		var endpoint=config.paneloffsets[pindex]+(pindex==0? 0 : config.beltoffset) 
		
		if (pindex==0 && direction=='forward' || config.currentpanel==0 && direction=='back'){ 
			config.$belt.animate({left: -config.paneloffsets[config.currentpanel]-(direction=='forward'? 100 : -30)+'px'}, 'normal', function(){
				config.$belt.animate({left: -endpoint+'px'}, 'slow', function(){config.onslideaction(this)})
			})
		}
		else
			config.$belt.animate({left: -endpoint+'px'}, 'slow', function(){config.onslideaction(this)})
		config.currentpanel=pindex
		this.statusreport(galleryid)
	},
	statusreport:function(galleryid){
		var config=stepcarousel.configholder[galleryid]
		var startpoint=config.currentpanel 
		var visiblewidth=0
		for (var endpoint=startpoint; endpoint<config.paneloffsets.length; endpoint++){ 
			visiblewidth+=config.panelwidths[endpoint]
			if (visiblewidth>config.gallerywidth){
				break
			}
		}
		startpoint+=1 
		endpoint=(endpoint+1==startpoint)? startpoint : endpoint 
		var valuearray=[startpoint, endpoint, config.panelwidths.length]
		for (var i=0; i<config.statusvars.length; i++){
			window[config.statusvars[i]]=valuearray[i] 
			config.$statusobjs[i].html(valuearray[i]) 
		}
	},
	setup:function(config){
		
		document.write('<style type="text/css">\n#'+config.galleryid+'{overflow: hidden;}\n</style>')
		jQuery(document).ready(function($){
			config.$gallery=$('#'+config.galleryid)
			config.gallerywidth=config.$gallery.width()
			config.$belt=config.$gallery.find('.'+config.beltclass) 
			config.$panels=config.$gallery.find('.'+config.panelclass) 
			config.onpanelclick=(typeof config.onpanelclick=="undefined")? function(target){} : config.onpanelclick 
			config.onslideaction=(typeof config.onslide=="undefined")? function(){} : function(beltobj){$(beltobj).stop(); config.onslide()} 
			config.oninit=(typeof config.oninit=="undefined")? function(){} : config.oninit 
			config.beltoffset=stepcarousel.getCSSValue(config.$belt.css('marginLeft')) 
			config.statusvars=config.statusvars || []  
			config.$statusobjs=[$('#'+config.statusvars[0]), $('#'+config.statusvars[1]), $('#'+config.statusvars[2])]
			config.currentpanel=0
			stepcarousel.configholder[config.galleryid]=config 
			if (config.contenttype[0]=="ajax" && typeof config.contenttype[1]!="undefined") 
				stepcarousel.getremotepanels($, config)
			else
				stepcarousel.alignpanels($, config) 
		}) 
		jQuery(window).bind('unload', function(){ 
			jQuery.each(config, function(ai, oi){
				oi=null
			})
			config=null
		})
	}
}
