// functions.js, written by blackwind.

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 bwToggleBonus() {
  if (bwCookies_Get("bonus") == "1") {
    bwCookies_Delete("bonus");
    alert("Bonus disabled!");
  } else {
    bwCookies_Set("bonus", "1", "3650");
    alert("Bonus enabled!");
  }
}

// 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: 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"});
  }
});
