/**
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//\\\       \\\\\\\\|
//\\\ @@    @@\\\\\\| Hot Toddy Framework
//\\ @@@@  @@@@\\\\\|
//\\\@@@@| @@@@\\\\\|
//\\\ @@ |\\@@\\\\\\| http://www.hframework.com
//\\\\  ||   \\\\\\\| (c) Copyright 2009 Richard York, All rights Reserved
//\\\\  \\_   \\\\\\|
//\\\\\        \\\\\| Use and redistribution are subject to the terms of the license.
//\\\\\  ----  \@@@@| http://www.hframework.com/license
//@@@@@\       \@@@@|
//@@@@@@\     \@@@@@|
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
*/

var vars = window.location.search.substring(1).split('&');

var get = [];

if (vars.length) {
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split('=');
    get[pair[0]] = decodeURIComponent(pair[1]);
  };
};

$.fn.extend({
  outerHTML : function()
  {
    if (arguments[0]) {
      return this.replaceWith(arguments[0]);
    }

    var temporary = $("<div></div>").append($(this).clone());
    var html = temporary.html();

    // Don't leak memory.
    temporary.remove();
    return html;
  },

  disabled : function()
  {
    return this.attr('disabled', 'disabled');
  },

  checked : function()
  {
    return this.attr('checked', 'checked');
  },
  
  selected : function()
  {
    return this.attr('selected', 'selected');
  },

  getFileNameFromSrc : function()
  {
    return this.attr('src').split('/').pop();
  },

  buttons : function()
  {
    if (this.attr('src').indexOf('None') != -1) {
      this
        .hover(
          function() {
            $(this).sourceFile($(this).getFileNameFromSrc().replace(/None|Active|Hover/, 'Hover'));
          },
          function() {
            $(this).sourceFile($(this).getFileNameFromSrc().replace(/None|Active|Hover/, 'None'));
          }
        )
        .mousedown(
          function() {
            $(this).sourceFile($(this).getFileNameFromSrc().replace(/None|Active|Hover/, 'Active'));
          }
        )
        .mouseup(
          function() {
            $(this).sourceFile($(this).getFileNameFromSrc().replace(/None|Active|Hover/, 'None'));
          }
        );
    }
  },

  button : function(mousedownSource, mouseupSource)
  {
    this.mousedown(
      new Function(
        "$(this).sourceFile('" + mousedownSource + "');"
      )
    );
    
    this.mouseup(
      new Function(
        "$(this).sourceFile('" + mouseupSource + "');"
      )
    );
    
    if (arguments[2]) {
      this.click(arguments[2]);
    }
  },

  active : function()
  {
    if (arguments[0]) {
      this.mousedown(arguments[0]);

      if (arguments[1]) {
        this.mouseup(arguments[1]);
      }
    
      if (arguments[2]) {
        this.click(arguments[2]);
      }
    } else {
      this.mousedown();
      this.mouseup();
      this.click();
    }

    return this;
  },
  
  splitId : function()
  {
    return hot.splitId(this, arguments[0]? arguments[0] : null, arguments[1]? arguments[1] : null);
  },

  splitNumericId : function()
  {
    return hot.splitNumericId(this);
  },
  
  select : function(set)
  {
    return hot.select(this, set, arguments[1]? arguments[1] : null, arguments[2]? arguments[2] : null);
  },

  scrollTo : function(element)
  {
    $(this).get(0).scrollLeft = element.offsetLeft;
    $(this).get(0).scrollTop  = element.offsetTop;
  },

  sourceFile : function(file)
  {
    var path = this.attr('src').split('/');
    path.pop();
    path.push(file);
    return this.attr('src', path.join('/'));
  }
});

hot.desktopApplication = (hot.userAgentOS == 'Desktop Application');

$.ajaxSetup({
  cache: false
});

