/*

WE SHAPE

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

BACKEND by BONACODE
http://bonacode.com/

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

*/


function startDrag(){
	if(dragTranstion == 'horizontal'){
		startDragHorizontal();
	}else if(dragTranstion == 'vertical'){
		startDragVertical();
	}
}

function startDragHorizontal(){	
	$("#sl-0-sensor").width($("#the-slider").width());	
	$("#sl-0-sensor").draggable({
		start: function() {
			inicial_x = $("#sl-0-sensor").position().left;
			$("#sl-0-sensor").width($("#the-slider").width());
			if(mainArea == 'design'){				
				hideHi();
			}
		},
		drag: function() {
			$("#the-slider").css("left", $("#sl-0-sensor").position().left);
		},
		stop: function() {
			if(dragSnap){
				final_x = $("#sl-0-sensor").position().left;
				delta = final_x - inicial_x;
				stopDragMe(delta);
			}			
		},
		axis: 'x'
	});			
	if(dragOrder == 'reverse'){
		context = "sl-0";
		var total = $("#"+context+" .group").length;
		total = total -1;
		t = "#sl-0-"+ total;
		sliderScrollTo( context, t, 0 )
	}	
}

function startDragVertical(){
	$("#sl-0-sensor").height($("#the-slider").height());
	$("#sl-0-sensor").draggable({
		start: function() {
			inicial_y = $("#sl-0-sensor").position().top;
			$("#sl-0-sensor").height($("#the-slider").height());
		},
		drag: function() {
			$("#the-slider").css("top", $("#sl-0-sensor").position().top);
		},
		stop: function() {
			if(dragSnap){
				final_y = $("#sl-0-sensor").position().top;
				delta = final_y - inicial_y;
				stopDragMe(delta);
			}			
		},
		axis: 'y'
	});
	if(dragOrder == 'reverse'){
		context = "sl-0";
		var total = $("#"+context+" .group").length;
		total = total -1;
		t = "#sl-0-"+ total;
		sliderScrollTo( context, t, 0 )
	}
}


function stopDragMe(delta){
	if(dragTranstion == 'horizontal'){
		stopDragMeHorizontal(delta);
	}else if(dragTranstion == 'vertical'){
		stopDragMeVertical(delta);
	}
}

function stopDragMeHorizontal(delta){
	context = "sl-0";
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	if(delta < 0){
		if(dragOrder == 'default'){
			selected_index++;
		}else if(dragOrder == 'reverse'){
			selected_index++;
		}		
	}else{
		if(dragOrder == 'default'){
			selected_index--;
		}else if(dragOrder == 'reverse'){
			selected_index--;
		}		
	}	
	if(selected_index < 0){
		selected_index = 0;
	}
	if(selected_index > (total -1)){
		selected_index = total -1; 
	}
	checkSidebar(selected_index);	
	if(selected_index == 0){		
		if(dragOrder == 'default'){
			novoX = 0;
		}else if(dragOrder == 'reverse'){
			dif = selected_index * -1
			novoX = $(window).width() * dif;
		}
	}else{
		dif = selected_index * -1
		novoX = $(window).width() * dif;
	}
	t = "#sl-0-"+ selected_index;
	
	$("#media-"+ selected_index).show();
		
	$("#sl-0-sensor").animate({ left: novoX }, "slow",  function() {
		//nope
	});
	$("#the-slider").animate({ left: novoX }, "slow",  function() {
		markSelectedGroup( context, t);
		disableButtons( context, false );
		updateAtual(selected_index);
		if(mainArea == 'design'){
			showHi();
		}
	});

}

function stopDragMeVertical(delta){
	context = "sl-0";
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	if(delta < 0){
		if(dragOrder == 'default'){
			selected_index++;
		}else if(dragOrder == 'reverse'){
			selected_index++;
		}		
	}else{
		if(dragOrder == 'default'){
			selected_index--;
		}else if(dragOrder == 'reverse'){
			selected_index--;
		}		
	}
	if(selected_index < 0){
		selected_index = 0;
	}
	if(selected_index > (total -1)){
		selected_index = total -1; 
	}
	checkSidebar(selected_index);	
	if(selected_index == 0){		
		if(dragOrder == 'default'){
			novoY = 0;
		}else if(dragOrder == 'reverse'){
			dif = selected_index * -1
			novoY = $(window).height() * dif;
		}
	}else{
		dif = selected_index * -1
		novoY = $(window).height() * dif;
	}
	t = "#sl-0-"+ selected_index;		
	$("#sl-0-sensor").animate({ top: novoY }, "slow",  function() {
	    markSelectedGroup( context, t);
		disableButtons( context, false );
	});
	$("#the-slider").animate({ top: novoY }, "slow",  function() {
	   	if(mainArea == 'design'){
			showHi();
		}
	});
}

