/*
 *
 * Scripts generales
 *
 */ 
	
	// Funciones generales
	var FUNCs = {

		// Animación 'flotación'
		flote: function(id,movpx)
		{
			if(!movpx){movpx = 5;};
			
			$(id).animate({marginTop:"-="+movpx+"px"},{queue:true, duration:450}).animate({marginTop:"+="+movpx+"px"},450, function() {
			setTimeout("FUNCs.flote('" + id + "',"+movpx+")", 0);
			});

		},
		
		
		// Rollover destacados
		rollDest: function(id)
		{
			$(id).mouseover(function(){$(this).stop('true').animate({top:"310px"},{queue:false, duration:600, easing: 'easeOutElastic'});});
			$(id).mouseout(function(){ $(this).stop('true').animate({top:"320px"},{queue:false, duration:1000, easing: 'easeOutElastic'});});
		},
		
		
		// Cometa		
		cometa: function (id)
		{
			id = '#cometa';
			var lower = 100;
			var higher = parseInt($('#chupachups').css('height'))-200;
			if ($.browser.msie) {				
				higher = 1200;				
			};
			var top = parseInt(rand_no = Math.floor((higher - (lower - 1)) * Math.random()) + lower);
			$(id).css({top: top+'px',left: '-100px'}).animate({left:"105%"},2500,function(){
			$(id).css({top: top+'px',left: '-100px'});
			setTimeout(function(){FUNCs.cometa();}, 7500);
			
			});				
		},

		
		// Acciones de inicio
		init: function()
		{
			
			$("#slogan").animate({"top": "+=70px"}, 1500,"easeOutElastic");
			
			if($("#dest0").length >0 )
			{
				$("#dest0").animate({"marginTop": "+=360px"}, 500,"easeOutElastic",function(){						
					$("#cont_dest0").animate({"top": "+=291px"}, 500,"easeOutElastic",function(){
						$("#dest1").animate({"top": "+=520px"}, 2000,"easeOutElastic");
						$("#dest2").animate({"top": "+=520px"}, 1500,"easeOutElastic");
						/*$("#dest3").animate({"top": "+=520px"}, 2000,"easeOutElastic");*/
						
					});
				});
			};
		},
		
		go:	function()
		{
			setTimeout(function(){FUNCs.init();}, 1200);
			$('#menu li a').show();
			$('#copyright').show();
			$('.legal').show();
			

			// Chuck
			$('#chuck')
			.sprite({fps: 8, no_of_frames: 2})
			.spRandom({top: 200,left:200, right: ($(document).width()-200), bottom: 600,speed: 4000, pause: 3500, chgDir:true})
			.isDraggable({ 
				start: function() {
				$('#chuck').spState(2);
				},
				stop: function() {
				$('#chuck').spState(1);
				$("#chuck").animate({"top": "+=30px"}, 2000,"easeOutElastic",function(){FUNCs.flote("#chuck");});
				}
			});
		},
		
		getRuta: function()
		{
			var rutatmp = location.href;
			var rutapos	= rutatmp.lastIndexOf("/");
			var ruta 	= rutatmp.substr(0,rutapos+1);
			return ruta;
		
		},
		
		ir: function (url)
		{
			if(!url){return false};
			document.location.href = url;
		
		},
		
		descarga: function (file)
		{			
			document.location.href='download.php?apdo=descargables/walls&file='+file;			
		},
		
		
		//Rollover botones cuadro+img
		rollBotsCuadro: function (colorFondo)
		{
			 colorFondo = colorFondo || 'ebb6d1';

			$('.cuadro').corner('8px cc:#'+colorFondo);
			$('.imgCuadro').corner('bottom 8px');
			$('.cabCuadro').corner('top 8px cc:#'+colorFondo);
						
			$('.cuadro').each(function () {	
			
				var a = $(this).children("a");
				var i = $(this).children("a").children('.imgCuadro').children('div');
			
				$(this).mouseover(function(event)
				{			
					$(a).css('color','#ffffff');
					$(i).stop().animate({marginTop: "-15px"},{queue:false, duration:200, easing:'jswing'});
				});
				$(this).mouseout(function(event)
				{				
					$(a).css('color','#5c100c');
					$(i).stop().animate({marginTop: "0px"},{queue:false, duration:400, easing:'easeOutBounce', complete:function(){
					}});
					
				});
				$(this).click(function(event)
				{
					document.location.href = $(a).attr('href');
				});
				
			
			});	
		},
		
		
		// Preload imgs botones			
		loadImgBot: function (num,url)
		{
			var img = new Image();
			$(img).load(function () 
			{
			  $(this).hide();
			  $('#img'+num).append(this);
			  $(this).fadeIn();					  
			})
			.attr('src', url);
		}
	
	};

	
	$(document).ready(function()
	{	
		
		// Chuck preload
		$('.chuckPreload').sprite({fps: 8, no_of_frames: 2}).spState(3);
		
		// Chuck a flotar!
		FUNCs.flote('#chuck');
		
		// Aviso legal
		$("a[rel='popup']").colorbox({width:"700px", height:"500px", iframe:true});
		
		// Formulario
		/*
		$("select[name='tipo'],select[name='categoria'], input:radio[name='sexo'], input:checkbox[name='info'],input:file[name='filename']").uniform({fileDefaultText: '',fileBtnText: 'Seleccionar...'});
		*/
		
		// Mundo peg. volvel Home
		$("#mundoPeq").hover(
		  function () {
			$(this).css('background-position','0px -160px');
		  }, 
		  function () {
			$(this).css('background-position','0px 0px');
		  }
		);
		FUNCs.flote('#mundoPeq',4);
		$('#mundoPeq').click(function () {document.location.href="../index2.html";});
		
		
		// Desactiva mouseRollOver de destacados no activos
		$('a[name="void"]').css('cursor','default');

		
		// Click en Contacto: scroll hacia abajo 				
		var $elem = $('#contenido').height()-parseFloat($('#sat_contacto').css('top'));
		$('#sat_contacto').click(function (e) {$('html, body').animate({scrollTop: $elem},2000);});
		
		// Click en Home: scroll hacia arriba (sólo en pag.Home)
		if(pag=="" || pag=="index" || pag=="index2"){$('.home').bind('click',function (e) {$('html, body').animate({scrollTop:0}, 'slow'); });};

		// Mon productos
		/*
		$('#sat_productos').click(function () {document.location.href="productos/lamarca-productos-the-best-of.html";});
		$('#sat_productos').animate({"left": "50%"}, 1000,"easeOutQuad");
		*/
		
		// Ajuste-parche de scroll horizontal según navegador		
		var b = false;
		$.each($.browser, function(i, val) {
			if ((i=='safari' && val==true) || (i=='opera' && val==true) || (i=='mozilla' && val==true) || (i=="msie" && $.browser.version >= 8)){ b = true;}
		});		
		if(b){$('body').css('overflow-x','hidden');};

		
   
		// Si no hay preload: comenzamos directamente.
		if(!$.isFunction($.Preload)){ FUNCs.go();};
		
		
				
		//Mundochuck
		/*
		f = pag.indexOf("-");
		apdo = pag.substring(0,f);
		if(apdo =="mundochuck")
		{
			if( $('.subselect').length >0 ){ $(".subselect").uniform({selectClass: 'selector2'}); };

			// Menu-selecc		
			$("#sub_classic").bind("change",function () {

				$("#sub_classic option:selected").each(function () 
				{        
					selec = $(this).val();
					
					switch(selec){			
						case 'wallpaper':
						document.location.href="mundochuck-descargables.html";
						break;
						case 'salvapantallas':
						document.location.href="mundochuck-descargables-salvapantallas.html";
						break;
						case 'movil':
						document.location.href="mundochuck-descargables-movil.html";
						break;
						case 'emoticonos':
						document.location.href="mundochuck-descargables-emoticonos.html";
						break;							
						default:
						break;
					};
				});
			});
			
			//Scroll
			$('.scroll').jScrollPane({
					scrollbarWidth : 15,
					showArrows : true,
					arrowSize : 20,		
					enableKeyboardNavigation: true
				});

			// Config de boton
			$('.cuadroWall').corner('8px cc:#f3bd6b');
			$('.wall').corner('8px');
			
			
			//comobobox
			/*
			$("#combobox").uniform({selectClass: 'selector3'});
			
			$("#combobox").bind("change",function () {
				$("#combobox option:selected").each(function () 
				{ 
				
					opcion = $(this).val();
					if(opcion==0){$('#papelChuck').show();$('#papelCorporatiu').hide();};
					if(opcion==1){$('#papelChuck').hide();$('#papelCorporatiu').show();};
				
				});
			}).trigger('change');
			*/
		/*
		};
		*/

		
	});
	
	$(window).load(function () 
	{
		// Si no estamos en pag.Home: bajamos satélite Contacto
		if(pag!="" && pag!="index" && pag!="index2"){ $('#sat_contacto').animate({'top': '290px'},1000);};
		
		
		//Animaciones flotación
		FUNCs.flote('#sat_redes',4);
		FUNCs.flote('#sat_contacto',6);	
		/*FUNCs.flote('#sat_productos',5);*/
		// Rollover destacados
		FUNCs.rollDest('#dest1');
		FUNCs.rollDest('#dest2');
		/*FUNCs.rollDest('#dest3');*/
		FUNCs.cometa('#cometa');		

		
	});


	// En qué pag. estamos?
	var pathurl=window.location.pathname;
	var ini = pathurl.lastIndexOf('/');
	var fin = pathurl.lastIndexOf('.');
	var pag = "";
	fin <=0  ? pag = pathurl.substring(ini+1): pag = pathurl.substring(ini+1,fin);	









