var slidesInfo;
var currentSlide;
var i = 1;
var t;
var delay;
var bUseTimer = 1;

$(document).ready(function() {

	currentSlide = $("#flash-slide");
	$("#flash-logo").animate({marginTop: "0px"}, 800, "linear", function() {
		$.get("./nyuad_homepage.xml", {}, function(data) { 

			slidesInfo = $(data).children().children();

			progressBar = $("#flash-slideshow-progress");

			progressBar.append("<li class=\"leftArrow png\"><a href=\"#\">&nbsp;</a></li>");
//			progressBar.append("<li class=\"leftArrow\"><img src=\"./images/carousel.left.arrow.png\" /></li>");
		
			slidesInfo.each(function(j) {

				progressBar.append("<li><a href=\"#\">&nbsp;</a></li>");

				if($(this).children('photo')) {
					tmp = document.createElement('img');
					tmp.setAttribute('src', $(this).children('photo').text());
					document.getElementById('flash-hidden').appendChild(tmp);
				}
			});

			progressBar.append("<li class=\"rightArrow png\"><a href=\"#\">&nbsp;</a></li>");

			var tmp;
			$("#flash-slideshow-progress li a").click(function(e) {

				e.preventDefault();
				len = $("#flash-slideshow-progress li a").length;
				tmp = $("#flash-slideshow-progress li a").index(this);
				if(!isNaN(tmp)) { 
					if(tmp == 0) {
						if(bUseTimer) {
							if(i-2 == 0) {
								i = len - 2;
							} else {
								i = i-2;
							}
						} else {
							if(i-1 == 0) {
								i = len - 2;
							} else {
								i = i - 1;
							}
						}
						bUseTimer = 1;
						continueShow();
					} else if (tmp == (len - 1)) {
						if(!bUseTimer) {
							i++;
						}
						bUseTimer = 1;
						continueShow();
					} else {
						bUseTimer = 0;
						continueShow(tmp); 
					}
				}

			});
			continueShow(); 

		}, "xml");
	});

});

function continueShow(num) {

	clearTimeout(t);
	if(typeof num == "undefined" || isNaN(num)) {
		if(i == slidesInfo.length+1) { i = 1; }
		$("#flash-slideshow-progress li:not(:eq(" + (i) + "))").removeClass('selected');
	} else {
		if(!isNaN(num)) {
			i = num;
			$("#flash-slideshow-progress li:not(:eq(" + (i) + "))").removeClass('selected');
		}
	}
	$("#flash-slideshow-progress li:eq(" + i + ")").addClass('selected');
	showSlide(slidesInfo[i-1]); 
		

}

function showSlide(data) {

	var slideInfo = $(data);

	var sidebarColor = slideInfo.children('sidebarColor').text();
	var photitle = slideInfo.children('photitle').text();
	var phocaption = slideInfo.children('phocaption').text();
	var link = slideInfo.children('link').text();
	$("#flash-text h3, #flash-text p").hide(1, function() {
		$("#flash-text").fadeOut(500, function() {
			$("#flash-text").css('background-color', sidebarColor).fadeIn(500, function() {
				$("#flash-text h3").text(photitle);
				$("#flash-text p").text($(phocaption).text());
				$("#flash-text h3, #flash-text p").fadeIn(400);
				$("#flash-text a").attr('href', link);
			});
		});
	});
	var photo = slideInfo.children('photo').text();
	var slogan = slideInfo.children('slogan').text();
	var hexcolor = slideInfo.children('hexcolor').text();

	$("#flash-slideGroup").append('<img src="' + photo + '" />');
	if(typeof t == "number") {
		$("#flash-slide img:eq(0)").animate({marginLeft: -($("#flash-slide img:eq(0)").width())}, 1000, "swing", function() {
			$("#flash-slide img:eq(0)").remove();
		});
	}
	
	$("#flash-slogan").fadeOut(500, function() {
		$("#flash-slogan").css('marginRight', '800px').css('color', hexcolor).css('opacity', '0').fadeIn(200);
		$("#flash-slogan").text($(slogan).text());
		$("#flash-slogan").animate({marginRight: '10px', opacity: '1'}, 2000);
	});

	if(typeof delay == "undefined") {
		delay = slideInfo.children('delay').text();
	}

	if(bUseTimer == 1) {
		i++;
		t = setTimeout("continueShow()", delay);
	}

}


