/*
   Filename: quiz_tools.js
   Author:   Falko Zander
   Created:  2007-06-12
   Update:   2007-09-12

   Abstract: This file contains all necessary JavaScript for Quizzes implementation.

   Notes:

*/

String.prototype.reverse = function() {
    return this.split("").reverse().join("");
}
function Numsort(a, b) {
    return parseInt(a) - parseInt(b);
}
function get_max_dimensions(id_prefix, count) {
    var cur = { max_width: 0, max_height: 0, max_width_idx: -1, max_height_idx: -1 };
    for (var i = count - 1; i >= 0; i--) {
        var e = document.getElementById(id_prefix + i);
        if (e.offsetWidth > cur.max_width) {
            cur.max_width = e.offsetWidth;
            cur.max_width_idx = i;
        }
        if (e.offsetHeight > cur.max_height) {
            cur.max_height = e.offsetHeight;
            cur.max_height_idx = i;
        }
    }
    return cur;
}
Array.prototype.mix = function() {
    if (this.length < 2) return;
    for (var j = this.length - 1; j > 0; j--) {
        var r = Math.floor(Math.random() * (j + 1));
        var t = this[j];
        this[j] = this[r];
        this[r] = t;
    }
}
Array.prototype.copy = function() {
    var new_array = [];
    for (var i = 0; i < this.length; i++) {
        new_array[i] = this[i];
    }
    return new_array;
}
function getAbsoluteDimensions(elem) {
    var r = [];

    r.offsetWidth = elem.offsetWidth;
    r.offsetHeight = elem.offsetHeight;
    r.offsetLeft = elem.style.pixelLeft || elem.offsetLeft;
    r.offsetTop = elem.style.pixelTop || elem.offsetTop;
    r.rel_offsetLeft = r.offsetLeft;
    r.rel_offsetTop = r.offsetTop;
    r.parentsLeft = 0;
    r.parentsTop = 0;

    var par = elem.offsetParent;
    while (par) {
        r.offsetLeft += par.offsetLeft;
        r.offsetTop += par.offsetTop;
        r.parentsLeft += par.offsetLeft;
        r.parentsTop += par.offsetTop;
        par = par.offsetParent;
    }
    return r;
}
function intervalOverlapping(i1_lower, i1_upper, i2_lower, i2_upper) {
    var result = [];
    var r = [];

    if (i1_lower <= i2_lower && i2_lower <= i1_upper) {
        if (i2_upper <= i1_upper) {
            result.push(r);
            r.lower = i2_lower;
            r.upper = i2_upper;
            return result;
        } else {
            result.push(r);
            r.lower = i2_lower;
            r.upper = i1_upper;
            return result;
        }
    }

    if (i2_lower <= i1_lower && i1_lower <= i2_upper) {
        if (i1_upper <= i2_upper) {
            result.push(r);
            r.lower = i1_lower;
            r.upper = i1_upper;
            return result;
        } else {
            result.push(r);
            r.lower = i1_lower;
            r.upper = i2_upper;
            return result;
        }
    }

    return result;
}
function rectOverlapping(rect1, rect2) {
    if (rect1 == null || rect2 == null) return -1;

    var r1 = getAbsoluteDimensions(rect1);
    var r2 = getAbsoluteDimensions(rect2);

    var r1_x1 = r1.offsetLeft;
    var r1_y1 = r1.offsetTop;
    var r1_x2 = r1.offsetWidth + r1_x1;
    var r1_y2 = r1.offsetHeight + r1_y1;
    var r2_x1 = r2.offsetLeft;
    var r2_y1 = r2.offsetTop;
    var r2_x2 = r2.offsetWidth + r2_x1;
    var r2_y2 = r2.offsetHeight + r2_y1;

    var res1 = [];
    res1 = intervalOverlapping(r1_x1, r1_x2, r2_x1, r2_x2);
    if (res1.length == 0) return -1;
    var res2 = [];
    res2 = intervalOverlapping(r1_y1, r1_y2, r2_y1, r2_y2);
    if (res2.length == 0) return -1;

    return ((res1[0].upper - res1[0].lower) * (res2[0].upper - res2[0].lower));
}
function convert_time(time_in_seconds) {
    var hours = Math.floor(time_in_seconds / 3600);
    var x = time_in_seconds - hours * 3600;
    var minutes = Math.floor(x / 60);
    var seconds = x - minutes * 60;
    if (hours.toString().length < 2) hours = "0" + hours;
    if (minutes.toString().length < 2) minutes = "0" + minutes;
    if (seconds.toString().length < 2) seconds = "0" + seconds;
    return hours + ":" + minutes + ":" + seconds;
}
function getFlashMovieObject(movieName)
{
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet")==-1) {
        if (document.embeds && document.embeds[movieName]) {
            return document.embeds[movieName];
        }
    } else /* if (navigator.appName.indexOf("Microsoft Internet")!=-1) */ {
        return document.getElementById(movieName);
    }
}
function wait_for_img_load(img_id, cb_fun) {
    id = img_id;
    func  = cb_fun;
    var fun = function() {
        if (document.getElementById(id).complete == true) {
            wait_for_img_load2();
        }
    }
    iv = window.setInterval(fun, 100);
}
function wait_for_img_load2() {
    window.clearInterval(iv);
    eval(func + "();");
}

/*
   Copyright Notice:

   Copyright 2007, datango AG
   ALL RIGHTS RESERVED

   UNPUBLISHED -- Use of a copyright notice is precautionary only and
   does not imply publication or disclosure.

   THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
   INFORMATION OF DATANGO AG. ANY DUPLICATION, MODIFICATION,
   DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS
   STRICTLY PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF
   DATANGO AG.

*/
