/*

WE SHAPE

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

BACKEND by BONACODE
http://bonacode.com/

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

*/





$(window).resize(function(){
});


$(window).smartresize(function(){
	sliderCorrectOnResize( "sl-0" );
	updateSidebar();
});


$(window).load(function(){
});


$(document).ready(function(){
	if(mainArea == 'language-select'){
		initLangBtns();
	}	
	loadDesign();
	if(mainArea == 'design' || mainArea == 'we'){
		startSlider( "sl-0", false );
		startDrag();
	}
	if(mainArea == 'design'){		
		initSidebar();
		initSideBarControl();
	}
	if(mainArea == 'design'){
		$("#top-menu").hide();	
		if(easing == true){
			loadCurrentImage();	
		}else{
			$(".low-res").hide();
			checkLast();
		}
		var idioma = $.cookie("idioma");
		if(idioma == "en"){
			blogUrl = '/blog/?hide_header=1&lang=en';
		}else{
			blogUrl = '/blog/?hide_header=1';
		}
		$('#stage-1').load( blogUrl, function() {
		  $(".wb-canvas-fixed").hide();
		  initBlog();
		});					
	}	
});


$(document).mousemove(function(evt){
	var doc_height = $(window).height();
	//var doc_width = $(document).width();
	
	if( evt.pageY/doc_height>0.925 ){
		$("#sl-0-sensor").addClass("disabled");
	} else {
		$("#sl-0-sensor").removeClass("disabled");
	}
});


/* DESIGN */

function loadDesign(){
	correctSliderButtons();
	mouseHint();
	if(mainArea == 'design' || mainArea == 'we'){
		positionProjetos();
	}
	skipVideo();

	$(".canvas-fixed .menu-top-icon, .canvas-fixed .menu-middle-icon, .canvas-fixed .menu-bottom-icon").click(function(evt){
		menuSwap();
	});
	
	$("#contato-form .close").click(function(evt){
		contatoHide();
		evt.preventDefault();
	});
}

function skipVideo(){	
	$("#skip-video, #skip-video-button").click(function(){
		location.href="weshape.php";
	});
}

function correctSliderButtons(){
	$(".specific").html("_");
}

function mouseHint() {
	$("#hint-horizontal").hide();
	$("#hint-vertical").hide();
	$("#hint-home").hide();
	var doc_height = $(window).height();
	
	$(".transition-horizontal #sl-0-sensor").mousemove(function(evt){  
		$("#hint-horizontal").show();
        $("#hint-horizontal").css({
            top: (evt.pageY + 30) + "px",
            left: (evt.pageX - 27) + "px"
        });
		if(subArea == 'home'){
			$("#hint-home").show();
			$("#hint-home").css({
	            top: (evt.pageY + 50) + "px",
	            left: (evt.pageX - 1) + "px"
	        });
		}else{
			$("#hint-home").hide();
		}
    });

	$(".transition-horizontal #sl-0-sensor").mouseleave(function(e){
        $("#hint-horizontal").hide();
		$("#hint-home").hide();
    });
	
	$(".transition-vertical #sl-0-sensor, #sidebar-sensor").mousemove(function(evt){
        $("#hint-vertical").show();
		var secure_page_y = ( doc_height-evt.pageY<57 ) ? doc_height-75 : (evt.pageY - 18);
        $("#hint-vertical").css({
            top: secure_page_y + "px",
            left: (evt.pageX + 23) + "px"
        });
    });

	$(".transition-vertical #sl-0-sensor, #sidebar-sensor").mouseleave(function(e){
        $("#hint-vertical").hide();
    });
}



function menuAllHide( projeto_height ){
	if( $("#projetos").hasClass("shown") ){
		$("#projetos ul").animate({
			top: projeto_height
		}, 250, function() {
			$("#projetos").removeClass("shown");
			$("#projetos").hide();
			menuHide();
		});
	} else {
		menuHide();
	}
	contatoHide();
}

function positionProjetos(){
	$("#projetos").css( "top", (($("#projetos").height()*-1)+40)+"px" );
	$("#projetos").css( "left", (($(".option-2").offset().left)-9)+"px" );
	var projeto_height = $("#projetos").height();

	$("#projetos ul").css( "top", projeto_height+"px" );
	$("#projetos").hide();
	
	$("li a[class|=option]").click(function(evt){
		//evt.preventDefault();
	});
	
	//home
	$(".option-0").click(function(evt){
		if(subArea != 'home'){
			goHome();	
		}
		evt.preventDefault();
	});
	//nos
	$(".option-1").click(function(evt){
		if(subArea != 'we'){
			goWe();
		}
		evt.preventDefault();
	});
	//design
	$(".option-2").click(function(evt){
		if(subArea != 'design'){
			goDesign();
		}
		evt.preventDefault();		
	});	
	//design - projetos
	$("#projetos-toggle-sub").click(function(evt){
		if( $("#projetos").hasClass("shown") ){
			projetoHide( projeto_height );
		} else {
			projetoShow();
		}
		evt.preventDefault();
	});	
	//inspiração
	$(".option-3").click(function(evt){
		if(subArea != 'blog'){
			goBlog();
		}
		evt.preventDefault();
	});
	//contato
	$(".option-4").click(function(evt){
		if( $("#contato-form").hasClass("shown") ){
			contatoHide();
		} else {
			contatoShow();
		}
		evt.preventDefault();
	});
}

