//: http://www.dynamicdrive.com.

var featuredcontentslider = {

	//3 variables below you can customize if desired:
	ajaxloadingmsg: '<div style="margin: 20px 0 0 20px"><img src="loading.gif" /> Fetching slider Contents. Please wait...</div>',
	bustajaxcache: true, //bust caching of external ajax page after 1st request?
	enablepersist: true, //persist to last content viewed when returning to page?

	settingcaches: {}, //object to cache "setting" object of each script instance

	jumpTo: function (fcsid, pagenumber) { //public function to go to a slide manually.
		this.turnpage(this.settingcaches[fcsid], pagenumber)
	},

	buildcontentdivs: function (setting) {
		var alldivs = document.getElementById(setting.id).getElementsByTagName("div")
		for (var i = 0; i < alldivs.length; i++) {
			if (this.css(alldivs[i], "contentdiv", "check")) { //check for DIVs with class "contentdiv"
				setting.contentdivs.push(alldivs[i])
				var stl = alldivs[i].style
				stl.display = "none" //collapse all content DIVs to begin with
				stl.position = "absolute"
			}
		}
	},

	buildpaginate: function (setting) {
		this.buildcontentdivs(setting)
		var sliderdiv = document.getElementById(setting.id),
		pdiv = document.getElementById("paginate-" + setting.id),
		phtml = "",
		toc = setting.toc,
		nextprev = setting.nextprev
		if (typeof toc == "string" && toc != "markup" || typeof toc == "object") {
			for (var i = 1; i <= setting.contentdivs.length; i++) {
				phtml += '<a href="#' + i + '" class="toc">' + (typeof toc == "string" ? toc.replace(/#increment/, i) : toc[i - 1]) + '</a> '
			}
			phtml = (nextprev[0] != '' ? '<a href="#prev" class="prev">' + nextprev[0] + '</a> ' : '') + phtml + (nextprev[1] != '' ? '<a href="#next" class="next">' + nextprev[1] + '</a>' : '')
			pdiv.innerHTML = phtml
		}
		var pdivlinks = pdiv.getElementsByTagName("a"),
		toclinkscount = 0 //var to keep track of actual # of toc links
		for (var i = 0; i < pdivlinks.length; i++) {
			if (this.css(pdivlinks[i], "toc", "check")) {
				if (toclinkscount > setting.contentdivs.length - 1) { //if this toc link is out of range (user defined more toc links then there are contents)
//					pdivlinks[i].style.display = "none" //hide this toc link
					continue
				}
				pdivlinks[i].setAttribute("rel", ++toclinkscount) //store page number inside toc link
				pdivlinks[i][setting.revealtype] = function () {
					featuredcontentslider.turnpage(setting, this.getAttribute("rel"))
					return false
				}
				setting.toclinks.push(pdivlinks[i])
			}
			else if (this.css(pdivlinks[i], "prev", "check") || this.css(pdivlinks[i], "next", "check")) { //check for links with class "prev" or "next"
				pdivlinks[i].onclick = function () {
					featuredcontentslider.turnpage(setting, this.className)
					return false
				}
			}
		}
		this.turnpage(setting, setting.currentpage, true)
		if (setting.autorotate[0]) { //if auto rotate enabled
			pdiv[setting.revealtype] = function () {
				featuredcontentslider.cleartimer(setting, window["fcsautorun" + setting.id])
			}
			sliderdiv["onclick"] = function () { //stop content slider when slides themselves are clicked on
				featuredcontentslider.cleartimer(setting, window["fcsautorun" + setting.id])
			}
			setting.autorotate[1] = setting.autorotate[1] + (1 / setting.enablefade[1] * 50) //add time to run fade animation (roughly) to delay between rotation
			this.autorotate(setting)
		}
	},

	urlparamselect: function (fcsid) {
		var result = window.location.search.match(new RegExp(fcsid + "=(\\d+)", "i")) //check for "?featuredcontentsliderid=2" in URL
		return (result == null) ? null : parseInt(RegExp.$1) //returns null or index, where index (int) is the selected tab's index
	},

	turnpage: function (setting, thepage, autocall) {
		var currentpage = setting.currentpage, //current page # before change
		totalpages = setting.contentdivs.length,
		turntopage = (/prev/i.test(thepage)) ? currentpage - 1 : (/next/i.test(thepage)) ? currentpage + 1 : parseInt(thepage)
		turntopage = (turntopage < 1) ? totalpages : (turntopage > totalpages) ? 1 : turntopage //test for out of bound and adjust
		if (turntopage == setting.currentpage && typeof autocall == "undefined") //if a pagination link is clicked on repeatedly
			return
		setting.currentpage = turntopage
		$(setting.contentdivs).css({ 'z-index': setting.topzindex })
		setting.contentdivs[turntopage - 1].style.zIndex = setting.topzindex+1
		this.cleartimer(setting, window["fcsfade" + setting.id])
		setting.cacheprevpage = setting.prevpage
		if (setting.enablefade[0] == true) {
			setting.curopacity = 0
			this.fadeup(setting)
		}
		if (setting.enablefade[0] == false) { //if fade is disabled, fire onChange event immediately (verus after fade is complete)
//			setting.contentdivs[setting.prevpage - 1].style.display = "none" //collapse last content div shown (it was set to "block")
			setting.onChange(setting.prevpage, setting.currentpage)
		}
		setting.contentdivs[turntopage - 1].style.visibility = "visible"
		setting.contentdivs[turntopage - 1].style.display = "block"
		if (setting.prevpage <= setting.toclinks.length) //make sure pagination link exists (may not if manually defined via "markup", and user omitted)
			this.css(setting.toclinks[setting.prevpage - 1], "selected", "remove")
		if (turntopage <= setting.toclinks.length) //make sure pagination link exists (may not if manually defined via "markup", and user omitted)
			this.css(setting.toclinks[turntopage - 1], "selected", "add")
		setting.prevpage = turntopage
		if (this.enablepersist)
			this.setCookie("fcspersist" + setting.id, turntopage)
	},

	fadeup: function (setting) {
		var targetobject = setting.contentdivs[setting.currentpage - 1]
		for (var divInd in setting.contentdivs)
			if (divInd != setting.currentpage - 1)
				$(setting.contentdivs[divInd]).fadeOut(setting.fadeDuration)
		$(targetobject).fadeIn(setting.fadeDuration)
	},

	cleartimer: function (setting, timervar) {
		if (typeof timervar != "undefined") {
			clearTimeout(timervar)
			clearInterval(timervar)
			if (setting.cacheprevpage != setting.currentpage) { //if previous content isn't the same as the current shown div
//				setting.contentdivs[setting.cacheprevpage - 1].style.display = "none"
			}
		}
	},

	css: function (el, targetclass, action) {
		var needle = new RegExp("(^|\\s+)" + targetclass + "($|\\s+)", "ig")
		if (action == "check")
			return needle.test(el.className)
		else if (action == "remove")
			el.className = el.className.replace(needle, "")
		else if (action == "add")
			el.className += " " + targetclass
	},

	autorotate: function (setting) {
		window["fcsautorun" + setting.id] = setInterval(function () { featuredcontentslider.turnpage(setting, "next") }, setting.autorotate[1])
	},

	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 (setting) {
		var persistedpage = this.getCookie("fcspersist" + setting.id) || 1
		var urlselectedpage = this.urlparamselect(setting.id) //returns null or index from: mypage.htm?featuredcontentsliderid=index
		this.settingcaches[setting.id] = setting //cache "setting" object
		setting.contentdivs = []
		setting.toclinks = []
		setting.topzindex = 0
		setting.currentpage = urlselectedpage || ((this.enablepersist) ? persistedpage : 1)
		setting.prevpage = setting.currentpage
		setting.revealtype = "on" + (setting.revealtype || "click")
		setting.curopacity = 0
		setting.onChange = setting.onChange || function () { }
		setting.fadeDuration = setting.fadeDuration ? setting.fadeDuration : 1000
		if (setting.contentsource[0] == "inline")
			this.buildpaginate(setting)
	}

}
