if(!window.WSC){var WSC={};}
(function($){
	WSC.Newsticker = function(id, url){
		/* set the options first */
		var options =   $.extend({
			Type:	'ticker',
			Speed:	1,
			Time:	5,
			Debug:	false
		}, arguments[2] || {});
		
		/* Set some private variables */
		var Counter = 0;
		var Links = null;
		var A1 = null;
		var A2 = null;
		var $NewsBar, Move, $Scroller, $Container, $Current, $Debug;
		var width, height, padTop, padRight, padBottom, padLeft, scrollwidth;
		var pause = false;
		
		/* Some private methods here */
		var _page_load_ticker = function(){
			/* Hide the original */
			$('A:first', $NewsBar).css({display:'none'});
			/* Create the container */
			$Container = $('<div></div>').css({height: height+'px', position:'relative', overflow:'hidden'});
			$Scroller = $('<div></div>').css({position:'absolute', left: '0px', whiteSpace:'nowrap', width:'auto'});
			/* Add the links */
			for (var x=0, y=Links.length; x<y; x++) {
				$Scroller.append('<a href="'+  Links[x].link +'">'+ Links[x].text +'</a><span>&bull;</span>');
			}
			$Container.append($Scroller);
			$NewsBar.append($Container);
			scrollwidth = $Scroller.width();
			$Scroller.html($Scroller.html() + $Scroller.html());
			/* Set the speed */
			if(options.Speed > 5){options.Speed = 5; }
			var arrSpeed = [.01, .03, .05, .07, .09, .11];
			var speed = scrollwidth/arrSpeed[options.Speed];
			_ticker(speed);
			/* Set the hover action to pause */
			//$Scroller.hover(function(){ $(this).stop();});
			if(options.Debug){
				$NewsBar.append($Debug);
			}
		};

		var _ticker = function(speed){
			$Scroller.animate({left: '-='+ scrollwidth}, speed, "linear", function(){ $Scroller.css("left", 0); _ticker(speed);});
		};	


		var _page_load_scroll = function(){
			/* Get the original link */
			$NewsBar.css({position: 'relative', height: height+'px', overflow: 'hidden'});
			
			A1 = $('A:first', $NewsBar).css({position:'absolute', whiteSpace:'nowrap', top:padTop, left:padLeft}).attr('href', Links[0].link).html(Links[0].text);
			/* Create the new link */
			A2 = A1.clone().attr('href','').html('');
			$NewsBar.append(A2);
			Current = A1;
			switch (options.Type.toLowerCase()) {
				case 'fade' :
					A2.css({opacity: 0});
					aBefore = function(){ Current.hide().css({opacity: 0});};
					aProperties = {opacity: 1}; 
					aDuration = "normal";
				  	break;
				
				case 'up' :
					A2.css({top: height});
					aBefore = function(){ Current.hide().css({top: height});};
					aProperties = {top: padTop}; 
					aDuration = "normal";
					break;
				case 'down' :
					A2.css({top: -height});
					aBefore = function(){ Current.hide().css({top: -height});};
					aProperties = {top: padTop}; 
					aDuration = "normal";
					break;
				case 'left' :
					A2.css({left: width});
					aBefore = function(){ Current.hide().css({left: width});};
					aProperties = {left: padLeft}; 
					aDuration = "slow";
					break;
			}
			var method = function(){_scroll();};
			var ourInterval = setInterval(method, (options.Time*1000));
		};

		var _scroll = function(){
			aBefore();
			Counter = ++Counter%Links.length;
			Current = (Current==A1) ? A2 : A1;
			Current.attr('href', Links[Counter].link).html(Links[Counter].text).show().animate(aProperties,aDuration);
		};
		

		/* Page load */
		var load = (options.Type=='ticker') ? _page_load_ticker : _page_load_scroll;
		$(function(){ 
			$NewsBar = $('#'+ id).removeClass('hide');
			/* Get some variables */
			width = $NewsBar.width();
			height = $NewsBar.height();
			padTop = $NewsBar.css('padding-top');
			padRight = $NewsBar.css('padding-right');
			padBottom = $NewsBar.css('padding-bottom');
			padLeft = $NewsBar.css('padding-left');
			
			/* get the data */
			$.getJSON(url, function(data){Links=data; load();});
		}); 
	
		
		/* Return object */
		var public = {};
	
		return public;
	};
})(jQuery);