var sportmens = function( container, ball ){
	var
		isLocked = 1,
		iconsCounter = 0,
		afterFunction = 0,
		selectedBlock,
		lottery_numbers = $('#lottery_numbers > div'),
		current_lottery = $('#ball_results .current_lottery'),
		tv_glow = $('#rules_icons .tv_glow'),
		current_item = container.find('.note.selected');

		$('#lottery_numbers').hide();
		$('#results_icons').hide();
		$('#center_ball').prepend(tv_glow);

	var special = {
		results:{
			afterShow:function(){
				for (var i = 0; i < lottery_numbers.size(); i++){
					delay_animation(i);
				}
				
				current_lottery.animate(
					{opacity:1},
					300,
					function(){this.style.filter=''}
				);
				isLocked = 0;
			},
			beforeShow:function(){
				lottery_numbers.css('opacity', 0);
				current_lottery.css('opacity', 0);
			},
			beforeHide:function(){
				lottery_numbers.animate(
					{opacity:0},
					300
				);
				current_lottery.animate(
					{opacity:0},
					300
				);
			}
		},
		
		rules: {
			afterShow: function(){
				if( !$.browser.msie || ( $.browser.msie && $.browser.version < 7 ) ){
					tv_glow.fadeIn();
				} else {
					tv_glow[0].style.display = 'block';
				}
				isLocked = 0;
			},
			beforeHide:function(){
				tv_glow.removeAttr('style');
			}
		}
	};
	
	function delay_animation(i){
		setTimeout(function(){
			lottery_numbers.eq(i).animate({opacity:1}, 200, function(){this.style.filter=''});
		}, 100*i);
	}

	function afterAnimate(){
		iconsCounter--;

		if( iconsCounter == 0 ){
			if( afterFunction ){
				afterFunction();
				afterFunction = 0;
			}
			else{
				if( special[selectedBlock] && special[selectedBlock].afterShow ){
					special[selectedBlock].afterShow();
				}
				else{
					isLocked = 0;
				}
			}
		}
	}
	
	function animation( blockName, hide ){
		selectedBlock = blockName;
		var i, icons = $('#' + blockName + '_icons').find('div');

		if( !icons[0].cssLeft ){
			for(i = 0; i < icons.length; i++){
				icons[i].cssLeft = icons.eq(i).css('left');
				icons[i].cssTop = icons.eq(i).css('top');
			}
		}
			
		if( !hide ){
			if( special[selectedBlock] && special[selectedBlock].beforeShow ){
				special[selectedBlock].beforeShow();
			}
			icons.css({
				left: -100,
				top: 100
			});	
		}
		else if( special[selectedBlock] && special[selectedBlock].beforeHide ){
			special[selectedBlock].beforeHide();
		}

		afterFunction = hide;
		iconsCounter = icons.length;
		for(i = 0; i < icons.length; i++){
			if( !hide ){
				var
					iLeft = icons[i].cssLeft,
					iTop = icons[i].cssTop
					
				if( iLeft == 'auto' ){
					iLeft = 0;
				}
				if( iTop == 'auto' ){
					iTop = 0;
				}
			} else {
				var
					iLeft = -100,
					iTop = 100
			}
			
			icons.eq(i).animate(
				{
					left: iLeft,
					top: iTop
				},
				400,
				afterAnimate
			);
		}
	}
	
	function actions(me){
		var	parent = me.parents('.note:first');
			
		if( isLocked || parent.hasClass('selected') ){
			return false;				
		} 
		isLocked = 1;
		
		var
			j_prev_item = current_item.find('svg, shape'),
			j_new_item = parent.find('svg, shape');
		
		j_prev_item.find(":first-child").attr('fill', '#a7d927');
		j_prev_item.attr('fillcolor', '#a7d927');
		
		var shape = parent.find('.shape')[0];
		
		current_item = parent;
			
		j_new_item.find(":first-child").attr('fill', '#f7eed7');
		j_new_item.attr('fillcolor', '#f7eed7');
		
		
		animation( selectedBlock, 
			function(){
				container.find('.selected').removeClass('selected');
				parent.addClass('selected');
				$('#ball_' + me[0].id).addClass('selected');
				$('#' + me[0].id + '_icons').addClass('selected');
				animation( me[0].id, 0 );
			}
		);
	}
	
	var
		links = container.find('span.pseudo_link'),
		links_amount = links.size(),
		current_link,
		current_link_position = 0;
	
	links.each(function(i){
		links.eq(i).click(
			function(){
				current_link = $(this);
				current_link_position = i;
				actions(current_link);
			}
		);
	});

	var
		content = $('#center_ball .ball_transparent'),
		arrow_left = content.find('.arrow_left'),
		arrow_right = content.find('.arrow_right');
	
	if( $.browser.msie && $.browser.version == 6 ){
		var
			img_src = 'i/arrow_empty.png',
			img_left = document.createElement('img'),
			img_right = document.createElement('img'),
			arrow_left_img = arrow_left.find('img').addClass('not_display'),
			arrow_right_img = arrow_right.find('img').addClass('not_display');
			
		img_left.src = img_src;
		img_right.src = img_src;
		
		arrow_left.append($(img_left).removeAttr('width, height'));
		arrow_right.append($(img_right).removeAttr('width, height'));
	}
	
	arrow_left.click(function(){
		if( current_link_position - 1 >= 0 ){
			current_link_position--;
		} else {
			current_link_position = links_amount - 1;
		}
		actions(links.eq(current_link_position));
		return false;
	});
	
	arrow_right.click(function(){
		if( current_link_position + 1 < links_amount ){
			current_link_position++;
		} else {
			current_link_position = 0;
		}
		actions(links.eq(current_link_position));
		return false;
	});

	var image = new Image();
	image.onload = function() {
		$('#results_icons').show();
		$('#lottery_numbers').show();
		animation('results', 0);
	};
	image.src = $('.ball_transparent img')[0].src;
};


$(
	function() {
		sportmens($('#blocks'), $("#center_ball"));
	}
);
