/*
 * jQuery tsign rotator
 */
(function($) {
$.widget('tsign.icanslider', {

	_init: function() {
		var self = this;
		this.container = $(this.element).find('ul');
		this.mode = ($(this.element).hasClass('small')) ? 2 : 1 ;
		this.items = this.container.children();
		this.itemWidth = 0;
		this.containerWidth = 0;
		this.items.each(function(){
			var item = $(this);
			item.addClass(self.options.itemClassTrigger);
			self._animate($(item), false);
			itemWidth = item.width();
			self.containerWidth += 270;
		});
		this.currentItem = 0;
		this.container.width(this.containerWidth);
		this.container.css('left', 0);
		$(this.options.nextButtonTrigger).click(function(){
			self._setNext();
		});
		$(this.options.prevButtonTrigger).click(function(){
			self._setPrev();
		});
		this._animate(this.items[0], true);
		
		switch(this.mode) {
			case 1:
				self.container.prepend(self.container.children(":last-child"));
				self.container.prepend(self.container.children(":last-child"));
				break;
				
			case 2:
				self.container.prepend(self.container.children(":last-child"));
				self.container.prepend(self.container.children(":last-child"));
				break;
		}
		
	},
	
	_setNext: function() {
		var self = this;
		self._animate($(self.items[self.currentItem]), false);
		self.currentItem = (self.currentItem + 1 <= self.items.size() - 1) ? self.currentItem + 1 : 0;
		var pos = parseInt(self.container.css('left'));
		var move = pos + (itemWidth);
		self.container.append(self.container.children(":first-child"));
		self.container.css('left', move);
		self.container.animate({left: 0});
		self._animate($(self.items[self.currentItem]), true);
	},
	
	_setPrev: function() {
		var self = this;
		self._animate($(self.items[self.currentItem]), false);
		self.currentItem = (self.currentItem - 1 < 0 ) ? self.items.size() -1  : self.currentItem - 1;
		var pos = parseInt(self.container.css('left'));
		var move = pos - (itemWidth);
		self.container.prepend(self.container.children(":last-child"));
		self.container.css('left', move);
		self.container.animate({left: 0});
		self._animate($(self.items[self.currentItem]), true);
		
	},
	
	
	_animate :function(obj, active) {
		var self = this;
		switch(this.mode) {
			case 1:
				if(active) {
					$(obj).animate({width: '270', marginLeft: 55, marginRight: 20, marginTop: -15});
//					$(obj).find('img').animate({opacity: "1", width: '270', height: '60' });
					$(obj).find('img').animate({width: '194', height: '70' });
				}
				else {
					$(obj).animate({width: '150', marginLeft: '0', marginRight: 0, marginTop: 0});
//					$(obj).find('img').animate({opacity: "0.5", width: '150', height: '30' });
					$(obj).find('img').animate({width: '111', height: '40' });
				}
				break;
				
			case 2:
				if(active) {
					$(obj).animate({width: '260', height: 110,  marginTop: '0', marginLeft: '-130', marginRight: '-130'});
					var img = $(obj).find('img');
//					img.animate({opacity: "1", width: '260', height: '94'});
					img.animate({width: '260', height: '94'});
				}
				else {
					$(obj).animate({width: '100', height: 50, marginTop: '120', marginLeft: '0', marginRight: '0'});
					var img = $(obj).find('img');
//					img.animate({opacity: "0.5", width: '100', height: '36'});
					img.animate({width: '100', height: '36'});
				}
				break;
		}
	}
	
	
});
icanslider = $.extend($.tsign.icanslider, {
	version: '1.0.0',
	defaults: {
		itemClassTrigger: 'item',
		prevButtonTrigger: '#button1',
		nextButtonTrigger: '#button2'
	}
});

})(jQuery);