var tpgTickerCl = function(){
	this.initFromXML = function(tickerId, src, lang, cssClass){
		this.isInit = true;
		this.tickerId = tickerId;
		this.xmlSrc = src;
		this.lang = lang;
		
		var tickerObj = $('#' + tickerId);
		if (tickerObj == 'undefined') return;
		
		if (cssClass == 'undefined') cssClass = '';
		
		var _this = this;
		var nx = this.itemSpacing;
		var i = 0;
		$.ajax({
			type:"GET",
			url:src,
			dataType:"XML",
			success: function(xml){
				$(xml).find('item').each( function(){
					var title = $(this).find(lang).text();
					if (title == "") return;
					
					var link = $(this).attr('link');					
					
					i++;
					var id = tickerId + '_item_' + i;
					tickerObj.append('<a id="' + id + '" class="' + cssClass + '" href="' + link + '">' + title+ '</a>');
					
					var item = $('#' + tickerId + '_item_' + i);
					item.css('display', 'none');
					item.css('position', 'absolute');
					item.css('white-space', 'nowrap');
					item.css('left', nx);
					item.delay(200 * i).fadeIn(500);
					item.hover( function(){ if (_this.overPause != true) return; _this.isHovering = true; _this.stop(); }, function(){ if (_this.isStopped != true) return; if (_this.isHovering != true) return; _this.start(); } );
					nx += Number(item.width()) + _this.itemSpacing;
					_this.items.push(item.attr('id'));
				});
			}
		});
		
		setTimeout(function(){ _this.start(); }, 500);
	}
	
	this.initFromPage = function(tickerId){
		this.isInit = true;
		this.tickerId = tickerId;
		
		var tickerObj = $('#' + tickerId);
		if (tickerObj == 'undefined') return;
		
		var _this = this;
		var nx = this.itemSpacing;
		var i = 0;
		tickerObj.find('a').each( function(){
			i++;
			$(this).css('display', 'none');
			$(this).css('position', 'absolute');
			$(this).css('white-space', 'nowrap');
			$(this).css('left', nx);
			$(this).delay(200 * i).fadeIn(500);
			$(this).hover( function(){ if (_this.overPause != true) return; _this.isHovering = true; _this.stop(); }, function(){ if (_this.isStopped != true) return; if (_this.isHovering != true) return; _this.start(); } );
			nx += Number($(this).width()) + _this.itemSpacing;
			_this.items.push($(this).attr('id'));
		});
		
		setTimeout(function(){ _this.start(); }, 500);
	}

	this.doScroll = function(){
		if (this.isInit != true) return "Ticker Instance needs to be initialized first";
		var _this = this;
		$('#' + this.tickerId).find('a').each( function(){
			var nx = Number(String($(this).css('left')).replace("px", "")) - _this.scrollSpeed;
			$(this).css('left', nx);
		});
		
		$('#' + this.tickerId).find('a').each( function(){
			var nx = Number(String($(this).css('left')).replace("px", ""));
			var itemWidth = Number($(this).width());
			if (nx <= 0 - itemWidth - (_this.itemSpacing / 2)){
				var tailObj = $('#' + _this.items[_this.items.length - 1]);
				var endx = Number(String(tailObj.css('left')).replace("px", "")) + tailObj.width() + _this.itemSpacing;
				$(this).css('left', endx);
				$(this).css('display', 'none');
				$(this).fadeIn(500);
				var swapId = _this.items.shift();
				_this.items.push(swapId);
			}
		});
	};
	
	this.start = function(){
		if (this.isInit != true) return "Ticker Instance needs to be initialized first";
		if (this.tickerInterval != -1) return;
		this.isStopped = false;
		var _this = this;
		this.tickerInterval = setInterval(function(){ _this.doScroll(); }, 41);
	}
	
	this.stop = function(){
		if (this.isInit != true) return "Ticker Instance needs to be initialized first";
		clearInterval(this.tickerInterval);
		this.tickerInterval = -1;
		this.isStopped = true;
	}
	
	this.lang = "en";
	this.isHovering = false;
	this.isStopped = false;
	this.isInit = false;
	this.tickerId = null;
	this.tickerInterval = -1;
	this.items = new Array();
	
	this.scrollSpeed = 1;
	this.overPause = true;
	this.itemSpacing = 30;
}
