
/* GLOBAL VARIABLES  */
var _oem_mr_slides = null;
var _oem_mr_slideTransitionMs = 1000;
var _oem_mr_displaySlideMs = 7000;
var _oem_mr_interval = null;
var _oem_mr_InitWrapperInterval = null;
var _oem_mr_InitContent = null;
var _oem_mr_pageID = 1;
var _oem_mr_currentIndex=0;

/* -------------------------------------------------------------------------------------------
	 The following line continues to run the "oem_mr_setMarqeeWrapperClass" until successful so
   that the marqueewrapper's height and width are set as soon as possible and not after
   the page is completely loaded.
------------------------------------------------------------------------------------ */
function oem_mr_go(pageID, displaySlideSec, slideTransitionSec) {
	if (_oem_mr_slides != null) {
		_oem_mr_InitWrapperInterval = setInterval("oem_mr_setMarqeeWrapperClass()",1);
		_oem_mr_InitContent = setInterval("oem_mr_initiateContent(" + pageID + "," + displaySlideSec*1000 + "," + slideTransitionSec*1000 + ")",1);
	}
}


/* Initiates the content array and begins the slideshow as soon as the page is loaded
window.onload=function(){
	if (document.all || document.getElementById){
		if (_oem_mr_slides != null) {
			oem_mr_startRotation();
			oem_mr_makevisible('divMRButtons');
		}
	}
}
 */

function oem_mr_initiateContent(pageID, displaySlideMs, slideTransitionMs) {
	var date = new Date();
	var dailyFeaturedIndex = (date.getDate() % (_oem_mr_slides.length-1));
	var objWrapper = document.getElementById('divMarqueeWrapper');
	var objContent = document.getElementById('divMarqueeWrapper');
	var nextIndex = dailyFeaturedIndex;

	
	if (displaySlideMs != null) {
		_oem_mr_displaySlideMs = displaySlideMs;
	}
	if (slideTransitionMs != null) {
		_oem_mr_slideTransitionMs = slideTransitionMs;
	}

	_oem_mr_pageID = pageID;

	if (oem_mr_getCookie('mr_' + _oem_mr_pageID + '_lastIndex') != null) {
		nextIndex = oem_mr_calculateNextIndex(Number(oem_mr_getCookie('mr_' + _oem_mr_pageID + '_lastIndex')));
	}

	if (objWrapper != null && objContent != null) {
		clearInterval(_oem_mr_InitContent);
		oem_mr_startRotation();
		if (_oem_mr_slides != null) {
			oem_mr_makevisible('divMRButtons');
		}
		_oem_mr_InitContent = null;
		oem_mr_changeCurrentIndex(nextIndex);
		oem_mr_changeContent(_oem_mr_slides[nextIndex]);
		oem_mr_updateButtonsGo(nextIndex);
	}
}


/* Makes the slideshow buttons visible */
function oem_mr_makevisible(objectid) {
    var theObject = document.getElementById(objectid).style;
    if (theObject != null) {
    	theObject.display = "block";
    	theObject.visibility = "visible";
    }
}

/* ----------------------------------------------------------------------------------
		Function sets the height and width of the marquee wrapper div
		depending on the width of the user's screen
------------------------------------------------------------------------------------ */
function oem_mr_setMarqeeWrapperClass() {
	var objWrapper = document.getElementById('divMarqueeWrapper');
	if (objWrapper != null) {
		clearInterval(_oem_mr_InitWrapperInterval);
		_oem_mr_InitWrapperInterval = null;
		beenChanged = 1;
		if (screen.width == 800) {
			objWrapper.className = 'marqueeWrapper800';
		}
		else if (screen.width >= 1280) {
			objWrapper.className = 'marqueeWrapper1280';
		}
		else { // default to ((screen.width == 1024) && (screen.height == 768))
			objWrapper.className = 'marqueeWrapper1024';
		}
	}
}


/* ----------------------------------------------------------------------------------
		Delays the call to the "oem_mr_updateButtonsGo" function so that the highlight change
		from one button to the next coincides with the timing of the slide transition.
------------------------------------------------------------------------------------ */
function oem_mr_updateButtons(activeIndex) {
	setTimeout("oem_mr_updateButtonsGo("+ activeIndex +")",_oem_mr_slideTransitionMs*1.25);
}

/* Changes which button is set to active */
function oem_mr_updateButtonsGo(activeIndex) {
	var btnSlide = null;
	for (i=0; i<_oem_mr_slides.length;i++) {
		btnSlide = document.getElementById("marquee_" + i);
		if (btnSlide != null) {
			btnSlide.className="defaultIndex";
		}
	}
	btnSlide = document.getElementById("marquee_" + activeIndex);
	if (btnSlide != null) {
		btnSlide.className="activeIndex";
	}
}

