var SelectionSliding = Class.extend({
	container: null,
	video_player: false,
	selected_id: -1,
	
	init: function(container,video_player) {
	    this.container = container;
	    this.video_player = video_player;

		this.refresh(this);
	},
	
	refresh: function(ref) {
		var w = $('#'+ref.container.attr('id') + ' .selection_all_content').width();
		if(w <= 0) {
			setTimeout(function() { ref.refresh(ref); },100);
			return;
		}
		
		var length = $('#'+ref.container.attr('id')+' .selection ul li').length;
		$('#'+ref.container.attr('id')+' .selection ul').css({width:(length * 122) + "px"})

		$('#'+ref.container.attr('id')+' .selection ul li').each(function(i,element) {
			$(element).click(function() {
				ref.select(i,element);
				return false;
			});
			
			if($(element).hasClass("selected")) {
			    ref.selected_id = i;
			    
				$('#'+ref.container.attr('id')+' .selection_content_sliding').css({ 
					marginLeft: -(i*w)+"px"
				});
				
				$('#'+ref.container.attr('id')+' .selection ul').css({
					backgroundPosition: (i*122+112/2-8)+"px 0px"
				});
				
				if(ref.video_player) {
				    $('#'+ref.container.attr('id')+' .selection_content .video')[i].SetVariable("player:jsPlay", "");
				}
			}
		});
	},
	
	select: function(i, element) {
	    if(this.video_player)
	        $('#'+this.container.attr('id')+' .selection_content .video')[this.selected_id].SetVariable("player:jsStop", "");
	    
		var w = $('#'+this.container.attr('id') + ' .selection_all_content').width();
		$('#'+this.container.attr('id')+' .selection_content_sliding').animate({ 
			marginLeft: -(i*w)+"px"
		}, {duration:500});
		
		var ref = this;
		$('#'+this.container.attr('id')+' .selection ul li').each(function(i,e) {
			$(e).removeClass("selected");
			if(e == element)
			    ref.selected_id = i;
		});
		
		if(this.video_player)
		    $('#'+this.container.attr('id')+' .selection_content .video')[this.selected_id].SetVariable("player:jsPlay", "");
		
		$(element).addClass("selected");
		
		$('#'+this.container.attr('id')+' .selection ul').animate({
			backgroundPosition: "("+(i*122+112/2-8)+"px 0px)"
		}, {duration:500});
	}
	
});