/**
 * RAOS JQuery Tools
 */ 

/**
 * Appends selection to spec'd object (or body by default) 
 * so that it's 'visible' (can take dimensions of it), but not visible to the user.
 *
 * Wanted a standard way of doing this... in case we decide it a bad way of doing it.
 */ 
$.fn.appendToHidden = function(el) {
  el = el || 'body';
  $(this).css({
    position:'absolute',
    left:-5000
  });
  return $(this).appendTo(el);
}

/**
 * Grab both dimenstions from an element in an easy to use object.
 *
 * @param string types Space delim. list of types:
 *  true : height() and width() 
 *  inner, outer and/or
 *  total : outer + margin
 */  
$.fn.dimensions = function(types) {
  types = (type || 'true').split(' ');
  out = {};
  $(types).each(function() {
    switch(types) {
      case 'true':
        out['height'] = $(this).height();
        out['width'] = $(this).width();
        break;
      case 'inner':
        out['innerHeight'] = $(this).innerHeight();
        out['innerWidth'] = $(this).innerWidth();
        break;
      case 'outer':
        out['outerHeight'] = $(this).outerHeight();
        out['outerWidth'] = $(this).outerWidth();
        break;
      case 'total':
        out['totalHeight'] = $(this).outerHeight(true);
        out['totalWidth'] = $(this).outerWidth(true);
        break;
    }
  });
  return out;
};

RAOS  = {
  logPipe : function() {
    this.log.apply(this.log,arguments);
    return arguments[0];
  },
  // x-browser debug helpers.
  log : function() {
    if(window['console'] && window.console['log']) {
      if(console.log.apply) console.log.apply(console,arguments);
      else { // *some* brower's consoles (IE) don't support apply?!
        var out=''; 
        for(var i = 0; i < arguments.length; i++) {
          out += arguments[i];
        }
        console.log(out);
      }
    }
    else if (window['jash'] && jash.dump) jash.dump.apply(jash,arguments); 
  },
  alert : function(msg) {
    if(window['console'] && window.console['info'] && console.info.apply) console.info.apply(console,arguments);
    else if (window['jash'] && jash.dump) jash.dump.apply(jash,arguments); 
    else alert(msg);
  },
  /* draw box around element */
  highlight : function(el) {
  }
};

var raos = RAOS;

/** experimental helpers **/
$.fn.matchPosition = function(obj, adjust) {
  var obj = $(obj);
  var pos = obj.offset();
  if(adjust) {
    if(adjust.left == 'center') {
      pos.left += ((obj.width()/2) - ($(this).width()/2)); 
    } else pos.left += adjust.left;
    if(adjust.top == 'center') {
      pos.top += ((obj.height()/2) - ($(this).height()/2)); 
    } else pos.top += adjust.top;
  }
  var out = {};
  if(!(adjust && adjust.left == 'none')) out['left'] = post.left;
  if(!(adjust && adjust.top == 'none')) out['top'] = post.left;
  $(this).css(out);
  return this;
}
$.fn.matchSize = function(obj) {
 var obj = $(obj);
 $(this).height(obj.height());
 $(this).width(obj.width());
 return this;
}
$.fn.matchAll = function(obj) {
  obj = $(obj);
  $(this)
    .matchSize(obj)
    .matchPosition(obj);
  return this;
}

$.fn.toggleClass = function(cn) {
  if($(this).hasClass(cn)) $(this).removeClass(cn);
  else $(this).addClass(cn);
}

/**
 * URL Encode
 * Found... somewhere... source?
 */ 
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

/**
 * sprintf() for JavaScript v.0.4
 *
 * Copyright (c) 2007 Alexandru Marasteanu <http://alexei.417.ro/>
 * Thanks to David Baird (unit test and patch).
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}