function myScrollTo(target, spd ){
	if(dragTranstion == 'horizontal'){
		myScrollToHorizontal(target, spd );
	}else if(dragTranstion == 'vertical'){
		myScrollToVertical(target, spd );
	}
}

function myScrollToHorizontal(target, spd ){
	if(mainArea == 'design'){
		hideHi();
	}
	context = "sl-0";
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	if(selected_index == 0){		
		if(dragOrder == 'default'){
			novoX = 0;
		}else if(dragOrder == 'reverse'){
			dif = selected_index * -1
			novoX = $(window).width() * dif;
		}
	}else{
		dif = selected_index * -1
		novoX = $(window).width() * dif;
	}
	
	$("#media-"+ selected_index).show();
	updateAtual(selected_index);
		
	$("#sl-0-sensor").css("left", novoX);
	$("#the-slider").animate({ left: novoX }, 400,  function() {
	   	if(mainArea == 'design'){
			showHi();
		}
	});
	checkSidebar(selected_index);
}

function myScrollToVertical(target, spd ){
	context = "sl-0";
	var total = $("#"+context+" .group").length;
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	if(selected_index == 0){		
		if(dragOrder == 'default'){
			novoY = 0;
		}else if(dragOrder == 'reverse'){
			dif = selected_index * -1
			novoY = $(window).height() * dif;
		}
	}else{
		dif = selected_index * -1
		novoY = $(window).height() * dif;
	}	
	$("#sl-0-sensor").css("top", novoY);
	$("#the-slider").animate({ top: novoY }, 400,  function() {
	   	if(mainArea == 'design'){
			showHi();
		}
	});
	checkSidebar(selected_index);
}

function scrollToProject(p, selected_index){
	if(dragTranstion == 'horizontal'){
		scrollToProjectHorizontal(p, selected_index);
	}else if(dragTranstion == 'vertical'){
		scrollToProjectVertical(p, selected_index );
	}
}

function scrollToTheProject(p, destino){
	if(destino != slideAtual){
		if(mainArea == 'design'){
			hideHi();
		}
		
		context = "sl-0";
		var selected_id = findSelectedGroup( context );
		var selected_index = parseFloat( selected_id.split("-").pop() );
	
		if(destino > selected_index){
			primeiro = selected_index +1;
			ultimo = destino -1;
			novoXTemp = $("#the-slider").position().left - $(window).width();
		}else{
			primeiro = destino +1;
			ultimo = selected_index -1;
			novoXTemp = $("#the-slider").position().left - $(window).width();
		}	
		for(x=primeiro; x<=ultimo; x++){
			$("#sl-0-"+ x).hide();
		}		
		if(destino == 0){		
			if(dragOrder == 'default'){
				novoX = 0;
			}else if(dragOrder == 'reverse'){
				dif = destino * -1
				novoX = $(window).width() * dif;
			}
		}else{
			dif = destino * -1
			novoX = $(window).width() * dif;
		}	
		if(destino < selected_index){
			$("#the-slider").css("left", (novoX - $(window).width()) );
			novoXTemp = novoX;
		}	
	
		$("#media-"+ destino).show();
		updateAtual(destino);
	
		$("#the-slider").animate({ left: novoXTemp }, 500,  function() {
			$("#sl-0-sensor").css("left", novoX);	
			$("#the-slider").css("left", novoX);
			$(".group").show();	
			if(mainArea == 'design'){
				showHi();
			}
			checkWe();			
		});	
		checkSidebar(destino);
		t = "#sl-0-"+ destino;	
		markSelectedGroup( "sl-0", t);
		disableButtons( "sl-0", false );
	}
}

