var position = 0;
var direction = "left";
var timer = null;
var tmp = Array();
var slides = Array();
var banners = Array();
var currentSlides = Array();
var currentTime = new Date();
var maxslides = null;
var imgWidth = null;
var imgHeight = null;
var lastPosition = 1;
var nextPosition = 2;
isSliding = false;

function parseContent(response) {
	var doc = null;
	if (window.ActiveXObject) {
		doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = "false";
		doc.loadXML(response);
	} else {
		var parser = new DOMParser();
		var doc = parser.parseFromString(response, "text/xml");
	}
	if (doc != null) {
		tmp = doc.getElementsByTagName("slideshow");
		timer = 1000 * tmp[0].getAttribute("timer");
		maxslides = tmp[0].getAttribute("maxslides");
		imgWidth = eval(tmp[0].getAttribute("imgWidth"));
		imgHeight = eval(tmp[0].getAttribute("imgHeight"));
		tmp = doc.getElementsByTagName("slide");
		for ( var i = 0; i < tmp.length; i++) {
			slides[i] = [ Date.parse(tmp[i].getAttribute("date")),
					tmp[i].getAttribute("type"), tmp[i].getAttribute("slide"),
					tmp[i].getAttribute("alt"), tmp[i].getAttribute("url"),
					tmp[i].getAttribute("source"),
					tmp[i].getAttribute("legend") ];
		}
		slides.sort();
		renderSlides(slides);
		presetStage();
		noDelay = setTimeout(changeClickedPosition, 1);
		delay = maxslides != 1 ? setTimeout(changeAutoPosition, timer) : null;
	}
}

function presetStage() {
	jQuery("#slider").css("width", imgWidth * maxslides);
	jQuery(".url").attr("href", "http://www.sbgglobal.com");
}

function renderSlides(slides) {
	for ( var j = 1; j <= maxslides; j++) {
		jQuery("#slider").append(
				"<div id='slide" + j + "' style='width: " + imgWidth
						+ "px; float: left;'></div>");
		for ( var i = slides.length - 1; i >= 0; i--) {
			if ((slides[i][0] < currentTime.getTime()) && (slides[i][2] == j)) {
				document.getElementById("slide" + j).innerHTML = slides[i][1] == "flash" ? swfobject
						.embedSWF(slides[i][5], "slide" + j, imgWidth,
								imgHeight, "9.0.0", {}, {}, {
									wmode : 'transparent'
								})
						: "<a href='" + slides[i][4] + "'><img src='"
								+ slides[i][5] + "' alt='" + slides[i][3]
								+ "' title='" + slides[i][3]
								+ "' style='border: 0px;' /></a>";
				currentSlides[j] = [ slides[i][6], "http://www.sbgglobal.com" ];
				i = -1;
			}
		}
	}
	jQuery("#slider").append("<div style='clear:both'></div>");
}

function changeClickedPosition() {
	jQuery("#slideshow-left-btn").click(function() {
		if (!isSliding) {
			isSliding = true;
			clearTimeout(delay);
			position = position == 0 ? 0 : position + imgWidth;
			$("#slider").animate( {
				"left" : position + "px"
			}, "slow", "easeInOutExpo", function() {
				isSliding = false;
			});
			delay = setTimeout(changeAutoPosition, timer);
		}
	});
	jQuery("#slideshow-right-btn").click(function() {
		if (!isSliding) {
			isSliding = true;
			clearTimeout(delay);
			var lastpos = imgWidth * (maxslides - 1)
			position = position == -lastpos ? -lastpos : position - imgWidth;
			$("#slider").animate( {
				"left" : position + "px"
			}, "slow", "easeInOutExpo", function() {
				isSliding = false;
			});
			delay = setTimeout(changeAutoPosition, timer);
		}
	});
}

function changeAutoPosition() {
	if (!isSliding) {
		isSliding = true;
		direction = position == 0 ? "left"
				: (position == -((maxslides - 1) * imgWidth) ? "right"
						: direction);
		position = direction == "left" ? position - imgWidth : 0;
		lastPosition = position - 1;

		var k = (position / (-imgWidth) + 1);
		jQuery("#slider").animate( {
			"left" : position + "px"
		}, "slow", "easeInOutExpo", function() {
			isSliding = false;
		});
		$(".url").attr("href", currentSlides[k][1]);
		nextPosition = position + currentSlides[k][1];
		jQuery(".buttons").css("font-weight", "normal");
		jQuery("#button" + k).css("font-weight", "bold");
	}
	delay = setTimeout(changeAutoPosition, timer);
}
