/*--------------------------------------------------------------------
=Open new window
-----------------------------------------------------------------------*/
$(document).ready(function(){
  //IE6
  if($.browser.msie && $.browser.version<"7.0"){window.top.location = "http://www.dbamosmak.pl/index.php?id=238";}
  //Open new window
  $("a[rel=external]").click(function(){
    var linkExterno = window.open($(this).attr("href"));
    return linkExterno.closed;
  });
  $("a[rel=alternate]").click(function(){
    var imprimir = window.print();
    return false;
  });
})
/*-----------------------------------------------------------------------
=RESOLUTIONS adjustments
-----------------------------------------------------------------------*/
$(document).ready(function(){
  var wWindow = $(window).width();
  var hWindow = $(window).height();
  //abertura do site
	fixPosition();
	//Doing resize of window
  $(window).resize(function () {fixPosition();});
  //Funcoes
  function fixPosition(){
    var wWindow = $(window).width(), hWindow = $(window).height();
		if(wWindow <1280 || hWindow >= 615 ){
       $("body").addClass("biger_view");
		}else{
       $("body").removeClass("biger_view");
		}

  }
})

/*-----------------------------------------------------------------------
=PRELOADING SITE
-----------------------------------------------------------------------*/
$(document).ready(function(){
	$words = $("#preload .words li");
  sequence($words, 0);
  // load
  $('#preloadHolder').load("http://www.dbamosmak.pl/fileadmin/dbamosmak_renewed/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) };
  })
}
/*-----------------------------------------------------------------------
=MENU  (y=mx+b)
-----------------------------------------------------------------------*/
$(document).ready(function(){
  var centroBtn = $('#btnGO').width()/2;
  $("nav li:eq(0)").addClass("nomouse");

  function fGotoIndex(yDrag){
  	var diferenca = 1000;
    var yMenu = 0;
    var index = 0;
    $("nav li").each(function(){
      var aux = parseInt($(this).css("top"));
      var compLi = $(this).height()/2;
      var yMenuFinal = aux+compLi;
      var newDiferenca = Math.abs(yMenuFinal-yDrag);
      if (newDiferenca <= diferenca){
        diferenca = newDiferenca;
        yMenu = yMenuFinal;
        index = $(this).index();
      }
    });//end each
    var yFinal = Math.round(yMenu-centroBtn);
    $('.next').stop().animate({left: yFinal}, 200,"easeOutBack");
    clickGo(index*-1130); // height of a slide
    //mask dots
    fMask(yFinal+15);
    //links off and mouse
    $("nav li").removeClass("nomouse");
    $("nav li:eq("+index+")").addClass("nomouse");
    fLinksOff(yFinal);
    //url hash
    var hrefId = index+1;
    $.bbq.pushState({ go: hrefId });
    //Botao dia noite
    dianoite(index);
  }//end function

///CLICK  
  var clicado=0;
  $("nav li").click(function(){
    var a = parseInt($(this).css("top"));
    var comp = $(this).height()/2;
    var ui = Math.round(a+comp-centroBtn);
    $('.next').stop().animate({top: ui}, 400,"easeOutExpo");
    //
    clicado = $(this).index();
    clickGo(clicado*-1130);    
    //mask dots
    fMask(ui+15);
    //links off and mouse
    $("nav li").removeClass("nomouse");
    $(this).addClass("nomouse");
    fLinksOff(ui);
    //url hash
    dianoite(clicado);
  })

	$("h1").click(function(){$("nav li:eq("+0+")").click();});
  $("#contact").click(function(){$("nav li:eq("+3+")").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();
  });
////*******FUNCTIONS
  function fLinksOff(yCord){
    $("nav li").each(function(){
      var aux = parseInt($(this).css("top"));
      var comp = $(this).height()/2;
      var position = aux+comp;
      if ( position < yCord ){
        $(this).addClass("visited");
      }else{
        $(this).removeClass("visited");
      }
    })//end each
  }//end function

  function fMask(comp){
    $(".after").css({"clip": "rect(0px 443px 10px "+comp+"px)"});
  }//end function

/// When Click stage goes to
  function clickGo(px){
   // $("#z300").stop().animate({"top": px}, 1350, "easeInOutCubic").delay(1200).fadeIn(700).css({"opacity": '1'});
    $("#z295").stop().delay(100).animate({"top":  px}, 1350, "easeInOutCubic");
    $("#z290").stop().delay(200).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z285").stop().delay(300).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z280").stop().delay(400).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z275").stop().delay(450).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z200").stop().delay(150).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z195").stop().delay(250).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z190").stop().delay(300).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z185").stop().delay(350).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z180").stop().delay(400).animate({"top": px}, 1350, "easeInOutCubic");
    $("#z150").stop().delay(450).animate({"top": px}, 1350, "easeInOutCubic");
    $("#home").stop().delay(400).animate({"top": px}, 1350, "easeInOutCubic"); 
  }//end function

  function dianoite(index){
    if(index == 3){
      $("#btnMudar").addClass("out");
    }else{
      $("#btnMudar").removeClass("out");
    }
  }//end function

})