function moveToTheProject(p, destino){	
	context = "sl-0";
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	
	if(destino > selected_index){
		primeiro = selected_index +1;
		ultimo = destino -1;
		novoXTemp = $("#the-slider").position().left - $(window).width();
	}else{
		primeiro = destino +1;
		ultimo = selected_index -1;
		novoXTemp = $("#the-slider").position().left - $(window).width();
	}	
	for(x=primeiro; x<=ultimo; x++){
		$("#sl-0-"+ x).hide();
	}		
	if(destino == 0){		
		if(dragOrder == 'default'){
			novoX = 0;
		}else if(dragOrder == 'reverse'){
			dif = destino * -1
			novoX = $(window).width() * dif;
		}
	}else{
		dif = destino * -1
		novoX = $(window).width() * dif;
	}	
	if(destino < selected_index){
		$("#the-slider").css("left", (novoX - $(window).width()) );
		novoXTemp = novoX;
	}	
	
	$("#media-"+ destino).show();
	updateAtual(destino);
	
	$("#sl-0-sensor").css("left", novoX);	
	$("#the-slider").css("left", novoX);
	$(".group").show();	
	
	if(mainArea == 'design'){
		showHi();
	}	
	
	checkSidebar(destino);
	t = "#sl-0-"+ destino;	
	markSelectedGroup( "sl-0", t);
}

function scrollToProjectHorizontalFast(p, selected_index){
	if(selected_index == 0){		
		if(dragOrder == 'default'){
			novoX = 0;
		}else if(dragOrder == 'reverse'){
			dif = selected_index * -1
			novoX = $(window).width() * dif;
		}
	}else{
		dif = selected_index * -1
		novoX = $(window).width() * dif;
	}
	distancia = $("#sl-0-sensor").position().left - novoX;
	if(distancia < 0){
		distancia *= -1;
	}
	speed = 3000;
	$("#sl-0-sensor").css("left", novoX);
	$("#the-slider").animate({ left: novoX }, speed,  function() {
	   	if(mainArea == 'design'){
			showHi();
		}
	});
	checkSidebar(selected_index);
	t = "#sl-0-"+ selected_index;	
	markSelectedGroup( "sl-0", t);
}


function scrollToProjectHorizontal(p, selected_index){
	if(selected_index == 0){		
		if(dragOrder == 'default'){
			novoX = 0;
		}else if(dragOrder == 'reverse'){
			dif = selected_index * -1
			novoX = $(window).width() * dif;
		}
	}else{
		dif = selected_index * -1
		novoX = $(window).width() * dif;
	}
	distancia = $("#sl-0-sensor").position().left - novoX;
	if(distancia < 0){
		distancia *= -1;
	}
	speed = parseInt(distancia /10);
	$("#sl-0-sensor").css("left", novoX);
	$("#the-slider").animate({ left: novoX }, speed,  function() {
	   	if(mainArea == 'design'){
			showHi();
		}
	});
	checkSidebar(selected_index);
	t = "#sl-0-"+ selected_index;	
	markSelectedGroup( "sl-0", t);
}

function scrollToProjectVertical(p, selected_index){
	if(selected_index == 0){		
		if(dragOrder == 'default'){
			novoY = 0;
		}else if(dragOrder == 'reverse'){
			dif = selected_index * -1
			novoY = $(window).height() * dif;
		}
	}else{
		dif = selected_index * -1
		novoY = $(window).height() * dif;
	}
	distancia = $("#sl-0-sensor").position().top - novoY;
	if(distancia < 0){
		distancia *= -1;
	}
	speed = parseInt(distancia /10);	
	$("#sl-0-sensor").css("top", novoY);
	$("#the-slider").animate({ top: novoY }, 400,  function() {
	   	if(mainArea == 'design'){		
			showHi();
		}
	});
	checkSidebar(selected_index);	
	t = "#sl-0-"+ selected_index;	
	markSelectedGroup( "sl-0", t);
}

var firstLoad = true;

function updateAtual(index){
	if(firstLoad == true){
		firstLoad = false;
	}else{
		$.cookie("lastSlide", index);
	}
	if(index == home_position){
		homeScroll();
	}else{
		if(slideAtual == home_position){
			startDrag();
		}
	}
	slideAtual = index;
	updateMenu();
}

