var GenericUtil = new Object();

Object.extend(GenericUtil, {
    gotoWithArgs: function(args) {
        var pathname = document.location.pathname;
        var search = document.location.search;
        var query = search.parseQuery();
        for (var arg in args) {
            query[arg] = args[arg];
        }
        var newurl = pathname +'?'+ $H(query).toQueryString();
        document.location = newurl;
    },
    filledArray: function(size, filler) {
        var array = new Array(size);
        for (var i = 0; i < size; i++) {
            array[i] = filler;
        }
        return array;
    },
    objRef: function(ref) {
        if (typeof ref == 'string') {
            return eval(ref);
        }
        else {
            return ref;
        }
    },
    // Convert an array into a multidemtional array of the given width and height.  The grid is filled Left to Right, Top to Bottom.  If a dimension is not given, then that dimension is unlimited.  If neither is given, then it will try and make a square.
    arrayToGrid: function(data, width, height) {
        var grid = new Array();
        
        if (!width && !height) { width = Math.ceil(Math.sqrt(data.length)); }
        if (!width) { width = Math.ceil(data.length / height); }
        if (!height) { height = Math.ceil(data.length / width); }
        
        var i = 0;
        var end = width*height;
        var row = 0;
        while (i < end) {
            if (i >= data.length) { break; }
            grid[row] = new Array();
            for (col = 0; col < width; col++) {
                if (i >= data.length) { break; }
                grid[row][col] = data[i];
                i++;
            }
            row++;
        }
        return grid;
    },
    waitFor: function(func, seconds) {
        var date = new Date();
        var curDate = null;
        var millis = seconds * 1000;      
        var result;
        
        do { 
            curDate = new Date(); 
            result = func();
        } 
        while ( !result && (curDate-date < millis) );
        
        return result;
    },    
    // akamai
    isLive: function() {
        return false;
        var hostname = this.hostname();
        var bits = hostname.match(/^(mini|dev|stage|redesign|dev2|stage2)?(.*)$/);
        GenericUtil.__isLive = (bits.length && !bits[1]) ? true : false;
        GenericUtil.isLive = function() { return this.__isLive; };
        return this.__isLive;
    },
    wwwHost: function() {
        GenericUtil.__wwwHost = 'www.'+this.rawHost();
        GenericUtil.wwwHost = function() { return this.__wwwHost; };
        return this.__wwwHost;
    },
    rawHost: function() {
        var hostname = this.hostname();
        var bits = hostname.match(/^(mini|dev.?|stage.?|www|redesign|i)?\.?(.*)$/);
        GenericUtil.__rawHost = bits[2];
        GenericUtil.rawHost = function() { return this.__rawHost; };
        return this.__rawHost;
    },
    siteBase: function() {
        var scheme = document.location.protocol;
        var hostname = GenericUtil.hostname();
        GenericUtil.__siteBase = scheme+'//'+hostname;
        return this.__siteBase;
    },
    akamaiBase: function(force) {
        var scheme = document.location.protocol;
        var hostname;
        if (this.isLive() || force) {
            hostname = 'i.' + this.rawHost();
        }
        else {
            hostname = this.hostname();
        }
        GenericUtil.__akamaiBase = scheme+'//'+hostname;
        return this.__akamaiBase; 
    },
    akamaize: function(path, force) {
        return this.akamaiBase(force) + path;
    },
    // syndication
    syndicated: function() {
        GenericUtil.__syndicated = typeof wsmlMakeLeakHref == 'function' 
            && wsmlMakeLeakHref('test') != 'test';
        GenericUtil.syndicated = function() { return this.__syndicated; };
        return this.__syndicated;
    },
    syndicatedHost: function() {
        var leak = wsmlMakeResourceUrl('/');
        var bits = leak.match(new RegExp('^.*?://(.*?)/','i'));
        GenericUtil.__syndicatedHost = bits[1];
        GenericUtil.syndicatedHost = 
            function() { return this.__syndicatedHost; };
        return this.__syndicatedHost;
    },
    hostname: function() {
        GenericUtil.__hostname = GenericUtil.syndicated() ?
            this.syndicatedHost() : document.location.host;
        GenericUtil.hostname = function() { return this.__hostname; };
        return this.__hostname;
    },
    // url without the file name
    urlpath: function(url) {
        var bits = url.match(new RegExp('^(.*)/[^/]*$'));
        return bits ? bits[1] : url;
    },
    // get paramter from current url, including key value pairs in the anchor
    urlparam: function(param) {
        var query = document.location.search + '&' + document.location.hash.slice(1);
        var params = query.toQueryParams();
        return params[param];
    }

});




