/*
 * $Id: default.js 196 2009-02-19 21:39:44Z marius $
 */

var flashMsgTimer;
var flashMsgTitle;
var flashMsgContent;

/**
* Data dumper 2.1
* By Marius van Witzenburg, http://www.kitara.nl
*
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
    var dumped_text = "";
    if (!level) {
        level = 0;
    }
    var level_padding = "";
    for(var j=0; j<level; j++) {
        level_padding += "    ";
    }
    if(typeof(arr) == 'object') {
        for(var item in arr) {
            var value = arr[item];
            if(typeof(value) == 'object') {
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else {
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}

/**
* Center Window 1.4
* By Marius van Witzenburg, http://www.kitara.nl
*/
function centerWindow(element) {
    if($(element) != null) {
        if(typeof window.innerHeight != 'undefined') {
            $(element).style.top = Math.round(document.viewport.getScrollOffsets().top + ((window.innerHeight - $(element).getHeight()))/2)+'px';
            $(element).style.left = Math.round(document.viewport.getScrollOffsets().left + ((window.innerWidth - $(element).getWidth()))/2)+'px';
        } else {
            $(element).style.top = Math.round(document.body.scrollTop + (($$('body')[0].clientHeight - $(element).getHeight()))/2)+'px';
            $(element).style.left = Math.round(document.body.scrollLeft + (($$('body')[0].clientWidth - $(element).getWidth()))/2)+'px';
        }
    }
}

/*
 * Generate unix time from browser time
 * By Marius van Witzenburg, http://www.kitara.nl
 */
function unixtime() {
    var curdate = new Date;
    var unixtime_ms = curdate.getTime();
    var unixtime = parseInt(unixtime_ms / 1000);
    return unixtime;
}


function ajaxlayer_start(message) {
    if (message == undefined) {
        message = 'Een ogenblik a.u.b.';
    }
    $('ajaxlayerText').update(message);
    centerWindow('ajaxlayer');
    $('ajaxlayer').visualEffect('appear', {duration: 0.5});
}
function ajaxlayer_stop(justhide) {
    if (justhide != undefined) {
        $('ajaxlayer').hide();
    } else {
        $('ajaxlayer').visualEffect('fade', {delay: 0.5, duration: 0.5});
    }
}

function flashMsgStart(size, title, content, time, fetchFrom) {
    if (size == undefined) {
        size = 'Narrow';
    } else {
        if (size != 'Narrow' && size != 'Wide') {
            title = 'Usage error!';
            size = 'Wide';
        }
    }
    if (title == undefined) {
        title = '...';
    }
    if (content == null) {
        content = '';
    }
    if (time == undefined) {
        time = null;
    }
    if (fetchFrom == undefined) {
        fetchFrom = false;
    }

    $('flashMsg' + size + 'Title').update(title);

    if (fetchFrom != false) {
        new Ajax.Updater('flashMsg' + size + 'Content', fetchFrom, {
            evalScripts:true,
            onCreate: function() {
                ajaxlayer_start();
            },
            onComplete: function() {
                setTimeout(function() { flashMsgShow(size, time) }, 500);
            }
        });
    } else {
        $('flashMsg' + size + 'Content').update(content);
        flashMsgShow(size, time);
    }
}
function flashMsgShow(size, time) {
    centerWindow('flashMsg' + size);
    ajaxlayer_stop(true);
    if (time != null && !isNaN(time)) {
        $('flashMsg' + size).visualEffect('appear', {duration: 0.3, complete: flashMsgTimer = setTimeout('flashMsgStop("' + size + '")', time + '000')});
    } else {
        $('flashMsg' + size).visualEffect('appear', {duration: 0.3});
    }
    $('blackOverlay').visualEffect('appear', {duration: 0.3});
}
function flashMsgStop(size) {
    if (size == undefined) {
        size = 'Narrow';
    }
    if (flashMsgTimer != undefined) {
        $('flashMsg' + size).visualEffect('fade', {duration: 0.3, complete: clearTimeout(flashMsgTimer)});
        $('blackOverlay').visualEffect('fade', {duration: 0.3, complete: clearTimeout(flashMsgTimer)});
    } else {
        $('flashMsg' + size).visualEffect('fade', {duration: 0.3});
        $('blackOverlay').visualEffect('fade', {duration: 0.3});
    }
}