var dragDirection = null;

function homeScroll(){
	$("#sl-0-sensor").draggable({
		start: function() {
			//
		},
		drag: function() {
			followMulti();
		},
		stop: function() {			
			if(dragSnap){
				stopDragMeMulti(0);
			}			
		},
		axis: 'x,y'
	});
}

var masterDeltaX = 0;
var masterDeltaY = 0;

function followMulti(){
	finalX = $("#sl-0-sensor").position().left;
	beginX = $("#the-slider").position().left;
	deltaX = finalX - beginX;
	finalY = $("#sl-0-sensor").position().top;
	beginY = $("#the-slider").position().top;
	deltaY = finalY - beginY;

	if(dragDirection == null){
		if(deltaX < 0){
			deltaXX = deltaX *  -1;
		}else{
			deltaXX = deltaX;
		}
		if(deltaY < 0){
			deltaYY = deltaY *  -1;
		}else{
			deltaYY = deltaY;
		}
		if(deltaXX > deltaYY){
			dragDirection = "x";
		}else if(deltaXX < deltaYY){
			dragDirection = "y";
		}
	}	
	if(dragDirection == "x"){
		if(deltaX != 0){
			masterDelta = deltaX;
		}	
		$("#the-slider").css("left", finalX );
		masterDeltaX = deltaX;		
	}else if(dragDirection == "y"){
		if(deltaY != 0){
			masterDelta = deltaY;
		}
		$("#the-slider").css("top", finalY );	
		$("#stage-1").css("top", ($("#the-slider").height() + finalY) );	
		masterDeltaY = deltaY;
	}
	
}

function stopDragMeMulti(){
	if(dragDirection == "x"){		
		$("#sl-0-sensor").animate({ top: 0 }, "fast");
		$("#the-slider").animate({ top: 0 }, "fast");
		$("#stage-1").animate({ top: "100%" }, "fast");
		masterDelta = masterDeltaX;
		stopDragMe(masterDelta);
	}else if(dragDirection == "y"){
		goBlog();
	}
	dragDirection = null;
}

function goUp(){
	$("#body").removeClass("blog-active");
	$("#body").addClass("blog-inactive");
	lastSlide = $.cookie("lastSlide");
	if(lastSlide < home_position){
		subArea = 'we';
	}else if(lastSlide == home_position){
		subArea = 'home';			
	}else if(slideAtual > home_position){
		subArea = 'design';
	}else{
		subArea = 'home';
		lastSlide = home_position;
	}
	moveToTheProject(0, lastSlide);
	$("#top-menu").fadeIn();
	$(".wb-canvas-fixed").fadeOut();
	$("#stage-0").css("top", "-100%");
	$("#stage-1").css("top", "0%");
	$("#stage-0").animate({ top: "0%" }, "slow");	
	$("#stage-1").animate({ top: "100%" }, "slow", function() {
		if(subArea == 'home'){
			homeScroll();
		}else{
			$("#the-slider").css("top", 0 );
			showHi();
			startDrag();
		}
		
	});	
	updateMenu();
	checkSidebar(lastSlide);
}

function goHome(){
	//$("#sl-0-sensor").css("top", 0 );
	//$("#the-slider").css("top", 0 );
	if(subArea == 'blog'){
		moveToTheProject(0, home_position);
		$("#top-menu").fadeIn();
		$(".wb-canvas-fixed").fadeOut();
		$("#stage-0").animate({ top: "0%" }, "slow");				
		$("#stage-1").animate({ top: "100%" }, "slow",  function() {
			$("#the-slider").css("top", 0 );
			showHi();
			subArea = 'home';
			updateMenu();
			homeScroll();			
		});
	}else{		
		scrollToTheProject(0, home_position);
		subArea = 'home';
		updateMenu();
	}
}

function goWe(){
	if(subArea == 'blog'){
		moveToTheProject(0, we_position);
		$("#top-menu").fadeIn();
		$(".wb-canvas-fixed").fadeOut();
		$("#stage-0").animate({ top: "0%" }, "slow");				
		$("#stage-1").animate({ top: "100%" }, "slow",  function() {
			$("#the-slider").css("top", 0 );
			showHi();
			startDrag();
			subArea = 'we';	
			updateMenu();			
		});			
	}else{		
		scrollToTheProject(0, we_position);
		subArea = 'we';	
		updateMenu();
	}
}

