var S = jQuery.noConflict();

var rotatingList = {
	timer: [], delay: [], current: [], running: [],
	show: function(objId, menu, timer, idx) // show selected list item
	{ // CSS identification of the list of items, milliseconds of delay, timer controller, current index
		var newlist = S(objId), staticRandom=false;
		if (idx == "rand") {
			idx = Math.floor(Math.random() * newlist.length);
			staticRandom = true;
		}
		else {
			idx = (idx == "run")? rotatingList.current[timer] : ((idx == parseInt(idx))? parseInt(idx) : 0);
			idx = (idx >= newlist.length)? 0 : ((idx < 0)? newlist.length - 1 : idx);
		}
		if (menu && menu.length > 0) {
			S(menu + " .itemsel").attr({ className:"item" });
			S(S(menu + " .item")[idx]).attr({ className:"itemsel" });
		}
		rotatingList.current[timer] = idx;
		newlist.css({ display:"none" });
		S(newlist[idx]).fadeIn();
		if (rotatingList.running[timer] && !staticRandom) {
			rotatingList.timer[timer] = setTimeout("rotatingList.show('" + objId + "', '" +
			 menu + "', " + timer + ", " + (idx + 1) + ");", rotatingList.delay[timer]);
		}
	},
	control: function(self, objId, menu, listId, idx) // controls interraction
	{
		if (!self || !objId | !menu || !listId || (idx !== 0 && !idx)) { return; }
		var halt = false;
		if (idx == "play") { // pause/play toggle
			if (rotatingList.running[listId] === true) { // list running, then stop
				S(menu + " .playsel").attr({ className:"play" }); // display paused control
				rotatingList.running[listId] = false; // turn off the run status
				clearTimeout(rotatingList.timer[listId]); // kill the current timer
			}
			else { // list is stopped, start from current item
				S(menu + " .play").attr({ className:"playsel" }); // display play control
				rotatingList.running[listId] = true; // turn on the run status
				rotatingList.show(objId, menu, listId, rotatingList.current[listId]); // start rotation
			}
		}
		else if (idx == "next") { // next item requested
			rotatingList.current[listId] = rotatingList.current[listId] + 1;
			halt = true;
		}
		else if (idx == "prev") { // previous item requested
			rotatingList.current[listId] = rotatingList.current[listId] - 1;
			halt = true;
		}
		else if (idx == parseInt(idx)) { // an item was selected
			rotatingList.current[listId] = idx;
			halt = true;
		}
		if (halt === true) {
			S(menu + " .playsel").attr({ className:"play" }); // display paused control
			rotatingList.running[listId] = false; // turn off the run status
			clearTimeout(rotatingList.timer[listId]); // kill the current timer
			rotatingList.show(objId, menu, listId, rotatingList.current[listId]); // show selected
		}
	},
	menu: function(objId, menu, listId, pause)
	{ // create the interractive controls
		var obj=S(objId), item="", i, sel, playmode=(pause)?"play":"playsel";
		var ctls = "<div class=\"media_controls\"><\/div>";
		var play = "<a class=\"" + playmode + "\" href=\"javascript:rotatingList.control(this,'" +
		 objId + "','" + menu + "','" + listId + "','play')\" title=\"pause/play\">&nbsp;</a>";
		for (i=0; i < obj.length; i++) { // add a control for each roating object
			item += "<a class=\"item\" href=\"javascript:rotatingList.control(this,'" + objId +
			 "','" + menu + "','" + listId + "'," + i + ")\" title=\"#" + (i + 1) + "\">&nbsp;<\/a>";
		}
		var prev = "<a class=\"prev\" href=\"javascript:rotatingList.control(this,'" + objId +
		 "','" + menu + "','" + listId + "','prev')\" title=\"previous\">&nbsp;<\/a>";
		var next = "<a class=\"next\" href=\"javascript:rotatingList.control(this,'" + objId +
		 "','" + menu + "','" + listId + "','next')\" title=\"next\">&nbsp;</a>";
		S(menu).wrapInner("<div style=\"position:relative\"><\/div>"); // wrap inner with relative div
		S(menu + ">div").append(ctls); // insert the controls container
		S(menu + " .media_controls").append(play + item + prev + next); // add the controls
	},
	init: function(objId, delay, options) // initialize rotating list
	{ //  CSS identification of the list of items, milliseconds of delay
		if (S(objId).length === 0) { return; } // zero referenced objects found
		var len=rotatingList.timer.length, menu="", item=0;
		rotatingList.timer[len] = null; // create timer controller
		delay = parseInt(delay);
		rotatingList.delay[len] = (delay > 500)? delay : 500; // store animation delay
		rotatingList.current[len] = 0; // set start point
		rotatingList.running[len] = true; // set run indicator
		if (options) { // setup any additional options and parameters
			if (options.index) { item = (options.index == "rand")? "rand" : parseInt(options.index); }
			if (options.menu) {
				menu = options.menu;
				rotatingList.menu(objId, menu, len, (options.index && options.index == "rand")? true : false);
			}
		}
		rotatingList.show(objId, menu, len, item); // start rotation
	}
};