/* ----------------------------------------------------------------------------------
		When user clicks one of the slide buttons, this function immediately changes
		slideshow to display the desired slide.  This is done w/o transition effects.
------------------------------------------------------------------------------------ */
function oem_mr_btnChooseSlide_Click(slideIndex){
	oem_mr_pauseRotation();
	oem_mr_changeCurrentIndex(slideIndex);
	oem_mr_changeContent(_oem_mr_slides[slideIndex]);
	oem_mr_startRotation();
	oem_mr_updateButtonsGo(slideIndex);
}


/* ----------------------------------------------------------------------------------
		The variable _oem_mr_interval is used to set the interval between slides.
------------------------------------------------------------------------------------ */
function oem_mr_startRotation() {
	if (_oem_mr_interval==null) {
		_oem_mr_interval = setInterval("oem_mr_rotatecontent()", _oem_mr_displaySlideMs + _oem_mr_slideTransitionMs);
	}
}

/* ----------------------------------------------------------------------------------
		Pauses slideshow only if it has not already been paused.  Setting _mr_ineterval
		to null is being used as the flag indicating that the slideshow has been paused
------------------------------------------------------------------------------------ */
function oem_mr_pauseRotation() {
	if (_oem_mr_interval!=null) {
		clearInterval(_oem_mr_interval);
		_oem_mr_interval = null;
	}
}

/* ----------------------------------------------------------------------------------
		This is the function that updates the slideshow to the next slide
------------------------------------------------------------------------------------ */
function oem_mr_rotatecontent(){
	oem_mr_changeCurrentIndex((_oem_mr_currentIndex<_oem_mr_slides.length-1)? _oem_mr_currentIndex+1 : 0)
	oem_mr_rotateSlide(_oem_mr_slides[_oem_mr_currentIndex], _oem_mr_slideTransitionMs);
	oem_mr_updateButtons(_oem_mr_currentIndex);
}

/* ----------------------------------------------------------------------------------
		When a user hovers over the slideshow, the slideshow is paused.  This function
		starts the slideshow when the user is no longer hovering over the slide.
------------------------------------------------------------------------------------ */
function oem_mr_wrapper_OnMouseOver() {
	if (_oem_mr_interval!=null) {
		if(_oem_mr_slides.length > 0) {
			oem_mr_pauseRotation();
			oem_mr_startRotation();
		}
	}
}

/* ----------------------------------------------------------------------------------
		Handles event for pause/play button.
------------------------------------------------------------------------------------ */
function oem_mr_btnPlayPause_Click(imgPlay, imgPause) {
	var strPlay = '<img src="~/static/North America/image/rotatingMarquee_play.gif" width="12" height="12" border="0" alt="Play">';
	var strPause = '<img src="~/static/North America/image/rotatingMarquee_pause.gif" width="12" height="12" border="0" alt="Pause">';
	if (imgPlay!= null) {
		strPlay = imgPlay;
	}

	if (imgPause!= null) {
		strPause = imgPause;
	}
	var nextIndex = oem_mr_getNextIndex();
	var theButton = document.getElementById('btnPlayPause');
	if (_oem_mr_interval==null) { // slide show is paused, so restart
	  oem_mr_rotateSlide(_oem_mr_slides[nextIndex], _oem_mr_slideTransitionMs);
	  oem_mr_changeCurrentIndex(nextIndex);
	  oem_mr_updateButtonsGo(nextIndex);
		oem_mr_startRotation();
		if (theButton != null) {
			theButton.innerHTML = strPause;
		}
	}
	else { // slide show is running, pause
		oem_mr_pauseRotation();
		if (theButton != null) {
			theButton.innerHTML = strPlay;
		}
	}
}


/* ----------------------------------------------------------------------------------
		Handles event for next button.
------------------------------------------------------------------------------------ */
function oem_mr_btnNext_Click() {
	var nextIndex = oem_mr_getNextIndex();
	oem_mr_changeContent(_oem_mr_slides[nextIndex]);
	oem_mr_changeCurrentIndex(nextIndex);
	oem_mr_updateButtonsGo(nextIndex);
	if (_oem_mr_interval!=null) { // slide show is running, pause
		oem_mr_pauseRotation();
		oem_mr_startRotation();
	}
}


/* ----------------------------------------------------------------------------------
		Handles event for previous button.
------------------------------------------------------------------------------------ */
function oem_mr_btnPrevious_Click() {
	var prevIndex = oem_mr_getPreviousIndex();
	oem_mr_changeContent(_oem_mr_slides[prevIndex]);
	oem_mr_changeCurrentIndex(prevIndex);
	oem_mr_updateButtonsGo(prevIndex);
	if (_oem_mr_interval!=null) { // slide show is running, pause
		oem_mr_pauseRotation();
		oem_mr_startRotation();
	}
}

