/**
 * ae.js
 *
 * for www.accesseng.com
 * Author: Rendahl Weishar
 * 6/2009
 *
 */

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
var is_ie = false;
if (browser == "Microsoft Internet Explorer") {
    is_ie = true;
}

// Load the images for the the changing traffic signal
signal_image_srcs = new Array();
signal_image_srcs[0] = "art/traffic_light_green_base_l.png";
signal_image_srcs[1] = "art/traffic_light_yellow_base_l.png";
signal_image_srcs[2] = "art/traffic_light_red_base_l.png";

rot_img_ids = new Array();
rot_img_ids[0] = id="id_env_eng_img2";
rot_img_ids[1] = id="id_env_eng_img1";
rot_img_ids[2] = id="id_env_eng_img0";
var rot_img_id_idx = 0;

// used for alternating animated fading text
text_anim_ids = new Array();
text_anim_ids[0] = "tran_plan";
text_anim_ids[1] = "traff_eng";
text_anim_ids[2] = "acc_man";
text_anim_ids[3] = "ill_des";
var text_anim_idx = 0;

/*
var now = new Date();
var seconds = now.getSeconds();
var sig_img_idx = 1;
var oldEvt = window.onload; window.onload = function() { if (oldEvt) oldEvt(); trafficSignal(); }
function trafficSignal() {
    document.getElementById("traffic_signal").src = signal_image_srcs[sig_img_idx++];
    setTimeout(trafficSignal,3000);
    if(sig_img_idx > 2) {
        sig_img_idx = 0;
    }
}
*/
var date = new Date();
//window.onload = fade(1.0,"id_env_eng_img");
//fade(1,"id_env_eng_img");
function fade(op, what) {
    if(op <= 0) {
        // Stay on the image, then callback.
        document.getElementById(rot_img_ids[((rot_img_id_idx+1)%3)]).style.zIndex = 2
        document.getElementById(rot_img_ids[((rot_img_id_idx+2)%3)]).style.zIndex = 1;
        document.getElementById(rot_img_ids[rot_img_id_idx]).style.zIndex = 0;
        document.getElementById(rot_img_ids[rot_img_id_idx]).style.opacity = 1;
        rot_img_id_idx++;
        //alert("calling imageFX in 2 seconds");
        setTimeout(imageFX, 2000);
    }
    else {
        document.getElementById(what).style.opacity = op;
        var myFun = function() { fade((op-0.05),what); };
        //fade((op-0.05), what);
        setTimeout(myFun, 200);
        //fade((op-0.05), what);
    }
}

function fadeIn(what, op) {
    if(is_ie) {
        if((op*100) >= 100) {
            var myFun = function() { fadeOut(what, 1); };
            setTimeout(myFun, 1000);

            return;
        }
    }
    if(op >= 1) {
        //var callback = function() { fadeCallback("done fadding out"); };
        var myFun = function() { fadeOut(what, 1); };
        setTimeout(myFun, 1000);

        return;
    }
    else {
        if(is_ie) {
            document.getElementById(what).filters.alpha.opacity = (op*100);
        } else {
            document.getElementById(what).style.opacity = op;
        }
        var myFun = function() { fadeIn(what,(op+0.05)); };
        setTimeout(myFun, 200);
    }
}
function fadeOut(what, op) {
    if(is_ie) {
        if((op*100) <= 0) {
            document.getElementById(what).filters.alpha.opacity = 0;
            return;
        }
    }
    if(op <= 0) {
        // make sure we end on 0 opacity
        document.getElementById(what).style.opacity = 0;
        return;
    }
    else {
        if(is_ie) {
            document.getElementById(what).filters.alpha.opacity = (op*100);
        } else {
            document.getElementById(what).style.opacity = op;
        }
        var myFun = function() { fadeOut(what,(op-0.05)); };
        setTimeout(myFun, 200);
    }
}
/**
 * image fades
 */

function imageFX() {
    if(rot_img_id_idx > 2) {
        rot_img_id_idx = 0;
    }
    fade(1, rot_img_ids[rot_img_id_idx])

}
function animationController() {
    fadeIn(text_anim_ids[text_anim_idx], 0);
    animateX(text_anim_ids[text_anim_idx], 120, 300);
}
var left_val = 120;
function animateX(what, x_val, max_x) {
    if(x_val > max_x) {
        document.getElementById(what).style.left = "120px";
        // ensure that it disappears
        document.getElementById(what).style.opacity = 0;
        text_anim_idx++;
        if(text_anim_idx >= text_anim_ids.length) {
            text_anim_idx = 0;
        }
        animationController();
        // do we ever get here? YES
        return;
    }
    else {
        document.getElementById(what).style.left = x_val+"px";
        var spacing = ((x_val-left_val)/140);

        if(spacing > 1) {
            document.getElementById(what).style.letterSpacing = spacing+"px";
        }

        var myFun = function() { animateX(what,(x_val+1),max_x); };
        setTimeout(myFun, 50);
    }
}
