﻿/* Copyright © DR Solutions International Ltd / Author: Andrew MacKie Green / 2009.10 */
/* DRS client script */

// GLOBAL VARIABLES:
var cUrl = window.location.pathname;
var cPageName = cUrl.replace(new RegExp(/\//gi), ""); if (cPageName.length == 0) { cPageName = "default"; }
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:
$.fn.idle = function(time, callback) {
  var i = $(this);
  i.queue(function() { setTimeout(function() { i.dequeue(); callback(i); }, time); });
};

$.setFooter = function() {
  var b = $("#body"), f = $("#foot");
  f.addClass("fixed");
  //if ($("#body").offset().top + $("#body").height() < $(window).height()) { $("#foot").addClass("absolute"); } else { $("#foot").removeClass("absolute"); }
  b.css("min-height", ((f.offset().top - 2) - ($("#head").height() + $("#menu").height())));
  f.removeClass("fixed");
};

$.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.setJuitter = function() {
  var div = $(this);
  $.Juitter.start({
    searchType: "searchWord",
    searchObject: "drsolutions,winavwcampervan,horseracinghome,bonesteelers,m247,studio401,frankandfaith,annaceleste1979",
    lang: "en",
    live: "live-20",
    placeHolder: div.attr("id"),
		loadMSG: "&#62; Getting tweets.. Please wait!",
		imgName: "loader.gif",
		total: 4,
		readMore: "Read it on Twitter",
		nameUser:"image",
		openExternalLinks:"newWindow"
  });
};

$.fn.setBanner = function() {
  var div = $(this), api = null, apiAuto = null;
  var prev = div.find("a.prev"), next = div.find("a.next");
  var sec = 10000, fade = 0.50, full = 1.00;
  api = div.scrollable({
    api: true,
    size: 1,
    speed: 500,
    loop: true,
    clickable: false
  });
  apiAuto = div.autoscroll({
    api: true,
    steps: 1,
    interval: sec,
    autoplay: true,
    autopause: true
  });
  api.onSeek(function() {
    if (div.data("hover")) {
      if (api.getIndex() > 0) {
        if (!prev.is(":visible")) { prev.show().fadeTo(0, 0); }
        if (!prev.data("hover")) { prev.fadeTo(400, fade); }
      } else {
        prev.fadeOut(250);
      }
      if (api.getIndex() < api.getSize() - 1) {
        if (!next.is(":visible")) { next.show().fadeTo(0, 0); }
        if (!next.data("hover")) { next.fadeTo(400, fade); }
      } else {
        next.fadeOut(250);
      }
    }
  });
  div.hover(
    function() {
      $(this).data("hover", true);
      apiAuto.stop();
      if (api.getIndex() > 0) {
        if (!prev.is(":visible")) { prev.show().fadeTo(0, 0); }
        prev.fadeTo(400, fade);
      }
      if (api.getIndex() < api.getSize() - 1) {
        if (!next.is(":visible")) { next.show().fadeTo(0, 0); }
        next.fadeTo(400, fade);
      }
    },
    function() {
      $(this).data("hover", false);
      prev.fadeOut(250);
      next.fadeOut(250);
      $().idle(sec, function() {
        if (!div.data("hover")) { apiAuto.play(); }
      });
    }
  );
  prev.hover(
    function() {
      prev.data("hover", true);
      prev.fadeTo(250, full);
    },
    function() {
      prev.data("hover", false);
      prev.fadeTo(250, fade);
    }
  );
  next.hover(
    function() {
      next.data("hover", true);
      next.fadeTo(250, full);
    },
    function() {
      next.data("hover", false);
      next.fadeTo(250, fade);
    }
  );
};

$.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: "error",
    errorPlacement: function(err, elm) {
      err.hide();
    }
  });
  frm.submit(function() {
    frm.find("input").focus();
    if (frm.valid()) {
      frm.unbind("submit").submit();
      $("p#err").slideUp(200);
      $("p#suc").slideDown(200);
    } else {
      $("p#err").slideDown(200);
    }
    return false;
  });
};

// GLOBAL EVENTS:
$(window).resize(function() { $.setFooter(); });

// DOM READY:
$(document).ready(function() {
  $("a[rel*='external']").each(function() { this.target = "_blank"; });  // Sets target "_blank" for all <a> elements with rel "external".
  $.setFooter();
  $("form").each(function() { $(this).setForm(); });
  if (cPageName == "portfolio") { /*if (location.hash) {*/$.scrollTo($("a[rel*='" + location.hash.replace(/#/, "") + "']"), 500, { easing: "swing", offset: { top: -10} }); /*}*/ }
  if ($("#juitter").length) { $("#juitter").idle(500, function(elm) { elm.setJuitter(); }); }
  if ($("#banner").length) { $("#banner").setBanner(); }
  if ($("div#googleMap").is(":visible")) { $("div#googleMap").googleMap("16-18 Beach Road, Littlehampton, West Sussex, United Kingdom. BN17 5HT"); }
});
