function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

function swapImages(){   
	  var $active = $('.hover #Gallery .active');
      var $next = ($('.hover #Gallery .active').next().length > 0) ? $('.hover #Gallery .active').next() : $('.hover #Gallery img:first');
	$active.removeClass('active');
	$active.addClass('last-active');
    $next.removeClass('last-active');
	$next.addClass('active');     
}

function showFicha(content,Cat){ 
	
	$("#contenido_box").empty();
		var idP = content.parent().children('.info').text();
		$.ajax({
		   type: "POST",
		   url: "getFicha.php",
		   data: "id="+idP+"&activeCat="+Cat,
		   success: function(html){
		  /* $("#toormixLogo").hide();*/
			 $("#contenido_box").append(html);
		   }
		 });
		$("body").css("overflow","hidden");
		$("#box_voile").fadeIn(100, function(){
			$("#toormixLogo").hide();
		});	
	    $("#footer").css({'display' : 'none'});
}

function showFichaHome(idp,Cat){ 
	
	$("#contenido_box").empty();
		$.ajax({
		   type: "POST",
		   url: "getFicha.php",
		   data: "id="+idp+"&activeCat="+Cat,
		   success: function(html){
		  /* $("#toormixLogo").hide();*/
			 $("#contenido_box").append(html);
		   }
		 });
		$("body").css("overflow","hidden");
		$("#box_voile").fadeIn(300, function(){
			$("#toormixLogo").hide();
		});
	    $("#footer").css({'display' : 'none'});
}

function rolloverOn($content){
		if(!inArray('topic-box', $content.attr('class').split(" "))){
			var idP = $content.find('.info').text();
			$content.css({'z-index' : '8'});
			$content.css("background-image", "url(images/fondo80.png)");  
			$content.addClass("hover").stop()
				.animate({
					 marginLeft: '-27px',
					 marginTop: '-35px',
					 paddingTop: '40px',
					 paddingLeft: '40px',
					 paddingRight: '40px',
					 paddingBottom: '40px',
				}, 1);
				$content.children(".tag").stop()
				.animate({
					 top: '30px',			 
				}, 1);				
		}
}
	
function rolloverOff($content){
		if(!inArray('topic-box', $content.attr('class').split(" "))){
			var idP = $content.children('.info').text();
			$content.removeClass("hover").stop()
			.animate({
				marginTop: '5px',
				marginRight: '13px',
				marginBottom: '15px',
				marginLeft: '13px',
				paddingTop: '0px',
				paddingRight: '0px',
				paddingBottom: '0px',
				paddingLeft: '0px',
			}, 1, function() {
				$content.css({'z-index' : '0'});
				$content.css("background-image", ""); 
			});
			$content.children(".tag").stop()
				.animate({
					 top: '-10px',			 
			}, 1);
		}
}

function getFooterText(texto){
	$.ajax({
		   type: "POST",
		   url: "footer.ajax.php",
		   data: "nombre="+texto,
		   success: function(html){ 
			 $("#footer-content").empty();
			 $("#footer-content").append(html);
			 if(texto!='newsletter'){
				$('#footer-text-content2').css({'margin-left' : '880px'});
			 }else{
				$('#footer-text-content2').css({'margin-left' : '460px'});
			 }
		   }
	});
}

function confirmNews(){

	var nom = $('#nombreNews').val();
	var email = $('#emailNews').val();
	var errMsg = '';
	if(nom==''){
		errMsg += '- '+news_err_nom+'\n';
	}
	if(email==''){
		errMsg += '- '+news_err_mail+'\n';
	}
	if($("input[name=privacidad]").is(':checked')){
		
	}else{
		errMsg += '- '+news_err_cond+'\n';
	}
	if(errMsg != ''){
		alert(errMsg);
	}else{
		$.ajax({
		   type: "POST",
		   url: "newsletter.ajax.php",
		   data: "action=up&nombre="+nom+"&email="+email,
		   success: function(html){
		     $("#formFoot").hide();
			 $("#footer-text-content1").empty();
			 $("#footer-text-content1").append(html); 
		   }
		});
	}
}


