/*******************************************************************
*
* File    : AnimatedFader.js
*
* Created : 2000/05/28
*
* Author  : Roy Whittle  (Roy@Whittle.com) www.Roy.Whittle.com
*
* Purpose : To create fading text descriptions
*
* History
* Date         Version        Description
*
* 2000-05-28	1.0		Initial version. Based on the State Transition
*					Diagram created for animated rollovers I
*					modified the code to fade text.
* 2000-05-29	1.1		I did not follow the STD correctly and introduced
*					a bug that left objects in the ON state.
*					This is now corrected.
* 2000-06-07	1.2		Introduced a start colour so that the script
*					can be used on different backgrounds.
*					Change the var AnimationRunning and FrameInterval
*					so they don't conflict with animate2.js
* 2000-08-09	1.3		Added Gecko ( Netscape 6 Preview Release 1 ) support
*					- Ken workman k.workman@3DASA.com
* 2000-10-15	1.4		Modified to make Netscpe 6 (PR3) more efficient
*					by just changing the color instead of replacing
*					the whole content of the DIV.
***********************************************************************/
/*** Create some global variables ***/
var FadingObject = new Array();
var FadeRunning=false;
var FadeInterval=30;

/*******************************************************************************/
/*** These are the simplest HEX/DEC conversion routines I could come up with ***/
/*** I have seen a lot of fade routines that seem to make this a             ***/
/*** very complex task. I am sure somene else must've had this idea          ***/
/*******************************************************************************/
var hexDigit=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function dec2hex(dec)
{
	return(hexDigit[dec>>4]+hexDigit[dec&15]);
}
function hex2dec(hex)
{
	return(parseInt(hex,16))
}
/******************************************************************************************/

/***********************************************************
* Function   : createFaderObject
*
* Parameters : theDiv   - The name of the DIV in which to fade the text
*              numSteps - The number of steps to use in the fade.
*		       startColor - The background colour of the page.
*              
* Description : Creates an object that can hold the current
*               state of the fade animation for a particular DIV
***********************************************************/
function createFaderObject(theDiv, numSteps, startColor)
{
	if(!startColor)
		startColor = "000000";
		
	this.name		= theDiv;
	this.text		= null;
	this.color		= "FFFFFF";
	this.next_text	= null;
	this.next_color	= null;
	this.state		= "OFF";
	this.index		= 0;
	this.steps		= numSteps;
	this.r		= hex2dec(startColor.slice(0,2));
	this.g		= hex2dec(startColor.slice(2,4));
	this.b		= hex2dec(startColor.slice(4,6));
}

