if (top.location != self.location) top.location = self.location; 

$.ajaxSetup({
    cache: false
});

$(document).ready( function() {
	$("A[rel='external']").live("click", function() {
		$(this).attr("target", "_blank");
    });
	
	/* Search */
	$('#searchbox, #PostQ').blur(function(){ if($(this).val() == '') $(this).val('Buscar...');});
	$('#searchbox, #PostQ').focus(function(){ if($(this).val() == 'Buscar...') $(this).val(''); });
	$('#PostSearchForm').submit(function(){ 	
		top.location = base_url + 'buscar/' + $('#PostQ').val();
		return false;
	});
	$('#SearchForm').submit(function(){ 	
		top.location = base_url + 'buscar/' + $('#searchbox').val();
		return false;
	});
	
	try{ 
		$('.tabbed-content div.section').hide();
		$(".sidebar-tabs").minitabs( { content : ".sections", nav: ".box-tabs", effect: "slide", speed: 150});
	} catch(e){$('.tabbed-content div.section').show();};
	
	
	$('.post-vote-link').live('click', function(){
		var id = $(this).attr('href').split('/');
		var sign = id[4];
		var post_id = id[3];
		var $link = $(this);
		
		$('#post-' + post_id + ' .post-vote-action').text('...');
		
		$.getJSON($(this).attr('href'), function(data) {
			if(data.error){
				alert(data.error);
			}
			else{
				var votes = parseInt(data.post.votes_registered) + parseInt(data.post.votes_anonymous);
				var message = '¡te picó!';
				if(sign == 'n') {
					message = ':-(';
					$link.parent().hide();
				}
				$('#post-' + post_id + ' .post-vote-action').text(message);
				$('#post-' + post_id + ' .post-vote-info').html(votes + '<br /><span>picores</span>');	
				$('#post-' + post_id + ' .post-karma').html(data.post.karma);
				$('#post-' + post_id + ' .post-karma').attr('title', 'karma: ' + data.post.karma);
				$('#post-' + post_id + ' .post-vote-negative').hide();
			}
		});
		return false;
	});
		
	$('.post-share').hover(
		function(){ $('#' + $(this).attr('id') + ' > .post-share-links').fadeIn();},
		function(){ $('#' + $(this).attr('id') + ' > .post-share-links').fadeOut(); }
	);
	
	$('.comment-vote-link').live('click', function(){
		var id = $(this).attr('id').split('-');
		var comment_id = id[4];
		var sign = id[3];
		$this = $(this);
		
		$.getJSON($(this).attr('href'), function(data) {
			if(data.error){
				alert(data.error);
			}
			else {
				if(sign == 'positive'){
					$this.parent().html('<img src="' + base_url + 'img/voted-up.png" alt="Positivo" />');
				}
				else{
					$this.parent().html('<img src="' + base_url + 'img/voted-down.png" alt="Negativo" />');
				}
				$('#comment-info-' + comment_id).html('votos: ' + data.comment.vote_count + ' karma: ' + data.comment.karma);
			}
		});
		
		return false;
	});
	try{
	$.validator.addMethod(
	        "regex",
	        function(value, element, regexp) {
	            var check = false;
	            var re = new RegExp(regexp);
	            return this.optional(element) || re.test(value);
	        },
	        "Comprueba el campo."
	);
	}catch(e){}
	


	// Edit Comments
	$(".edit-comment").live('click', function(e){
		var div = $(this).parents("div:eq(1)").attr('id');
		$.ajax({
			type: "GET", 
			url: this.href, 
			success: function(html){ 
				$('#' + div + ' > .comment-body').html(html);
				$('#' + div + ' .form-header').hide();
				$('#' + div + ' .form').css('margin', '0 0 0 0');
				$('#' + div + ' form').css('border', 'none');
				$('#' + div + ' .textarea label').hide();
				$('#' + div + ' .form br').hide();
			}
		});
		return false;
	});
	
	jQuery(".fadeThis").append(
	'<span class="hover"></span>').each(
	function() {
		var b = jQuery("> span.hover", this).css(
				"opacity", 0);
		jQuery(this).hover(function() {
			b.stop().fadeTo(333, 1);
		}, function() {
			b.stop().fadeTo(333, 0);
		});
	});
	jQuery("ul.menuList li a").nudge( {
		property : "padding",
		direction : "left",
		amount : 6,
		duration : 166
	});
});

