/**
* Fusionbox 1.0 - jQuery Plug-in
*
*/

if (!String.prototype.htmlentities)
{
  String.prototype.htmlentities = function() {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
    
    var div = document.createElement('div');
    var text = document.createTextNode(this);
    div.appendChild(text);
    return div.innerHTML;
  }
}
if (!String.prototype.trim)
{
  String.prototype.trim = function() {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
    
    return this.replace(/^\s*(.*)\s*$/, '$1');
  }
}
/**
*
*  modified from:
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
if (!String.prototype.raw_url_encode)
{
  String.prototype.raw_url_encode = function () {
    return escape(this);
  };
}
if (!String.prototype.raw_url_decode)
{
  String.prototype.raw_url_decode = function () {
    return unescape(this);
  };
}


(function($)  {
  
  $(function() {
    
    $.fbmvc = {};
    $.fbmvc.App = 'site';
    var admin_app = 'admin';
    var controller_i = 0;
    $.fbmvc.Controller = 'home';
    $.fbmvc.Action = 'index';
    $.fbmvc.Id = null;
    
    $( window.location.pathname.replace(/^\//, '').replace(/\/$/, '').split('/') ).each(function(i, val) {
      val = val.raw_url_decode().trim();
      
      if (val=='')
      {
        controller_i++;
        return;
      }
      
      if (i==controller_i)
      {
        if (val==admin_app)
        {
          $.fbmvc.App = admin_app;
          controller_i = 1;
        }
        else
        {
          $.fbmvc.Controller = val;
        }
        return true;
      }
        
      if (i==controller_i + 1)  {  $.fbmvc.Action = val;  return true;  }
      if (i==controller_i + 2)  {  $.fbmvc.Id = val;  return true;  }
    })
  });
  
  $.fn.swapTabs = function(class_name, turn_on_element_names)
  {
    $(this).siblings('a.'+ class_name).each(function()
    {
      $(this).removeClass('on').addClass("off");
    });
    
    $('div.'+ class_name).each(function()
    {
      $(this).removeClass('on').addClass("off");
    });
    
    $.each(turn_on_element_names, function() {
      $('#' + this).removeClass('off').addClass("on");
    });
    
    return false;
  };
  
  $.fn.replace_ajax = function(url, data, callback)
  {
    current_element = $(this);
    $.get(url, data, function(response, status) {
      current_element.replaceWith(response);
      if (callback)  callback();
    });
    
    return this;
  };
  
  $.fn.one = function()
  {
    if ($(this).length > 0)
    {
      return $(this)[0];
    }
    
    return null;
  };
  
  $.fn.first = $.fn.one;
})(jQuery);