/***********************************************************
* Function   : FadingText
*
* Parameters : theDiv   - The name of the DIV in which to fade the text
*              numSteps - The number of steps to use in the fade.
*              
* Description : Library function to be called from the main HTML.
*		        Creates an object that can hold the current
*               state of the fade animation for a particular DIV
***********************************************************/
function FadingText(theDiv, numSteps, startColor)
{
	FadingObject[theDiv] = new createFaderObject(theDiv, numSteps, startColor);
}
/*****************************************************************
* Function    : start_fading
*
* Description : If the FadeAnimation loop is not currently running
*		        then it is started.
*****************************************************************/
function start_fading()
{
	if(!FadeRunning)
		FadeAnimation();
}
/*****************************************************************
* Function    : set_text
*
* Description : In the new W3C DOM model we need only set the text
*			once, after that we can just change the colour
*****************************************************************/
function set_text(f)
{
	if(navigator.appName.indexOf("Netscape") != -1
		&& document.getElementById)
	{
		var theElement = document.getElementById(f.name);
		var newRange   = document.createRange();
		newRange.setStartBefore(theElement);
		var strFrag    = newRange.createContextualFragment(f.text);	

		while (theElement.hasChildNodes())
			theElement.removeChild(theElement.lastChild);
		theElement.appendChild(strFrag);
		theElement.style.color="#"+f.startColor;
	}
}
/*****************************************************************
*
* Function   : getColor
*
* Parameters : f - the fade object for which to calculate the colour.
*
* Description: Calculates the color of the link depending on
*		       how far through the fade we are.
*
*****************************************************************/
function getColor(f)
{
	var r=hex2dec(f.color.slice(0,2));
	var g=hex2dec(f.color.slice(2,4));
	var b=hex2dec(f.color.slice(4,6));

	r2= Math.floor(f.r+(f.index*(r-f.r))/(f.steps) + .5);
	g2= Math.floor(f.g+(f.index*(g-f.g))/(f.steps) + .5);
	b2= Math.floor(f.b+(f.index*(b-f.b))/(f.steps) + .5);

	return("#" + dec2hex(r2) + dec2hex(g2) + dec2hex(b2));
}
/*****************************************************************
*
* Function   : setColor
*
* Parameters : fadeObj   - The TextFader object to set
*
* Description: Gets the color of the text and writes it to
*		       the DIV.
*
*****************************************************************/
function setColor(fadeObj)
{
	var theColor=getColor(fadeObj);
	var str="<FONT COLOR="+ theColor + ">" + fadeObj.text + "</FONT>";
	var theDiv=fadeObj.name;
	
//if IE 4+
	if(document.all)
	{
		document.all[theDiv].innerHTML=str;
	}	
//else if NS 4
	else if(document.layers)
	{
		document.nscontainer.document.layers[theDiv].document.write(str);
		document.nscontainer.document.layers[theDiv].document.close();
	}
//else if NS 6 (supports new DOM, may work in IE5) - see Website Abstraction for more info.
//http://www.wsabstract.com/javatutors/dynamiccontent4.shtml
	else if (document.getElementById)
	{
		theElement = document.getElementById(theDiv);
		theElement.style.color=theColor;
	}
	
}
/*****************************************************************
*
* Function   : fade_up
*
* Parameters : theDiv  - The div in which to display the text
*		       newText - The text to display when faded in
*		       newColor- The color the text will be.
*
* Description: Depending on the current "state" of the fade
*		       this function will determine the new state and
*		       if neccessary, start the fade effect.            
*
*****************************************************************/
function fade_up(theDiv, newText, newColor)
{
	var f=FadingObject[theDiv];

	if(newColor == null)
		newColor="FFFFFF";

	if(f.state == "OFF")
	{
		f.text  = newText;
		f.color = newColor;
		f.state = "FADE_UP";
		set_text(f);
		start_fading();
	}
	else if( f.state == "FADE_UP_DOWN"
		|| f.state == "FADE_DOWN"
		|| f.state == "FADE_DOWN_UP")
	{
		if(newText == f.text)
			f.state = "FADE_UP";
		else
		{
			f.next_text  = newText;
			f.next_color = newColor;
			f.state      = "FADE_DOWN_UP";
		}
	}
}
/*****************************************************************
*
* Function   : fade_down
*
* Parameters : theDiv  - The div in which to display the text
*
* Description: Depending on the current "state" of the fade
*		       this function will determine the new state and
*		       if neccessary, start the fade effect.            
*
*****************************************************************/
function fade_down(theDiv)
{
	var f=FadingObject[theDiv];

	if(f.state=="ON")
	{
		f.state="FADE_DOWN";
		start_fading();
	}
	else if(f.state=="FADE_DOWN_UP")
	{
		f.state="FADE_DOWN";
		f.next_text = null;
	}
	else if(f.state == "FADE_UP")
	{
		f.state="FADE_UP_DOWN";
	}
}
/*******************************************************************
*
* Function    : Animate
*
* Description : This function is based on the Animate function
*		        of animate.js (animated rollovers).
*		        Each fade object has a state. This function
*		        modifies each object and changes its state.
*****************************************************************/
function FadeAnimation()
{
	FadeRunning = false;
	for (var d in FadingObject)
	{
		var f=FadingObject[d];

		if(f.state == "FADE_UP")
		{
			if(f.index < f.steps)
				f.index++;
			else
				f.index = f.steps;
			setColor(f);

			if(f.index == f.steps)
				f.state="ON";
			else
				FadeRunning = true;
		}
		else if(f.state == "FADE_UP_DOWN")
		{
			if(f.index < f.steps)
				f.index++;
			else
				f.index = f.steps;
			setColor(f);

			if(f.index == f.steps)
				f.state="FADE_DOWN";
			FadeRunning = true;
		}
		else if(f.state == "FADE_DOWN")
		{
			if(f.index > 0)
				f.index--;
			else
				f.index = 0;
			setColor(f);

			if(f.index == 0)
				f.state="OFF";
			else
				FadeRunning = true;
		}
		else if(f.state == "FADE_DOWN_UP")
		{
			if(f.index > 0)
				f.index--;
			else
				f.index = 0;
			setColor(f);

			if(f.index == 0)
			{
				f.text      = f.next_text;
				f.color     = f.next_color;
				f.next_text = null;
				f.state     ="FADE_UP";
				set_text(f);
			}
			FadeRunning = true;
		}
	}
	/*** Check to see if we need to animate any more frames. ***/
	if(FadeRunning)
		setTimeout("FadeAnimation()", FadeInterval);

}



