var headeropen 		= true;
var contenidoShow	= false;
var seccionAntigua;
var reel			= true;
var mostrar 		= true;

$(document).ready(function() {
	
	
	var parpadeo		= setInterval("parpadeo()",1700);
	
	$('#play').hide();
	
	$('#slider').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	
	$('#reel').addClass('active');
	
	
	$("#menu_footer li a").hover(function() {
		$('img',this).stop().fadeTo(300, 1.0);
        $("em",this).animate({opacity: "show", top:"32"},"fast");
    }, function() {
    	$('img',this).stop().fadeTo(300, 0);
       	$("em",this).animate({opacity: "hide", top: "55"},"fast");
    });
	
	$("#menu_footer li a#enviar").hover(function() {
		clearInterval(parpadeo);
    }, function() {
    	parpadeo = setInterval("parpadeo()",1700);
    });
    
	$('.email_form button').hover(function() {
		$(this).find('span').css({'color':'#fff'});
    }, function() {
		$(this).find('span').css({'color':'#ffde00'});
    });
	
	
	$("#videos li").live("mouseover",function(){
      $("span.oculto",this).stop().animate({top: "-18px"},"fast");
    }).live("mouseout",function(){
      $("span.oculto",this).stop().animate({top: "60px"},"slow");
    });
	
	
	$('#contact, #about, #projects, #clientes, #colabora').live('click',function(e){
		e.preventDefault();
	});
	
	
	$('#contact:not(.active), #about:not(.active), #projects:not(.active), #clientes:not(.active), #colabora:not(.active)').live('click',function(e){
		e.preventDefault();
		if(mostrar){
			mostrar			= false;
			var contenido	= $(this).attr("id");
			MediaPlayer.pause();
			mostrarSeccion(contenido);
		}
	});
	
	
	$('#mostrar').mouseenter(function(){
      	if(headeropen){
      		$("#contenido").stop().animate({marginTop:"0px",top:'0px'},500);
      		$('#header').stop().animate({marginTop:"0px"});
      	}
    });
    $('#mostrar').mouseleave(function(){
      	if(headeropen){
  	    	$('#header').stop().animate({marginTop:"-183px",height:"183px"},500);
    	  	$("#contenido").stop().animate({marginTop:"-265px",top:'0px',height:"265px"},800,function(){});
    	}
    });


	$('#reel:not(.active)').live('click',function(e){
    	e.preventDefault();
		reel		= true;
		file_vido 	= $(this).attr('href');
		var obj 	= {file:file_vido};
		mostrarVideoReel(obj);
		
    });
    
    
    $('.wrap_cont .cerrar').live('click',function(e){
    	contenidoShow	= false;
    	ocultarTop();
		ocultarFooter();
		ocultarHeader();
    });
	
	
	$('.projects #videos a').live('click',function(e){
		e.preventDefault();
		reel		= false;	
		file_vido 	= $(this).attr('href');	
		var obj 	= {file:file_vido};
				
		var dataString 				= 	'id='			+ $(this).attr('id');	
				
		$.ajax({
    		url: "ajax/descripcion_video.php",
       	 	async:true,
        	data: dataString,
        	dataType: "html",
        	global: true,
        	ifModified: false,
        	processData:true,
        	beforeSend: function(objeto){
        		$('#info_video').remove();
        	},
        	success: function(datos){
        		if(datos)
  	    			$('body').append('<div id="info_video">'+datos+'</div>');
        	},
        	timeout: 3000,
        	type: "POST"
     	});
		
		mostrarVideoReel(obj);
	});
    
    
    $(window).resize(function() {
  		lol(true);
	});
	
	
	$("#love").click(function(e){
        e.preventDefault();
        var bookmarkUrl = this.href;
        var bookmarkTitle = this.title;
        if (window.sidebar) {
			window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
        } else if( window.external || document.all) {
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
        } else if(window.opera) {
			$("#love").attr("href",bookmarkUrl);
			$("#love").attr("title",bookmarkTitle);
			$("#love").attr("rel","sidebar");
        } else {
 			alert('Your browser does not support this bookmark action');
			return false;
        }
	});
	
	
	$('#contacto, .follow_contact .contactanos').click(function(e){
		e.preventDefault();
		if($('#contct_bg').height()<300){
			$(".projects #videos").children().remove();
			contenidoShow	= false;
			$('.wrap_cont').css({'z-index':'0'});
			$('#logo img').animate({height:"80px"},function(){
				$('#header').animate({height:"183px"});
				$('#contenido').animate({height:"265px"},function(){
					$('.wrap',this).fadeOut();
					$('#video_menu').css({'z-index':'10'});
					$('#nav-menu a').removeClass('active');
				});
			});
			$('#contct_bg').animate({height:"350px"}, {duration: 1000, easing: "easeOutQuad",complete:function(){
				$('#emviar_email').fadeIn();
			}});
		}else{
			$('#emviar_email').fadeIn();
			$('#emviar_email_amigo').fadeOut();
		}
	});
	
	
	$('#emviar_email .cerrar, #emviar_email_amigo .cerrar, #contact, #about, #projects, #colabora').click(function(e){
		e.preventDefault();
		ocultarFormularios();
	});
	
	
	$("#emviar_email").validate({
 			submitHandler: function(form) {
 				var name 					= $('#name').val();
 				var company					= $('#company').val();
				var email 					= $('#email').val();
				var comments	 			= $('#comments1').val();
				
				var dataString 				= 	'name='			+ name +
												'&company=' 	+ company +
												'&email=' 		+ email +
												'&comments=' 	+ comments;
										
				$.ajax({
    				url: "ajax/envio_contacto.php",
       			 	async:true,
        			data: dataString,
        			dataType: "html",
        			global: true,
        			ifModified: false,
        			processData:true,
        			beforeSend: function(objeto){
        				$('#emviar_email button').fadeOut();
        			},
        			success: function(datos){
        				if(datos=='succes'){
        					$('.aviso').html('Su información ha sido enviada correctamente.').fadeIn();
        					setTimeout('ocultarAviso()',3000);
        				}else{
        					$('.aviso').html('No se ha podido enviar el formulario.').fadeIn();
        					setTimeout('ocultarAviso()',3000);
        				}
        				
        				$('#emviar_email button').fadeIn(function(){
        					$('#emviar_email_amigo,#emviar_email').find('input[type="text"]').val('');
							$('#emviar_email_amigo,#emviar_email').find('textarea').val('');
        				});
        			},
        			timeout: 3000,
        			type: "POST"
     			});
 			}
 	});
 	
 	 	
 	$("#emviar_email_amigo").validate({

 			submitHandler: function(form) {
 				var yourname				= $('#yourname').val();
 				var yourmail				= $('#yourmail').val();
				var friendname				= $('#friendname').val();
				var emailfriend	 			= $('#emailfriend').val();
				var comments	 			= $('#comments2').val();
				
				var dataString 				= 	'yourname='		+ yourname +
												'&yourmail=' 	+ yourmail +
												'&friendname=' 	+ friendname +
												'&emailfriend=' + emailfriend +
												'&comments=' 	+ comments;
										
				$.ajax({
    				url: "ajax/envio_amigo.php",
       			 	async:true,
        			data: dataString,
        			dataType: "html",
        			global: true,
        			ifModified: false,
        			processData:true,
        			beforeSend: function(objeto){
        				$('#emviar_email_amigo button').fadeOut();
        			},
        			success: function(datos){
        				if(datos=='succes'){
        					$('.aviso').html('Su información ha sido enviada correctamente.').fadeIn();
        					setTimeout('ocultarAviso()',3000);
        				}else{
        					$('.aviso').html('No se ha podido enviar el formulario.').fadeIn();
        					setTimeout('ocultarAviso()',3000);
        				}
        				
        				$('#emviar_email_amigo button').fadeIn(function(){
        					$('#emviar_email_amigo,#emviar_email').find('input[type="text"]').val('');
							$('#emviar_email_amigo,#emviar_email').find('textarea').val('');
        				});
        				
        			},
        			timeout: 3000,
        			type: "POST"
     			});
 			}
 	});
	
});