function fillTooltip(comment, author){
	html = '<div class="tooltip-content">';
	if(author) html += '<div class="tooltip-author">' + author + '</div>';
	html +=	comment + '</div>';

	return html;
}

function showTooltip(link, post_id, order)
{
	var div = document.createElement('div');
	var $tooltip = $(div);
	$tooltip.attr('id', 'tooltip');
	$tooltip.hide();
	$('body').append(div);
	
	$tooltip.html(fillTooltip('cargando comentario ...', null));
	
	
	
	var comment = $('#comment-' + order + ' > .comment-body').html();
	var author = $('#comment-' + order + ' > .comment-head > .comment-author').text();
	
	if(comment){
		$tooltip.html(fillTooltip(comment, author));
	}else{
		var tooltipUrl = base_url + 'comments/tooltip/' + post_id + '/' + order;
		$.getJSON(tooltipUrl, function(data) {
			$tooltip.html(fillTooltip(data.Comment.message, data.Comment.username));
		});
	}
	
	var left = $(link).position().left + $(link).width() + 3;
	var top =  $(link).position().top + 20;
	
	$('#tooltip').css("left", left + "px");
	$('#tooltip').css("top", top + "px");
	$('#tooltip').show();
}

function hideTooltip()
{
	$('#tooltip').remove();
}

function showComment(id)
{
	$('#comment-' + id + ' .comment-body').show();
	$('#comment-' + id + ' .comment-show').hide();
}
/************************
Simple format functions
**********************************/
/*
  Code from http://www.gamedev.net/community/forums/topic.asp?topic_id=400585
  strongly improved by Juan Pedro López for http://meneame.net
  2006/10/01, jotape @ http://jplopez.net
*/

function applyTag(id, tag) {

	obj = document.getElementById(id);
	if (obj) wrapText(obj, tag, tag);
}

function wrapText(obj, tag) {
	if(typeof obj.selectionStart == 'number') {
		// Mozilla, Opera and any other true browser
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		if (start == end || end < start) return false;
		obj.value = obj.value.substring(0, start) +  replaceText(obj.value.substring(start, end), tag) + obj.value.substring(end, obj.value.length);
	} else if(document.selection) {
		// Damn Explorer
		// Checking we are processing textarea value
		obj.focus();
		var range = document.selection.createRange();
		if(range.parentElement() != obj) return false;
		if (range.text == "") return false;
		if(typeof range.text == 'string')
	        document.selection.createRange().text =  replaceText(range.text, tag);
	} else
		obj.value += text;
}

function replaceText(text, tag) {
	text = text.replace(/(^|\s)[\*_]([^\s]+)[\*_]/gm, '$1$2');
	text = text.replace(/([^\s]+)/gm, tag+"$1"+tag);
	//text = tag + text + tag;
	return text;
}

function replyComment(order){
	try{
		$('#CommentMessage').val('#' + order + ' ');
		$('#CommentMessage').focus();
	}catch(e){};
	return false;
}

jQuery.cookie = function(b, j, m) {
	if (typeof j != "undefined") {
		m = m || {};
		if (j === null) {
			j = "";
			m = jQuery.extend( {}, m);
			m.expires = -1;
		}
		var e = "";
		if (m.expires
				&& (typeof m.expires == "number" || m.expires.toUTCString)) {
			var f;
			if (typeof m.expires == "number") {
				f = new Date();
				f.setTime(f.getTime() + (m.expires * 24 * 60 * 60 * 1000));
			} else {
				f = m.expires;
			}
			e = "; expires=" + f.toUTCString();
		}
		var l = m.path ? "; path=" + (m.path) : "";
		var g = m.domain ? "; domain=" + (m.domain) : "";
		var a = m.secure ? "; secure" : "";
		document.cookie = [ b, "=", encodeURIComponent(j), e, l, g, a ]
				.join("");
	} else {
		var d = null;
		if (document.cookie && document.cookie != "") {
			var k = document.cookie.split(";");
			for ( var h = 0; h < k.length; h++) {
				var c = jQuery.trim(k[h]);
				if (c.substring(0, b.length + 1) == (b + "=")) {
					d = decodeURIComponent(c.substring(b.length + 1));
					break;
				}
			}
		}
		return d;
	}
};

