var x = 0;
var y = 0;
var posX = 0;
var posY = 0;
var helpBoxWidth=350;

var sur_lien=false;
var is_visible=false;

var ajaxRequeteHelpBox=null;
var last_param=0;

jQuery(document).ready(function(){
	$(document).mousemove(function(e){
		if(sur_lien)
		{
			x=e.pageX;
			y=e.pageY;
				
			if( ($(window).width()-x) < (helpBoxWidth+40) )
			{
				var width_temp=$('#helpBox').width();
				$('#helpBox').css('left',(x-width_temp));
			}
			else
			{
				$('#helpBox').css('left',(x+10) );
			}
			$('#helpBox').css('top', (y+20) );
			$('#helpBox').show();
		}
	}); 
})

/*
if (document.getElementById)
{
	if(navigator.appName.substring(0,3).toLowerCase() == "net")
	{
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = posSouris;
}

function posSouris(e)
{
	if(!e) e = window.event;
	x = e.clientX;
	y = e.clientY; 
	
	if(sur_lien)
	{
		// Si la bulle est visible, on calcule en temps reel sa position ideale
		
		// Si on n'est pas sous IE
		if (navigator.appName!="Microsoft Internet Explorer")
		{
			x=e.pageX;
			y=e.pageY;
		}
		else
		{
			if(document.documentElement.clientWidth>0)
			{
				x=event.x+document.documentElement.scrollLeft;
				y=event.y+document.documentElement.scrollTop;
			}
			else
			{
				x=event.x+document.body.scrollLeft;
				y=event.y+document.body.scrollTop;
			}
		}
		
		if( (document.body.offsetWidth-x) < (helpBoxWidth+40) )
		{
			var width_temp=$('#helpBox').width();
			$('#helpBox').css('left',(x-width_temp));
		}
		else
		{
			$('#helpBox').css('left',(x+10) );
		}
		$('#helpBox').css('top', (y+20) );
		$('#helpBox').show();

	}
	
}*/

function helpBox(content)
{
	sur_lien=true;
	$('#helpBox').html(content);
}

function ajaxHelpBox(param)
{
	if (last_param != param)
	{
		last_param = param;
		
		if (ajaxRequeteHelpBox != null) 
			ajaxRequeteHelpBox.abort();
		
		ajaxRequeteHelpBox = $.ajax({
			type: 'POST',
			url: './scripts/getHelpBox.php',
			cache: true,
			data: ({
				param: param
			}),
			dataType: 'html',
			beforeSend: function(XMLHttpRequest){
				sur_lien = true;
				$('#helpBox').html('<img src="./img/load.gif" />');
			},
			success: function(data){
				$('#helpBox').html(data);
			}
		});
	}
	else
	{
		sur_lien = true;
	}
}

function helphide(id)
{
	sur_lien=false;

	$('#'+id).hide();
	//$('#'+id).html('');
}

function load_content()
{	
	var population = ($('#check_population:checked').length>0) ? 1 : 0;
	var ville = ($('#check_ville:checked').length>0) ? 1 : 0;
	var ressources = ($('#check_ressources:checked').length>0) ? 1 : 0;
	var chantiers = ($('#check_chantiers:checked').length>0) ? 1 : 0;
	var evolutions = ($('#check_evolutions:checked').length>0) ? 1 : 0;
	var outremonde = ($('#check_outremonde:checked').length>0) ? 1 : 0;
	var liens = ($('#check_liens:checked').length>0) ? 1 : 0;

	var fileName='';

	var type = ($('#type_code_bbcode:checked').length>0) ? 1 : 0;
	if(type==1)
		fileName='getBBContent.php';
	else
		fileName='getContent.php';
	
	var data=$.ajax({
		type : 'POST',
		url : './scripts/'+fileName,
		cache : false,
		data: ({ population:population, ville:ville, ressources:ressources, chantiers:chantiers, evolutions:evolutions, outremonde:outremonde, liens:liens }),
		dataType : 'html',
		beforeSend : function(XMLHttpRequest){
			$('#t').html('chargement en cours... veuillez patienter.');
		},
		success : function(data){
			$('#t').html( data );
		}
	});	
}


