window.fbartime = "";
var clickthumb = true;
var cktime;
var cxy = "0-0";
var lxy;
var cmx;
var cmy;
window.cgal = 1;
window.cfile = 0;
window.tfiles = 0;
var fplayer;
var mover = 0;

function setmover() {
  $(function(){

    //Get our elements for faster access and set overlay width

    var div = $('div.sc_menu'),

    ul = $('ul.sc_menu'),

    ulPadding = 15;
    div.die();

    //Get menu width

    var divWidth = div.width();



    //Remove scrollbars

    div.css({overflow: 'hidden'});



    //Find last image container

    var lastLi = ul.find('li:last-child');



    //When user move mouse over menu

    div.mousemove(function(e){

      //As images are loaded ul width increases,

      //so we recalculate it each time

      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

      var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;

      div.scrollLeft(left);
      //div.animate({scrollLeft: left}, 'fast');
    });

  });
}

function setred(i) {
  $("#fplayer").hide();

  if(i<=6) {
    $(".sfbmtextred").removeClass("sfbmtextred").addClass("sfbmtext");
    $("#smf"+i).removeClass("sfbmtext").addClass("sfbmtextred");
    $(".msub3").hide();
    $("#sub3-"+i).show();
    $(".firsttb"+i).click();
    return;
  } else {
    $(".sb3").removeClass("sfbmtextred").addClass("sfbmtext");
    $("#smf"+i).removeClass("sfbmtext").addClass("sfbmtextred");
    window.cgal = i;
    getgal();
    return;
  }




}

function checkcxy() {
  if(cxy != lxy) {
    clearTimeout(window.fbartime);
    var wh = $(window).height();
    $('#fplayer').height(wh-150);
    //$(".fbbar").fadeIn();
    window.fbartime=setTimeout("hidefbbar()",2000);
  }
  lxy = cxy;
  cktime=setTimeout("checkcxy()",100);
}
cktime=setTimeout("checkcxy()",100);


function closepfs() {
  $(".bgstretcher").hide();
  $('#fplayer').html("");
  $("#froot").hide();
  $("#mroot").show();
}

function hidefbbar() {
  var wh = $(window).height();
  var checkY = wh - 150;
  if(cmy <= checkY) {
    $(".fbbar").fadeOut();
  }
  $('#fplayer').height(wh-45);
}

function resetgetimgwh() {
  var inh = window.iheight * 100 / window.oih;
  inh = Math.round(inh);
  var inw = inh * window.oiw / 100;
  inw = Math.round(inw)
  //alert(inw);
  return inw;
}

function getimgwh(iw,ih) {
  window.oih = ih;
  window.oiw = iw;
  var inh = window.iheight * 100 / ih;
  inh = Math.round(inh);
  var inw = inh * iw / 100;
  inw = Math.round(inw)
  //alert(inw);
  return inw;
}

function showpfs() {
  $("#mroot").hide();
  $("#froot").show();
  fbartime=setTimeout("hidefbbar()",3000);
}

function settbs() {
  $(".othumb").mouseover(function() {
    v = $(this).attr("v");
    if(window.cfile != v) {
      $("#bwtn"+v).hide();
      $("#tn"+v).show();
    }
  }).mouseout(function(){
    v = $(this).attr("v");
    if(window.cfile != v) {
      $("#tn"+v).hide();
      $("#bwtn"+v).show();
    }
  });

  $(".othumb").click(function() {
    $("#frootimg").show();
    $(".colortb").hide();
    $(".bwtb").show();
    $("#frootimg").hide();
    var f = $(this).attr("f");
    var v = $(this).attr("v");
    var iw = $(this).attr("iw");
    var ih = $(this).attr("ih");
    var bg_color = $(this).attr("rel") || "#ffffff";
    $("#froot").css("background-color", bg_color);
    window.cfile = v;
    if(f != "") {
      var ext = f.split('.').pop();

      if(ext == "flv") {
        setvidh(f);
        $("#frootimg").hide();
        $("#fplayer").show();
        window.cfile = v;
        $("#bwtn"+v).hide();
        $("#tn"+v).show();
        $f("fplayer").play();
        $(".limgtext").hide();
        $("#ltext"+v).show();
      } else {
        $("#fplayer").html("");
        $("#frootimg").hide();
        $("#fplayer").hide();
        var inw = getimgwh(iw,ih);

        $("#frootimg").attr("src","images/gallery/"+f);
        $("#frootimg").attr("width",inw);
        $("#frootimg").width(inw);
        //$("#frootimg").attr("height",iheight);

        window.cfile = v;
        $(".limgtext").hide();
        $("#bwtn"+v).hide();
        $("#tn"+v).show();
        $("#frootimg").fadeIn();
        $("#ltext"+v).show();
      }
    }
  });


  $(".mtnbt").click(function(){
    if(clickthumb == true) {
      clickthumb = false;
      var cltc = setTimeout("clickthumb = true",500);
      var v = $(this).attr("v");
      if(v == "left") {
        var c = parseInt(window.cfile);
        if(c >0) {
          var a = c - 1;
          $("#oth"+a).click();

        }

      } else if(v=="right") {

        var c = parseInt(window.cfile);
        //alert(c);
        if(c < window.tfiles ) {
          var a = c + 1;
          $("#oth"+a).click();

        }

      }

    }

  });

  function setclickthumb() {
    clickthumb = true;
  }

  $(document).keydown(function(e) {
    if ($('#froot').is(':visible')) {

      if(e.keyCode == 37) {
        // left
        $("#lefttnbt").click();

      }
      else if(e.keyCode == 39) { // right
        $("#righttnbt").click();


      }
    }
  });

}

