/*
 * JTicker 0.5 Beta
 * Adjusted by Dom Kipps
 * A ticker plugin for the jquery library.
 */
$.JTickerObjArray = new Array();
$.fn.jticker = function(passedOptions) {
	var options = {
	    TickerID: "",
		delay: 1000,
		newwindow: false,
		url: "data.xml",
		transition: "slide",
		speed: "slow",
		prevBtn: "prev",
		nextBtn: "next",
		prevBtn_a: "a-left",
		contentcounter: -1,
		ArrayIndex: -1,
		dataXML: {
		},
		timerid: -1,
		setRSS: function(ArrayIndex) {
			$("#" + options.TickerID).hide();
			// Load Data
			$.get(options.url, function(data){
				options.dataXML = data;
				options.contentcounter = -1;
				options.FillSlide();
				$("#" + options.prevBtn).fadeTo("slow", 0.6);
				$("#" + options.prevBtn_a).addClass("disabled");
			});
		},
		FillSlide: function() {
			var Item, TickerHTML, Title, URL, Desc, PrevBtn, NextBtn;
			
			$("#" + options.TickerID).empty();
			options.contentcounter++;
			if(options.contentcounter < -1) {
				options.contentcounter = -1;
			}
			if (options.contentcounter == $("item", options.dataXML).length) {
				options.contentcounter = 0;
			}
			Item = $("item", options.dataXML).get(options.contentcounter);
			Title = $("title", Item).text();
			URL = $("link", Item).text();
			TickerHTML = "<span class=\"JTickerTitle\"><a href=\"" + URL + "\""
			TickerHTML += ">" + Title + "</a></span>";
			
			$("#" + options.TickerID).append(TickerHTML);
			options.EnterSlide();
			
		},
	    ExitSlide: function() {
			if (options.contentcounter < 0) {
				options.contentcounter = -1;
				$("#" + options.prevBtn).fadeTo("slow", 0.6);
				$("#" + options.prevBtn_a).addClass("disabled");
			} else {
				$("#" + options.prevBtn).fadeTo("slow", 1);
				$("#" + options.prevBtn_a).removeClass("disabled");
			}
			if (options.contentcounter >= -1) {
				clearTimeout(options.timerid);
				switch (options.transition.toLowerCase()) {
					case "slide":
						$("#" + options.TickerID).slideUp(
							options.speed,
							options.ExitSlideStep2()
						);
						break;
					default:
						$("#" + options.TickerID).fadeOut(
							options.speed,
							options.ExitSlideStep2()
						);
						break;
				}
			} else {
				options.ExitSlideStep2(ArrayIndex);
			}
	    },
	    ExitSlideStep2: function() {
			var tempid;
			tempid = setTimeout("$.JTickerObjArray[" + options.ArrayIndex + "].FillSlide();", 500);
		},
	    EnterSlide: function() {
			var ArrayIndex = options.ArrayIndex;
			switch (options.transition.toLowerCase()) {
				case "slide":
					$("#" + options.TickerID).slideDown(
						options.speed,
						options.EnterSlideStep2()
					);
					break;
				default:
					$("#" + options.TickerID).fadeIn(
						options.speed,
						options.EnterSlideStep2()
					);
					break;
			}
	    },
	    EnterSlideStep2: function() {
			options.timerid = setTimeout("$.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", options.delay);
		}
	};
	if (passedOptions) {
		$.extend(options, passedOptions);
	}
	
	return this.each(function(){
		
		options.TickerID = this.id;
		$.JTickerObjArray.push(options);
		options.ArrayIndex = $.JTickerObjArray.length - 1;
		
		$("#" + options.TickerID).hover(function() {
			clearTimeout(options.timerid);
		}, function() {
			options.timerid = setTimeout("$.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", options.delay);
		});
		
		$("#" + options.nextBtn).hover(function() {
			clearTimeout(options.timerid);
		}, function() {
			options.timerid = setTimeout("$.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", options.delay);
		});
		
		$("#" + options.prevBtn).hover(function() {
			clearTimeout(options.timerid);
		}, function() {
			options.timerid = setTimeout("$.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", options.delay);
		});
		
		$("#" + options.nextBtn).click(function() {
			var tempid;
			tempid = setTimeout("$.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", 500);
			//alert('go forward one');
		});
		
		$("#" + options.prevBtn).click(function() {								
			options.contentcounter -= 2;
			var tempid;
			tempid = setTimeout("$.JTickerObjArray[" + options.ArrayIndex + "].ExitSlide();", 500);
		});
		
		$.JTickerObjArray[options.ArrayIndex].setRSS(options.ArrayIndex);
	});
};