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;
    },
    // akamai
    isLive: function() {
        return false;
        var hostname = document.location.host;
        var bits = hostname.match(/^(mini|dev|stage|dev2|stage2)?(.*)$/);
        return false;
        return (bits.length && !bits[1]) ? true : false;
    },
    wwwHost: function() {
        return 'www.'+this.rawHost();
    },
    rawHost: function() {
        var hostname = document.location.host;
        var bits = hostname.match(/^(mini|dev|stage|dev2|stage2|www)?\.?(.*)$/);
        return bits[2];
    },
    akamaiBase: function() {
        if (this.isLive()) {
            var scheme = document.location.protocol;
            var hostname = this.rawHost();
            // force to aveda.de since .ch and .at also run on this host
            hostname = 'aveda.de';
            return scheme+'//i.'+hostname;
        }
        else {
            return '';
        }
    },
    akamaize: function(path) {
        return this.akamaiBase() + path;
    }    
});