function getgal() {
  $("#frootimg").hide();
  //$("#ulsthumbs").html("");
  $(".othumb").remove();
  $("#sthumbs").append('<div class="tajax" style="color:#FFF;float:left;width:auto;"><img src="/images/loading.gif" width="15" height="15" /> A carregar...</div>');

  $.ajax({
    type: "GET",
    url: "system/visual/froot/get_gal.php?cgal="+window.cgal,
    dataType: "xml",
    success: function(xml) {
      $(".tajax").remove();
      $(".limgtext").remove();
      i = 0;

      $(xml).find('file').each(function(){
        var file = $(this).attr('name');
        var iw = $(this).attr('width');
        var ih = $(this).attr('height');
        var tb = $(this).attr('tb');
        var text = $(this).text();
        var bg_color = $(this).attr("bg_color");
        var ifile = file.replace("flv", "jpg");
        if(tb == "") {
          var img = '<img rel="' + bg_color + '" class="colortb" id="tn'+i+'" style="display:none;"  src="images/gallery/thumbs/t_'+ifile+'" width="80" height="46" />';
          img += '<img rel="' + bg_color + '" class="bwtb" id="bwtn'+i+'" src="images/gallery/thumbs/bw_t_'+ifile+'" width="80" height="46" />';
        } else {
          var img = '<img rel="' + bg_color + '" class="colortb" id="tn'+i+'" style="display:none;"  src="images/gallery/thumbs/bwthumbnail.png" width="80" height="46" />';
          img += '<img rel="' + bg_color + '" class="bwtb" id="bwtn'+i+'" src="images/gallery/thumbs/thumbnail.png" width="80" height="46" />';
        }
        $("#ulsthumbs").append('<li iw="'+iw+'" ih="'+ih+'" id="oth'+i+'" f="'+file+'" v="'+i+'" class="othumb fbmthumb" rel="' + bg_color + '">'+img+'</li>');

        var dtext = '<div style="display:none;height: 104px;overflow:auto;" class="limgtext" id="ltext'+i+'">'+text+'</div>';
        $("#imgtext").append(dtext);
        var fww = $(window).width();
        var fwh = $(window).height();
        $("#ltext"+i).width(fww-290);
        $("#blackdiv").width(fww).height(fwh);
        window.tfiles = i;
        i++;
        if(file.indexOf("flv") <= 0) {
          jQuery.preLoadImages("/images/gallery/" + file);
        }
      });
      window.cfile = 0;
      settbs();

      $("#oth0").click();
      var div = $('div.sc_menu');
      div.unbind();
      setmover();
    }
  });
  //------------->
  return;
}

function setfbimg() {
  var ww = $(window).width();
  var wh = $(window).height();
  $("#frootimg").attr("height",wh - 47);
  window.iheight = wh - 47;
  var inw = resetgetimgwh();
  $("#frootimg").attr("width",inw);
  $("#frootimg").width(inw);
}

function setvidh(vid) {
  var wh = $(window).height();
  $('#fplayer').height(wh-150);
  $f("fplayer", "/vid/flowplayer.swf", "/images/gallery/video/"+vid);
}

$(document).ready(function(){
  setfbimg();
  window.fbartime=setTimeout("hidefbbar()",3000);
  //projekktor('#fplayer');

  $(window).resize(function() {
    var fww = $(window).width();
    var fwh = $(window).height();
    $("#imgtext").width(fww-258);
    $("#blackdiv").width(fww).height(fwh);
    $("#froot").height(fwh);

    setfbimg();
  });

  $("#froot").mousemove(function(e){
    cxy = e.pageX +'-'+ e.pageY;
    cmx = e.pageX;
    cmy = e.pageY;
    var wh = $(window).height();
    var checkY = wh - 150;

    if(cmy>=checkY){$(".fbbar").fadeIn();}
    // document.title = clickthumb;
  });
});

function getImgSize(imgSrc){
  var newImg = new Image();
  newImg.src = imgSrc;
  var height = newImg.height;
  var width = newImg.width;
  p = $(newImg).ready(function(){
    return {width: newImg.width, height: newImg.height};
  });
  //alert (p[0]['width']+" "+p[0]['height']);
  return p;
}
