// Common JavaScript code across your application goes here.
function addLoadEvent(func) {
    Event.observe(window,'load',func)
}

String.prototype.format = function(){
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
}
function attr(field, val) {
    return "{0}='{1}'".format(field,val);
}
function tag(tag, attrs, inner) {

    var str_attrs = "";
    for(var i = 0;i<attrs.length;i++) {
        str_attrs += " " + attrs[i];
    }
    var retval =  "<{0} {1}>{2}</{3}>".format(tag,str_attrs,inner,tag);
    return(retval);
}

var js_href_void = attr("href","javascript: void(0);");

function get_size() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];

}
function convert_to_percent(el_style) {
    sz = get_size();
    px_left = parseInt(el_style.left.replace(/px/gi,''));
    return [100 * (el_style.top.replace(/px/gi,'') / sz[1]),100 * ( (px_left + (px_left * 0.01) ) / sz[0]) ];
}
function convert_from_percent(el_style) {
    sz = get_size();
    perc_left = parseInt(el_style.left.replace(/\%/gi,''));
    //adjust - rmt
    perc_left = perc_left + (perc_left * 0.01);
    perc_top = parseInt(el_style.top.replace(/\%/gi,''));
    return [sz[1] * ( perc_top / 100), sz[0] * (perc_left / 100)];
}

function normalize_coordinate(c, axis)
{
    if (/\%/.test(c) )    { return parseInt(c.replace(/\%/gi,'')); }
    sz = get_size();
    if( /px/.test(c) )
    {        num = parseInt(c.replace(/px/gi,''));    } else
    {        num = c    /* if no suffix, assume pixels */    }
    if(axis=='top')
    {        return ( 100  * ( num / sz[1]));    }
    else
    {        return ( ( 100  * (num / sz[0] ) ) - (num * 0.01) )    }
}
