/*

WE SHAPE

FRONTEND by LEO FAVRE
http://leofavre.com/

BACKEND by BONACODE
http://bonacode.com/

DESIGN by WE SHAPE
http://weshape.com.br/

*/





$(document).ready(function(){
	if(typeof(window["subArea"]) == "undefined"){
		initBlog();
	}
});

function initBlog(){
	$(".feedback").click( function( evt ){
		$(this).parent().find(".post-comments").toggle();
		var max_content = $(this).parent();
		commentNav( max_content );
		evt.preventDefault();
	});
	
	$(".option-8").click(function(evt){
		$("#archive-month").slideToggle( "fast" );
		$("#archive-tag").slideUp( "fast" );
		evt.preventDefault();
	});
	
	$(".option-9").click(function(evt){
		$("#archive-tag").slideToggle( "fast" );
		$("#archive-month").slideUp( "fast" );
		evt.preventDefault();
	});
	
	$("#archive-month, #archive-tag").mouseleave(function(){
		$("#archive-month").slideUp( "fast" );
		$("#archive-tag").slideUp( "fast" );
	});
		
	$(".wb-canvas-fixed .menu-top-icon, .wb-canvas-fixed .menu-middle-icon, .wb-canvas-fixed .menu-bottom-icon").click(function(evt){
		menuSwapBlog();
	});
	
	$("#wb .close").click(function(evt){
		var max_context = $(this).closest('.post-comments').parent();
		max_context.find(".post-comments").toggle();
		evt.preventDefault();
	});
}

function goBackToSite(){
	if(typeof(window["subArea"]) == "undefined"){
		window.location.replace("/weshape.php");
	}else{		
		goUp();
		//evt.preventDefault();
		return false;
	}
}


function menuSwapBlog(){
	if( !$("body").hasClass("ipad") ){
		var menu_top_width = ( $("body").hasClass("language-pt") ) ? 97 : 97;
		var menu_middle_width = ( $("body").hasClass("language-pt") ) ? 108 : 108;
		var menu_bottom_width = ( $("body").hasClass("language-pt") ) ? 272 : 278;
	} else {
		var menu_top_width = ( $("body").hasClass("language-pt") ) ? 138 : 138;
		var menu_middle_width = ( $("body").hasClass("language-pt") ) ? 148 : 148;
		var menu_bottom_width = ( $("body").hasClass("language-pt") ) ? 399 : 420;
	}
	
	if( $(".wb-canvas-fixed .menu-top, .wb-canvas-fixed .menu-middle, .wb-canvas-fixed .menu-bottom").hasClass("closed") ){
		menuShowBlog( menu_top_width, menu_middle_width, menu_bottom_width );
	} else {
		menuAllHideBlog();
	}
}

function menuShowBlog( menu_top_width, menu_middle_width, menu_bottom_width ){
	menuAllHideBlogFix();
	
	$(".wb-canvas-fixed .menu-top").animate({
		width: menu_top_width
	}, 350, function() {
		$(".wb-canvas-fixed .menu-top").removeClass("closed");
	});
	
	$(".wb-canvas-fixed .menu-middle").animate({
		width: menu_middle_width
	}, 350, function() {
		$(".wb-canvas-fixed .menu-middle").removeClass("closed");
	});
	
	$(".wb-canvas-fixed .menu-bottom").animate({
		width: menu_bottom_width
	}, 350, function() {
		$(".wb-canvas-fixed .menu-bottom").removeClass("closed");
	});
}

function menuHideBlog(){
	var min_width = ( !$("body").hasClass("ipad") ) ? 50 : 70;
	
	$(".wb-canvas-fixed .menu-top, .wb-canvas-fixed .menu-middle, .wb-canvas-fixed .menu-bottom").animate({
		width: min_width
	}, 350, function() {
		$(" .wb-canvas-fixed.menu-top, .wb-canvas-fixed .menu-middle, .wb-canvas-fixed .menu-bottom").addClass("closed");
		menuAllHideBlogFix();
	});
}

function menuAllHideBlog(){
	$("#archive-month:visible").slideUp( "fast", function(){ menuHideBlog(); } );
	$("#archive-tag:visible").slideUp( "fast", function(){ menuHideBlog(); } );
	
	if( !$("#archive-tag").is(":visible") && !$("#archive-month").is(":visible") ){
		menuHideBlog();
	}
}

function menuAllHideBlogFix(){
	$(".wb-canvas-fixed .menu-top li, .wb-canvas-fixed .menu-middle li, .wb-canvas-fixed .menu-bottom li").toggle();
}

function commentNav( max_content ){
	var context       = max_content.find('.commentlist');
	var menu_context  = max_content.find('.comments-navigation');
	var child         = 'div';
	var num_childs    = context.find(child).length;
	var current_child = 1;
	commentShow(context, child, current_child);
	
	menu_context.find('.prev').click(function(evt){
		current_child--;
		current_child = (current_child<1) ? num_childs : current_child;
		commentShow(context, child, current_child);
		evt.preventDefault();
	});
	
	menu_context.find('.next').click(function(evt){
		current_child++;
		current_child = (current_child>num_childs) ? 1 : current_child;
		commentShow(context, child, current_child);
		evt.preventDefault();
	});
	
	function commentShow(context, child, current_child){
		context.find(child).hide();
		context.find(child+':nth-child('+current_child+')').show();
	}
}
