﻿// Start Loading
$(document).ready(function() {
    LavaLampNavigation();
    StandardPageElemets();
});

/*
*   Navigation
*/
function LavaLampNavigation () {
    $("#1, #2").lavaLamp({
        fx: "backout",
        speed: 700,
        click: function(event, menuItem) {
        }
    });
}

/*
*   Standard Page Elements
*/
function StandardPageElemets() {
    $("#tabs").tabs();
    // Dialog                   
    $('#dialog').dialog({
        autoOpen: false,
        width: 600,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
            }
            //"Cancel": function() {
            //    $(this).dialog("close");
            //}
        }
    });
    // Dialog Link
    $('#dialog_link').click(function() {
        $('#dialog').dialog('open');
        return false;
    });
    //hover states on the static widgets
    $('#dialog_link, ul#icons li').hover(
        function() { $(this).addClass('ui-state-hover'); },
        function() { $(this).removeClass('ui-state-hover'); }
    );
}

/*
*   Ajax Activity Stuff
*/
function ShowActivity() {
    $("#activity").html('<img src="/Content/images/activity.gif" />').pause(500);
}

function HideActivity() {
    $("#activity").html('');
}


/*
*   Misc.
*/

function CleanURL(myURL) {
    var firstpass = myURL.replace("http://", "");
    var secondpass = firstpass.replace('/', "%2F");
    return secondpass;
}

function LoadDropDown(response, id, selectedID) {
    //alert(selectedID);
    var options = '';
    for (var i = 0; i < response.length; i++) {
        if (response[i].Value != selectedID) {
            options += "<option value='" + response[i].Value + "'>" +
                                            response[i].Text + "</option>";
        }
        else {
            options += "<option selected='selected'  value='" + response[i].Value + "'>" +
                                            response[i].Text + "</option>";
        }
    }
    $(id).removeAttr('disabled').html(options);
}


/*
* Jonathan Howard
*
* jQuery Pause
* version 0.2
*
* Requires: jQuery 1.0 (tested with svn as of 7/20/2006)
*
* Feel free to do whatever you'd like with this, just please give credit where
* credit is do.
*
*
*
* pause() will hold everything in the queue for a given number of milliseconds,
* or 1000 milliseconds if none is given.
*
*
*
* unpause() will clear the queue of everything of a given type, or 'fx' if no
* type is given.
*/
$.fn.pause = function(milli, type) {
    milli = milli || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $.dequeue(self);
        }, milli);
    });
};

$.fn.clearQueue = $.fn.unpause = function(type) {
    return this.each(function() {
        type = type || "fx";
        if (this.queue && this.queue[type]) {
            this.queue[type].length = 0;
        }
    });
};








var maxMode = false;
var e;

function maxMap() {
    $("div#map").css("position", "fixed"); //position: absolute;
    $("div#map").css("left", "0px"); //left: 0px;
    $("div#map").css("right", "0px"); //right: 0px;
    $("div#map").css("top", "0px"); //top: 0px;
    $("div#map").css("width", getWindowWidth()); //width: 100%;
    $("div#map").css("height", getWindowHeight()); //height: 100%;
    $("div#map").css("z-index", "2"); //z-index:3
    $(".lavaLampNoImage").css("z-index", "0");
    map.checkResize();
    map.setCenter(new GLatLng(11.050000, 1.1000000), 2);
    document.onkeyup = processKey;
    maxMode = true;
}

function restoreMap() {
    $("div#map").css("position", "relative"); //position: absolute;
    $("div#map").css("left", "0px"); //left: 0px;
    $("div#map").css("right", "0px"); //right: 0px;
    $("div#map").css("top", "0px"); //top: 0px;
    $("div#map").css("width", '100%'); //width: 100%;
    $("div#map").css("height", '250px'); //height: 100%;
    $("div#map").css("z-index", "0"); //z-index:3
    $(".lavaLampNoImage").css("z-index", "0");
    map.checkResize();
    map.setCenter(new GLatLng(11.050000, 1.1000000), 2);
    maxMode = false;
}

function processKey(e) {
    e = getKeyCode(e);
    if (e == 27 && maxMode) {    // 27 => ESC 
        restoreMap();
    }
}