function filtrar(id){
	if(id!=0){
		$('#videos li').not('.cat_'+id).fadeTo("fast", 0).hide();
		$('.cat_'+id+'').fadeTo("fast", 1);
	}else{
		$('#videos li').fadeTo("fast", 1);
	}
	var li_width	= $('#videos li').width();
	var new_width	= ($('#videos li:visible').length)*(li_width+6);
	$('#videos').width(new_width);
}


function ocultarAviso(){
	$('.aviso').css({'z-index':'1'}).fadeOut();
}


function mostrarTop(){
	$("#contenido").animate({marginTop:"0px",top:'0px'},500);
	setTimeout("mostrarHeader()",250);
}


function ocultarTop(){
	$(".projects #videos").children().remove();
	$('.wrap_cont').css({'z-index':'0'});
	$('#play').fadeOut(function(){
		$('#pause').fadeIn();
    });
    $('#header').animate({marginTop:"-183px",height:"183px"},500);
    $('#logo img').animate({height:"80px"},1000);
   	setTimeout("ocultarContenido()",150);
}


function ocultarContenido(){
	$("#contenido").animate({marginTop:"-265px",top:'0px',height:"265px"},800,function(){
		$('#nav-menu a').removeClass('active');
		$('.'+seccionAntigua).fadeOut();
	});
}


function mostrarFooter(){
	$('#footer').animate({height:"140px"},function(){
		$("#menu_footer").fadeIn("fast");
	});
}

function parpadeo(){
	$('img',"#menu_footer li a#enviar").fadeTo(700, 1.0,function(){
		$(this).fadeTo(1000, 0);
	});
}


function ocultarFooter(){
	$('#footer').animate({height:"0px"},function(){
		ocultarFormularios();
		$("#menu_footer").fadeOut("fast");
		$('#blackbg').animate({height:"0px"});
		clearInterval(parpadeo);
	});
}


