function getElementContent(el) {
	if (!el) {
		return false;
	}
	if (el.textContent) {
		return el.textContent;
	} else if (el.innerText) {
		return el.innerText;
	} else {
		for (var i = 0; i <= 4; i++) {
			var value = el.childNodes[i].nodeValue;
			if (!value) {
				value = getElementContent(el.childNodes[i]);
			}
			if (value) {
				return value;
			}
		}
	}
	return false;
}

var max_slides_loaded = 0;

function loadSlideshow(request) {
	//load slideshow data and start the show
	var xml_doc = false;
	//parse the XML
	if (window.ActiveXObject) {
		xml_doc = new ActiveXObject('Microsoft.XMLDOM');
		xml_doc.async = 'false';
		xml_doc.loadXML(request.responseText);
	} else if (window.DOMParser) {
		xml_doc = new DOMParser().parseFromString(request.responseText, 'text/xml');
	}
	//load the slideshow data
	if (xml_doc) {
		var articles = $('slideshow-container');
		for (var i = 1; i <= 4; i++) {
			var article = xml_doc.getElementsByTagName('article_' + i)[0];
			var article_url = article.getElementsByTagName('article_url')[0];
			var dek_url = article.getElementsByTagName('dek_url')[0];
			var article_title = article.getElementsByTagName('title')[0];
			if (article_title) {
				//check for links in the title and use that text instead
				var article_title_links = article_title.getElementsByTagName('a');
				if (article_title_links && article_title_links.length) {
					article_title = article_title_links[0];
				}
			}
			var article_dek = article.getElementsByTagName('dek')[0];
			var article_deklink = article.getElementsByTagName('deklink')[0];
			var article_img = article.getElementsByTagName('img')[0];
			//different properties for browser compatibility
			article_url = getElementContent(article_url);
			dek_url = getElementContent(dek_url);
			article_title = getElementContent(article_title);
			article_dek = getElementContent(article_dek);
			article_deklink = getElementContent(article_deklink);
			article_img = getElementContent(article_img);
			//generate slideshow HTML
			var output = '<div id="slideshow-article-' + i + '" class="newsLayout newsRotation" style="display:none;">'
					+ '<div class="introContent">';
			if (article_title) {
				output += '<h2 class="headline">';
				if (article_url) {
						output += '<a href="' + article_url + '">';
				}
				output += article_title;
				if (article_url) {
					output += '</a>';
				}
				output += '</h2>';
			}
			if (article_dek) {
				output += '<p class="dek">' + article_dek + '</p>';
			}
			if (article_deklink) {
				output += '<p class="dek">';
				output += '<a href="' + dek_url + '">';
				output += article_deklink;
				output += '</a>';
				output += '</p>';
			}
			output += '</div>';
			if (article_img) {
				output += '<div class="introImage">';
				if (article_url) {
						output += '<a href="' + article_url + '">';
				}
				output += '<img id="slideshow-image-' + i + '" src="'
						+ article_img + '" />';
				if (article_url) {
					output += '</a>';
				}
				output += '</div>';
			}
			output += '</div>';

			articles.innerHTML += output;

		}
		//play the slideshow
			startSlideshow();
	}
}

var slideshow_timer = false;
var slideshow_block = false;
var active_slide = false;
var slide_seconds = 10.0;
var effect_options = {
		duration: 1.0
	}

function toggleElement(element_id) {
	//scriptaculous fade effect for a DOM element
	Effect.toggle(element_id, 'appear', effect_options);
}
function toggleSlide(slide) {
	//toggle all elements in a slide
	toggleElement('slideshow-article-' + slide);
	return slide;
}

function turnOffElement(element_id) {
	//scriptaculous fade effect for a DOM element
	if ($(element_id).style.display != 'none') {
		toggleElement(element_id);
	}
}
function turnOffSlide(slide) {
	//toggle all elements in a slide
	turnOffElement('slideshow-article-' + slide);
	return slide;
}

function turnOnElement(element_id) {
	//scriptaculous fade effect for a DOM element
	if ($(element_id).style.display == 'none') {
		toggleElement(element_id);
	}
}
function turnOnSlide(slide) {
	//toggle all elements in a slide
	turnOnElement('slideshow-article-' + slide);
	return slide;
}

function setActiveSlide(slide) {
	//set a slide as the (only) active slide
	if (slideshow_block) {
		//don't start a switch if another is already starting
		return false;
	} else {
		slideshow_block = slide;
	}
	//check that all slides are off the but the desired one
	for (var i = 1; i <= 4; i++) {
		if (i == slide) {
			active_slide = turnOnSlide(i);
		} else {
			turnOffSlide(i);
		}
	}
	//release block after effect time has passed
	function releaseBlock() {
		slideshow_block = false;
	}
	setTimeout(releaseBlock, effect_options.duration * 1000);
	return active_slide;
}
function playSlide(slide) {
	if (!slide) {
		//pick a starting slide unless it was specified
		if (active_slide) {
			slide = active_slide + 1;
			if (slide > 4) {
				slide = 1;
			}
		} else {
			slide = 1;
		}
	}
	setActiveSlide(slide);
	//pick the next slide
	var next_slide = slide + 1;
	if (next_slide > 4) {
		next_slide = 1;
	}
	function nextSlide() {
		playSlide(next_slide);
	}
	//change slide after set time period
	slideshow_timer = setTimeout(nextSlide, slide_seconds * 1000);
}

function startSlideshow() {
	//play from current position
	playSlide();
}


function getIEVersion() {
	// Returns the version of Internet Explorer or false if not using IE
	var rv = false;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null) {
			rv = parseFloat( RegExp.$1 );
		}
	}
	return rv;
}

function getTransparentCode(image_url, container_id, width, height) {
	//gets the code to display a transparent image
	var ie_version = getIEVersion();
	if (ie_version && (ie_version >= 5.5) && (ie_version < 7)) {
		//a version of IE that does not support transparent images natively
		return '<span id="' + container_id
				+ '" style="width: ' + width + 'px; height: ' + height
				+ 'px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''
				+ image_url + '\');">&nbsp;</span>';
	} else {
		//a browser that does not need the workaround
		return '<img src="' + image_url + '" alt="" id="' + container_id
				+ '" style="width: ' + width + 'px; height: ' + height + 'px;" />';
	}
}


// main routine

if (slideshow_url) {

	//generate HTML
	document.write('<div id="slideshow-container"></div>');

	//load the slideshow
	var get_params = {
			method: 'get',
			onSuccess: loadSlideshow
		};
	var getter = new Ajax.Request(slideshow_url, get_params);
}


