<!--jQuery.noConflict();-->

var featuredcontentglider={
	csszindex: 100,
	ajaxloadingmsg: '<b>Загрузка...</b>',
	glide:function(config1, showpage1, isprev1){
		var selected=parseInt(showpage1)
		if (selected>=config1.$contentdivs1.length){ //if no content exists at this index position
			alert("No content exists at page "+(selected+1)+"! Loading 1st page instead.")
			selected=0
		}
		var $target1=config1.$contentdivs1.eq(selected)
		//Test for toggler not being initialized yet, or user clicks on the currently selected page):
		if (config1.$togglerdiv.attr('lastselected')==null || parseInt(config1.$togglerdiv.attr('lastselected'))!=selected){
			var $selectedlink1=config1.$toc.eq(selected)
			config1.$next.attr('loadpage', (selected<config1.$contentdivs1.length-1)? selected+1+'pg' : 0+'pg')
			config1.$prev.attr('loadpage', (selected==0)? config1.$contentdivs1.length-1+'pg' : selected-1+'pg')
			var startpoint=(isprev1=="previous")? -config1.startpoint : config1.startpoint
			$target1.css(config1.leftortop, startpoint).css("zIndex", this.csszindex++) //hide content so it's just out of view before animating it
			var endpoint=(config1.leftortop=="left")? {left:0} : {top:0} //animate it into view
			$target1.animate(endpoint, config1.speed)
			config1.$toc.removeClass('selected')
			$selectedlink1.addClass('selected')
			config1.$togglerdiv.attr('lastselected', selected+'pg')
		}
	},

	getremotecontent:function($, config1){
		config1.$glider.html(this.ajaxloadingmsg)
		$.ajax({
			url: config1.remotecontent,
			error:function(ajaxrequest){
				config1.$glider.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config1.$glider.html(content)
				featuredcontentglider.setuptoggler1($, config1)
			}
		})
	},

	aligncontents1:function($, config1){
		config1.$contentdivs1=$("#"+config1.gliderid+" ."+config1.contentclass)
		config1.$contentdivs1.css(config1.leftortop, config1.startpoint).css({height: config1.$glider.height(), visibility: 'visible'}) //position content divs so they're out of view:
	},

	setuptoggler1:function($, config1){
		this.aligncontents1($, config1)
		config1.$togglerdiv.hide()
		config1.$toc.each(function(index){
				$(this).attr('pagenumber1', index+'pg')
				if (index > (config1.$contentdivs1.length-1))
					$(this).css({display: 'none'}) //hide redundant "toc" links
		})
		var $nextandprev=$("#"+config1.togglerid+" .next, #"+config1.togglerid+" .prev")
		$nextandprev.click(function(event){ //Assign click behavior to 'next' and 'prev' links
			featuredcontentglider.glide(config1, this.getAttribute('loadpage'), this.getAttribute('buttontype'))
			event.preventDefault() //cancel default link action
		})
		config1.$toc.click(function(event){ //Assign click behavior to 'toc' links
			featuredcontentglider.glide(config1, this.getAttribute('pagenumber1'))
			event.preventDefault()
		})
		config1.$togglerdiv.fadeIn(1000, function(){
			featuredcontentglider.glide(config1, config1.selected)
			if (config1.autorotate1==true){ //auto rotate contents?
				config1.stepcount=0 //set steps taken
				config1.totalsteps=config1.$contentdivs1.length*config1.autorotate1config1[1] //Total steps limit: num of contents x num of user specified cycles)
				featuredcontentglider.autorotate1(config1)
			}
		})
		config1.$togglerdiv.click(function(){
			featuredcontentglider.cancelautorotate1(config1.togglerid)
		})
	},

	autorotate1:function(config1){
		var rotatespeed=config1.speed+config1.autorotate1config1[0]
		window[config1.togglerid+"timer"]=setInterval(function(){
			if (config1.totalsteps>0 && config1.stepcount>=config1.totalsteps){
				clearInterval(window[config1.togglerid+"timer"])
			}
			else{
				config1.$next.triggerHandler('click')
				config1.stepcount++
			}
		}, rotatespeed)
	},

	cancelautorotate1:function(togglerid){
		if (window[togglerid+"timer"])
			clearInterval(window[togglerid+"timer"])
	},

	getCookie1:function(Name1){
		var re=new RegExp(Name1+"=[^;]+", "i") //construct RE to search for target1 Name1/value pair
		if (document.cookie.match(re)) //if cookie found
			return document.cookie.match(re)[0].split("=")[1] //return its value
		return null
	},

	setCookie1:function(Name1, value){
		document.cookie = Name1+"="+value
	},

	init:function(config1){
		jQuery(document).ready(function($){
			config1.$glider=$("#"+config1.gliderid)
			config1.$togglerdiv=$("#"+config1.togglerid)
			config1.$toc=config1.$togglerdiv.find('.toc')
			config1.$next=config1.$togglerdiv.find('.next')
			config1.$prev=config1.$togglerdiv.find('.prev')
			config1.$prev.attr('buttontype', 'previous')
			var selected=(config1.persiststate)? featuredcontentglider.getCookie1(config1.gliderid) : config1.selected
			config1.selected=(isNaN(parseInt(selected))) ? config1.selected : selected //test for cookie value containing null (1st page load) or "undefined" string
			config1.leftortop=(/up/i.test(config1.direction))? "top" : "left" //set which CSS property to manipulate based on "direction"
			config1.heightorwidth=(/up/i.test(config1.direction))? config1.$glider.height() : config1.$glider.width() //Get glider height or width based on "direction"
			config1.startpoint=(/^(left|up)/i.test(config1.direction))? -config1.heightorwidth : config1.heightorwidth //set initial position of contents based on "direction"
			if (typeof config1.remotecontent!="undefined" && config1.remotecontent.length>0)
				featuredcontentglider.getremotecontent($, config1)
			else
				featuredcontentglider.setuptoggler1($, config1)
			$(window).bind('unload', function(){ //clean up and persist
				config1.$togglerdiv.unbind('click')
				config1.$toc.unbind('click')
				config1.$next.unbind('click')
				config1.$prev.unbind('click')
				if (config1.persiststate)
					featuredcontentglider.setCookie1(config1.gliderid, config1.$togglerdiv.attr('lastselected'))
				config1=null

			})
		})
	}
};