var Ticker = new Class({
	
	initialize: function(oContainer, sURL, iItemsPerRow){
		this.moContainer = $(oContainer);
		this.moContainer.getElement('ul').empty();
		
		this.mbWide = (this.moContainer.hasClass('wide'));
		this.miItemsPerRow = iItemsPerRow || 4;
		
		(new Request.JSON({
			url: sURL, 
			method: 'get', 
			onComplete: this.build.bind(this)
		})).send();
	}, 
	
	build: function(oResponse) {
		if (!$type(oResponse) || oResponse.length === 0){
			return;
		}
		
		// add extra ul for wide tickers
		if (this.mbWide === true){
			this.moContainer.getElement('div.list').adopt(new Element('ul'));
		}
		
		this.moContainer.removeClass('loading');
		var iListCounter = 0;
		$A(oResponse).each(function(oItem, iIndex){
			if (this.mbWide === true && iIndex >= this.miItemsPerRow && iListCounter === 0){
				iListCounter += 1;
			}
			
			// preload image
			(new Asset.image(oItem.image));
			// prepare element
			var o_item = new Element('li');
			var o_link = new Element('a', {
				'href': ($type(oItem.window) !== false ? '#' : oItem.link),
				'title': oItem.title,
				'html': oItem.title
			});
			// if window defined, open new window on click
			if ($type(oItem.window) !== false && oItem.window.match(/^\d+x\d+$/) !== null){
				var a_size = oItem.window.match(/^(\d+)x(\d+)$/);
				o_link.addEvent('click', function(oEvent){
					oEvent.stop();
					window.open(oItem.link, '', 'width=' + a_size[1] + ',height=' + a_size[2]);
				});
			}
			
			o_item.adopt(o_link);
			// inject item
			this.moContainer.getElements('div.list ul')[iListCounter].adopt(o_item);
			
			// finish properties
			o_item.data = oItem;
			o_item.addEvent('mouseenter', (function(){
				this.selectItem(o_item, true);
			}).bind(this)).addEvent('mouseleave', (function(){
				this.deSelectItem(o_item);
			}).bind(this));
			
		}, this);
		
		this.nextItem();
		this.startLooping();
	},
	
	nextItem: function(){
		// reset item counter
  		if ($type(this.miCurrentItem) === false || this.miCurrentItem < 0 || this.miCurrentItem >= this.moContainer.getElements('li').length){
			this.miCurrentItem = 0;
		}
		
		// show current item
		this.selectItem(this.moContainer.getElements('li')[this.miCurrentItem]);
		this.miCurrentItem += 1;
	},
	
	selectItem: function(oItem, bStopLooping) {
		// on user intervention, stop looping throught items
		if ($type(bStopLooping) !== false && bStopLooping === true){
			this.stopLooping();
		}
		
		// prepare title link
		var o_link = new Element('a', {
			'html': oItem.data.title, 
			'class': 'title ' + oItem.data.section, 
			'title': oItem.data.title, 
			'href': ($type(oItem.data.window) !== false ? '#' : oItem.data.link)
		});
		// prepare 'more' link
		var o_more_link = new Element('a', {
			'text': (oItem.data.comments > 0 ? oItem.data.comments + ' ' + (oItem.data.comments == 1 ? oItem.data.text.comment : oItem.data.text.comments) + ' | ' : '') + oItem.data.text.more, 
			'class': 'comments',
			'href': ($type(oItem.data.window) !== false ? '#' : oItem.data.link)
		});
		// if window, open link in window
		if ($type(oItem.data.window) !== false && oItem.data.window.match(/^\d+x\d+$/) !== null){
			var a_size = oItem.data.window.match(/^(\d+)x(\d+)$/);
			o_link.addEvent('click', function(oEvent){
				oEvent.stop();
				window.open(oItem.data.link, '', 'width=' + a_size[1] + ',height=' + a_size[2]);
			});
			o_more_link.addEvent('click', function(oEvent){
				oEvent.stop();
				window.open(oItem.data.link, '', 'width=' + a_size[1] + ',height=' + a_size[2]);
			});
		}
		
		// clear container
		var o_teaser= this.moContainer.getElement('div.teaser');
		o_teaser.empty();
		o_teaser.adopt(o_link);
		// teaser image
		o_teaser.adopt((new Element('img', {
			'src': oItem.data.image
		})));
		
		// teaser text
		var o_body = new Element('div', {
			html:'<p>' + oItem.data.teaser + '</p><hr>', "class":"body"});
		o_body.adopt(o_more_link);
		o_teaser.adopt(o_body);
		// reset all items
		this.moContainer.getElements('li').each(function(oLi) {
			oLi.set('class', '');
		});
		oItem.addClass("active").addClass(oItem.data.section);
	}, 
	
	deSelectItem: function(){
		this.miCurrentItem -= 1;
  		this.nextItem();
  		this.startLooping();
	}, 
	
	stopLooping: function(){
  		$clear(this.moLoopEvent);
	}, 
	
	startLooping:function() {
		this.moLoopEvent = this.nextItem.periodical(5000, this);
	}
});

