//** Featured Content Slider script- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)

var csbustcachevar=0 //bust potential caching of external pages after initial Ajax request? (1=yes, 0=no)
var enabletransition=1 //enable fade into view transition effect? (1=yes, 0=no)
var enablepersist=true
var slidernodes=new Object() //Object array to store references to each content slider's DIV containers (<div class="headline_article">)
var csloadedobjects="" //Variable to store file names of .js/.css files already loaded (if Ajax is used)

function ContentSlider(sliderid, autorun, PhotoLinks , customPaginateText, customNextText){
	var slider=document.getElementById(sliderid)
	if (typeof customPaginateText!="undefined" && customPaginateText!="") //Custom array of pagination links text defined?
		slider.paginateText=customPaginateText
	if (typeof customNextText!="undefined" && customNextText!="") //Custom HTML for "Next" link defined?
		slider.nextText=customNextText
	slidernodes[sliderid]=[] //Array to store references to this content slider's DIV containers (<div class="headline_article">)
	var alldivs=slider.getElementsByTagName("div")
	for (var i=0; i<alldivs.length; i++){
		if (alldivs[i].className=="box_headline")
			slider.box_headline=alldivs[i]
		else if (alldivs[i].className=="headline_article"){
			slidernodes[sliderid].push(alldivs[i]) //add this DIV reference to array
		}
	}
	ContentSlider.buildpagination(sliderid,PhotoLinks)
	var loadfirstcontent=true
	if (enablepersist && getCookie(sliderid)!=""){ //if enablepersist is true and cookie contains corresponding value for slider
		var cookieval=getCookie(sliderid).split(":") //process cookie value ([sliderid, int_pagenumber (div content to jump to)]
		if (document.getElementById(cookieval[0])!=null && typeof slidernodes[sliderid][cookieval[1]]!="undefined"){ //check cookie value for validity
			ContentSlider.turnpage(cookieval[0], parseInt(cookieval[1])) //restore content slider's last shown DIV
			loadfirstcontent=false
		}
	}
	if (loadfirstcontent==true) //if enablepersist is false, or cookie value doesn't contain valid value for some reason (ie: user modified the structure of the HTML)
		ContentSlider.turnpage(sliderid, 0) //Display first DIV within slider
	if (typeof autorun=="number" && autorun>0) //if autorun parameter (int_miliseconds) is defined, fire auto run sequence
		window[sliderid+"timer"]=setTimeout(function(){ContentSlider.autoturnpage(sliderid, autorun)}, autorun)
}

ContentSlider.buildpagination=function(sliderid,PhotoLinks){

    var MyArray = new Array (4);
    MyArray= PhotoLinks.split (";");
 var n = slidernodes[sliderid].length;
 	var slider=document.getElementById(sliderid)
	var paginatediv=document.getElementById("paginate-"+sliderid) //reference corresponding pagination DIV for slider
	var pcontent =''
	for (var i=0; i<slidernodes[sliderid].length; i++) //For each DIV within slider, generate a pagination link
			pcontent+=' <a href="#" onClick=\"ContentSlider.turnpage(\''+sliderid+'\', '+i+'); return false\"><img style="text-align:right; width:70px;border: 1px solid #C0C0C0; height:70px ; padding: 0px; margin: 0px; " src="' + MyArray[n-i-1] + '" / ></a><br/>'
	pcontent+=''
	
	paginatediv.innerHTML=pcontent
	paginatediv.onclick=function(){ //cancel auto run sequence (if defined) when user clicks on pagination DIV
	if (typeof window[sliderid+"timer"]!="undefined")
		clearTimeout(window[sliderid+"timer"])
	}
}

ContentSlider.turnpage=function(sliderid, thepage){
	var paginatelinks=document.getElementById("paginate-"+sliderid).getElementsByTagName("a") //gather pagination links
	for (var i=0; i<slidernodes[sliderid].length; i++){ //For each DIV within slider
		paginatelinks[i].className="" //empty corresponding pagination link's class name
		slidernodes[sliderid][i].style.display="none" //hide DIV
	}
	paginatelinks[thepage].className="active" //for selected DIV, set corresponding pagination link's class name
	if (enabletransition){
		if (window[sliderid+"fadetimer"])
			clearTimeout(window[sliderid+"fadetimer"])
		this.setopacity(sliderid, 0.1)
	}
	slidernodes[sliderid][thepage].style.display="block" //show selected DIV
	if (enabletransition)
		this.fadeup(sliderid, thepage)
	//Set "Next" pagination link's (last link within pagination DIV) "rel" attribute to the next DIV number to show
	paginatelinks[paginatelinks.length-1].setAttribute("rel", thenextpage=(thepage<paginatelinks.length-1)? thepage+1 : 0)
	if (enablepersist)
		setCookie(sliderid, sliderid+":"+thepage)
}

ContentSlider.autoturnpage=function(sliderid, autorunperiod){
	var paginatelinks=document.getElementById("paginate-"+sliderid).getElementsByTagName("a") //Get pagination links
	var nextpagenumber=parseInt(paginatelinks[paginatelinks.length-1].getAttribute("rel")) //Get page number of next DIV to show
	ContentSlider.turnpage(sliderid, nextpagenumber) //Show that DIV
	window[sliderid+"timer"]=setTimeout(function(){ContentSlider.autoturnpage(sliderid, autorunperiod)}, autorunperiod)
}

ContentSlider.setopacity=function(sliderid, value){ //Sets the opacity of targetobject based on the passed in value setting (0 to 1 and in between)
	var targetobject=document.getElementById(sliderid).box_headline || null //reference slider container itself
	if (targetobject && targetobject.filters && targetobject.filters[0]){ //IE syntax
		if (typeof targetobject.filters[0].opacity=="number") //IE6
			targetobject.filters[0].opacity=value*100
		else //IE 5.5
			targetobject.style.filter="alpha(opacity="+value*100+")"
		}
	else if (targetobject && typeof targetobject.style.MozOpacity!="undefined") //Old Mozilla syntax
		targetobject.style.MozOpacity=value
	else if (targetobject && typeof targetobject.style.opacity!="undefined") //Standard opacity syntax
		targetobject.style.opacity=value
	targetobject.currentopacity=value
}

ContentSlider.fadeup=function(sliderid){
	var targetobject=document.getElementById(sliderid).box_headline || null //reference slider container itself
	if (targetobject && targetobject.currentopacity<1){
		this.setopacity(sliderid, targetobject.currentopacity+0.1)
		window[sliderid+"fadetimer"]=setTimeout(function(){ContentSlider.fadeup(sliderid)}, 100)
	}
}

function getCookie(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 ""
}

function setCookie(name, value){
	document.cookie = name+"="+value
}
