﻿/* Copyright © DR Solutions International Ltd / Author: Andrew MacKie Green / 2009.10 */
/* DRS client script */

// GLOBAL VARIABLES:
var cUrl = window.location.pathname;
var cFilename = cUrl.substring(cUrl.lastIndexOf('/') + 1); if (cFilename.length == 0) { cFilename = "default.asp"; }
var cImageUrl = "resources/";  // Url path for image files.
//var cRequestUrl = "json.asp";  // Url path and filename to JSON request file.
//var cSplit = "|";  // Delimiter char for values.
//var cSplitSub = "~";  // Delimiter char for sub values.
//var cRecLimit = 10; // Default record limit count.
//var cDate = new Date();  // Current date.
//var cDay = cDate.getDay();  // Current day.
//var cWeekday = new Array(7); cWeekday[0] = "Sunday"; cWeekday[1] = "Monday"; cWeekday[2] = "Tuesday"; cWeekday[3] = "Wednesday"; cWeekday[4] = "Thursday"; cWeekday[5] = "Friday"; cWeekday[6] = "Saturday";
//var cFileSizeLimit = 209715200;  // Upload file size limit in bytes // 200MB.

// GLOBAL 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 elm = $(this);
  elm.queue(function() {
    setTimeout(function() {
      elm.dequeue();
      if ($.isFunction(callback)) { callback(elm); }
    }, time);
  });
  return elm;
};

$.fn.setHistoryBack = function() {
  var a = $(this);
  a.click(function() {
    history.go(-1);
  });
  return false;
};

$.fn.setLightbox = function() {
  var img = $(this);
  img.lightBox({
    overlayBgColor: "#000000",
    overlayOpacity: 0.5,
    imageLoading: "resources/lightbox-ico-loading.gif",
    imageBtnClose: "resources/lightbox-btn-close.gif",
    imageBtnPrev: "resources/lightbox-btn-prev.gif",
    imageBtnNext: "resources/lightbox-btn-next.gif",
    containerResizeSpeed: 500
  });
};

/*
$.fn.googleMap = function(address, options) {
  var defaults = {
    lat: 50.80863989938619,
    long: -0.5403470993041992,
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    //      mapTypeId: google.maps.MapTypeId.SATELLITE
    //      mapTypeId: google.maps.MapTypeId.HYBRID
    //      mapTypeId: google.maps.MapTypeId.TERRAIN
  };
  options = $.extend(defaults, options || {});
  var center = new google.maps.LatLng(options.lat, options.long);
  var map = new google.maps.Map(this.get(0), $.extend(options, { center: center }));
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({ address: address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results.length) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
        //map.set_center(results[0].geometry.location);
        var marker = new google.maps.Marker({
          position: results[0].geometry.location,
          map: map
        });
      }
    }
  });
};
*/

$.fn.setMenu = function() {
  var speedShow = 200;
  var speedHide = 300;
  var menu = $(this);
  menu.children("li").each(function() {
    var item = $(this);
    if (item.children("ul").length > 0) {
      var sub = item.children("ul");
      sub.data("active", false);
      item.hover(
        function() { // mouseover:
          sub.stop(true, true);
          if (!sub.data("active")) {
            sub.slideDown(speedShow).delay(speedShow).data("active", true);
          }
        },
        function() { // mouseout:
          sub.delay(500).slideUp(speedHide).delay(speedHide).data("active", false);
        }
      );
      sub.mouseover(function() { // mouseover:
        sub.stop(true, true).slideDown(speedShow);
      });
    }
  });
};

$.fn.setCycle = function() {
  var div = $(this);
  div.cycle({ fx: "fade", random: 1, timeout: 5000, speed: 1000 });
};

$.fn.setGallery = function() {
  var div = $(this), thumbs = div.find("ul#thumbs"), full = div.find("div.full"), img = full.find("img"), a = null, src = null, tmp = null;
  thumbs.find("a").click(function() {
    a = $(this);
    if (a.is(":not([class*='active'])")) {
      a.data("active", true);
      src = img.attr("src").split("/");
      src.length = src.length - 1;
      src = src.join("/") + "/" + a.attr("rel");
      thumbs.find("a").removeClass("active");
      a.addClass("active");
      tmp = img.clone().appendTo(full).addClass("fixed");
      img.attr("src", src);
      img.hide();
      img.fadeIn(500);
      tmp.fadeOut(500);
    }
  });
};

// GLOBAL EVENTS:

// DOM READY:
$(document).ready(function() {
  $("a[rel*='back']").each(function() { $(this).setHistoryBack(); });
  $("a[rel*='external']").each(function() { this.target = "_blank"; });  // Sets target "_blank" for all <a> elements with rel "external".
  $("a[rel*='lightbox']").each(function() { $(this).setLightbox(); });
  if ($("#menu").length) { $("#menu").setMenu(); }
  if ($("#cycle").length) { $("#cycle").setCycle(); }
  if ($("#page[class='gallery']").length) { $("#page[class='gallery']").setGallery(); }
  
  if ($(window).height() > $("div#wrap").height()) {
    $("div#wrap").css("margin-top", "-" + ($("div#wrap").height() /2) + "px");
  } else {
    $("div#wrap").css("margin-top", "0px").css("top", "20px");
    $("body").css("height", ($("div#wrap").height() + 40) + "px");
  }
  
});

