//Step Carousel Viewer: By Dynamic Drive, at http://www.dynamicdrive.com
//Created: March 19th, 08'

jQuery.noConflict()

var stepcarousel_1={
	ajaxloadingmsg: '<div style="margin: 1em; font-weight: bold"><img src="ajaxloadr.gif" style="vertical-align: middle" /> Fetching Content. Please wait...</div>', //customize HTML to show while fetching Ajax content
	configholder: {},

	getCSSValue:function(val){ //Returns either 0 (if val contains 'auto') or val as an integer
		return (val=="auto")? 0 : parseInt(val)
	},

	getremotepanel_1s:function($, config){ //function to fetch external page containing the panel_1 DIVs
		config.$belt_1.html(this.ajaxloadingmsg)
		$.ajax({
			url: config.contenttype[1], //path to external content
			async: true,
			error:function(ajaxrequest){
				config.$belt_1.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config.$belt_1.html(content)
				config.$panel_1s=config.$gallery.find('.'+config.panel_1class)
				stepcarousel_1.alignpanel_1s($, config)
			}
		})
	},

	alignpanel_1s:function($, config){
		var panel_1offset=0
		config.panel_1offsets=[panel_1offset] //array to store upper left offset of each panel_1 (1st element=0)
		config.panel_1widths=[] //array to store widths of each panel_1
		config.$panel_1s.each(function(index){ //loop through panel_1s
			var $currentpanel_1=$(this)
			$currentpanel_1.css({float: 'none', position: 'absolute', top: panel_1offset+'px'}) //position panel_1
			$currentpanel_1.bind('click', function(e){return config.onpanel_1click(e.target)}) //bind onpanel_1click() to onclick event
			panel_1offset+=stepcarousel_1.getCSSValue($currentpanel_1.css('marginRight')) + parseInt($currentpanel_1.get(0).offsetHeight) //calculate next panel_1 offset
			config.panel_1offsets.push(panel_1offset) //remember this offset
			config.panel_1widths.push(panel_1offset-config.panel_1offsets[config.panel_1offsets.length-2]) //remember panel_1 width
		})
		config.panel_1offsets.pop() //delete last offset (redundant)
		config.$belt_1.css({height: panel_1offset+'px'}) //Set belt_1 DIV to total panel_1s' widths
		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_1.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.panel_1offsets.length-1)
		var endpoint=config.panel_1offsets[pindex]+(pindex==0? 0 : config.belt_1offset)
		config.$belt_1.animate({top: -endpoint+'px'}, 'slow', function(){config.onslideaction(this)})
		config.currentpanel_1=pindex
		this.statusreport(galleryid)
	},

	stepBy_1:function(galleryid, steps){
		var config=stepcarousel_1.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' //If "steps" is negative, that means backwards
		var pindex=config.currentpanel_1+steps //index of panel_1 to stop at
		//var sg = config.panel_1offsets.length - 9
		//if(pindex>=sg){pindex = config.panel_1offsets.length }
		
		pindex=(pindex>config.panel_1offsets.length-1 || pindex<0 && pindex-steps>0)? 0 : (pindex<0)? config.panel_1offsets.length+steps : pindex //take into account end or starting panel_1 and step direction 
		var endpoint=config.panel_1offsets[pindex]+(pindex==0? 0 : config.belt_1offset) //left distance for belt_1 DIV to travel to
		if (pindex==0 && direction=='forward' || config.currentpanel_1==0 && direction=='back'){ //decide whether to apply "push pull" effect
			config.$belt_1.animate({top: -config.panel_1offsets[config.currentpanel_1]-(direction=='forward'? 500 : -500)+'px'}, 'normal', function(){
				config.$belt_1.animate({top: -endpoint+'px'}, 'slow', function(){config.onslideaction(this)})
			})
		}
		else
			config.$belt_1.animate({top: -endpoint+'px'}, 'slow', function(){config.onslideaction(this)})
		config.currentpanel_1=pindex
		this.statusreport(galleryid)
	},

	statusreport:function(galleryid){
		var config=stepcarousel_1.configholder[galleryid]
		var startpoint=config.currentpanel_1 //index of first visible panel_1 
		var visiblewidth=0
		for (var endpoint=startpoint; endpoint<config.panel_1offsets.length; endpoint++){ //index (endpoint) of last visible panel_1
			visiblewidth+=config.panel_1widths[endpoint]
			if (visiblewidth>config.gallerywidth){
				break
			}
		}
		startpoint+=1 //format startpoint for user friendiness
		endpoint=(endpoint+1==startpoint)? startpoint : endpoint //If only one image visible on the screen and partially hidden, set endpoint to startpoint
		var valuearray=[startpoint, endpoint, config.panel_1widths.length]
		for (var i=0; i<config.statusvars.length; i++){
			window[config.statusvars[i]]=valuearray[i] //Define variable (with user specified name) and set to one of the status values
			config.$statusobjs[i].html(valuearray[i]) //Populate element on page with ID="user specified name" with one of the status values
		}
	},

	setup:function(config){
		//Disable Step Gallery scrollbars ASAP dynamically (enabled for sake of users with JS disabled)
		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_1=config.$gallery.find('.'+config.belt_1class) //Find belt_1 DIV that contains all the panel_1s
			config.$panel_1s=config.$gallery.find("."+config.panel_1class) //Find panel_1 DIVs that each contain a slide
			config.onpanel_1click=(typeof config.onpanel_1click=="undefined")? function(target){} : config.onpanel_1click //attach custom "onpanel_1click" event handler
			config.onslideaction=(typeof config.onslide=="undefined")? function(){} : function(belt_1obj){$(belt_1obj).stop(); config.onslide()} //attach custom "onslide" event handler
			config.oninit=(typeof config.oninit=="undefined")? function(){} : config.oninit //attach custom "oninit" event handler
			config.belt_1offset=stepcarousel_1.getCSSValue(config.$belt_1.css('marginLeft')) //Find length of belt_1 DIV's left margin
			config.statusvars=config.statusvars || []  //get variable names that will hold "start", "end", and "total" slides info
			config.$statusobjs=[$('#'+config.statusvars[0]), $('#'+config.statusvars[1]), $('#'+config.statusvars[2])]
			config.currentpanel_1=0
			stepcarousel_1.configholder[config.galleryid]=config //store config parameter as a variable
			if (config.contenttype[0]=="ajax" && typeof config.contenttype[1]!="undefined") //fetch ajax content?
				stepcarousel_1.getremotepanel_1s($, config)
			else
				stepcarousel_1.alignpanel_1s($, config) //align panel_1s and initialize gallery
		}) //end document.ready
		jQuery(window).bind('unload', function(){ //clean up
			jQuery.each(config, function(ai, oi){
				oi=null
			})
			config=null
		})
	}
}


