﻿
$(document).ready(function(){
  //IE6
  if($.browser.msie && $.browser.version<"7.0"){window.top.location = "http://tunebig.com/error/index.html";}
  //Open new window
  $("a[rel=external]").click(function(){
    var linkExterno = window.open($(this).attr("href"));
    return linkExterno.closed;
  });
	//Mandar Imprimir
  $("a[rel=alternate]").click(function(){
    var imprimir = window.print();
    return false;
  });
})

/*-----------------------------------------------------------------------
=Inserir E-mails correctos: Obrigatorio class="email", e [-at-]
-----------------------------------------------------------------------*/
$(document).ready(function(){
   $("a.email").each(function(){
     var mailReal = $(this).text().replace("[-at-]","@");
		 $(this).text(mailReal);
     $(this).attr("href", "mailto:"+mailReal);
	 })
})


/*-----------------------------------------------------------------------
=PRELOADING SITE
-----------------------------------------------------------------------*/
$(document).ready(function(){
	$words = $("#preload .words li");
  sequence($words, 0);
  // load
  $('#preloadHolder').load("toLoad.html",function(){
    $('#listLoad').imagesLoaded(function(){
      $("#preload").fadeTo(200,0, function(){$(this).hide();});
    });
  });
})

function aleatorio(minN,maxN){
	var aux = (maxN - minN + 1);
	return minN + Math.floor(aux * (Math.random() % 1));
}
function sequence($list, n) {
	var number = aleatorio(0,27);
  $list.eq(number).doTimeout( 150, function(){
   $(this).addClass("on");
   if(n < 100) { sequence($list, n+1) };
  })
}


/*-----------------------------------------------------------------------
Paralax
-----------------------------------------------------------------------*/
$(document).ready(function(){
  $(document).mousemove(function(e){
    var xMouse = e.pageX;
    var xCenter = $(window).width()/2;
    var offset = xMouse - xCenter;
	$(".body-bg").css({"left": -offset*0.01});

    $(".home-pic").css({"left": +offset*0.01});
	$(".about-pic").css({"left": +offset*0.01});
	$(".work-pic").css({"left": +offset*0.01});
	$(".how-pic").css({"left": +offset*0.01});
	$(".contact-pic").css({"left": +offset*0.01});

	
	$(".home1").css({"left": +offset*0.03});
	$(".home2").css({"left": +offset*0.05});
	$(".home3").css({"left": +offset*0.07});
	$(".home4").css({"left": +offset*0.04});
	$(".home5").css({"left": +offset*0.01});
	
	$(".about1").css({"left": +offset*0.03});
	$(".about2").css({"left": +offset*0.05});
	$(".about3").css({"left": +offset*0.01});
	
	$(".how1").css({"left": +offset*0.01});
	$(".how2").css({"left": +offset*0.05});
	$(".how3").css({"left": +offset*0.03});
	$(".how4").css({"left": +offset*0.03});
	
   })//end mouse move
})


