var $j = jQuery.noConflict();

$j(document).ready(function(){ 
        $j("div.functions a.more").hide();
        $j("div.functions a.more-post").hide();
  
        $j("div.page div.functions a.less-post").hide();
        $j("div.page div.hide").hide();
  
        $j(".search div.hide").hide();
        $j(".search div.functions a.less").hide();
        $j(".search div.functions a.less-post").hide();
        $j(".search #content .page .hide").html('');
        $j(".search div.functions a.more").show();
        $j(".search div.functions a.more-post").show();
  
        $j(".page-template-default .publicaciones div.entry ul li:last-child").each (function() {
        	$j(this).addClass('last');
         });
        $j(".page-template-default div.entry ul li:first").addClass('first');
        $j(".page-template-default div.entry ul li:even").addClass('even');
        $j(".page-template-default div.entry ul li:odd").addClass('odd');
  
        $j(".investigacion table tr").attr({valign: "top"});
        $j(".investigacion table td:first").addClass('first');
        $j(".investigacion table td:even").addClass('even');
        $j(".investigacion table td:odd").addClass('odd');
        $j(".investigacion table td:last").addClass('last');
       
        $j("div.entry img").each (function() {
        	var parentName = $j(this).parent().parent().parent().parent().attr("id");
        	$j(this).parent().attr("rel", parentName);
        });
  		$j("a.popup").fancybox();

        $j("div.email").dialog({autoOpen:false, modal: true, title: 'Email sharing', dialogClass: 'popup email_sharing', resizable: false});
        $j("div#form-popup").dialog({autoOpen:false, modal: true, title: 'Login', dialogClass: 'popup', resizable: false});

        $j("a.email").click(function(){
           $j("div.email_form").show();
           $j("div.email_info").html('');
           fromName = $j(".user-logged-info .user-name").html();
           if (fromName != '') {
           		$j(".from_name").attr("value", fromName);   		
           }
            $j("div.email").dialog('open'); 
        });

        $j("a.login").click(function(){
           $j("div#form-popup").dialog('open');
        });
        
        
        $j("input.email_sharing").click(function(){
            var from = $j("input.from_name").val();
            var to = $j("input.to_name").val();
            var email = $j("input.to_email").val();
            var permalink = $j("input.permalink").val();
            if(from != '' && to != '' && email != '' && permalink != '') {
                $j("div.email_form").hide();
                $j("div.email_info").html('<div class="loading"><img src="http://www.almudena-ocana.es/wp-content/themes/almudena/images/ajax-loader.gif" alt="processing"/></div>');
                setTimeout(function() {
                    $j('div.loading').fadeOut();
                    $j.post("http://www.almudena-ocana.es/wp-content/themes/almudena/email_sharing.php", {from:from, to:to, email:email, permalink:permalink}, function(data){
                    if(data == 'true'){
                        $j("div.email_info").html('<p>El mensaje se ha enviado correctamente</p><a class="email-again" href="javascript:">enviar otro!</a><a class="email-close" href="#">cerrar</a>');
                        $j("a.email-close").click(function(){
        					$j("div.email").dialog('close');
				        });
				        
				        $j("a.email-again").click(function(){
				        	$j("div.email_info").html('');
				        	$j(".to_name").attr("value", "");
				        	$j(".to_email").attr("value", "");
				        	$j(".email-close").hide();
				        	$j(this).hide();
				        	$j("div.email_form").show();
				        });
                    } else {
                        $j("div.email_info").html('<p>No se ha podido enviar el mensaje.<br/>Perdone las molestias</p><a class="email-close" href="javascript:">cerrar</a>');
                    }
                    }, "text");
                }, 3000);

                /*console.log('ok, no problem\n'+from +', '+to+', '+email+', '+permalink);*/
            } else {
                $j("div.email_info").html('Debe rellenar todos los campos');
            }
        });

        function check_more() {
                var count = $j("a.less-post:visible");
                if (count.length == 0) {
                        $j("div.functions a.less").hide();
                        $j("div.functions a.more").show();
                }
        }

        function check_less() {
                var count = $j("a.more-post:visible");
                if (count.length == 0) {
                        $j("div.functions a.more").hide();
                        $j("div.functions a.less").show();
                }
        }

        $j("a.less").click(function() {
                $j("a.less").hide();
                $j("a.less-post").hide();
                $j("a.more-post").show();
                $j("a.more").show();
                $j("div#content div.content div.hide").slideUp('slow');

        });
        $j("a.more").click(function() {
                $j("a.more").hide();
                $j("a.more-post").hide();
                $j("a.less-post").show();
                $j("a.less").show();
                $j("div#content div.content div.hide").slideDown('slow');
        });
        $j("a.more-post").click(function() {
                $j(this).hide();
                $j(this).prev().show();
                $j(this).parent().next().children('div.content').children('div.hide').slideDown('slow');
                check_less();
        });
        $j("a.less-post").click(function() {
                $j(this).hide();
                $j(this).next().show();
                $j(this).parent().next().children('div.content').children('div.hide').slideUp('slow');
                check_more();
        });
});