/* ----------------------------------------------------------------------------------
		This function is used to use the transition effects with the slide change.
------------------------------------------------------------------------------------ */
function oem_mr_rotateSlide(id, millisec) {
	oem_mr_fadeSlide(100, 0, millisec);
	setTimeout("oem_mr_changeContent('"+ id + "')",millisec);
	setTimeout("oem_mr_fadeSlide(0, 100, " + (millisec*2.5) + ")",millisec*.99);
}

/* ----------------------------------------------------------------------------------
		Function changes the actual content of the slide.
------------------------------------------------------------------------------------ */
function oem_mr_changeContent(id) {
	var wrapperobject = document.getElementById('divMarqueeWrapper');
	var contentobject = document.getElementById(id);
	if ((wrapperobject!=null) && (contentobject!=null)) {
		wrapperobject.innerHTML = contentobject.innerHTML;
	}
}

/* ----------------------------------------------------------------------------------
		This function fades the slide in or out.
------------------------------------------------------------------------------------ */
function oem_mr_fadeSlide(opacStart, opacEnd, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;

	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("oem_mr_changeOpac(" + i + ",'divMarqueeWrapper')",(timer * speed));
			timer++;
		}
	}
	else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("oem_mr_changeOpac(" + i + ",'divMarqueeWrapper')",(timer * speed));
			timer++;
		}
	}
}

/* ----------------------------------------------------------------------------------
		The fade is done by changing the opacity multiple times.  Each time the opacity
		is changed, this function is called.
------------------------------------------------------------------------------------ */
function oem_mr_changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

/* ----------------------------------------------------------------------------------
		Quick function that returns the next index, ensuring that it falls
		within the content array.
------------------------------------------------------------------------------------ */
function oem_mr_getNextIndex(){
	var theIndex = _oem_mr_currentIndex+1;
	if (theIndex>(_oem_mr_slides.length-1))
		theIndex = 0;
	//alert(theIndex);
	return theIndex;
}


/* ----------------------------------------------------------------------------------
		Quick function that returns the next index, ensuring that it falls
		within the content array.
------------------------------------------------------------------------------------ */
function oem_mr_calculateNextIndex(thisIndex){
	var theIndex = thisIndex+1;
	if (theIndex>(_oem_mr_slides.length-1) || theIndex<0)
		theIndex = 0;
	return theIndex;
}
/* ----------------------------------------------------------------------------------
		Quick function that returns the last index, ensuring that it falls
		within the content array.
------------------------------------------------------------------------------------ */
function oem_mr_getPreviousIndex(){
	var theIndex = _oem_mr_currentIndex;
	theIndex=(theIndex==0)? _oem_mr_slides.length-1 : theIndex-1
	return theIndex;
}


/* ----------------------------------------------------------------------------------
		This function should be used any time the current index is
		updated so that the cookie can be updated as well.
------------------------------------------------------------------------------------ */
function oem_mr_changeCurrentIndex(newIndex) {
	var date = new Date();
	date.setTime(date.getTime()+(24*60*60*1000));
	var tomorrow = date.toGMTString();
	oem_mr_setCookie('mr_' + _oem_mr_pageID + '_lastIndex',newIndex, tomorrow);
	oem_mr_setCookie('mr_' + _oem_mr_pageID + '_lastDayLooked',date.getDate(), tomorrow);
	_oem_mr_currentIndex = newIndex;
}

/* ----------------------------------------------------------------------------------
		Set the cookie
------------------------------------------------------------------------------------ */
function oem_mr_setCookie(name, value, expires) {
	ss_curCookie = name + "=" + escape(value) + ";expires=" + expires;
  document.cookie = ss_curCookie;
}

/* ----------------------------------------------------------------------------------
		get the cookie
------------------------------------------------------------------------------------ */
function oem_mr_getCookie(name)
{
    var ss_dc = document.cookie;
    var ss_prefix = name + "=";
    var ss_begin = ss_dc.indexOf("; " + ss_prefix);
    if (ss_begin == -1)
    {
        ss_begin = ss_dc.indexOf(ss_prefix);
        if (ss_begin != 0) return null;
    }
    else
    {
        ss_begin += 2;
    }
    var ss_end = document.cookie.indexOf(";", ss_begin);
    if (ss_end == -1)
    {
        ss_end = ss_dc.length;
    }
    return unescape(ss_dc.substring(ss_begin + ss_prefix.length, ss_end));
}