//** Featured Content Slider script- (c) Dynamic Drive DHTML code library: http://www.dynamicdrive.com.
//** May 2nd, 08'- Script rewritten and updated to 2.0.
//** June 12th, 08'- Script updated to v 2.3, which adds the following features:
			//1) Changed behavior of script to actually collapse the previous content when the active one is shown, instead of just tucking it underneath the later.
			//2) Added setting to reveal a content either via "click" or "mouseover" of pagination links (default is former).
			//3) Added public function for jumping to a particular slide within a Featured Content instance using an arbitrary link, for example.	

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)
},

ajaxconnect:function(setting){
	var page_request = false
	if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
		try {
		page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else
		return false
	var pageurl=setting.contentsource[1]
	page_request.onreadystatechange=function(){
		featuredcontentslider.ajaxpopulate(page_request, setting)
	}
	document.getElementById(setting.id).innerHTML=this.ajaxloadingmsg
	var bustcache=(!this.bustajaxcache)? "" : (pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	page_request.open('GET', pageurl+bustcache, true)
	page_request.send(null)
},

ajaxpopulate:function(page_request, setting){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
		document.getElementById(setting.id).innerHTML=page_request.responseText
		this.buildpaginate(setting)
	}
},

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])
				alldivs[i].style.display="none" //collapse all content DIVs to begin with
		}
	}
},

buildpaginate:function(setting){
	this.buildcontentdivs(setting)
	var sliderdiv=document.getElementById(setting.id)
	var pdiv=document.getElementById("paginate-"+setting.id)
	var phtml=""
	var toc=setting.toc
	var 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")
	var 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[setting.revealtype]=function(){
			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)
	}
},

turnpage:function(setting, thepage, autocall){
	var currentpage=setting.currentpage //current page # before change
	var totalpages=setting.contentdivs.length
	var 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[turntopage-1].style.zIndex=++setting.topzindex
	this.cleartimer(setting, window["fcsfade"+setting.id])
	if (setting.enablefade[0]==true){
		setting.curopacity=0
		setting.cacheprevpage=setting.prevpage
		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)
},