/*-----------------------------------------------------------------------
=MENU  (y=mx+b)
-----------------------------------------------------------------------*/
$(document).ready(function(){
  var centroBtn = $('#btnGO').width()/2;
  $("nav li:eq(0)").addClass("nomouse");

///BTN ELASTIC DRAG
	$('#btnGO')
      .mousedown(function(){
      	$(this).addClass("press");
      })
      .mouseup(function(){
      	$(this).removeClass("press");
      })
		.drag(function( ev, dd ){
			$(this).css({
       left: Math.min( 404, Math.max( 0, dd.offsetX ) )
			});
      var auxX=parseInt($(this).css("left"));
      var aux=-11.88*auxX;
      fdrag(aux); //move stage
      fLinksOff(auxX+centroBtn-5); //compensar no drag
      fMask(auxX+15); //mask dots
 		},{ relative:true })
    .drag("end", function(){
      var aux = parseInt($(this).css("left"));
      var xDrag = aux + centroBtn; //deltaX ao centro do botao
      fGotoIndex(xDrag);
    })//end Drag

  function fGotoIndex(xDrag){
  	var diferenca = 1000;
    var xMenu = 0;
    var index = 0;
    $("nav li").each(function(){
      var aux = parseInt($(this).css("left"));
      var compLi = $(this).width()/2;
      var xMenuFinal = aux+compLi;
      var newDiferenca = Math.abs(xMenuFinal-xDrag);
      if (newDiferenca <= diferenca){
        diferenca = newDiferenca;
        xMenu = xMenuFinal;
        index = $(this).index();
      }
    });//end each
    var xFinal = Math.round(xMenu-centroBtn);
    $('#btnGO').stop().animate({left: xFinal}, 200,"easeOutBack");
    clickGo(index*-1600);
    //mask dots
    fMask(xFinal+15);
    //links off and mouse
    $("nav li").removeClass("nomouse");
    $("nav li:eq("+index+")").addClass("nomouse");
    fLinksOff(xFinal);
    //url hash
    var hrefId = index+1;
    $.bbq.pushState({ go: hrefId });
    //Botao dia noite
    dianoite(index);
  }//end function

///CLICK  (clicado needs be global)
  var clicado=0;
  $("nav li").click(function(){
    var a = parseInt($(this).css("left"));
    var comp = $(this).width()/2;
    var ui = Math.round(a+comp-centroBtn);
    $('#btnGO').stop().animate({left: ui}, 400,"easeOutExpo");
    //
    clicado = $(this).index();
    clickGo(clicado*-1600);
    //mask dots
    fMask(ui+15);
    //links off and mouse
    $("nav li").removeClass("nomouse");
    $(this).addClass("nomouse");
    fLinksOff(ui);
    //url hash
    var auxHrefId = $(this).attr("id");
    var hrefId = auxHrefId.replace("navL","");
    $.bbq.pushState({ go: hrefId });
    //Botao dia noite
    dianoite(clicado);
  })

	$("h1").click(function(){$("nav li:eq("+0+")").click();});
  $("#contact").click(function(){$("nav li:eq("+4+")").click();});

//KEY PRESS
  $(document).keydown(function(e){
      switch(e.which){
        case 39:
          var aux = clicado+1;
          $("nav li:eq("+aux+")").click();
          break;
        case 37:
          var aux= clicado-1;
          $("nav li:eq("+aux+")").click();
          break;
        default:
          break;
      }//end switch
  });//end keypress

//MOUSE WHEEL
  var last = 0;
  $("body").mousewheel(function(event, deltaY) {
    var now = new Date().getTime();
    var elap = now - last;
    last = now;
    if(elap > 100){
      if (deltaY < 0){
        var aux = clicado+1;
        $("nav li:eq("+aux+")").click();
      }else{
        var aux= clicado-1;
        $("nav li:eq("+aux+")").click();
      }
    }
    event.preventDefault();
  });

//SWIPE  
  $("body").swipe({
    swipeLeft: function(){
      var aux = clicado+1;
      $("nav li:eq("+aux+")").click();
    },
    swipeRight: function(){
      var aux= clicado-1;
      $("nav li:eq("+aux+")").click();
    }
  });//end swipe

////*******FUNCTIONS
/// When drag, turn off links used btnGo as reference
  function fLinksOff(xCord){
    $("nav li").each(function(){
      var aux = parseInt($(this).css("left"));
      var comp = $(this).width()/2;
      var position = aux+comp;
      if ( position < xCord ){
        $(this).addClass("visited");
      }else{
        $(this).removeClass("visited");
      }
    })//end each
  }//end function

/// When drag use mask to dots
  function fMask(comp){
    $(".after").css({"clip": "rect(0px 443px 10px "+comp+"px)"});
  }//end function

/// When drag, stage goes to
  function fdrag(aux){
    $("#z300").stop().animate({"left": aux}, 200, "easeOutCubic");
    $("#z295").stop().animate({"left": aux}, 300, "easeOutCubic");
    $("#z290").stop().animate({"left": aux}, 400, "easeOutCubic");
    $("#z285").stop().animate({"left": aux}, 500, "easeOutCubic");
    $("#z280").stop().animate({"left": aux}, 600, "easeOutCubic");
    $("#z275").stop().animate({"left": aux}, 700, "easeOutCubic");
    $("#z200").stop().animate({"left": aux}, 800, "easeOutCubic");
    $("#z195").stop().animate({"left": aux}, 900, "easeOutCubic");
    $("#z190").stop().animate({"left": aux}, 1000, "easeOutCubic");
    $("#z185").stop().animate({"left": aux}, 1100, "easeOutCubic");
    $("#z180").stop().animate({"left": aux}, 1200, "easeOutCubic");
    $("#z150").stop().animate({"left": aux}, 1300, "easeOutCubic");
    $("#home").stop().animate({"left": aux}, 700, "easeOutCubic");
    $("#about").stop().animate({"left": aux}, 700, "easeOutCubic");
    $("#our_work").stop().animate({"left": aux}, 700, "easeOutCubic");
	$("#work").stop().animate({"left": aux}, 700, "easeOutCubic");
    $("#how").stop().animate({"left": aux}, 700, "easeOutCubic");
	$("#contact_us").stop().animate({"left": aux}, 700, "easeOutCubic");

  }//end function

/// When Click stage goes to
  function clickGo(px){
    $("#z300").stop().animate({"left": px}, 1350, "easeInOutCubic");
    $("#z295").stop().delay(100).animate({"left":  px}, 1350, "easeInOutCubic");
    $("#z290").stop().delay(200).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z285").stop().delay(300).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z280").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z275").stop().delay(500).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z200").stop().delay(150).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z195").stop().delay(250).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z190").stop().delay(300).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z185").stop().delay(350).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z180").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");
    $("#z150").stop().delay(450).animate({"left": px}, 1350, "easeInOutCubic");
    $("#home").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");
    $("#about").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");
    $("#our_work").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");
	$("#work").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");
    $("#how").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");
	$("#contact_us").stop().delay(400).animate({"left": px}, 1350, "easeInOutCubic");

    if(px != 0){
      $("h1").addClass("rato");
    }else{
      $("h1").removeClass("rato");
    }
  }//end function


})