// get the kode of the key that was just pressed
// This is from http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=17  
function getKeyCode(e) {
    if (!e) {
        if (window.event) {
            e = window.event;
        } else {
            return;
        }
    }
    if (typeof (e.which) == 'number') {
        //NS 4, NS 6+, Mozilla 0.9+, Opera
        e = e.which;
    } else if (typeof (e.keyCode) == 'number') {
        //IE, NS 6+, Mozilla 0.9+
        e = e.keyCode;
    } else if (typeof (e.charCode) == 'number') {
        //also NS 6+, Mozilla 0.9+
        e = e.charCode;
    } else {
        return;
    }
    return e;
}

function getWindowHeight() {
    if (typeof (window.innerWidth) == 'number') {
        // Non-IE
        return window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        // IE 6+ in 'standards compliant mode'
        return document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        // IE 4 compatible
        return document.body.clientHeight;
    }
}

function getWindowWidth() {
    if (typeof (window.innerWidth) == 'number') {
        // Non-IE
        return window.innerWidth;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        // IE 6+ in 'standards compliant mode'
        return document.documentElement.clientWidth;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        // IE 4 compatible
        return document.body.clientWidth;
    }
}

/*
 * Tipsy.
 *
 * - Removed gravity code. Always gravitate towards center of screen.
 * - Removed all the extra whitespace.
 * - Added tip callback.
 * - Added sane variable naming.
 *
 * Original:
 * http://plugins.jquery.com/project/tipsy
 * The MIT License
 * Copyright (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
(function($) {
  $.fn.tipsy = function(opts) {
    opts = $.extend({fade: false}, opts || {});
    var tip = null, cancelHide = false;
 
    this.hover(function() {
      $.data(this, 'cancel.tipsy', true);
 
      var tip = $.data(this, 'active.tipsy');
      if (!tip) {
        var inner = $('<div class="tipsy-inner">').append(opts.tip ? opts.tip() : $(this).attr('original-title'));
        tip = $('<div class="tipsy">').append(inner);
        tip.css({position: 'absolute', zIndex: 100000});
        $(this).attr('original-title', '');
        $.data(this, 'active.tipsy', tip);
      }
 
      var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
      tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
      var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
 
      // TODO: Brutal. Clean this up later.
      var half_width  = $(window).width() / 2;
      var half_height = $(window).height() / 2;
      if (pos.top < half_height) {
        tip.css({top: pos.top})
        if (pos.left < half_width) tip.css({left: pos.left + pos.width}).addClass('tipsy-nw');
        else tip.css({left: pos.left - actualWidth}).addClass('tipsy-ne');
      }
      else {
        tip.css({top: pos.top - (actualHeight - pos.height)})
        if (pos.left < half_width) tip.css({left: pos.left + pos.width}).addClass('tipsy-sw');
        else tip.css({left: pos.left - actualWidth}).addClass('tipsy-se');
      }
 
      if (opts.fade) {
        tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 1});
      } else {
        tip.css({visibility: 'visible'});
      }
    }, function() {
      $.data(this, 'cancel.tipsy', false);
      var self = this;
      setTimeout(function() {
        if ($.data(this, 'cancel.tipsy')) return;
        var tip = $.data(self, 'active.tipsy');
        if (opts.fade) {
          tip.stop().fadeOut(function() { $(this).remove(); });
        } else {
          tip.remove();
        }
      }, 100);
    });
  };
})(jQuery);

/*
 * textfill: Expand text to fill a container.
 *
 * Uses a binary search rather than the linear search found in:
 * * http://plugins.jquery.com/project/TextFill
 * * http://parkingstructure.displayawesome.com/resources/js/vertical.fill.js
 *
 * TODO: Take padding into account.
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2009 Stateless Systems (http://statelesssystems.com)
 *
 * @author   Shane Hanna (shane.hanna -at- gmail -dawt- com)
 * @requires jQuery v1.3 or later
 * @version  0.1
 */

(function ($) {
  $.extend($.fn, {
    textfill: function (options) {
      var defaults = {max: 100};
      options      = $.extend(defaults, options);

      return this.each(function () {
        var el     = $(this);
        var width  = el.parent().width() - 5;
        var height = el.parent().height() - 5;
        var low    = 0;
        var high   = options.max;

        while (low <= high) {
          var mid = Math.round(low + ((high - low) / 2));
          el.css('font-size', mid + 'px');
          

          if (el.height() > height || el.width() > width) { high = mid - 1; continue;}
          if (el.height() < height && el.width() <= width) { low  = mid + 1; continue;}
          break;
        }

        if ((low - 1) > 0) el.css('font-size', (low - 1) + 'px');
      });
    }
  });
})(jQuery);

function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
