// JavaScript Document
var _dynamicRotator;
var _mainRotator;

function getElementsByClassName(parentObj, searchForClass, searchForTag) {
	if ( (typeof searchForTag == "undefined") || (searchForTag == null) ) {
		searchForTag="*";
	}
	
	result=new Array();
	
	allElements=parentObj.getElementsByTagName(searchForTag);
	
	if ( (typeof allElements == "undefined") || (allElements == null) ) {
		return result;
	}

	for (allElementsCount=0; allElementsCount<allElements.length; allElementsCount++) {
		if ( (typeof allElements[allElementsCount].className != "undefined") && (allElements[allElementsCount].className != null) ) {
			if (allElements[allElementsCount].className==searchForClass) {
				result[result.length]=allElements[allElementsCount];
			}
		}
	}
	
	return result;
}

function DynamicRotator(template_url, content_url, refresh_rate) {
	_dynamicRotator=this;
	document.write("<div id='rotator_target'>Rotator Loading.</div>");
	
	this.template_url=template_url;
	this.content_url=content_url;
	this.refresh_rate=refresh_rate;
	
	this.httpRequest=null;
	
	this.state=0;	// Making the template request.
	this.request(this.template_url);
	
}

DynamicRotator.prototype.request=function(url) {
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		this.httpRequest = new XMLHttpRequest();
		if (this.httpRequest.overrideMimeType) {
			this.httpRequest.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	
	if (!this.httpRequest) {
		alert('Giving up - Cannot create an XMLHTTP instance');
		return false;
	}
	
	this.httpRequest.onreadystatechange = function() { _dynamicRotator.response(); };
	this.httpRequest.open('GET', url, true);
	this.httpRequest.send('');
	
}
	
DynamicRotator.prototype.response=function() {
	if (this.httpRequest.readyState == 4) {
		if (this.httpRequest.status == 200) {
			this.process();
		} else {
			alert('There was a problem with the request.');
		}
	}
}

DynamicRotator.prototype.process=function() {
	switch (this.state) {
		case 0 :
			this.displayTemplate();
			break;
		case 1 :
			this.parseContent();
		default :
			break;
	}
}

DynamicRotator.prototype.displayTemplate=function() {
	document.getElementById('rotator_target').innerHTML=this.httpRequest.responseText;
	
	// Set up the event handlers on the image link
	document.getElementById('rotator_image_link').onmouseover=function() {
		if (_mainRotator) {
			_mainRotator.pause();
		}
	}
	document.getElementById('rotator_image_link').onmouseout=function() {
		if (_mainRotator) {
			_mainRotator.resume();
		}
	}
	
	// Handle the next load
	this.state=1;
	this.request(this.content_url);
}

DynamicRotator.prototype.parseContent=function() {
	this.content=document.createElement("div");
	this.content.innerHTML=this.httpRequest.responseText;
	this.stories=getElementsByClassName(this.content, "rotator_story", "div");
	
	// Get all of the link urls in to this.links as an array.
	// Get all of the image urls in to this.images as an array.
	// Get all of the story contents in to this.story_contents as an array
	this.links=new Array();
	this.images=new Array();
	this.story_contents=new Array();
	for (i=0; i<this.stories.length; i++) {
		
		//apply metrics (SFW-2019)
		var tempLinks = this.stories[i].getElementsByTagName('a');
		for(j=0; j<tempLinks.length; j++)
		{
			var queryString = tempLinks[j].href.split('?');
			
			if(queryString.length > 1)
			{
				tempLinks[j].href = tempLinks[j].href + '&ps=bb' + (i + 1);
			}
			else
			{
				tempLinks[j].href = tempLinks[j].href + '?ps=bb' + (i + 1);
			}
		}
		//end SFW-2019
		
		thisLinks=getElementsByClassName(this.stories[i], "rotator_image_link", "a");
		if (thisLinks.length>0) {
			this.links[this.links.length]=thisLinks[0].href;
		}
		
		thisImages=getElementsByClassName(this.stories[i], "rotator_image", "img");
		if (thisImages.length>0) {
			this.images[this.images.length]=thisImages[0].src;
		}
		
		thisContent=getElementsByClassName(this.stories[i], "rotator_content", "div");
		if (thisContent.length>0) {
			tempNode=document.createElement("div");
			tempNode.innerHTML=thisContent[0].innerHTML;
			this.story_contents[this.story_contents.length]=tempNode;
		}
	}
	
	// Build the indicators
	this.indicators=new Array();	
	for (i=0; i<this.images.length; i++) {
		document.getElementById('rotator_paginator_list').innerHTML+='<li><a href="#" onclick="_mainRotator.hardstop(); _mainRotator.setImage('+i+'); return false;" id="rotator_state_'+i+'">'+(i+1)+'</a></li>';
		this.indicators[this.indicators.length]="rotator_state_"+i;
	}
	
	_mainRotator=new Rotator('rotator_image_img', 'rotator_content', this.refresh_rate, this.images, this.links, this.story_contents, this.indicators);
	_mainRotator.start();
}