$(document).ready(function(){
	
	/*  disable fighter info click */
	$(".showfightersinfo").each(function(index) {                                                           
    		$(this).attr('onClick', 'return false;');
    		current_href = $(this).attr('href');
    		$(this).attr('href', current_href.substring(current_href.lastIndexOf('/')+1) );
        });

	/* show popup with fighter's info */
	$('a.showfightersinfo').click(function(e){
		
		if( ! $(this).hasClass('active') && $(this).attr('href') != undefined && $(this).attr('href').length > 3 ) {

			$.get(('/api/fighter-info.php?id='+$(this).attr('href')), function(data) {
			
			$("#fighter-info-box .fighter-info-box").html(data);
			
			$(this).addClass('active');
			fighter_id = $(this).attr('rel');
			
			var winWidth = ( window.innerWidth ) ? window.innerWidth : ( ( document.all ) ? document.documentElement.offsetWidth : null );
			var winHeight = ( window.innerHeight ) ? window.innerHeight : ( ( document.all ) ? document.documentElement.offsetHeight : null );
			
			var boxWidth = $("#fighter-info-box").innerWidth();
			var boxHeight = $("#fighter-info-box").innerHeight();
			var winScroll = $(window).scrollTop();
	
			var offset = $(this).offset();
			
			var padd_y = 20;
			var padd_x = 20;
			var padd_top = 0;
			var padd_left = 0;
			
			if( winHeight + winScroll - 30 > e.pageY + boxHeight ) {
				
				padd_top = ( e.pageY + padd_y );
				
				
			} else {
				
				padd_top = ( winHeight - boxHeight + winScroll - padd_y );
				padd_x = 40;
				
			}
			
			padd_left = ( e.pageX > ( winWidth / 2 ) ) ? e.pageX - boxWidth - padd_x : e.pageX + padd_x;
			
			    $("#fighter-info-box").css("top",padd_top+"px").css("left",padd_left+"px").delay(0).fadeIn(700,function(){$(this).addClass('active');});
			    //alert('Load was performed.');
			});			
			
			
		} else {
			
			$("#fighter-info-box").fadeOut().removeClass('active');
			$('a.showfightersinfo').removeClass('active');
			
		}
		
		return false;
	
	});
	
	/* hide popup */
	$("#fighter-info-box").live('mouseleave',function(){
		
		if( $(this).hasClass('active') ) {
			$(this).fadeOut().removeClass('active');
			$('a.showfightersinfo').removeClass('active');
			$("#fighter-info-box .fighter-info-box").html('');
		}
	});
	
	

});

