$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

function removeClassByPrefix(el, prefix) {
    var regx = new RegExp('\\b' + prefix + '.*?\\b\-.*?\\b\-.*?\\b.*?\\d*', 'g');
    el.className = el.className.replace(regx, '');
    return el;
}

$(document).ready(function(){
	 Shadowbox.init();	
	//MODIFICAMOS EL POST
	$('.post').each(function(){
		//var imgstr = $(this).find('.mod-txt').find('img');
		//$(this).find('.mod-txt').find('img').remove();
		//$(this).find('.mod-img').append(imgstr);
		/*$(this).find('.mod-txt').find("p").filter(function() {
				return $.trim($(this).text()) === ''
			})
			.remove()*/
		var entradilla = $(this).find('.post-meta').find('li').find('span').text();
		var lasentradilla = entradilla.split(":");
		var RealAutor = $(this).find('p.autor').find('a').text();
		var autorSet=false;
		$("ul.post-meta").find('li').each(function(){
			var who = $(this).find('span').text();
			
			who = who.substring(0,who.length-1);
			var texto = $(this).text();
			texto = texto.substring(texto.indexOf(":")+1);
			if (who.indexOf('Entradilla')!=-1){
					$(this).parent().parent().find('p.entradilla').text(texto);
				}else if (who.indexOf('Autor')!=-1){
					autorSet=true;
					$(this).parent().parent().find('p.autor').text("Publicado por:" + texto);
				}
		});
	
		if (!autorSet){
			/*if (RealAutor.indexOf("Esquire Spain")!=-1){
				RealAutor = "DANIEL ENTRIALGO";
			}else if (RealAutor.indexOf("Rodrigo Varona")!=-1) {
				RealAutor = "Departamento de Moda Esquire";
			}else if (RealAutor.indexOf("andres rodriguez")!=-1) {
				RealAutor = "Andrés Rodriguez";
			}else if (RealAutor.indexOf("andres rodriguez")!=-1) {
				RealAutor = "Andrés Rodriguez";
			}*/
			
			$(this).find('p.autor').find('a').remove();
			$(this).find('p.autor').text("Publicado por:" + RealAutor);
		}
		$(".blogger-post-footer").remove();
/*		var newHtml =$(this).find('p[class=""]').html().replace(/\<span.*?\>+/g,'<p>');
		newHtml = newHtml.replace(/\<\/span\>+/g,'<\/p>');*/

		
		$(this).find('p[class=""]').each(function(){
			var newHtml =$(this).html().replace(/\<span.*?\>+/g,'');
			newHtml = newHtml.replace(/\<\/span\>+/g,'');
			$(this).html(newHtml);
		});
		

		var rehtml = $(this).html().replace(/\<div style\=\"text\-align.*?\>+/g,'<p>');
		$(this).html(rehtml);
		$(this).find('img').each(function(){
			$(this).parent().attr('rel','shadowbox[roadtrip]');
		});
		//alert($(this).find('p[class=]').html());
	//		$(this).find('p[class=]').html(newHtml);
		//	$(this).find('p').html().replace(/\<\/span\>+/g,'</p>');
		$(this).find('.post-meta').remove();
		
		$(this).find('object').each(function(){
			var ancho = $(this).find('embed').attr('width');
			if ( ancho > $("div.col-d").css('width') ){
				$(this).find('embed').attr('width', $("div.col-d").css('width'));
			}
		});

	})
	//MODIFICAMOS EL MENU PRINCIPAL DE LAS CATEGORIAS
	
	$('.menu').find('.estirar').each(function(){
		$(this).find('li').each(function(){
            var imchildren = false;
			if ($(this).hasClass("current-cat-parent")){
				
				$(this).find('ul[class*="children"]').each(function(){
					$("ul.lista-subsecciones").html($(this).html());
				});
				var currentSubsec;
				$(this).find('ul').find('li[class*="current-cat"]').each(function(){
					currentSubsec = $(this).text().replace(/^\s*|\s*$/g,"");

					  $(this).removeClass();
					  $(this).find('a').addClass("activo");
						$("ul.lista-subsecciones").find('li').find('a').each(function(){
							var newsec = $(this).text().replace(/^\s*|\s*$/g,"");
							if ( newsec == currentSubsec){
								$(this).removeClass();
								$(this).addClass("active");
							}
						});
				});
	
				$('ul.lista-subsecciones').find('li').removeClass();	
				$('ul.children').remove();
				$(this).removeClass();
				$(this).find('a').addClass("activo");
//				alert($(this).find('a').text());
				$("#estasen").find('a').text($(this).find('a').text());
				$("#estasen").find('a').attr("href", $(this).find('a').attr('href'));
				$("#estasen").find('a').attr("title", $(this).find('a').attr('title'));

				
			}
			else if ($(this).hasClass("current-cat")){ // CATEGORIA SIN SUBCATEGORIAS
				var subsec = false;
				$(this).find('ul[class*="children"]').each(function(){
					$("ul.lista-subsecciones").html($(this).html());
					subsec = true;
				});
				if ( subsec == false ){
					$("#subsecciones").hide();
					$("ul.lista-subsecciones").hide();
				}
				$('ul.lista-subsecciones').find('li').removeClass();	
				$('ul.children').remove();
				$(this).removeClass();
				$(this).find('a').addClass("activo");
//				alert($(this).find('a').text());
				$("#estasen").find('a').text($(this).find('a').text());
				$("#estasen").find('a').attr("href", $(this).find('a').attr('href'));
				$("#estasen").find('a').attr("title", $(this).find('a').attr('title'));
			}
			else{
				$(this).removeClass();
								
			}
		});
		$("ul.children").remove();
		$(this).find('.children').remove();
		
		 
	});
	$('.menu').find('.estirar').find('li:last').addClass("ultimo");
	
	//Buscar
	$("a.btn-buscar").click(function(){
		$("#searchform").submit();
	});
	
});
var dividg;
		//run the currently selected effect
		function runEffect(){
			
			//var id=divid.substring(divid.length-1);
			
			//get effect type from 
			//var selectedEffect = $('#effectTypes').val();
			var selectedEffect = "bound";
			
			//most effect types need no options passed by default
			var options = {};
			//check if it's scale, transfer, or size - they need options explicitly set
			if(selectedEffect == 'scale'){  options = {percent: 100}; }
			else if(selectedEffect == 'transfer'){ options = { to: "#button", className: 'ui-effects-transfer' }; }
			else if(selectedEffect == 'size'){ options = { to: {width: 280,height: 185} }; }
			//run the effect
			$("#"+thediv).show(selectedEffect,options,500,callback);
		
		};
		
		//callback function to bring a hidden box back
		function callback(){
			setTimeout(function(){
			//	$("#"+dividg+":visible").removeAttr('style').hide().fadeOut();
			}, 1000);
		};
		
		//set effect from select menu value
		
		var tdivid;
		function MyEffect(divid){
			tdivid=divid;
			/*dividg=divid;
			thediv=divid;
			var id=divid.substring(divid.length-1);
			$("div[id^=blog_][id!="+divid+"]:not(:animated)").each(function(){
				var newID = $(this).attr("id").substring(divid.length-1);										 
				if (newID > id){
					//$(this).show(selectedEffect,options,500,callback);
					//$(this).toggle("blind",null,500,callback);


				}
				if (newID != id){
					MyEffectOut($(this).attr("id"));
				}
			});*/
	
	
	/* 		$("#"+divid).stop();
			$("#"+divid).find('li').find('a').find('img:not(:animated)').stop();
			$("#"+divid).find('span').stop();*/
			
			/*$("img[id="+divid+"]").stop().animate({
				opacity : 1
			});*/
			$("#"+divid).find('img').stop(false,true).animate({
				opacity : 1
			});

			$("."+divid).stop(false,true).effect("blind",{ mode: "show" },300,null);

			
/*			$("div[id^=blog_][id!="+divid+"]:not(:animated)").each(function(){
				var newID = $(this).attr("id").substring(divid.length-1);										 
				if (newID > id){
					//$(this).show(selectedEffect,options,500,callback);
					//$(this).toggle("blind",null,500,callback);


				}
			});
			*/
			
		};
		/*function reactivate(){
			$("#"+tdivid).mouseenter(function(){
				MyEffect($(this).attr('id'));
			}).mouseleave(function(){
				MyEffectOut($(this).attr('id'));
			});
		}*/
		function MyEffectOut(divid){
			/*dividg=divid;
			thediv=divid;
			var id=divid.substring(divid.length-1);
			
			//get effect type from 
			//var selectedEffect = $('#effectTypes').val();
			var selectedEffect = "squire";
			
			//most effect types need no options passed by default
			var options = {};
			//check if it's scale, transfer, or size - they need options explicitly set
			if(selectedEffect == 'scale'){  options = {percent: 100}; }
			else if(selectedEffect == 'transfer'){ options = { to: "#button", className: 'ui-effects-transfer' }; }
			else if(selectedEffect == 'size'){ options = { to: {width: 280,height: 185} }; }
			
			//run the effect
			$("#"+divid).find('div').hide();
			$("#"+divid).find('div').show();
			$("#"+divid).find('li').find('a').find('img').animate({
				opacity : 1
			});
			//var io = $("#"+divid).find('div');
			var io = $("#"+divid);
			//$("#"+divid).find('div').show("blind",null,500,callback);*/
			

				$("#"+divid+":not(:animated)").find('img').stop(false,true).animate({
					opacity : .3
				});
				
				$("."+divid).stop(false,true).effect("blind",{ mode: "hide" },300,null);
			
			
			
			
			


			//$("#"+divid).find('span').show("blind",null,500,callback);
//			$("#"+divid).find('div').runEffect($());
			//alert($("#"+divid).find('div').attr('id'));
			////alert($("#"+divid).find('li').find('span').text());
			
/*			
			$("div[id^=blog_][id!="+divid+"]:not(:animated)").each(function(){
				var newID = $(this).attr("id").substring(divid.length-1);										 
				if (newID > id){
					//$(this).show(selectedEffect,options,500,callback);
					//$(this).toggle("blind",null,500,callback);


				}
			});*/
			
			
		};