function contatoHide(){
	$("#contato-form").removeClass("shown");
	$("#contato-form").hide();
}

function contatoShow(){
	$("#contato-form").addClass("shown");
	$("#contato-form").show();
}

function projetoShow(){
	$("#projetos ul").css("top", 250);
	$("#projetos").addClass("shown");
	$("#projetos-toggle-sub").addClass("active");
	$("#projetos").show();
	$("#projetos ul").animate({
		top: 0
	}, 250);
	
	
	$("#projetos").mouseleave(function(e){
		var projeto_height = $("#projetos").height();
        projetoHide( projeto_height );
    });
}

function projetoHide( projeto_height ){
	var projeto_height = $("#projetos").height();
	$("#projetos ul").animate({
		top: projeto_height
	}, 250, function() {
		$("#projetos").removeClass("shown");
		$("#projetos-toggle-sub").removeClass("active");
		$("#projetos").hide();
	});
}


/* SLIDER */

function startSlider( context, allowLoop ){
	allowLoop = typeof(allowLoop) != "undefined" ? allowLoop : true;
	
	if( $("#"+context+" .group").length > 1 ){
		var currentSlide = $("#"+context+" .thumb.selected").closest(".group").attr("id");
		currentSlide = ( !currentSlide ) ? context+"-0" : currentSlide;
		//sliderScrollTo( context, "#"+currentSlide, true );
		disableButtons( context, allowLoop );
		
		$("#"+context+"-menu .next").click(function(evt){
			var target = findNext( context, allowLoop );
			sliderScrollTo( context, target );
			disableButtons( context, allowLoop );
			evt.preventDefault();
		});

		$("#"+context+"-menu .prev").click(function(evt){
			var target = findPrev( context, allowLoop );
			sliderScrollTo( context, target );
			disableButtons( context, allowLoop );
			evt.preventDefault();
		});
		
		$("#"+context+"-menu .specific").click(function(evt){
			var target = $(this).attr("href");
			if(magicMove == true){
				var target = parseFloat( target.split("-").pop() );
				scrollToTheProject( 0, target)
			}else{
				sliderScrollTo( context, target );
			}
			
			disableButtons( context, allowLoop );
			evt.preventDefault();
		});

	} else {
		// $("#"+context+"-menu").css("display", "none");
		$("#"+context+"-menu .next, #"+context+"-menu .prev, #"+context+"-menu .specific").css("visibility", "hidden");
	}
}

function sliderCorrectOnResize( context ){
	if(mainArea == 'design' || mainArea == 'we'){
		target= "#sl-0-"+ slideAtual;
		sliderScrollTo( context, target );
	}
}

function sliderScrollTo( context, target, nospeed ){
	var spd = (nospeed) ? 0 : 400;
	markSelectedGroup( context, target );
	myScrollTo( target, spd );
}

function markSelectedGroup( context, target ){
	$("#"+context+" .group").removeClass("selected");
	$(target).addClass("selected");
	markSliderMenu( context, target );
}

function markSliderMenu( context, target ){
	$("#"+context+"-menu a").removeClass("selected");
	$("#"+context+"-menu a[href="+target+"]").addClass("selected");
}

function findSelectedGroup( context ){
	if( !$("#"+context+" .group").hasClass("selected") ){
		$("#"+context+" .slider .group:first-child").addClass("selected");
	}
	return( $("#"+context+" .group.selected").attr("id") );
}

function findNext( context, allowLoop ){
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	if( allowLoop ){
		var target = ( selected_index == total-1 ) ? "#"+context+"-0" : "#"+context+"-"+(selected_index+1);
	} else {
		var target = ( selected_index == total-1 ) ? "#"+context+"-"+selected_index : "#"+context+"-"+(selected_index+1);
	}
	return target;
}

function findPrev( context, allowLoop ){
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	if( allowLoop ){
		var target = ( selected_index == 0 ) ? "#"+context+"-"+(total-1) : "#"+context+"-"+(selected_index-1);
	} else {
		var target = ( selected_index == 0 ) ? "#"+context+"-"+selected_index : "#"+context+"-"+(selected_index-1);
	}
	return target;
}

function disableButtons( context, allowLoop ){
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	if( !allowLoop ){
		if( selected_index == 0 ){
			$("#"+context+"-menu .prev").addClass("disabled");
			$("#"+context+"-menu .next").removeClass("disabled");
		} else if( selected_index == total-1 ){
			$("#"+context+"-menu .prev").removeClass("disabled");
			$("#"+context+"-menu .next").addClass("disabled");
		} else {
			$("#"+context+"-menu .prev").removeClass("disabled");
			$("#"+context+"-menu .next").removeClass("disabled");
		}
	}
}

function initLangBtns(){
	$(".pt").click(function(evt){
		$.cookie("idioma", "pt");
		//evt.preventDefault();
	});
	$(".en").click(function(evt){
		$.cookie("idioma", "en");
		//evt.preventDefault();
	});
}

function initSideBarControl(){
	$("#sidebar-control-up").click(function(evt){
		sideBarUp();
		evt.preventDefault();
	});
	$("#sidebar-control-dn").click(function(evt){
		sideBarDn();
		evt.preventDefault();
	});
	$("#sidebar-control").css("bottom", -5000);
}


