(function($){
	function getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		var arrayPageScroll = {'xScroll':xScroll,'yScroll':yScroll};
		return arrayPageScroll;
	}	
	function getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		var largestWidth;
		var largestHeight;
		var smallestWidth;
		var smallestHeight;
		if ( pageWidth >= windowWidth )
		{	largestWidth = pageWidth; smallestWidth = windowWidth;	}
		else
		{	largestWidth = windowWidth; smallestWidth = pageWidth;	}
		if ( pageHeight >= windowHeight )
		{	largestHeight = pageHeight; smallestHeight = windowHeight;	}
		else
		{	largestHeight = windowHeight; smallestHeight = pageHeight;	}
		var arrayPageSize = {'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight,'largestWidth':largestWidth,'largestHeight':largestHeight};
		return arrayPageSize;
	}		
	$().ready(function(){		
		$(".posts").each(function(){			
			var hide_opened_post = function(){
				var c = $("li.post_content_opened", posts);
				r = 0;
				if ( c.length ) {
					c.removeClass("selected");
					r = $(".post_content",c).outerHeight();
					$(".post_content",c).animate({height:"hide"}, {
						queue:false, duration:800, easing: "easeInOutExpo", complete:function(){
							$(this).parents("li").removeClass("post_content_opened");
						}
					});					
					$(".introducao",c).show();
				}				
				return r;
			};			
			var show_post_content = function(e, h, f) {
				var r = hide_opened_post();
				e = $(e);
				$(".introducao", e).animate({height:"hide"}, {
					queue:false, duration:800, easing: "easeInOutExpo", complete:function(){
						}
				});
				var cnt = $(".post_content", e);
				if (cnt.length) {
					if ( h == undefined ) {
						hh = parseInt($(e).data("post_content_height")) || undefined;
						if (h == undefined) {
							hh = "show";
						} else {
							hh = h +"px";
						}
					} else {
						hh = h+"px";
					}
					cnt.animate({height:"show"}, {
						queue:false, duration:800, easing: "easeInOutExpo", complete:function(){
							var o = e.offset(); o.top -= 40;
							var s = getPageScroll();
							if ( o.top < s.yScroll ) {
								$.scrollTo(o.top, 400);
							} else {
								var z = getPageSize();
								h = parseInt(h) || 0;
								h = o.top - s.yScroll + h - 0;
								if ( h > z.windowHeight ) {
									$.scrollTo(o.top, 400);
								}
							}							
							$(this).parents("li").addClass("post_content_opened");
							if (typeof f == "function") {
								f();
							}
						}
					});
				}
			};			
			var posts_waiting = false;
			var posts = this;			
			if ( $(".post_content", posts).length) {
				var def_li = $("li", posts).filter(".selected")[0];
				$(".post_content", posts).each(function(){
					var li = $(this).parents("li");
					var texto1,texto2,texto3;
					if($("#t1", li).html()!=null){
						text = $("#t1", li).html();
						texto1 = '<p>'+text+'</p>';
					}else{
						texto1 = '';
					}
					if($("#t2", li).html()!=null){texto2 = '<p>'+$("#t2", li).html()+'</p>';}else{texto2 = '';}
					if($("#t3", li).html()!=null){texto3 = '<p>'+$("#t3", li).html()+'</p>';}else{texto3 = '';}
					if($("#t4", li).html()!=null){texto4 = '<p>'+$("#t4", li).html()+'</p>';}else{texto4 = '';}
					if($("#t5", li).html()!=null){texto5 = '<p>'+$("#t5", li).html()+'</p>';}else{texto5 = '';}
					if($("#t6", li).html()!=null){texto6 = '<p>'+$("#t6", li).html()+'</p>';}else{texto6 = '';}
					if($("#t7", li).html()!=null){texto7 = '<p>'+$("#t7", li).html()+'</p>';}else{texto7 = '';}
					if($("#t8", li).html()!=null){texto8 = '<p>'+$("#t8", li).html()+'</p>';}else{texto8 = '';}
					if($("#t9", li).html()!=null){texto9 = '<p>'+$("#t9", li).html()+'</p>';}else{texto9 = '';}
					if($("#t10", li).html()!=null){texto10 = '<p>'+$("#t10", li).html()+'</p>';}else{texto10 = '';}
					if($("#without_more", li).html()!=null){
						linkk = '';
					}else{
						var linkk = '<p><a href="#" title="" class="post_ajax_title"><b>[+] Read more</b></a></p><p>&nbsp;</p>';
					}					
					$(".introducao", li).html(texto1+texto2+texto3+texto4+texto5+texto6+texto7+texto8+texto9+texto10+linkk);
				});
			}			
			$("a.post_ajax_title", this).each(function(){
				var self = this;
				var li = $($(this).parents("li")[0]);				
				$(this).bind("click", function(){											   
					this.blur();
					if (posts_waiting) return false;					
					li.addClass("selected");					
					posts_waiting = true;
					var href = $(this).attr("href");
					if (href != "") {
						if (li.is(".post_ajax_loaded")) {
							if (li.is(".post_content_opened")) {
								li.removeClass("selected");
								$(".post_content", li).animate({height:"hide"}, {
									queue:false, duration:800, easing: "easeInOutExpo", complete:function(){
										posts_waiting = false;
										li.removeClass("post_content_opened");
									}
								});
								$(".introducao", li).show();
								$.scrollTo(170, 400);
							} else {
								li.addClass("selected");
								show_post_content(li, $(li).data("post_content_height"), function(){
									posts_waiting = false;
								});
							}
						} else {
							li.append( $("<div></div>").addClass("loading").append( $("<div></div>").addClass("loading_bar") ) );
							animate_loading(li);							
							$.ajax({
								url:href,
								data:"",
								type:"GET",
								success: function(msg) {
									li.addClass("post_ajax_loaded");
									$(".loading", li).remove();
									var cnt = $(".post_content", li);
									if (!cnt.length) {
										cnt = $("<div></div").addClass("post_content").append( $("<div></div>").addClass("post_content_wrapper"));
									}
									cnt.hide().find(".post_content_wrapper").html(msg);									
									li.append(cnt);
									cnt.show().css({height:"auto"});
									var h = cnt.outerHeight();
									cnt.hide();									
									$(li).data("post_content_height", h);									
									show_post_content(li,h, function(){
										posts_waiting = false;
									});	
								},
								error: function(){
									posts_waiting = false;
									$(".loading", li).remove();
								}
							});
						}
					}
					return false;
				});
			});	
			if ( $(".post_content", posts).length) {
				var def_li = $("li", posts).filter(".selected")[0];
				$(".post_content", posts).each(function(){
					var li = $(this).parents("li");
					$(this).show().css({height:"auto"});
					li.data("post_content_height", $(this).outerHeight());
					li.addClass("post_ajax_loaded").removeClass("post_content_opened").removeClass("selected");
					$(this).hide();					
				});				
				if (def_li) {
					window.setTimeout(function(){
						var h = $(def_li).data("post_content_height");
						$(def_li).addClass("selected");
					},500);
				}
			}
		});
		$("*").bind("click", function(e){
			if (!e.target) return;
			if (!$(e.target).is(".button_tags")) {
				$(".button_tags").parent().removeClass("selected");
			}			
			if (!$(e.target).is(".button_share")) {
				if (!$(e.target).parents(".button_share_container").length) {
					$("#sharebox").hide();
				}
			}
		});
	});
})(jQuery);			
