// SETTINGS:
$.ajaxSetup({
  cache: false
});

// INCLUDES:
$.include("/java/date.js");
$.include("/java/date.format.js");
$.include("/java/jquery.url.js");
$.include("/java/jquery.tools-1.2.5.min.js");
$.include("/java/jquery.validate-1.6.min.js");
$.include("/java/jquery.cycle-lite-1.0.min.js");

// FUNCTIONS:
$.idle = function(time, callback) { // Idle function for delayed code execution.
  window.setTimeout(function() {
    if ($.isFunction(callback)) { callback(); }
  }, time);
  return $;
};

$.fn.idle = function(time, callback) { // Idle function for delayed code execution.
  var e = $(this);
  e.queue(function() {
    window.setTimeout(function() {
      e.dequeue();
      if ($.isFunction(callback)) { callback(e); }
    }, time);
  });
  return e;
};

$.fn.setForm = function() {
  var frm = $(this), sub = $("a[rel='submit']");
  sub.click(function() {
    frm.submit();
  });
  frm.find("input").each(function() {
    var inp = $(this), val = inp.val(), ttl = inp.attr("title");
    inp.focus(function() {
      val = inp.val();
      if (val == ttl) { inp.val(""); }
    });
  });
  frm.validate({
    errorClass: "form-error",
    errorPlacement: function(err, elm) {
      err.hide();
    }
  });
  frm.submit(function() {
    frm.find("input").focus();
    if (frm.valid()) {
      frm.unbind("submit").submit();
    }
    return false;
  });
};

$.fn.setCycle = function() {
  var div = $(this);
  div.cycle({ fx: "fade", random: 1, timeout: 5000, speed: 1000 });
};

$.fn.setThumbScroll = function(speed, delay, trans) {
  var title = $("a#thumbScrollTitle");
  var folder = $("input#thumbFolder").val();
  var div = $(this);
  var items = div.find(".items");
  var api = null;
  if (isNaN(speed)) { speed = 1000; }
  if (isNaN(delay)) { delay = 3000; }
  if (isNaN(trans)) { trans = 500; }
  title.empty();
  items.empty();
  if (folder != undefined && folder != null) {
    $.getJSON("/_getPhotos.php?folder=" + folder, function(json) {
      if (json.title) {
        title.attr("href", "/photo.html#" + folder)
             .html("<b>" + json.title + "</b>");
      }
      if (json.files) {
        $.each(json.files, function(index, item) {
          items.append("<a href='/photo.html#" + folder + ":" + (index + 1) + "'><img src='/" + item.name + "' /></a>");
        });
        items.width((items.children().length * 109) + 7);
        div.scrollable({
          speed: speed,
          easing: "swing",
          circular: true,
          keyboard: false
        }).autoscroll({
          interval: delay,
          steps: 6,
          autoplay: true,
          autopause: true
        });
        api = div.data("scrollable");
      }
    });
  }
};

$.fn.setPhotoScroll = function(speed, delay, trans) {
  var crumb = $("#albumCrumb");
  var title = $("#albumTitle");
  var counter = $("span#image-counter");
  var toggle = $("a#photoScrollToggle");
  var prev = $("a#photoScrollPrev");
  var next = $("a#photoScrollNext");
  var folder = $.url.attr("anchor");
  var div = $(this);
  var items = div.find(".items");
  var api = null;
  var apiAuto = null;
  var pos = null;
  var posDelay = 0;
  if (isNaN(speed)) { speed = 1000; }
  if (isNaN(delay)) { delay = 3000; }
  if (isNaN(trans)) { trans = 500; }
  if (folder != undefined && folder != null) {
    if (folder.search(/:/) > -1) {
      pos = folder.split(":")[1];
      folder = folder.split(":")[0];
      posDelay = 1000;
      if (isNaN(pos)) {
        pos = null;
        posDelay = 0;
      }
    }
    items.empty();
    toggle.data("flag", true);
    $.getJSON("/_getPhotos.php?folder=" + folder, function(json) {
      if (json.title) {
        crumb.html(json.title);
        title.html(json.title);
      }
      if (json.files) {
        $.each(json.files, function(index, item) {
          items.append("<span><img src='/" + item.name + "' /></span>");
        });
        items.width(items.children().length * 500);
        div.scrollable({
          speed: speed,
          easing: "swing",
          circular: false,
          keyboard: false
        }).autoscroll({
          interval: delay,
          steps: 1,
          autoplay: true,
          autopause: false
        });
        api = div.data("scrollable");
        if (pos != null) {
          pos = pos - 1;
          api.seekTo(pos, 0);
          items.css("left", "-" + (pos * 500) + "px");
        }
        api.onBeforeSeek(function(event, index) {
          if (div.css("opacity") == .0) { return true; }
          div.fadeTo(trans, .0, function() {
            if (index == api.getSize()) {
              api.seekTo(0, 0);
            } else {
              api.seekTo(index, 0);
            }
          });
          return false;
        });
        api.onSeek(function() {
          div.fadeTo(trans, 1.00);
          $.idle(speed, function() {
            counter.html((api.getIndex() + 1) + " of " + api.getSize());
          });
        });
        prev.click(function() { api.prev(speed); });
        next.click(function() { api.next(speed); });
        toggle.click(function() {
          if (toggle.data("flag")) {
            api.stop();
            toggle.data("flag", false);
            toggle.find("img").attr("src", "/graphics/play-slideshow.gif");
          } else {
            api.play();
            toggle.data("flag", true);
            toggle.find("img").attr("src", "/graphics/stop-slideshow.gif");
          }
        });
        counter.html((api.getIndex() + 1) + " of " + api.getSize());
        div.idle(1000, function(div) { div.children("div.items").fadeIn(trans); });
      }
    });
  }
};

$.setBlogger = function(json) {
  var div = $("td[background*='news-back.jpg']");
  var dates = $(div.find("td.news-date"));
  var news = $(div.find("td.tx-12"));
  $.getJSON("/_getBlogger.php", function(json) {
    for (var i = 0; i <= 1; i++) {
      var entry = json.feed.entry[i];
      var date = Date.parse(entry.published.$t.split(".")[0]);
      var month = $($(dates[i]).find("div")[0]);
      var day = $($(dates[i]).find("div")[1]);
      var link = entry.link[2].href;
      var title = entry.title.$t;
      var text = $("<div>").append(entry.content.$t).text();
      var post = $(news[i]);
      if (text.length > 130) { text = text.substring(0, 130) + "..."; }
      month.html(date.format("mmm").toUpperCase());
      day.html(date.format("dS"));
      post.html('<b><a href="../../Bulgaria_htdocs/java/%27%20+%20link%20+%20%27" target="_blank">' + title + '</a></b><br />' + text);
    }
  });
};

// DOCUMENT READY:
$(document).ready(function() {
  $("form").each(function() { $(this).setForm(); });
  $("div.cycle").each(function() { $(this).setCycle(); });
  if ($("#thumbScroll").length) { $("#thumbScroll").setThumbScroll(250, 2000, 500); }
  if ($("#photoScroll").length) { $("#photoScroll").setPhotoScroll(0, 5000, 500); }
  $.setBlogger();
});

