// Mootools Extensions
// String.trim, ltrim, rtrim
// Array.insert
// (c) Jordan Sherer 2008

Array.prototype.insert = function(obj, index){
    lastIndex = this.length - 1;
    if(index > lastIndex){ index = lastIndex; }
    this.splice(index, 0, obj);
}

String.prototype.trim = function (c) {
    if(!c)
    {
        return this.replace(/^\s*/, "").replace(/\s*$/, "");
    }
    else
    {
        var reBegining = RegExp("^"+c.escapeRegExp()+"*");
        var reEnding = RegExp(c.escapeRegExp()+"*$");
        return this.replace(reBegining, "").replace(reEnding, "");
    }
}

String.prototype.ltrim = function (c) {
    if(!c)
    {
        return this.replace(/^\s*/, "");
    }
    else
    {
        var reBegining = RegExp("^"+c.escapeRegExp()+"*");
        return this.replace(reBegining, "");
    }
}

String.prototype.rtrim = function (c) {
    if(!c)
    {
        return this.replace(/\s*$/, "");
    }
    else
    {
        var reEnding = RegExp(c.escapeRegExp()+"*$");
        return this.replace(reEnding, "");
    }
}

String.prototype.replaceRegex = function(searchTerm, replaceWith, ignoreCase ){
    var regex = "/"+ searchTerm +"/g";
	if( ignoreCase ) regex += "i";
	return this.replace(eval(regex), replaceWith);
}

function fix_ie6_first_last_child()
{
    if(Browser.Engine.trident4)
    {
        $$("body *:first-child").each(function(el){
            el.addClass("first-child");
            });
        $$("body *:last-child").each(function(el){
            el.addClass("last-child");
            });
    }
}
addOnLoad(fix_ie6_first_last_child);

function get_a()
{
    $(document).getElements('a[rel^=popup]').each(function(a){
        add_popup(a);
    });
}
//addOnLoad(get_a);

/*
adapted from
moopop: unobtrusive javascript popups via late binding using mootools 1.11 
copyright (c) 2007 by gonchuki - http://blog.gonchuki.com
*/
function add_popup(el)
{
    el.addEvent('click', function(e){ new Event(e).stop(); popup(el); }.bind(this));
    var size = el.getAttribute('rel').match(/\[(\d+),\s*(\d+)\]/) || ['', this.width, this.height];
    if (size[1]) el.setAttribute('popupprops', 'width=' + size[1] + ', height=' + size[2] );
}
  
function popup(el)
{
    window.open(el.href, '', el.getAttribute('popupprops') || '');
}