/**
 * Shared functions, defaults, etc
 */

// Yeah, some browsers don't support indexOf!
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

// resize
function setFlashHeight(newHeight){
    if(newHeight < 590)
    {
        newHeight = 590;
    }
    var flashContentHolderDiv = document.getElementById('media_container');
    flashContentHolderDiv.style.height = newHeight + 20 + "px";
    var flashDiv = document.getElementById('swf');
    flashDiv.style.height = newHeight + "px";
    return 22;
}

function getFlashY(){
    var offset = getCumulativeOffset(document.getElementById('swf'));
    var scroll = getScrollOffset();
    return (offset.y-scroll.y);
}

/*
 Get the calculated position of an element on the page
 Based on: http://www.quirksmode.org/js/findpos.html
 */
function getCumulativeOffset(obj) {
    var left, top;
    left = top = 0;
    if (obj.offsetParent) {
        do {
            left += obj.offsetLeft;
            top  += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return {
        x : left,
        y : top
    };
}

/**
 Calculate the scroll offset
 Based on: http://codesnippets.joyent.com/user/winton/tag/javascript
 */
function getScrollOffset() {
    var left, top;
    left = top = 0;
    if (self.pageYOffset) {
        // all except Explorer
        left = self.pageXOffset;
        top = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        // Explorer 6 Strict
        left = document.documentElement.scrollLeft;
        top = document.documentElement.scrollTop;
    } else if (document.body) {
        // all other Explorers
        left = document.body.scrollLeft;
        top = document.body.scrollTop;
    }
    return {
        x : left,
        y : top
    };
}

function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}

function getUrlVars( map ) {
    /*if( !map ) map = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        map[key] = value;
    });
    return map;*/
    return parseQueryString(window.location.href,map);
}

function parseQueryString( queryString, map ) {
    if( !map ) map = {};
    queryString.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        map[key] = value;
    });
    return map;
}