setopacity:function(setting, value){ //Sets the opacity of targetobject based on the passed in value setting (0 to 1 and in between)
	var targetobject=setting.contentdivs[setting.currentpage-1]
	if (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 (typeof targetobject.style.MozOpacity!="undefined") //Old Mozilla syntax
		targetobject.style.MozOpacity=value
	else if (typeof targetobject.style.opacity!="undefined") //Standard opacity syntax
		targetobject.style.opacity=value
	setting.curopacity=value
},

fadeup:function(setting){
	if (setting.curopacity<1){
		this.setopacity(setting, setting.curopacity+setting.enablefade[1])
		window["fcsfade"+setting.id]=setTimeout(function(){featuredcontentslider.fadeup(setting)}, 50)
	}
	else{ //when fade is complete
		if (setting.cacheprevpage!=setting.currentpage) //if previous content isn't the same as the current shown div (happens the first time the page loads/ script is run)
			setting.contentdivs[setting.cacheprevpage-1].style.display="none" //collapse last content div shown (it was set to "block")
		setting.onChange(setting.cacheprevpage, setting.currentpage)
	}
},

cleartimer:function(setting, timervar){
	if (typeof timervar!="undefined"){
		clearTimeout(timervar)
		clearInterval(timervar)
		if (setting.cacheprevpage)
			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
	this.settingcaches[setting.id]=setting //cache "setting" object
	setting.contentdivs=[]
	setting.toclinks=[]
	setting.topzindex=0
	setting.currentpage=(this.enablepersist)? persistedpage : 1
	setting.prevpage=setting.currentpage
	setting.revealtype="on"+(setting.revealtype || "click")
	setting.curopacity=0
	setting.onChange=setting.onChange || function(){}
	if (setting.contentsource[0]=="inline")
		this.buildpaginate(setting)
	if (setting.contentsource[0]=="ajax")
		this.ajaxconnect(setting)
}

}


 function fillPhotoText(x)
    {

    var x_PhotoText = document.getElementById('PhotoText');
        if (x == 1)
        {
        
        
            x_PhotoText.innerHTML = "25 MIU Dentistry students are travelling this summer to the Technical University of Dresden, Germany, for a hand-on workshop in Prosthodontics, Endodontics, Periodontics and Oral Surgery.<br /> <a href='articles/DresdenWorkshop.aspx' class='ReadMore'>Read more</a>";
           /*redtitle.style.zIndex = 2;        */


            fade_down('redtitle');
            fade_up('redtitle','25 Dentistry Students Attend a Workshop in Germany ','ffffff');
        }
        else if (x == 2)
        {
        x_PhotoText.innerHTML = "Eleven MIU Engineering students, specializing in Electronics and Communication, would travel to Sweden this August to acquire extensive training on using advanced equipment for the fabrication and characterization of solid state devices and circuits.<br/><a href='articles/midswedensummer.aspx' class='ReadMore'>Read more</a>";
        fade_down('redtitle');
        fade_up('redtitle','Summer Workshop for MIU students at Mid-Sweden University','ffffff');
        }
        else if (x == 3)
        {
            x_PhotoText.innerHTML = "About 30 exchange students from the US, the UK, Germany and Sweden will have the opportunity to learn about the Middle East region, its culture, language, history, economy, society, and politics from July 27th to August 21st 2008 through the annual Middle East Studies summer program.<br/><a href='articles/meastprogram.aspx' class='ReadMore'>Read more</a>  ";

            fade_down('redtitle');
            fade_up('redtitle','Intensive Summer Program for Exchange Students','ffffff');
        }   
        else if (x==4)
        {
            x_PhotoText.innerHTML = "About 70 MIU students and 17 staff members from all faculties are travelling this summer to Germany, France, and Canada to attend specialized and advanced language programs around the world.<br/><a href='articles/summerlanguageprogram.aspx' class='ReadMore'>Read more</a> ";

            fade_down('redtitle');
            fade_up('redtitle','MIU Students and Staff Fly to Germany, France and Canada ','ffffff');
        }
        else if (x == 5)
        {
            x_PhotoText.innerHTML = "Eight MIU students from the Faculty of Business visited the School of Management and Law, Zurich University of Applied Science this summer to learn about the economy of Switzerland. <br/><a href='articles/ZurichUniversity.aspx' class='ReadMore'>Read more</a> ";

            fade_down('redtitle');
            fade_up('redtitle','Business Students Attend a Workshop in Zurich this Summer','ffffff');
        }
    }