/*
 * Redirect to a path within the defined siteBase
 * By Marius van Witzenburg, http://www.kitara.nl
 *
 * The siteBase has to be defined to the root path of your site, this can be a http or relative url
 */
function redirect(target, external) {
    if (target != undefined) {
        if (external != undefined) {
            window.location = encodeURI(target);
        } else {
            window.location = siteBase + encodeURI(target);
        }
    } else {
        return false;
    }
}


function flashMovie(id, file, width, height, inscript, query) {
    if (id == undefined || file == undefined || width == undefined || height == undefined) {
        return false;
    }
    if (query == undefined) {
        query = 'site_version=' + siteVersion;
    } else {
        query = 'site_version=' + siteVersion + '&' + query
    }
    if (inscript == undefined) {
        AC_FL_RunContent('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
            'width', width,
            'height', height,
            'src', siteBase + '/flash/' + file + '?' + query,
            'quality', 'high',
            'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
            'align', 'middle',
            'play', 'true',
            'loop', 'true',
            'scale', 'showall',
            'wmode', 'transparent',
            'devicefont', 'false',
            'id', id,
            'name', id,
            'menu', 'false',
            'allowFullScreen', 'false',
            'allowScriptAccess','sameDomain',
            'movie', siteBase + '/flash/' + file + '?' + query,
            'salign', ''
        );
    } else {
        return AC_FL_RunScriptContent('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
            'width', width,
            'height', height,
            'src', siteBase + '/flash/' + file + '?' + query,
            'quality', 'high',
            'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
            'align', 'middle',
            'play', 'true',
            'loop', 'true',
            'scale', 'showall',
            'wmode', 'transparent',
            'devicefont', 'false',
            'id', id,
            'name', id,
            'menu', 'false',
            'allowFullScreen', 'false',
            'allowScriptAccess','sameDomain',
            'movie', siteBase + '/flash/' + file + '?' + query,
            'salign', ''
        );
    }
}

/*
 * Flash message 1.2
 * By Marius van Witzenburg, http://www.kitara.nl
 */
sessionFlash={
    show:function(time) {
        $('sessionFlashMessage').visualEffect('appear', {afterFinish:function() { $('sessionFlashMessage').visualEffect('shake', {distance: 3, duration:0.2}) }});
        this.timer = setTimeout('sessionFlash.hide()', time + '000');
    },
    hide:function() {
        $('sessionFlashMessage').visualEffect('fade');
        clearTimeout(this.timer);
        return false;
    }
};

/*
 * Popup 1.9
 * By Marius van Witzenburg, http://www.kitara.nl
 * Opens an url in new window and center it by default
 * Usage:
 * <a href="path/to/page" onclick="return Popup.open({url:this.href});">link title</a>
 */
var Popup = {
    open: function(options) {
        this.options = {
            url: '#',
            width: 600,
            height: 450,
            name:"_blank",
            location:"no",
            menubar:"no",
            toolbar:"no",
            status:"yes",
            scrollbars:"yes",
            resizable:"yes",
            left:"",
            top:"",
            normal:false
        }
        Object.extend(this.options, options || {});

        if (this.options.normal){
            this.options.menubar = "yes";
            this.options.status = "yes";
            this.options.toolbar = "yes";
            this.options.location = "yes";
        }

        this.options.width = (this.options.width < screen.availWidth) ? this.options.width : screen.availWidth;
        this.options.height = (this.options.height < screen.availHeight) ? this.options.height : screen.availHeight;
        var openoptions = 'width=' + this.options.width + ',height=' + this.options.height + ',location=' + this.options.location + ',menubar=' + this.options.menubar + ',toolbar=' + this.options.toolbar + ',scrollbars=' + this.options.scrollbars + ',resizable=' + this.options.resizable + ',status=' + this.options.status

        if (this.options.top != "") {
            openoptions += ",top=" + this.options.top;
        } else {
            openoptions += ",top=" + parseInt((parseInt(screen.height) / 2) - (parseInt(this.options.height) / 2));
        }
        if (this.options.left != "") {
            openoptions += ",left=" + this.options.left;
        } else {
            openoptions += ",left=" + parseInt((parseInt(screen.width) / 2) - (parseInt(this.options.width) / 2));
        }
        window.open(this.options.url, this.options.name,openoptions );
        return false;
    }
}



