//Featured Content Glider: By http://www.dynamicdrive.com
//Created: Dec 22nd, 07'
//Updated (Jan 29th, 08): Added four possible slide directions: "updown", "downup", "leftright", or "rightleft"
//Updated (Feb 1st, 08): Changed glide behavior to reverse direction when previous button is clicked
//Updated (Feb 12th, 08): Added ability to retrieve gliding contents from an external file using Ajax ("remotecontentrr" variable added to configuration)
//Updated (July 21st, 09): Updated to work in jQuery 1.3.x
//Updated (Dec 13th, 09): Added keyboard navigation, so left/ right arrow keys now move glider. Fixed bug with auto rotation when "next" link isn't defined.

jQuery.noConflict()

var featuredcontentgliderr={
	leftrightkeys: [37, 39], //keycodes to move glider back and forth 1 slide, respectively. Enter [] to disable feature.
	csszindex: 100,
	ajaxloadingmsg: '<b>Fetching Content. Please wait...</b>',

	glide:function(config, showpage, isprev){
		var selectedr=parseInt(showpage)
		if (selectedr>=config.$contentdivs.length){ //if no content exists at this index position
			alert("No content exists at page "+(selectedr+1)+"! Loading 1st page instead.")
			selectedr=0
		}
		var $target=config.$contentdivs.eq(selectedr)
		//Test for toggler not being initialized yet, or user clicks on the currently selectedr page):
		if (config.$togglerdiv.attr('lastselectedr')==null || parseInt(config.$togglerdiv.attr('lastselectedr'))!=selectedr){
			var $selectedrlink=config.$toc.eq(selectedr)
			config.nextslideindex=(selectedr<config.$contentdivs.length-1)? selectedr+1 : 0
			config.prevslideindex=(selectedr==0)? config.$contentdivs.length-1 : selectedr-1
			config.$next.attr('loadpage', config.nextslideindex+"pg") //store slide index to go to when "next" link is clicked on
			config.$prev.attr('loadpage', config.prevslideindex+'pg')
			var startpoint=(isprev=="previous")? -config.startpoint : config.startpoint
			$target.css(config.leftortop, startpoint).css("zIndex", this.csszindex++) //hide content so it's just out of view before animating it
			var endpoint=(config.leftortop=="left")? {left:0} : {top:0} //animate it into view
			$target.animate(endpoint, config.speed)
			config.$toc.removeClass('selectedr')
			$selectedrlink.addClass('selectedr')
			config.$togglerdiv.attr('lastselectedr', selectedr+'pg')
		}
	},

	getremotecontentrr:function($, config){
		config.$glider.html(this.ajaxloadingmsg)
		$.ajax({
			url: config.remotecontentrr,
			error:function(ajaxrequest){
				config.$glider.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config.$glider.html(content)
				featuredcontentgliderr.setuptoggler($, config)
			}
		})
	},

	aligncontents:function($, config){
		config.$contentdivs=$("#"+config.glideridr+" ."+config.contentclassr)
		config.$contentdivs.css(config.leftortop, config.startpoint).css({height: config.$glider.height(), visibility: 'visible'}) //position content divs so they're out of view:
	},

	setuptoggler:function($, config){
		this.aligncontents($, config)
		config.$togglerdiv.hide()
		config.$toc.each(function(index){
				$(this).attr('pagenumber', index+'pg')
				if (index > (config.$contentdivs.length-1))
					$(this).css({display: 'none'}) //hide redundant "toc" links
		})
		var $nextandprev=$("#"+config.toggleridr+" .next, #"+config.toggleridr+" .prev")
		$nextandprev.click(function(event){ //Assign click behavior to 'next' and 'prev' links
			featuredcontentgliderr.glide(config, this.getAttribute('loadpage'), this.getAttribute('buttontype'))
			event.preventDefault() //cancel default link action
		})
		config.$toc.click(function(event){ //Assign click behavior to 'toc' links
			featuredcontentgliderr.glide(config, this.getAttribute('pagenumber'))
			event.preventDefault()
		})
		config.$togglerdiv.fadeIn(1000, function(){
			featuredcontentgliderr.glide(config, config.selectedr)
			if (config.autorotate==true){ //auto rotate contents?
				config.stepcount=0 //set steps taken
				config.totalsteps=config.$contentdivs.length*config.autorotateconfig[1] //Total steps limit: num of contents x num of user specified cycles)
				featuredcontentgliderr.autorotate(config)
			}
		})
		config.$togglerdiv.click(function(){
			featuredcontentgliderr.cancelautorotate(config.toggleridr)
		})
		if (this.leftrightkeys.length==2){
			$(document).bind('keydown', function(e){
				featuredcontentgliderr.keyboardnav(config, e.keyCode)
			})
		}
	},

	autorotate:function(config){
		var rotatespeed=config.speed+config.autorotateconfig[0]
		window[config.toggleridr+"timer"]=setInterval(function(){
			if (config.totalsteps>0 && config.stepcount>=config.totalsteps){
				clearInterval(window[config.toggleridr+"timer"])
			}
			else{
				featuredcontentgliderr.glide(config, config.nextslideindex, "next")
				config.stepcount++
			}
		}, rotatespeed)
	},

	cancelautorotate:function(toggleridr){
		if (window[toggleridr+"timer"])
			clearInterval(window[toggleridr+"timer"])
	},

	keyboardnav:function(config, keycode){
		if (keycode==this.leftrightkeys[0])
			featuredcontentgliderr.glide(config, config.prevslideindex, "previous")
		else if (keycode==this.leftrightkeys[1])
			featuredcontentgliderr.glide(config, config.nextslideindex, "next")
		if (keycode==this.leftrightkeys[0] || keycode==this.leftrightkeys[1])
			featuredcontentgliderr.cancelautorotate(config.toggleridr)
	},

	getCookie:function(Name){ 
		var re=new RegExp(Name+"=[^;]+", "i") //construct RE to search for target name/value pair
		if (document.cookie.match(re)) //if cookie found
			return document.cookie.match(re)[0].split("=")[1] //return its value
		return null
	},

	setCookie:function(name, value){
		document.cookie = name+"="+value
	},

	initr:function(config){
		jQuery(document).ready(function($){
			config.$glider=$("#"+config.glideridr)
			config.$togglerdiv=$("#"+config.toggleridr)
			config.$toc=config.$togglerdiv.find('.toc')
			config.$next=config.$togglerdiv.find('.next')
			config.$prev=config.$togglerdiv.find('.prev')
			config.$prev.attr('buttontype', 'previous')
			var selectedr=(config.persiststate)? featuredcontentgliderr.getCookie(config.glideridr) : config.selectedr
			config.selectedr=(isNaN(parseInt(selectedr))) ? config.selectedr : selectedr //test for cookie value containing null (1st page load) or "undefined" string	
			config.leftortop=(/up/i.test(config.direction))? "top" : "left" //set which CSS property to manipulate based on "direction"
			config.heightorwidth=(/up/i.test(config.direction))? config.$glider.height() : config.$glider.width() //Get glider height or width based on "direction"
			config.startpoint=(/^(left|up)/i.test(config.direction))? -config.heightorwidth : config.heightorwidth //set initial position of contents based on "direction"
			if (typeof config.remotecontentrr!="undefined" && config.remotecontentrr.length>0)
				featuredcontentgliderr.getremotecontentrr($, config)
			else
				featuredcontentgliderr.setuptoggler($, config)
			$(window).bind('unload', function(){ //clean up and persist
				config.$togglerdiv.unbind('click')
				config.$toc.unbind('click')
				config.$next.unbind('click')
				config.$prev.unbind('click')
				if (config.persiststate)
					featuredcontentgliderr.setCookie(config.glideridr, config.$togglerdiv.attr('lastselectedr'))
				config=null
				
			})
		})
	}
}

