// functions.js, written by blackwind.

function bwAdjustSlidebar() {
  if ($("#sidebar").is(":visible")) {
    height = $("#slidebar").outerHeight(); margin = 10; speed = 100;
    min = $("#header").outerHeight() + $("#sidebar").outerHeight();
    max = $(document).height() - $("#footer").outerHeight() - margin;
    mod = ($(window).height() - height) / 2;
    offset = $(document).scrollTop() + (mod > margin ? mod : margin);

    // Work around browser quirks.
    if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
      min = margin * 4;
      max -= $("#header").outerHeight();
      offset -= $("#header").outerHeight();
    }

    // Don't scroll above the main sidebar or below the footer.
    if (offset < min) { offset = min; }
    if (offset + height > max) { offset = max - height; }

    // Scroll slidebar into view.
    $("#slidebar").animate({top: offset}, {duration: speed, queue: false});
  }
  return true;
}

function bwAdjustViewport() {
  var width = $("body").width();
  if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
    if ($("#sidebar").length == 0 || width <= 874) {
      // Low-resolution mode.
      //$("#sidebar").css({"display": "none"});
      $("img").css({"max-width": (width - 86) + "px"});
      $("#root #container").css({"width": (width - 72) + "px"});
    } else {
      // High-resolution mode.
      $("#sidebar").css({"display": "block"});
      $("img").css({"max-width": (width - 303) + "px"});
      $("#root #container").css({"width": (width - 289) + "px"});
    }
    $("#footer").css({"width": (width - 48) + "px !important"});
  } else {
    if ($("#sidebar").length == 0 || width <= 874) {
      // Low-resolution mode.
      //$("#sidebar").css({"display": "none"});
      $("img").css({"max-width": (width - 86) + "px"});
      $("#root #container").css({"background-image": "url(/images/faux_single.png)"});
      $("#main").css({"margin-right": "0px", "min-height": "0px", "min-width": (width - 72) + "px"});
    } else {
      // High-resolution mode.
      $("#sidebar").css({"display": "block"});
      $("img").css({"max-width": (width - 302) + "px"});
      $("#root #container").css({"background-image": "url(/images/faux_right.png)"});
      $("#main").css({"margin-right": "216px", "min-height": ($("#sidebar").outerHeight() + $("#slidebar").outerHeight() - 30) + "px", "min-width": (width - 288) + "px"});
    }
  }
  bwAdjustSlidebar();
  return true;
}

function bwAdjustViewport_IE6() {
  var width = $("body").width();
  if (width <= 874) {
    // Low-resolution mode.
    bwInjectCSS_IE6("#root #container { width: " + (width - 72) + "px; }");
  } else {
    // High-resolution mode.
    bwInjectCSS_IE6("#root #container { width: " + (width - 289) + "px; }");
  }
  return true;
}

function bwCookies_Delete(name, path, domain) {
  var path = (path == null) ? "/" : path;
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  if (bwCookies_Get(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=" + exp;
    return true;
  } else {
    return false;
  }
}

function bwCookies_Get(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}

function bwCookies_Set(name, value, expires, path, domain, secure) {
  var path = (path == null) ? "/" : path;
  var exp = new Date()
  exp.setDate(expires)
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + exp : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
  if (bwCookies_Get(name)) {
    return true;
  } else {
    return false;
  }
}

function bwCreateWindow(url, name, width, height) {
  window.open(url, name, "width=" + width + ", height=" + height);
  return true;
}

function bwInjectCSS(css) {
  var heads = document.getElementsByTagName("head");
  if (heads.length > 0) {
    var node = document.createElement("style");
    node.type = "text/css";
    node.innerHTML = css;
    heads[0].appendChild(node);
    return true;
  } else {
    return false;
  }
}

function bwInjectCSS_IE6(css) {
  document.write('<style type="text/css">' + css + '</style>');
  return true;
}

function bwLoader_SourceCSS(url) {
  var e = document.createElement("link");
  e.setAttribute("rel", "stylesheet");
  e.setAttribute("type", "text/css");
  e.setAttribute("href", url);
  if (typeof e != "undefined") {
    document.getElementsByTagName("head")[0].appendChild(e);
    return true;
  } else {
    return false;
  }
}

function bwLoader_SourceJS(url) {
  var e = document.createElement("script");
  e.setAttribute("type", "text/javascript");
  e.setAttribute("src", url);
  if (typeof e != "undefined") {
    document.getElementsByTagName("head")[0].appendChild(e);
    return true;
  } else {
    return false;
  }
}

function bwToggleBonus() {
  if (bwCookies_Get("bonus") == "1") {
    bwCookies_Delete("bonus");
    alert("Bonus disabled!");
  } else {
    bwCookies_Set("bonus", "1", "3650");
    alert("Bonus enabled!");
  }
  return true;
}

// Initialize Shadowbox.
if (typeof Shadowbox != 'undefined') {
  Shadowbox.init({
    continuous: true,
    initialHeight: 48,
    initialWidth: 128,
    language: "en",
    modal: true,
    players: ["flv", "iframe", "img"],
    skipSetup: true,
    useSizzle: false
  });
}

// EVENTS: Adjust viewport as needed.
$(window).load(function() { bwAdjustViewport(); });
$(window).resize(function() { bwAdjustViewport(); });

// EVENTS: Enable slidebar.
$(window).scroll(function() { bwAdjustSlidebar(); });

// EVENTS: Bind the requested selectors.
$(document).ready(function() {
  if (typeof $.fn.shadowbox == "function") {
    $("a[href$='.bmp'], a[href$='.gif'], a[href$='.jpg'], a[href$='.png']").addClass("special").shadowbox({gallery: "lightbox"});
    $("a[href$='.flv'], a[href$='.mp3'], a[href*='youtube.com/watch?v='], a[rel='video']").addClass("special").each(function() { $(this).shadowbox({width: 640, height: 360+4, flashVars: {file: "http://libs.blackwind.org/playlist.php?url=" + encodeURIComponent($(this).attr("href"))}, player: "flv"}) });
    $("a[rel='form']").shadowbox({width: 512, height: 384});
  }
  if (typeof $.fn.tipsy == "function") {
    $("a[title].tip-top").tipsy({gravity: "s"});
    $("a[title].tip-bottom").tipsy({gravity: "n"});
    $("a[title].tip-left").tipsy({gravity: "e"});
    $("a[title].tip-right").tipsy({gravity: "w"});
  }
});
