﻿
var intval = "";
var inc;
var _uid;
function LoadRotator(xmlName, ulID, increment, showNextPrev, linkColor) {

	_uid = ulID;
	inc = increment;
	//Load LI
    
	var liList = "";
	$.ajax(
		{
			type: "GET",
			url: xmlName,
			dataType: "xml",
			error: function (err, str, obj) {
				//alert(err.responseText);

			},
			success: function (xml) {
				for (var tt = 0; tt < 10; tt++) {

				}
				var first = true;
				$(xml).find('item').each(function () {

					var image_text = $(this).find('image').text();
					var url_text = $(this).find('url').text();
					var text = "";
					if (!first) {
						if (!showNextPrev)

							text = "<li><a href=\"" + url_text + "\"><img border=\"0\" src=\"" + image_text + "\"/></a></li>";
						else {
							text = "<li>";
							text += "<div style=\"width:95%;text-align:right;padding-right:10px;\"><a class=\"roratorLink\" href=\"javascript:rotate('" + ulID + "','previous', true);\">&laquo;&nbsp;previous</a>&nbsp;&nbsp;&nbsp;";
							text += "&nbsp;&nbsp;&nbsp;<a class=\"pauseLink\" href=\"javascript:pause();\">pause</a>&nbsp;&nbsp;&nbsp;&nbsp;";
							text += "<a class=\"roratorLink\" href=\"javascript:rotate('" + ulID + "','next', true);\">next&nbsp;&raquo;</a></div>";
							text += "<a href=\"" + url_text + "\"><img border=\"0\" src=\"" + image_text + "\"/></a>";
							text += "</li>";
						}

					}
					else {
						text = "<li class='show'><a href=\"" + url_text + "\"><img border=\"0\" src=\"" + image_text + "\"/></a></li>";
						if (!showNextPrev)
							text = "<li class='show'><a href=\"" + url_text + "\"><img border=\"0\" src=\"" + image_text + "\"/></a></li>";
						else {
							text = "<li class='show'>";
							

							text += "<div style=\"width:95%;text-align:right;padding-right:10px;\"><a class=\"roratorLink\" href=\"javascript:rotate('" + ulID + "','previous', true);\">&laquo;&nbsp;previous</a>&nbsp;&nbsp;&nbsp;";
							text += "&nbsp;&nbsp;&nbsp;<a class=\"pauseLink\" href=\"javascript:pause();\">pause</a>&nbsp;&nbsp;&nbsp;&nbsp;";
							text += "<a class=\"roratorLink\" href=\"javascript:rotate('" + ulID + "','next', true);\">next&nbsp;&raquo;</a></div>";
							text += "<a href=\"" + url_text + "\"><img border=\"0\" src=\"" + image_text + "\"/></a>";
							text += "</li>";
						}

						first = false;
					}
					liList += text;
				});


				$("ul#" + ulID).append(liList);

				$("ul#" + ulID + " li").css({ opacity: 0.0 });
				//Get the first image and display it (gets set to full opacity)
				$("ul#" + ulID + " li:first").css({ opacity: 1.0 });


				intval = setInterval("rotate('" + ulID + "','next', false)", increment);

			}
		});


}
function pause() {
	var action = "pause";

	$("a.pauseLink").each(function (index) {
		if ($(this).text() == "play") {
			action = "play";

			$(this).text("pause");
		}
		else {
			$(this).text("play");
		}

	});

	if (action == "pause") {
		clearInterval(intval)
	}
	else {
		rotate(_uid, 'next', true);
	}


}

function rotate(ulID, type, click) {
	
	if (click) {
		clearInterval(intval)
	}
	var current;
	if ($("ul#" + ulID + " li.show") != null) {
		current = $("ul#" + ulID + " li.show");
	}
	else {
		current = $("ul#" + ulID + " li:first");
	}

	var next;
	if ((current.next().length) > 0) {
		if ((current.next().hasClass('show'))) {
			next = $("ul#" + ulID + " li:first");
		}
		else {
			next = current.next();
		}
	}
	else {
		next = $("ul#" + ulID + " li:first");
	}

	var previous;
	if ((current.prev().length) > 0) {
		if ((current.prev().hasClass('show'))) {
			previous = $("ul#" + ulID + " li:last");
		}
		else {
			previous = current.prev();
		}
	}
	else {
		previous = $("ul#" + ulID + " li:last");
	}
	if (type == 'next') {
		//Set the fade in effect for the next image, the show class has higher z-index
		next.css({ opacity: 0.0 })
		next.addClass('show')
		next.animate({ opacity: 1.0 }, 1000);
	}
	else {
		previous.css({ opacity: 0.0 })
		previous.addClass('show')
		previous.animate({ opacity: 1.0 }, 1000);
	}
	//Hide the current image
	current.animate({ opacity: 0.0 }, 1000)
	current.removeClass('show');

	if (click) {
		intval = setInterval("rotate('" + ulID + "','next')", inc);
	}

};
