$(document).ready(function() {
	// Inicialitza slideshow
		$('#slideshow').cycle({fx: 'fade'});
		
	// Inicialitza els efectes dels menus
	$('#header ul.navegacio li').each(function(e){
		$(this).mouseover(function(){
			$('#header ul.subnavegacio').hide();
			$('#header ul.subnavegacio.'+this.className ).show();
		});
	});
	
	$('#header ul.subnavegacio').each(function(e){
		$(this).mouseleave(function(){
			$(this).hide();
		});
	});
		
});

function showBars( v1, v2, v3, v4, loc , com ){
	showBarsGraph( '.grafic2' , v1, v2, v3, v4, loc, com );
}

function showBarsModal( loc , com , v1, v2, v3, v4 ){
	showBarsGraph( '.grafic.modal' , v1, v2, v3, v4, loc , com );
	$(".mostra-modal").colorbox({width:"50%", inline:true, href:".grafic.modal"});
	//$('.grafic.modal').show();
}

function showBarsGraph( elm_id , v1, v2, v3, v4 , loc , com){
	
	var tot = parseInt(v1)+parseInt(v2)+parseInt(v3)+parseInt(v4);
	/*
	var av1 = Math.round(100/tot * v1);
	var av2 = Math.round(100/tot * v2);
	var av3 = Math.round(100/tot * v3);
	var av4 = Math.round(100/tot * v4);
	*/
	var av1 = (100/tot * v1).toFixed(1);
	var av2 = (100/tot * v2).toFixed(1);
	var av3 = (100/tot * v3).toFixed(1);
	var av4 = (100/tot * v4).toFixed(1);
	
	$(elm_id+' div.bars img').remove();
	$(elm_id+' div.bars').removeClass('hasGChart');
	$(elm_id+' div.bars').gchart({type: 'barVertGrouped', 
			backgroundColor: 'dfdddd',
			barWidth: 40, barSpacing: 20, minValue:0,
	    series: [$.gchart.series('Si', 	 [av1+2], 'ed6f01','',0), 
	        		 $.gchart.series('No', 	 [av2+2], '2d2c2c','',0), 
	        		 $.gchart.series('Blanc',[av3+2], 'ffffff','',0),
	        		 $.gchart.series('Nul',  [av4+2], 'c30e15','',0)],
	    axes : [
	    	$.gchart.axis('top', ['Si', 'No', 'Blanc', 'Nul'],[8,34,55,88],'000000', 'left',12),
	    	$.gchart.axis('bottom', [v1,v2,v3,v4],[5,34,60,88],'000000', 'left',12,'line')
	    ] 
	}); 
	
	$(elm_id+' .localitat').html( loc );
	$(elm_id+' .comarca').html( com );
	$(elm_id+' .val1').html( v1 );
	$(elm_id+' .val2').html( v2 );
	$(elm_id+' .val3').html( v3 );
	$(elm_id+' .val4').html( v4 );
	$(elm_id+' .avg1').html( av1+'%' );
	$(elm_id+' .avg2').html( av2+'%'  );
	$(elm_id+' .avg3').html( av3+'%'  );
	$(elm_id+' .avg4').html( av4+'%'  );
}