(function(a) {
	a.fn.minitabs = function(b) {
		a.fn.minitabs.defaults = {
			content : ".sections",
			nav : "ul:first",
			effect : "top",
			speed : 333,
			cookies : true
		};
		var c = a.extend( {}, a.fn.minitabs.defaults, b);

		return this.each(function() {
			var e = a(this);
			var h = e.attr("id");
			var d = a("#" + h + " " + c.nav);
			if (c.cookies) {
				var f = h;
				var j = a.cookie(f);
			}
			e.find(c.content + " >div:gt(0)").hide();
			if (c.cookies && (j != null)) {
				d.find("li." + j).addClass("active");
				var g = d.find("li." + j + " a");
				var i = g.attr("href");
				e.find(c.content + " div" + i).show();
			} else {
				d.find("li:last").addClass("active");
				e.find(c.content + " div:first").show();
			}
			d.find("li>a").click(function() {
				if (!a(this).parent("li").hasClass("active")) {
					d.find("li").removeClass("active");
					if (c.cookies) {
						var m = a(this).parent("li").attr("class");
						a.cookie(f, m, {
							path : "/"
						});
					}
					a(this).parent("li").addClass("active");
					a(this).blur();
					var k = /([_\-\w]+$)/i;
					var l = a("#" + h + " #" + k.exec(this.href)[1]);
					if (c.effect == "slide") {
						e.find(c.content + " >div").slideUp(c.effect);
					} else {
						e.find(c.content + " >div").hide();
					}
					switch (c.effect) {
					case "top":
						if (isIE) {
							l.css( {
								top : -300
							}).show().animate( {
								top : 0
							}, c.speed, "easeOutQuart");
						} else {
							l.css( {
								opacity : 0,
								top : -300
							}).show().animate( {
								opacity : 1,
								top : 0
							}, c.speed, "easeOutQuart");
						}
						break;
					case "slide":
						l.slideDown(c.speed);
						break;
					case "height":
						originalHeight = l.height();
						l.css( {
							opacity : 0,
							height : 0
						}).show().animate( {
							opacity : 1,
							height : originalHeight
						}, c.speed, "easeOutQuart");
						break;
					}
					return false;
				}
			});
		});
	}
})(jQuery);

if (isIE == "undefined") {
	var isIE = false;
}
if (isIE6 == "undefined") {
	var isIE6 = false;
}
if (lightbox == "undefined") {
	var lightbox = 0;
}
if (ajaxComments == "undefined") {
	var ajaxComments = 0;
}
if (redirectReadMore == "undefined") {
	var redirectReadMore = 0;
}





jQuery.fn.nudge = function(a) {
	a = jQuery.extend( {
		amount : 20,
		duration : 300,
		property : "padding",
		direction : "left",
		toCallback : function() {
		},
		fromCallback : function() {
		}
	}, a);
	this.each(function() {
		var h = jQuery(this);
		var e = a;
		var d = e.direction;
		var g = e.property + d.substring(0, 1).toUpperCase()
				+ d.substring(1, d.length);
		var c = h.css(g);
		var f = {};
		f[g] = parseInt(e.amount) + parseInt(c);
		var b = {};
		b[g] = c;
		h.hover(function() {
			h.stop().animate(f, e.duration, "", e.toCallback);
		}, function() {
			h.stop().animate(b, e.duration, "", e.fromCallback);
		});
	});
	return this;
};

function updateCookie(c, b) {
	var a = c.replace(/[#. ]/g, "");
	jQuery.cookie(a, b, {
		path : "/"
	});
}