var amai = {
		
	extras: true,
	bubbles: {},
		
	init: function(){
		
		//eventos de los extras
		$('.switch a').click(function(){	
			(amai.extras) ? amai.undecorate() : amai.decorate()
			return false;
		});
		
		//la lengua
		$('#lengua').hide();
		$('#amai_logo').hover(
			function(){if(amai.extras) $('#lengua').show();},
			function(){if(amai.extras) $('#lengua').hide();}
		);
		
		//las animaciones flash
	    swfobject.embedSWF(
    		"/layout/extras/amai_textoalt.swf", "amai_textoalt",
    		"600", "150", "9.0.0",
    		'/js/swfobject/expressInstall.swf',
    		{}, 
    		{wmode: 'transparent'},
    		{'class': 'extras'}
	    );
	    swfobject.embedSWF(
	    	"/layout/extras/stupid.swf", "stupid",
	    	"420", "180", "9.0.0",
	    	'/js/swfobject/expressInstall.swf',
	    	{}, 
	    	{wmode: 'transparent'},
	    	{'class': 'extras'}
	    );
	    //cogemos el idioma
	    var lang = $('#silly').attr('rel');
	    swfobject.embedSWF(
	    	"/layout/extras/silly-things-"+lang+".swf", "silly",
	    	"640", "150", "9.0.0",
	    	'/js/swfobject/expressInstall.swf',
	    	{}, 
	    	{wmode: 'transparent'},
	    	{'class': 'extras'}
	   );	    
		
		//y las burbujas
	    var gap = ($('body').width()-800)/2;
		var opts = {
			particleSourceX: gap,
			particleSourceY: $('body').height()-80,
			particleScatteringX: gap-20,
			particleScatteringY: $('body').height(),
			particleDirection: 'right',
			particleAnimationDuration:  5000,
			particleAnimationVariance:  2000,
			gravity: 0,
			particleSizeMin: 40,
			particleSizeMax: 100,
			//imgSource: '/layout/bubble.png'			
		}
		//configuramos las burbujas de abajo
		var bc = $.extend({imgSource: '/layout/extras/bubblec.png'}, opts);
		var bm = $.extend({imgSource: '/layout/extras/bubblem.png'}, opts);
		var by = $.extend({imgSource: '/layout/extras/bubbley.png'}, opts);		
		//configuramos las burbujas de arriba
		var top = {
			gravity: 500,
			particleDirection: 'left',
			particleSourceX: ($('body').width()-800)/2+800, 
			particleSourceY: 0,
			particleScatteringY: -opts.particleScatteringY
		};
		var tc = $.extend({}, bc, top);
		var tm = $.extend({}, bm, top);
		var ty = $.extend({}, by, top);
		
		//ponemos las burbujas
		amai.bubbles.bc = $().BubbleEngine(bc);
		amai.bubbles.bm = $().BubbleEngine(bm);
		amai.bubbles.by = $().BubbleEngine(by);		
		amai.bubbles.tc = $().BubbleEngine(tc);
		amai.bubbles.tm = $().BubbleEngine(tm);
		amai.bubbles.ty = $().BubbleEngine(ty);		
		
		//y decoramos
		var extras = $.cookie('amai-extras');
		amai.extras = (extras == 'off') ? false : true;
		(amai.extras) ? amai.decorate() : amai.undecorate();
	},
		
	decorate: function(){
		//alert('decoramos');
		amai.extras = true;
		$.cookie('amai-extras', 'on', {path: '/'});
        $('.extras').fadeIn();
        $('#switch_on').fadeOut();
		$('#amai_texto').animate({marginTop: 0, marginBottom: 64});
		for(i in amai.bubbles){
			var b = Math.round(Math.random()*3)+2;
			//console.log(b);
			amai.bubbles[i].addBubbles(b);
		}
        return true;
	},
	undecorate: function(){
		//alert('undecoramos');
		amai.extras = false;
		$.cookie('amai-extras', 'off', {path: '/'});
        $('.extras').fadeOut();
        $('#switch_on').fadeIn();
		$('#amai_texto').animate({marginTop: 40, marginBottom: 8});
		for(i in amai.bubbles) amai.bubbles[i].removeBubbles();
		return true;
	},
	
	//funcion extra para ajustar los márgenes del menú
	fixMenu: function(){
		var menu = $($('div.menu ul').get(0));
		var li = menu.children();
		var w0 = menu.width();
		if(!$.support.boxModel) w0 =- 40;
		var w1 = 0;
		li.each(function(){ w1 += $(this).width(); })
		var m = Math.floor((w0 - w1)/(li.length * 2));
		//alert(w0+' - '+w1+' - '+m);
		li.css({
			'marginLeft': m,
			'marginRight': m
		});		
	}
	
	
}
$(function(){ amai.fixMenu(); });
$(window).load(function(){
	amai.init();	
});