$.fn.carousel = function() {
	return this.each(function() {
	
		this.init = function() {
			this.gridvars = {width:$('div.carousel-container',this).width(),height:$('div.carousel-container',this).height(),locked:false,loop:true,autoplay:null};

			var _buttons = new Array();

			for(var i=0; i < $('div.item',this).length; i++) {
				_buttons.push('<div class="item-control ui-helper-float-left"><img src="http://www.lg.com/us/ui/smart-tv/img/bg-hero-item.png" alt="" /></div>');
			}

			$('div.cnscarousel-header').append('<div class="controls ui-helper-clear-after" style="width:'+(20*$('div.item',this).length)+';">'+_buttons.join("")+'</div>');

			$('div.item-control').click(function() {
				var _grid = $('div.carousel')[0];
				if(!_grid.gridvars.locked) {
					if(_grid.getCurrentPage() != $('div.item-control').index(this)) {
						window.clearInterval(_grid.gridvars.autoplay);
						$(this).addClass('highlight');
						_grid.scroll($('div.item-control').index(this),($('div.item-control').index(this) < _grid.getCurrentPage()) ?'left':'right');
					}
				}
			});
			
			$
			$('div.item-control').bind('mouseenter', function() {
				$(this).addClass('hover');
			});

			$('div.item-control').bind('mouseleave', function() {
				$(this).removeClass('hover');
			});

			this.gridvars.autoplay = window.setInterval("$('div.carousel')[0].scroll($('div.carousel')[0].getNextPage('right'),'right')",10000);
		
			this.update(0);
		}

		this.update = function(index) {
			//mjaTrack('us/tv-audio-video/discoverlgtvs/card'+(index+1)+'_views');
			$('div.item-control').removeClass('highlight').filter(':eq('+index+')').addClass('highlight');
		}
		
		this.scroll = function(index,direction) {
			if(!this.gridvars.locked) {
				this.gridvars.locked = true;
				this.gridvars.width = $('div.carousel-container',this).width();
				var _directions = {
					"left":[{left:-this.gridvars.width,top:0},{left:"+="+this.gridvars.width+"px",top:0}],
					"right":[{left:this.gridvars.width,top:0},{left:"-="+this.gridvars.width+"px",top:0}]
				};
				$('div.item:eq('+index+')').css(_directions[direction][0]);

				this.update(index);

				$('div.item:eq('+index+')',this).stop().animate(_directions[direction][1],2400,'swing',function() {
					var _grid = $(this).parents('div.carousel')[0];
					_grid.gridvars.locked = false;	
				});

				$('div.item:eq('+this.getCurrentPage()+')',this).stop().animate(_directions[direction][1],2400,'swing',function() {
					var _grid = $(this).parents('div.carousel')[0];
					$(this).css({left:-3000});
					_grid.gridvars.locked = false;	
				});
			}
		}

		this.getCurrentPage = function() {
			var _index = -1;
			$('div.item',this).each(function(i) {
				if(parseInt($(this).css('left')) == 0 && parseInt($(this).css('top')) == 0) {
					_index = i;
				}
			});
			return _index;
		}

		this.getNextPage = function(direction) {
			var _index = -1;
			if(direction == "left") {
				_index = this.getCurrentPage()-1;
				if(_index < 0) {
					_index = (this.gridvars.loop) ? $('div.item',this).length-1 : -1;
				}
			}
			if(direction == "right") {
				_index = this.getCurrentPage()+1;
				if(_index > $('div.item',this).length-1) {
					_index = (this.gridvars.loop) ? 0 : -1;
				}
			}
			return _index;			
		}

		this.init();
	});
}
