/***************************************************************
UI layer client-side   :: avalancheguild.info
© swingin'sam.designs | http://www.swinginsam.com
***************************************************************/

$(document).ready(function() {
  
  $("#ventrilo").hover(function() {
    $("#vent_data").fadeIn(300);
    $("#server_status_table").attr('style','');
  },function() {
    $("#vent_data").fadeOut(300);
  });
  
  get_realm_status();
  setInterval("get_realm_status()",40000);
  
  $("#logs").click(function() {
    window.open('http://www.worldoflogs.com/guilds/117547/');
  });
  
});

function theRotator() {
  //Set the opacity of all images to 0
  $('#rotator ul li').css({opacity: 0.0});
  //Get the first image and display it (gets set to full opacity)
  $('#rotator ul li:first').css({opacity: 1.0});
  //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
  setInterval('rotate()',8000);
}

function rotate() {
  //Get the first image
  var current = ($('#rotator ul li.show') ? $('#rotator ul li.show') : $('#rotator ul li:first'));
  //Get next image, when it reaches the end, rotate it back to the first image
  var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#rotator ul li:first') :current.next()) : $('#rotator ul li:first'));  
  //Set the fade in effect for the next image, the show class has higher z-index
  next
    .css({opacity: 0.0})
    .addClass('show')
    .animate({opacity: 1.0}, 1000);
  //Hide the current image
  current
    .animate({opacity: 0.0}, 1000)
    .removeClass('show');
  // set the image title overlay
  $("#rotator label").html($('#rotator ul li.show img').attr('title'));
};

function barstat(scale,title,val,unit) {
  $rgb = '#fff';
  if(val<24) {
    $rgb = '#c20002';
  } else if(val>=25&&val<=49) {
    $rgb = '#ff8000';
  } else if(val>=50&&val<=74) {
    $rgb = '#ccff66';
  } else if(val>75) {
    $rgb = '#5cb504';
  }
  var $scl  = (scale/100);
  var bar   = ((val*$scl)-2);
  if(val == 0) { bar = 1; }
  return "<div id=\"stat_"+title.replace(' ','_')+"\" class=\"barstat\"><span class=\"stat_title\">"+title+"&nbsp;&raquo;&nbsp;</span><div class=\"stat_bar\"><span style=\"display:block; margin:2px 1px; height:10px; width:"+bar+"px; background:"+$rgb+"\" alt=\""+title+"\" class=\"box-shadow\" /></div><span class=\"stat_value\">"+val+' '+unit+"</span><div class=\"clear\"></div></div>";
}

function get_realm_status() {
  $.getJSON('http://us.battle.net/api/wow/realm/status?realm=dathremar&jsonp=?',function(data) {
    for(key in data) {
      var realm           = data[key];
      for(key in realm) {
        var status_bool   = realm[key].status;
      }
    }
    status_bool == true ? status = 'up' : status = 'down';
    $("#realm").addClass(status);
    if(window.status) {
      window.status = "dath'remar server status :: " + status;
    }
  }); 
}

function set_archives() {
  $(".archive").each(function() {
    var $archive            = $(this);
    var $date               = $archive.attr('rel');
    var $id                 = $archive.attr('id');
    $archive.before('<header class="archive_trigger" rel="' + $id + '"><h1>ARCHIVE&nbsp;<span style="font-size:11px;">[click to read]</span></h1><h2> - ' + $date + '</h2><div class="clear"></div></header>');
  });
  
  $(".archive_trigger").click(function() {
    $("#"+$(this).attr('rel')).toggle();
  })
}

function set_vguide_posts(Subjects,Bodys,Stamps) {
  var UI            = "";
  cnt               = 0;
  for(key in Subjects) {
    if(cnt < 2) {
      UI            = UI + "<article class=\"current\" rel=\"" + Stamps[key] + "\"><header><h1>" + Subjects[key] + "</h1><h2>[POSTED " + Stamps[key] + "]</h2><div class=\"clear\"></div></header>" + Bodys[key] + "<p>~ Vitharr</p></article>";
    } else {
      UI            = UI + "<article class=\"archive\" id=\"archive_" + Stamps[key] + "\" rel=\"" + Stamps[key] + "\"><header><h1>" + Subjects[key] + "</h1><div class=\"clear\"></div></header>" + Bodys[key] + "<p>~ Vitharr</p></article>";
    }
    cnt++;
  }
  $("#posts").html(UI).removeClass('wait');
  
  set_archives();
}

function set_forum_posts(Subjects,Posters,Stamps,URLs) {
  var UI          = "";
  for(key in Subjects) {
    UI            = UI + "<span style=\"margin-right:10px;\"><a href=\"" + URLs[key] + "\" target=\"_blank\"><strong>" + Subjects[key] + "</strong></a> <small class=\"blue\">posted by " + Posters[key] + " on " + Stamps[key] + "</small></span>";
  }
  $("#forum").removeClass('wait');
  $("#forumposts").html(UI);
}