function bajaNews(){
	var nom = $('#nombreNews').val();
	var email = $('#emailNews').val();
	var errMsg = '';
	if(email==''){
		errMsg += '- '+news_err_mail+'\n';
	}
	if(errMsg != ''){
		alert(errMsg);
	}else{
		$.ajax({
		   type: "POST",
		   url: "newsletter.ajax.php",
		   data: "action=down&email="+email,
		   success: function(html){
			$("#formFoot").hide();
			 $("#footer-text-content1").empty();
			 $("#footer-text-content1").append(html); 
		   }
		});
	}
}
$(window).load(function(){
	
	//--------------   Init  Masonry  ------------------------------	
	var
	  speed = 200,
	  $wall = $('#demo').find('.wrap'),
	  $wall2 = $('#demo')
	;
	$('#footer').css({'display' : 'block'});
	$('#footer').fadeIn(800, function() {
		$('#wrapper').fadeIn(2000, function() {
			$('#pared').css({'display' : 'none'});
			var numP = $("#numPrjFicha").text();
			if(numP > 0){
				showFichaHome(numP,Category);
			}
		});
	});
	$('#demo').masonry({
		  columnWidth: 1, 
		  singleMode: false,
		  itemSelector: '.box:not(.invis)',
		  animate: true,
		  resizeable: true,
		  animationOptions: {
			duration: speed,
			easing: 'linear',
			queue: true
		  }
		});
	
	var initClass = '.' + $('#menunav .active a').attr('class');
	var Category = $('#menunav .active a').attr('class');
	var lang = $('#lngSelect').val();
	if(initClass=='.all') {
			$wall.children('.invis').not('.topic-box').toggleClass('invis').fadeIn(speed);
			$wall.children('.topic-box').not('.invis').toggleClass('invis').fadeOut(speed);
			$('#demo').find('.tagProj').not('.tagNoSelect').toggleClass('tagNoSelect');
			$('#demo').find('.tagProj').removeClass('tagNoSelect');
			$('#demo').find('.tagProj').addClass('tagSelect');
	 } else {  
			$wall.children().not(initClass).not('.invis').toggleClass('invis').fadeOut(speed);
			$wall.children('.topic-box').css({'display' : 'none'});
			$wall.children(initClass+'.invis').toggleClass('invis').fadeIn(speed);
			$wall.children(initClass+'.topic-box').css({'display' : 'block'});
			$wall.children(initClass+'.topic-box').children('h2').children('hr').css({'margin-top' : '16px'});
			if(initClass=='.selected'){
				$wall.children(initClass+'.topic-box').insertAfter($wall.children('.project:first'));
				$wall.children(initClass+'.topic-box').fadeIn(speed);
				$wall.children(initClass+'.topic-box').children('h2').css({'margin-top' : '6px'});
				$wall.children(initClass+'.topic-box').children('h2').css({'text-indent' : '0px'});
				$wall.children(initClass+'.topic-box').css("background-image", "url()"); 
				
				$('#demo').find('.tagProj').not('.tagNoSelect').toggleClass('tagNoSelect');
				$('#demo').find('.tagProj').removeClass('tagNoSelect');
				$('#demo').find('.tagProj').addClass('tagSelect');
			}else{
				$wall.children(initClass+'.topic-box').css("background-image", "url('images/categorias/"+lang+"/"+Category+".png')"); 
				$('#demo').find('.tagProj').not('.tagNoSelect').toggleClass('tagNoSelect');
				$tag = $wall.find('.tagProj'+initClass);
				$tag.each(function(){
					$parentTag = $(this).parent();
					$(this).removeClass('tagNoSelect');
					$(this).addClass('tagSelect');
					$(this).insertBefore($parentTag.find('.tagNoSelect:first'));
				});
			}
	}
	$('#demo').masonry();
});

$(document).ready(function(){
	var
	  speed = 500,  
	  $wall = $('#demo').find('.wrap'),
	  $wall2 = $('#demo')
	;
	var timer;
	$('#wrapper').hide();
    $wall.children('.topic-box').css({'display' : 'block'});
	var Category = $('#menunav .active a').attr('class');
	
	$("#demo .box").bind('mouseenter', function() {
			rolloverOn($(this));
	});
    $("#demo .box #infoProj").bind('mouseenter', function() {
			rolloverOn($(this).parent());
	});
	$("#demo .box #Gallery").bind('mouseenter', function() {
			rolloverOn($(this).parent());
	});
	
	
	$("#demo .box #infoProj").bind('mouseleave', function() {
			rolloverOff($(this).parent());
	});
	
	$("#demo .box #Gallery").bind('mouseleave', function() {
			rolloverOff($(this).parent());
	});
	
	$('#footer a').click(function(){
		if($(this).text()=="Twitter"){
		var haut = "200px";
		}else{
		var haut = "190px";
		}
		if($(this).text()!="E-mail"){
		if(($(this).attr('class')=="FootLink")||($(this).attr('class')=="FootLink footer-top-menu-margin-1")||($(this).attr('class')=="FootLink footer-top-menu-margin-2")){
			$('#footer-top-menu a').css("color", "#ffffff");
			$(this).css("color", "#fef200");
			$("#footer-content").animate({
				height: haut,
			}, 500 );
		}
		}
	});	
	
	$("#footer").bind("mouseleave",function(){
        if($('#footer-content').css('display')=='block'){
			$('#footer-top-menu a').css("color", "#ffffff");
			$("#footer-content").animate({
				height: "0px",
			}, 400 , function() {
				$("#footer-content").hide(); 
				$("#conditiones").hide(); 
			});
		}
    });
	
	$('#footer .newsletter').click(function(){
		var texto = $(this).attr('class');
		getFooterText(texto);
	});
	$('#footer .behance').click(function(){
		var texto = $(this).attr('class');
		getFooterText(texto);
	});
	$('#footer .issuu').click(function(){
		var texto = $(this).attr('class');
		getFooterText(texto);
	});
	$('#footer .flickr').click(function(){
		var texto = $(this).attr('class');
		getFooterText(texto);
	});
	$('#footer .facebook').click(function(){
		var texto = $(this).attr('class');
		getFooterText(texto);
	});
	$('#footer .twitter').click(function(){
		var texto = $(this).attr('class');
		getFooterText(texto);
	});

	$('#demo .box #Gallery').click(function(){
		showFicha($(this),Category);
		return false;
	});	
	
	$('#demo .box #infoProj').click(function(){
		showFicha($(this),Category);
	});	
	
	$('#menunav a').hover(function () {
			var colorClass = $(this).attr('class');
			var parentClass = $(this).parent().attr('class');
			if(parentClass=='active'){
			
			}else{
				$(this).addClass("menuOver");
				$('#mIma-'+colorClass).toggleClass("imaOff");
				$('#mIma-'+colorClass).toggleClass("imaOn");
			}
		  }, 
		  function () {
			$('#mIma').children().each(function(){
				if($(this).attr('class')=='imaOn'){
					$(this).removeClass("imaOn");
					$(this).addClass("imaOff");
				}
			});
			$(this).removeClass("menuOver");
		  }
		);	

		
 
});
