
jQuery.noConflict()

var featuredcontentglider={
	leftrightkeys: [37, 39], //Keycodes to move glider back and forth 1 slide, respectively. Enter [] to disable feature.
	csszindex: 100,
	ajaxloadingmsg: '<b>Cargando Noticias. Favor de Esperar...</b>',

	glide:function(config, showpage, isprev){
		var selected=parseInt(showpage)
		if (selected>=config.$contentdivs.length){ //If no Content exists at this Index Position.
			alert("No Content exists at Page "+(selected+1)+"! Loading 1st Page Instead.")
			selected=0
		}
		var $target=config.$contentdivs.eq(selected)
		//Test for toggler not being Initialized yet, or User clicks on the currently Selected Page):
		if (config.$togglerdiv.attr('lastselected')==null || parseInt(config.$togglerdiv.attr('lastselected'))!=selected){
			var $selectedlink=config.$toc.eq(selected)
			//Comportamiento de los Botones Anterior y Siguiente: Cuantas Slides se Cambian.
			config.nextslideindex=(selected<config.$contentdivs.length-1)? selected+1 : 0
			config.prevslideindex=(selected==0)? config.$contentdivs.length-1 : selected-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('selected')
			$selectedlink.addClass('selected')
			config.$togglerdiv.attr('lastselected', selected+'pg')
		}
	},

	getremotecontent:function($, config){
		config.$glider.html(this.ajaxloadingmsg)
		$.ajax({
			url: config.remotecontent,
			error:function(ajaxrequest){
				config.$glider.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config.$glider.html(content)
				featuredcontentglider.setuptoggler($, config)
			}
		})
	},

	aligncontents:function($, config){
		config.$contentdivs=$("#"+config.gliderid+" ."+config.contentclass)
		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.togglerid+" .next, #"+config.togglerid+" .prev")
		$nextandprev.click(function(event){ //Assign Click Behavior to 'Next' and 'Prev' Links.
			featuredcontentglider.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.
			featuredcontentglider.glide(config, this.getAttribute('pagenumber'))
			event.preventDefault()
		})
		config.$togglerdiv.fadeIn(500, function(){
			featuredcontentglider.glide(config, config.selected)
			if (config.autorotate==true){ //Auto Rotate Contents?
				config.stepcount=0 //Set Steps taken.
				//Total Steps Limit: Num of Contents x Num of User Specified Cycles.
				config.totalsteps=config.$contentdivs.length*config.autorotateconfig[1]
				featuredcontentglider.autorotate(config)
			}
		})
		config.$togglerdiv.click(function(){
			featuredcontentglider.cancelautorotate(config.togglerid)
		})
		if (this.leftrightkeys.length==2){
			$(document).bind('keydown', function(e){
				featuredcontentglider.keyboardnav(config, e.keyCode)
			})
		}
	},

	autorotate:function(config){
		var rotatespeed=config.speed+config.autorotateconfig[0]
		window[config.togglerid+"timer"]=setInterval(function(){
			if (config.totalsteps>0 && config.stepcount>=config.totalsteps){
				clearInterval(window[config.togglerid+"timer"])
			}
			else{
				featuredcontentglider.glide(config, config.nextslideindex, "next")
				config.stepcount++
			}
		}, rotatespeed)
	},

	cancelautorotate:function(togglerid){
		if (window[togglerid+"timer"])
			clearInterval(window[togglerid+"timer"])
	},

	keyboardnav:function(config, keycode){
		if (keycode==this.leftrightkeys[0])
			featuredcontentglider.glide(config, config.prevslideindex, "previous")
		else if (keycode==this.leftrightkeys[1])
			featuredcontentglider.glide(config, config.nextslideindex, "next")
		if (keycode==this.leftrightkeys[0] || keycode==this.leftrightkeys[1])
			featuredcontentglider.cancelautorotate(config.togglerid)
	},

	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
	},

	init:function(config){
		jQuery(document).ready(function($){
			config.$glider=$("#"+config.gliderid)
			config.$togglerdiv=$("#"+config.togglerid)
			config.$toc=config.$togglerdiv.find('.toc')
			config.$next=config.$togglerdiv.find('.next')
			config.$prev=config.$togglerdiv.find('.prev')
			config.$prev.attr('buttontype', 'previous')
			var selected=(config.persiststate)? featuredcontentglider.getCookie(config.gliderid) : config.selected
			config.selected=(isNaN(parseInt(selected))) ? config.selected : selected //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.remotecontent!="undefined" && config.remotecontent.length>0)
				featuredcontentglider.getremotecontent($, config)
			else
				featuredcontentglider.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)
					featuredcontentglider.setCookie(config.gliderid, config.$togglerdiv.attr('lastselected'))
				config=null
				
			})
		})
	}
}