function showBuildingsChilds()
{
	var elements=$('#chantiers .branche_tree_strike');
	if ($('#choiceOrderByDay:checked').length == 0)
	{
		if ($('#choiceBuildingsChilds:checked').length > 0) 
			elements.show();
		else 
			elements.hide();
	}
}

function showBuildingsByDay()
{
	var elements=$('#chantiers .branche_tree_by_day');
	var elements2=$('#chantiers .branche_tree');
	var elements3=$('#chantiers .branche_tree_strike');
	
	if ($('#choiceOrderByDay:checked').length > 0)
	{
		$('#choiceBuildingsChilds').attr('disabled', 'disabled');
		elements.show();
		elements2.hide();
		elements3.hide();
	}
	else
	{
		elements.hide();
		elements2.show();
		if( $('#choiceBuildingsChilds:checked').length>0 )
			elements3.show();
			
		$('#choiceBuildingsChilds').attr('disabled', '');
	}	
}


function showOldAttaks()
{
	var elements=$('#ville .infos_ville[title="old_attak"]');
	
	if( $('#choiceOldAttaks:checked').length>0 )
		elements.show();
	else
		elements.hide();
}

function showAttakChart( i, d, h, id_l)
{
	$('#graphs .chart_link').css( 'color', '#DDAB76' );
	
	$('#'+id_l).css( 'color', '#FFFFFF' );

	
	swfobject.embedSWF(
			"open-flash-chart.swf",
			"my_chart",
			"1100",
			"300",
			"9.0.0",
			"expressInstall.swf",
			{
				"data-file":"./graphs/attaques_ofc.php%3Fi%3D"+i+"%26d%3D"+d+"%26h%3D"+h,
				"loading":"Chargement..."
			}
		);
}

function showCompareChart( id_l )
{
	$('#graphs .chart_link').css( 'color', '#DDAB76' );
	
	$('#'+id_l).css( 'color', '#FFFFFF' );

	
	swfobject.embedSWF(
			"open-flash-chart.swf",
			"my_chart",
			"1100",
			"300",
			"9.0.0",
			"expressInstall.swf",
			{
				"data-file":"./graphs/attaques_ofc_compare.php",
				"loading":"Chargement..."
			}
		);
}

function findItems(val)
{
	var Exp = new RegExp('cat_');
	//var Exp2 = new RegExp('broken_');
	
	if(val=='-1')
	{
		hideLooking();
	}
	else if( Exp.test( val ) )
	{
		lookForCat( val.substring(4) );
	}
	/*else if( Exp2.test( val ) )
	{
		lookForBrokenItem(val.substring(7));
	}*/
	else
	{
		lookForItem(val);
	}
}

function lookForItem(id_item)
{
	var Exp = new RegExp(id_item);

	var content='';
	hideLooking();
	$('.zone[it!=""]').each(function()
	{
		content=$(this).attr('it');
		if( Exp.test( content ) )
		{
			$(this).children('.zone_content').css('background-color', '#FED801');
		}	    
	});
}

/*function lookForBrokenItem(img)
{
	var Exp = new RegExp('item_broken><img src=./img/icons/item_'+img+'\.gif');	

	look(Exp);
}*/

function lookForCat(cat_name)
{
	var Exp = new RegExp(cat_name);

	var content='';
	hideLooking();
	$('.zone[ct!=""]').each(function()
	{
		content=$(this).attr('ct');
		if( Exp.test( content ) )
		{
			$(this).children('.zone_content').css('background-color', '#FED801');
		}	    
	});
}