function ocultarFormularios(){
	$('#emviar_email, #emviar_email_amigo').hide(function(){
		$('#emviar_email_amigo,#emviar_email').find('input[type="text"]').val('');
		$('#emviar_email_amigo,#emviar_email').find('textarea').val('');
		$('#contct_bg').animate({height:"0px"}, {duration: 1000, easing: "easeOutQuad",complete:function(){
			ocultarAviso();
		}});
	});
}


function mostrarSeccion(seccion){
	if(!contenidoShow){
    	contenidoShow	= true;
		$('#nav-menu a').removeClass('active');
		$('#'+seccion+'').addClass('active');
		$('#video_menu').css({'z-index':'0'});
		seccionAntigua	= seccion;
		$('#header').animate({height:"273px"},function(){
			$('.'+seccion+'').show(0,function(){
			$('.'+seccion).parent().animate({height:"675px"}, {duration: 1000, easing: "easeOutQuad",complete:function(){
				$('.wrap_cont').css({'z-index':'10'});
				$('.'+seccion+' div .cerrar').fadeIn().css({'z-index':'4'});
				contenidoShow	= true;
				mostrar			= true;
				if(seccion=='projects'){
					$('.wrap_cont').css({'z-index':'4'});
					videosProjects();
				}	
			}});
			$('#logo img').animate({height:"145px"},1000);
			$('.'+seccion+'').find('.img_contact').fadeIn();
		});
		});
		
	}else{
		ocultarSeccion(seccion);
	}
}


function ocultarSeccion(seccion){
	$(".projects #videos").children().remove();
	$('.wrap_cont').css({'z-index':'0'});
	if(seccion){
		$('.'+seccionAntigua+' div .cerrar').css({'z-index':'1'}).fadeOut();
		$('.'+seccionAntigua).parent().animate({height:"265px"}, {duration: 1000, easing: "easeOutQuad",complete:function(){
			$('#video_menu').css({'z-index':'10'});
			$('#'+seccionAntigua+'').removeClass('active');
			$('.'+seccionAntigua+'').fadeOut(function(){
				contenidoShow	= false;
				if(seccion){
					mostrarSeccion(seccion);
				}
			});
				
		}});
	}else{
		$('.'+seccionAntigua+' div .cerrar').css({'z-index':'1'}).fadeOut();
		ocultarTop();
		ocultarFooter();
		ocultarHeader();
		$(".projects #videos").children().remove();
		$('#video_menu').css({'z-index':'10'});
		$('#'+seccionAntigua+'').removeClass('active');
		$('.'+seccionAntigua+'').fadeOut(function(){
			contenidoShow	= false;
		});
	}
}


function mostrarHeader(){
	$('#header').animate({marginTop:"0px"});
}


function ocultarHeader(){
	$('#header').animate({marginTop:"-183px", height: "183px"},function(){
		$('#logo img').animate({height:"80px"},1000);
		$('#video_menu').css({'z-index':'10'});
    	MediaPlayer.play();
	});
}


function mostrarVideoReel(obj){
	if(contenidoShow){
		ocultarSeccion();
	}
	$('#play').fadeOut(function(){
		MediaPlayer.playClip(obj);
    	$('#pause').fadeIn();
   	});
	$('#nav-menu a').removeClass('active');
	$('#reel').addClass('active');	
}


function videosProjects(){
	var dataString = 'id_seccion=2';
    $.ajax({
		url: "ajax/listar_videos.php",
    	async:true,
       	data: dataString,
       	dataType: "json",
       	global: true,
       	ifModified: false,
       	processData:true,
       	beforeSend: function(objeto){
       		$(".projects #videos").children().remove();
       	},
       	success: function(datos){
       		var videos = eval(datos);
       		for(i=0;i<videos.length;i++){
       			$('.projects #videos').append('<li class="cat_'+videos[i].categoria+'"><a href="'+videos[i].nombre_video+'" title="'+videos[i].titulo+'" id="'+videos[i].id+'"></a><img src="flash/pics/'+videos[i].nombre_imagen+'" alt=""/><span class="categoria'+videos[i].categoria+' oculto"></span></li>');
       		}
       		$(".projects #videos li:eq(0)").fadeIn("fast", function () {
				$(this).next().fadeIn("fast", arguments.callee); 
			});
			$('.projects #videos a').tooltip({ 
    			track: true, 
    			delay: 0, 
    			showURL: false, 
    			extraClass: "right" 
			});
			lol();
       	},
       	timeout: 3000,
       	type: "POST"
    });
}


function lol(resize){
	var browserwidth  = $(window).width();
	var ancho = browserwidth;
	var numero = $("#videos li").length;
	var anchoLi = $("#videos li").width();
	numero = (numero * (anchoLi+6));
	if(numero)$("#videos").css("width",numero+"px");
    var div = $('div.sc_menu'),
                 ul = $('#videos'),
                 ulPadding = 3;
    var divWidth = div.width();
    div.css({overflow: 'hidden'});
    var lastLi = ul.find('li:last-child');
	if(resize)div.scrollLeft(0);
    div.mousemove(function(e){
      	var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
      	var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      	div.scrollLeft(left);
    });
};