function goDesign(){
	if(subArea == 'blog'){
		moveToTheProject(0, portfolio_position);
		$("#top-menu").fadeIn();
		$(".wb-canvas-fixed").fadeOut();
		$("#stage-0").animate({ top: "0%" }, "slow");				
		$("#stage-1").animate({ top: "100%" }, "slow",  function() {
			$("#the-slider").css("top", 0 );
			showHi();
			startDrag();
			subArea = 'design';
			updateMenu();			
		});
	}else{	
		scrollToTheProject(0, portfolio_position);
		subArea = 'design';
		updateMenu();
	}	
}

function goBlog(){
	if(subArea != 'blog'){
		$("#top-menu").fadeOut();
		$(".wb-canvas-fixed").fadeIn();			
		$("#menu-top-0").removeClass("selected");
		$("#menu-top-1").removeClass("selected");
		$("#menu-top-2").removeClass("selected");
		$("#stage-0").animate({ top: "-100%" }, "slow");				
		$("#stage-1").animate({ top: "0%" }, "slow",  function() {
			$("#sl-0-sensor").css("top", 0);
			$("#the-slider").css("top", 0);
			//startBlog();
			$("#body").removeClass("blog-inactive");
			$("#body").addClass("blog-active");
		});		
		subArea = 'blog';
		updateMenu();
		$("#sidebar-control").hide();
		$("#sidebar-"+projetoAtual).hide();
	}
}

function updateMenu(){
	$("#menu-top-0").removeClass("selected");
	$("#menu-top-1").removeClass("selected");
	$("#menu-top-2").removeClass("selected");
	$("#menu-top-3").removeClass("selected");
	if(subArea != 'blog'){
		if(slideAtual < home_position){
			$("#menu-top-1").addClass("selected");
			subArea = 'we';
		}else if(slideAtual == home_position){
			$("#menu-top-0").addClass("selected");
			subArea = 'home';			
		}else if(slideAtual > home_position){
			$("#menu-top-2").addClass("selected");
			subArea = 'design';
		}
	}else{
		$("#menu-top-3").addClass("selected");
	}
	mouseHint();	
}

function showHi(){
	context = "sl-0";
	var selected_id = findSelectedGroup( context );
	var selected_index = parseFloat( selected_id.split("-").pop() );
	selected_index = slideAtual;
	video = videoSrc[selected_index];
	if(video == ''){
		carregada = imagesSts[selected_index];
		arquivo = imagesSrc[selected_index];
		if(carregada == false){			
			if(arquivo != ''){
				if(selected_index < home_position){
					caminho = "content/we/_optimized/"+ arquivo;
				}else{
					caminho = "content/projects/_optimized/"+ arquivo;
				}			
				$("#hi-res-"+ selected_index).html("<img id='media-h-"+ selected_index +"-image' src='"+  caminho +"' />");
				$("#hi-res-"+ selected_index).hide();
				$("#media-h-"+ selected_index +"-image").load(function () {	
					imagesSts[selected_index] = true;			
					$("#hi-res-"+ selected_index).show();
					$("#low-res-"+ selected_index).hide();
				});
			}			
		}else{
			if(arquivo != ''){
				$("#hi-res-"+ selected_index).show();
				$("#low-res-"+ selected_index).hide();	
			}			
		}		
	}		
}
function hideHi(){
	$(".hi-res").hide();
	if(we_vis == true){
		$(".low-res").show();
	}	
}

var we_vis = false;

function checkWe(){		
	if(we_vis == false){
		we_vis = true;
		$(".low-res").show();
		$("#top-menu").fadeIn();
		$(".wb-canvas-fixed").hide();
		
	}
}

function checkLast(){
	lastSlide = $.cookie("lastSlide");
	if(lastSlide == "we"){
		goWe();
	}else if(lastSlide == "design"){
		goDesign();
	}else{
		if(lastSlide == undefined || lastSlide == null){
			$.cookie("lastSlide", home_position);
			goHome();
		}else{
			scrollToTheProject(0, lastSlide);
		}
	}		
}