$.extend(
  hot, {
  _events : [],

  events : function(obj)
  {
    for (var event in obj) {
      hot.event(event, obj[event].fn, obj[event].context? obj[event].context : null); 
    }
  },

  event : function(event, fn)
  {  
    if (!this._events[event]) {
      this._events[event] = [];
    }

    this._events[event].push({
      fn: fn,
      context: arguments[2]? arguments[2] : null
    });
  },

  fire : function(event)
  {  
    var args = arguments[1]? arguments[1] : null;
    var defaultContext = this;
    var context = defaultContext;

    // context of "this" may be changed, so don't use it.
    if (hot._events[event] && hot._events[event].length) {          
      $(hot._events[event]).each(
        function(key, obj) {
          context = obj.context? obj.context : defaultContext;

          var rtn = obj.fn.call(context, args);

          if (rtn === false) {
            return false;
          }
        }
      );
    }
    
    return context;
  },

  path : function(path)
  {
    if (this.installPath != '/') {
      path = this.installPath + path;
    }

    if (this.userAgentOS == 'Desktop Application') {
      path = "http://" + server.host + path;
    }

    if (!arguments[1] && this.userAgentOS == 'Desktop Application') {
      arguments[1] = {}
    }

    if (this.userAgentOS == 'Desktop Application') {
      arguments[1].hDesktopApplicationStyle = 1;

      if (get.hUserAuthenticationToken) {  
        arguments[1].hUserAuthenticationToken = get.hUserAuthenticationToken;
      } else if (typeof(hUserID) != 'undefined' && typeof(hUserPassword) != 'undefined') {
        arguments[1].hUserAuthenticationToken = hUserID + ',' + hUserPassword;
      }
    }

    if (arguments[1] && typeof(arguments[1]) == 'object') {
      var $i = 0;

      for (var key in arguments[1]) {
        path += (($i == 0)? '?' : '&') + key + '=' + encodeURIComponent(arguments[1][key]);
        $i++;
      }
    } else {
      path += (path.indexOf('?') == -1? '?' : '') + arguments[1];
    }

    return path;
  },
  
  _selected : [],
  
  select : function(node, set)
  {
    if (this._selected[set]) {
      this.unselect(set);
    }

    this._selected[set] = node;

    node.addClass(set + 'Selected');

    // Allow a callback function
    if (arguments[2]) {
      arguments[2].call(arguments[3]? arguments[3] : node);
    }

    hot.fire.call(node, set + 'Selected');
    return node;
  },
  
  unselect : function(set)
  {
    var node = null;

    if (this._selected[set]) {
      var node = this._selected[set];

      node.removeClass(set + 'Selected');
      this._selected[set] = null;

      this.fire.call(node, set + 'Unselected');
    }

    return node;
  },

  selected : function(set)
  {
    return this._selected[set]? this._selected[set] : [];
  },
  
  splitId : function(node)
  {
    var id = !arguments[2]? node.attr('id') : arguments[2];

    if (!id) {
      return '';
    }

    if (id.indexOf('-') < 0) {
      return '';
    }

    if (arguments[1]) {
      var bits = id.split('-');
      bits = bits.reverse();
  
      for (i = 0; i < bits.length; i++) {
        if (i == arguments[1]) {
          return bits[i];  
        }
      }
    }

    return id.split('-').pop();
  },
  
  splitNumericId : function(node)
  {
    var id = node.attr('id');
    return id.indexOf('--') != -1 ? -(parseInt(id.split('-').pop())) : parseInt(id.split('-').pop());
  },

  debug: function(text)
  {
    if (!$('div.hFrameworkDebug').length) {
      debug = document.createElement('div');
      debug.className = 'hFrameworkDebug';

      $('body').append(debug);
    }

    $('div.hFrameworkDebug')
      .dblclick(
        function() {
          $(this).remove();
        }
      )
      .css({
        position: 'fixed',
        top: '10px',
        left: '10px',
        overflow: 'auto',
        background: '#fff',
        border: '1px solid rgb(128, 128, 128)',
        width: '500px',
        height: '500px',
        padding: '10px',
        zIndex: '30000',
        whiteSpace: 'pre',
        font: '12px Monaco, monospace'
      })
      .text(
        text + "\n\n" +
        $('div.hFrameworkDebug').text()
      )
  },

  include : function(path)
  {
    $.getScript(path, arguments[1]? arguments[1] : function() {});    
  },
  
  _windowCounter : 0,
  
  getOffsetOfArgumentWithType : function(args, type)
  {
    var counter = 0;
    
    for (var i = 0; i < args.length; i++) {
      if (typeof(args[i]) == type) {
        return counter;
      }

      counter++;
    }

    return 0;
  },

  countArgumentsOfType : function(args, type)
  {
    var counter = 0;
    
    for (var i = 0; i < args.length; i++) {
      if (typeof(args[i]) == type) {
        counter++;
      }
    }

    return counter;
  },
  
  getArgumentByType : function(args, type, defaultValue)
  {
    var offset = 1;
    
    if (arguments[3]) {
      offset = arguments[3];
    }

    var counter = 1;

    for (var i = 0; i < args.length; i++) {
      if (typeof(args[i]) == type) {
        if (offset == counter) {
          return args[i];
        } else {
          counter++;
        }
      }
    }

    return defaultValue;
  },
  
  window : function(url)
  {
    var getParameters = this.getArgumentByType(arguments, 'object', {});
    var width         = this.getArgumentByType(arguments, 'number', 800);
    var height        = this.getArgumentByType(arguments, 'number', 640, 2);
    var name          = this.getArgumentByType(arguments, 'string', 'window' + this._windowCounter, 2);
    var options       = this.getArgumentByType(arguments, 'object', {}, 2);

    this._windowCounter++;

    options.width = width;
    options.height = height;

    var optionsArray = [];
    
    for (var i in options) {    
      if (options[i] === true) {
        optionsArray.push(i + '=yes');
      } else if (options[i] === false) {
        optionsArray.push(i + '=no');
      } else {
        optionsArray.push(i + '=' + options[i]);
      }
    }

    // 'menubar=no, location=no, statusbar=no, titlebar=no, toolbar=no, scrollbars=yes, resizable=no, alwaysraised=yes, z-lock=yes, width=800, height=640'
    var popup = window.open(hot.path(url, getParameters), name, optionsArray.join(','));
    popup.moveTo((window.screen.width - width) / 2, (window.screen.height - height) / 2);
    popup.focus();

    return popup;
  },

  animationEnabled : true,
  
  IE : (hot.userAgent == 'ie'),

  IE6 : (hot.userAgent == 'ie' && hot.userAgentVersion < 7)
});

$(document).ready(
  function() {  
    var input = $('input[src*="/images/themes/aqua/buttons/"]');

    if (input.length) {
      input.buttons();
    }
  }
);

hot.include('/plugins/hDHTML/hFade/hFade.js');
