/**
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//\\\       \\\\\\\\|
//\\\ @@    @@\\\\\\| 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('&');

if (typeof($_GET) == 'undefined') {
  var $_GET = new Array();
};

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 $tmp = $("<div></div>").append($(this).clone());
    var $html = $tmp.html();

    // Don't leak memory.
    $tmp.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()
  {
    var $id = !arguments[1]? this.attr('id') : arguments[1];

    if (!$id) {
      return '';
    }

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

    return $id.split('-').pop();
  },

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

  SelectSet : [],
  SelectCallbackFn : [],

  SelectCallback : function($set, $select, $unselect)
  {
    $$.SelectCallbackFn[$set] = {
      select: $select,
      unselect: $unselect
    };
  },

  Select : function($set)
  {
    if ($$.SelectSet[$set]) {
      $$.Unselect.call($$.SelectSet[$set], $set);
    }

    $$.SelectSet[$set] = this;

    this.addClass($set + 'Selected');

    // Allow a callback function
    if (arguments[1]) {
      arguments[1].call(arguments[2]? arguments[2] : this);
    }
    
    if (typeof($$.SelectCallbackFn[$set]) != 'undefined' && typeof($$.SelectCallbackFn[$set].select) != 'undefined') {
      $$.SelectCallbackFn[$set].select.call(this);
    }

    return this;
  },

  Unselect : function($set)
  {
    if ($$.SelectSet[$set]) {
      $$.SelectSet[$set].removeClass($set + 'Selected');
      $$.SelectSet[$set] = null;
    }

    if (typeof($$.SelectCallbackFn[$set]) != 'undefined' && typeof($$.SelectCallbackFn[$set].unselect) != 'undefined') {
      $$.SelectCallbackFn[$set].unselect.call(this);
    }

    return this;
  },

  GetSelected : function($set)
  {
    return $$.SelectSet[$set]? $$.SelectSet[$set] : [];
  },

  Delete : function()
  {
    this.each(
      function() {
        this.parentNode.removeChild(this);
      }
    );
    
    return this;
  },
  
  Include : function($path) {
    var $script = document.createElement('script');
    $script.type = 'text/javascript';
    $script.src  = $$.Path($path);
    $('head').append($script);
  },
  
  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()
      )
  },

  ScrollTo : function($element)
  {
    $(this).get(0).scrollLeft = $element.offsetLeft;
    $(this).get(0).scrollTop  = $element.offsetTop;
  },
  
  Path : function(path)
  {
    if (hPath != '/') {
      path = hPath + path;
    }

    if (hUserAgentOS == 'Desktop Application') {
      path = "http://" + $_SERVER['HTTP_HOST'] + path;
    }

    if (!arguments[1] && hUserAgentOS == 'Desktop Application') {
      arguments[1] = {}
    }

    if (hUserAgentOS == '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]) {
      var $i = 0;

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

    return path;
  },

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

var $$ = $.fn;

if (hUserAgentOS == 'Desktop Application') {
  var $$$ = Titanium;
  var hDesktopApplication = true;
} else { 
  var hDesktopApplication = false;

}

$.ajaxSetup({
  cache: false
});

var PNG = function($img)
{
  if (hUserAgent == 'ie' && hUserAgentVersion < 7 && hUserAgentVersion > 5.2 && $img && $img.src) {
    $img.outerHTML = '<span src="' + $img.src + '"' + (($img.id)? " id='" + $img.id + "' " : '') + (($img.className)? " class='" + $img.className + "' " : '') + (($img.title)? " title='" + $img.title + "' " : '') + ' style="display: inline-block; width: ' + $img.width + 'px; height: ' + $img.height + "px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $img.src + "', sizingMethod='scale'); " + $img.style.cssText + '"></span>';
  }
};

var hFramework = {};

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

    if ($input.length) {
      $input.Buttons();
    }
  }
);

//$$.Include('/plugins/hHTTP/hHTTP.js');
$$.Include('/plugins/hDHTML/hFade/hFade.js');

hFramework.IE  = (hUserAgent == 'ie');
hFramework.IE6 = (hUserAgent == 'ie' && hUserAgentVersion < 7);