/*function look(Exp)
{
	var content='';
	hideLooking();
	$('.zone[it!=""]').each(function()
	{
		content=$(this).attr('it');
		if( Exp.test( content ) )
		{
			//$(this).css('background-color', '#FED801');
			$(this).children('.zone_content').css('background-color', '#FED801');
		}	    
	});
}*/

function lookForCitizens()
{
	hideLooking();

	$('.zone[h!="0"]').each(function()
	{
		$(this).children('.zone_content').css('background-color', 'green').css('opacity', '0.75');
	});
}

function lookForZombies()
{
	var nb_z=0;
	var element=null;
	
	var color_blue='#58666E';
	var color_red='#94390D';
	//var color_orange='#927625';
	var color_orange='#B97425';
	var color_green='#929C0C';

	hideLooking();

	$('.zone[z!="0"]').each(function()
	{
		nb_z= parseInt( $(this).attr('z') );
		if(nb_z>0)
		{
			element=$(this).children('.zone_content');
			if(nb_z>=9)
				element.css('background-color', color_blue);
			else if(nb_z>=5)
				element.css('background-color', color_red);
			else if(nb_z>=3)
				element.css('background-color', color_orange);
			else if(nb_z>=1)
				element.css('background-color', color_green);
				
			element.css('opacity', '0.75');
		}

	});
}

function hideLooking()
{
	$('.zone').children('.zone_content').css('background-color','').css('opacity', '');	
	$('.zone').css('background-image','');
}

function hideMask()
{
	$('#mask').hide();
	$('#c_chart').show();
}

function showMask()
{
	var mask=$('#mask');
	
    var maskHeight = $(document).height();
    var maskWidth = $(document).width();        
    var winH = $(window).height();
    var winW = $(window).width();
	
    $('#c_chart').hide();
    
	mask.css({'width':maskWidth,'height':maskHeight});
    mask.show();
    
    $(window).resize(event_resize_window).scroll(event_resize_window);
}

function event_resize_window()
{
	var maskHeight2 = $(document).height();
    var maskWidth2 = $(document).width();       
    var winH = $(window).height();
    var winW = $(window).width();
    
    $('#mask').each(function(i){
    	if( $(this).is(":visible") )
    		$(this).css({'width':maskWidth2,'height':maskHeight2}); 
    });
    
	return false;
}


function lookForExpedition(val)
{
	var points = val.split('|') ;	
	var i=0;
	var coord=0;
	var sens_in='';
	var sens_out='';
	hideLooking();
	for( i=0; i<points.length-1; i++ )
	{
	    coord=points[i].split(',');
	 
	    if( i==0 )
	    {
	        var city_x=coord[0];
	        var city_y=coord[1];
	    }
	    else
	    {
	        $('.zone').each(function(){
	            if( $(this).attr('x')==coord[0] && $(this).attr('y')==coord[1] )
	            {
					sens_in=getSens(points[i-1], points[i]);
					sens_out=getSens(points[i+1], points[i]);
					if(sens_in!=sens_out)
	                	$(this).css('background-image','url("./img/expes/'+sens_in+'_'+sens_out+'.gif")');
	            }
	        });
	    }
	}
}

function getSens(compare, me)
{
	var retour='';

    var c=compare.split(',');
    var m=me.split(',');

 	var compare_x = parseInt( c[0]);
	var me_x = parseInt( m[0]);
	var compare_y = parseInt( c[1]);
	var me_y = parseInt( m[1]);

	if( compare_x == me_x )
	{
	    if( compare_y < me_y )
	        retour='haut';
	    else if( compare_y > me_y )
	        retour='bas';
	}
	else if( compare_y == me_y )
	{
	    if( compare_x < me_x )
	        retour='gauche';
	    else if( compare_x > me_x )
	        retour='droite';
	}

	return retour;
}

function getPeriodicalTime()
{	
	var data=$.ajax({
		type : 'POST',
		url : './scripts/getPeriodicalTime.php',
		cache : false,		
		dataType : 'html',
		success : function(data){
			$('#periodical_time').html( data );
		}
	});	
}