﻿/* ANIMATION AND GENERAL TOOLS JS FILE
 * CREATED: 28 Oct 2008
 * AUTHOR: Jako van Rensburg
 */

var animate = {
    /* ANIMATION SECTION
     * CREATED: 10 SEPT 2008
     * AUTHOR: Jako van Rensburg
     */

    /* FADE OBJECT FROM OPACITY SPECIFIED --> OPACITY SPECIFIED */
    fadeObject: function(obj, from, to, speed, oncomplete){
        $d('JS Function', 'Fading Object: '+obj.id);
        var totalFadeTimer = 0;
        if (from > to) {
            for (i = from; i > to; i--) {
                totalFadeTimer += 1;
                setTimeout('animate._setOpacity("' + obj.id + '", ' + (i * 0.01) + ')', (totalFadeTimer * speed));
            }
        }
        else {
            animate._setOpacity(obj.id, 0);
            for (i = from; i < to; i++) {
                totalFadeTimer += 1;
                setTimeout('animate._setOpacity("' + obj.id + '", ' + (i * 0.01) + ')', (totalFadeTimer * speed));
            }
            setTimeout('animate._removeOpacity("' + obj.id + '")', (totalFadeTimer * speed));
        }
        if (oncomplete) {
            setTimeout(oncomplete, (totalFadeTimer * speed));
        }
    },

    /* SET OBJECT OPACITY */
    _setOpacity: function(objID, opacity){
        $(objID).style.opacity = opacity;
        try {
            $(objID).style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
        }
        catch(e){}
    },

    /* RESET THE OPACITY IF FADE IN OCCURES
     * FIXES WEIRD LOOKING TEXT BUG
     */
    _removeOpacity: function(objID){
        $(objID).style.opacity = '';
        if($(objID).style.removeAttribute) {
            $(objID).style.removeAttribute('filter');
        }
    },

    busySliding: false,
    /* SMOOTH RESIZE HEIGHT */
    smoothResizeHeight: function(objID, heightTo, speed) {
        var currentHeight = 0;
        var i = 0;
        var totalSpeedTimer = 0;
        currentHeight = $(objID).offsetHeight;
        if (currentHeight == 0) {
            currentHeight = $(objID).style.height.toString().replace('px', '');
        }
        $d('JS Function', 'smoothResizeHeight: objID - ' + objID + '<br />currentHeight - ' + currentHeight + '<br />' + 'heightTo - ' + heightTo);
        //IF DIFF BETWEEN HEIGHTS BIGGER THAN 1, RESIZE, ELSE DONT
        if (currentHeight != heightTo && currentHeight != (heightTo + 1) && currentHeight != (heightTo - 1)) {
            if (currentHeight > heightTo) {
                for (i = currentHeight; i > heightTo; i --) {
                    totalSpeedTimer += 1;
                    setTimeout('animate._setHeight("' + objID + '", ' + i + ');', (totalSpeedTimer * speed));
                }
                setTimeout('animate._setHeight("' + objID + '", ' + i + ');', (totalSpeedTimer * speed));
            }
            else {
                for (i = 0; i < heightTo; i ++) {
                    totalSpeedTimer += 1;
                    setTimeout('animate._setHeight("' + objID + '", ' + i + ');', (totalSpeedTimer * speed));
                }
                setTimeout('animate._setHeight("' + objID + '", ' + i + ');', (totalSpeedTimer * speed));
            }
        }
    },
    _setHeight: function(objID, heightTo) {
        $(objID).style.height = heightTo + 'px';
    }
};

/* TOOLS 
 ****************************************************************/
 
/* WEB SERVICE TIMER */
var timer = {
    time: 0,
    timerObj: {},

    /* START TIMER */
    start: function() {
        timer.time = 0;
        timer.timerObj = setTimeout(function(){timer.tick();},10);
    },

    /* TICK TIMER */
    tick: function() {
        timer.time += 10;
        timer.timerObj = setTimeout(function(){timer.tick();},10);
    },

    /* STOP TIMER */
    stop: function() {
        $hd('responseTimeTD','Response time: ' + (timer.time/1000) + 's');
        clearTimeout(timer.timerObj);
        timer.time = 0;
    }
};

/* DETECT PLUGINS */
var plugins = {
    detect: function(){
        var plugins = '';
        for (var prop = 0; prop < window.navigator.plugins.length; prop++) {
            plugins += 'Name&nbsp;=&nbsp;' + window.navigator.plugins[prop].name + '<br />' + 'Description&nbsp;=&nbsp;' + window.navigator.plugins[prop].description + '<br />' + 'File Name&nbsp;=&nbsp;' + window.navigator.plugins[prop].filename + '<br /><br />';
        }
        $d('Browser Plugins Detected', plugins);
    }
};

var date = {
    /* RETURN CURRENT TIME */
    getTime: function() {
        var dateNow = new Date();
        var hour = 0;
        hour = dateNow.getHours();
        var min = 0;
        min = dateNow.getMinutes();
        var sec = 0;
        sec = dateNow.getSeconds();
        if (hour < 10) {
            hour = '0' + hour;
        }
        if (min < 10) {
            min = '0' + min;
        }
        if (sec < 10) {
            sec = '0' + sec;
        }
        return hour + ':' + min + ':' + sec;
    }
};
