document.write(unescape("%3Cscript src=\"/script/flash_writer.js\" type=\"text/javascript\"%3E%3C/script%3E"));
//This file creates and displays the National Ad Video Player
//New Multi-Dimensional Array to store all current videos and accompanying information (preview image and title)
var mainArray = new Array();
var count = 0;

//Each array item contains the complete information for one video. Array items can be added and removed as needed.
mainArray[count++] = new Array("OldWoman_60_RAYJ1001H.flv", "OldWoman.jpg", "Emily Skinner");
mainArray[count++] = new Array("TheTree_60_RAYJ1004H.flv", "TheTree.jpg", "Magical Fruit");

var vc; //videoContainer div
var lc; //linkContainer div
//t and t2 are setTimeout variables, strImgs temporarily store each video preview image, strLinks temporarily store video links
var t, t2, strImgs = "", strLinks = "", title1 = "", width, height, playercolor, videobgcolor, playerBGColor, borderColorTop, borderColorBottom; 
var argArray; //array of arguments for the main function arguments - checks each argument against the mainArray video filenames

var serverPath, server_name, fullURL;
server_name = location.href.toLowerCase();

switch (true) {
    case server_name.indexOf("public1xa.rjf.com") != -1:
        serverPath = "http://public1xa.rjf.com";
        break;
    case server_name.indexOf("public1xc.rjf.com") != -1:
        serverPath = "http://public1xc.rjf.com";
        break;
    case server_name.indexOf("public1xw.rjf.com") != -1:
        serverPath = "http://public1xw.rjf.com";
        break;
    case server_name.indexOf("raymondjames.com") != -1:
        serverPath = "http://www.raymondjames.com";
        break;
    default:
        serverPath = "http://www.raymondjames.com";
        break;
}

fullURL = serverPath + "/Branches/library/National_Ads/";

//main controlling function. Sets main vars, checks filename args against mainArray vid filenames, creates preview images and links for each existing vid, sets styles for static elements, starts preview image slideshow
function main(vid1, vid2, vid3, vid4, w, h, pc, tbg, pbg, bct, bcb) {
    if (flashinstalled == 2 && flashversion > 7) {
        var boolVideoDiv = document.getElementById("videoContainer");
        var boolLinkDiv = document.getElementById("linkContainer");

        //create container elements
        if (!boolVideoDiv) {
            document.write("<div id=\"videoContainer\"></div>");
        }

        if (!boolLinkDiv) {
            document.write("<div id=\"linkContainer\"></div>");
        }

        //set number of vids to load per arguments, assign arguments to array
        if (!isNaN(w)) {
            width = w;
        }
        else {
            width = 320;
        }
        if (!isNaN(h)) {
            height = h;
        }
        else {
            height = 240;
        }
        if (checkHex(pc)) {
            playercolor = pc;
        }
        else {
            playercolor = "ff0000";
        }
        if (checkHex(tbg)) {
            videobgcolor = tbg;
        }
        else {
            videobgcolor = "000000";
        }
        if (checkHex(pbg)) {
            playerBGColor = pbg;
        }
        else {
            playerBGColor = "ffffff";
        }
        if (checkHex(bct)) {
            borderColorTop = bct;
        }
        else {
            borderColorTop = "7c7c7c";
        }
        if (checkHex(bcb)) {
            borderColorBottom = bcb;
        }
        else {
            borderColorBottom = "333333";
        }
        argArray = new Array();
        vc = document.getElementById("videoContainer");
        lc = document.getElementById("linkContainer");

        //fill argArray with existing arguments
        for (var i = 0; i < arguments.length; i++) {
            if (arguments[i] != "") {
                argArray.push(arguments[i]);
            }
        }

        //check if there are any vids in argArray and if they exist in mainArray - if any exist, the process starts to build the player, if not, nothing displays
        if (argArray.length > 0 && checkVidsExist()) {
            startIntro();
            setStyles();
            if (argArray.length > 1) {
                so_init();
            }
            else {
                document.getElementById("rotator").style.display = "block";
            }
        }
        else {
            vc.style.display = "none";
            lc.style.display = "none";
        }
    }
    else {

    }
}

//Validates hexadecimal color value for flash video player
function checkHex(value) {
    var exp = /^(#)?([0-9a-fA-F]{3})([0-9a-fA-F]{3})?$/;
    if (exp.test(value)) {
        return true;
    }
    else {
        return false;
    }
}

//check if vids in argArray exist in vidArray and remove ones that do not
function checkVidsExist() {

    var count = 0, numVids = 0, check = new Array(), checkCount = 0; //check is a temp array to store any existing vids, and purge the array of non-existing vids and non-filename arguments

    for (var i = 0; i < argArray.length; i++) {

        for (var x = 0; x < mainArray.length; x++) {
            if (argArray[i] == mainArray[x][0]) {
                //create video preview images
                strImgs += createImgTags(mainArray[x][1]);
                //create video links
                strLinks += createLinks(mainArray[x][0], mainArray[x][2]);
                if (numVids == 0) {
                    title1 = mainArray[x][2];
                }
                numVids++;
                //populate temp check array
                check.push(argArray[i]);
            }
        }
    }
    //set argArray equal to temp check array of filenames 
    argArray = check;

    //if any filenames exist, return true starts the intro process, if not, the script ends and displays nothing
    if (argArray.length > 0) {
        return true;
    }
    else {
        return false;
    }
}

//build div and img tags for rotating vid stills, append to videoContainer & linkContainer divs
function startIntro() {
    var vid = argArray[0], count = 0;
    var playButton = document.createElement("div");
    playButton.setAttribute("id", "playButton");
    playButton.innerHTML = "<a href='javascript:void(0);' onclick='changeVid(\"" + vid + "\", \"" + title1 + "\")'><img src='" + fullURL + "images/play.png' alt='' border='0' /></a>";
    var rotator = document.createElement("div");
    rotator.setAttribute("id", "rotator");
    rotator.innerHTML = strImgs;
    vc.appendChild(rotator);
    vc.appendChild(playButton);
    lc.innerHTML = strLinks;
}

//passing image filename from startIntro function to create preview images. set width and height of images according to main function arguments
function createImgTags(file){
    var str = "<img src='" + fullURL + "images/" + file + "' alt='' style='width:" + width + "px; height:" + height + "px;' />";
    return str;
}

//passing video filename and title from startIntro function to create video links. current and notCurrent classes determine which link is highlighted
var linkCount = 0;
function createLinks(vid, title) {
    var str = "";
    if (linkCount == 0) {
        str = "View Commercial: <a href='javascript:void(0);' onclick=\"changeVid('" + vid + "', '" + title + "');\" class=\"current\">" + title + "</a><br />";
    }
    else{
        str = "View Commercial: <a href='javascript:void(0);' onclick=\"changeVid('" + vid + "', '" + title + "');\" class=\"notCurrent\">" + title + "</a><br />";
    }
    linkCount++;
    return str;
}

//on click event to stop slideshow timeout and create and start videos
function changeVid(filename, title) {
    //stop scroll
    vc.innerHTML = "";
    clearTimeout(t);
    clearTimeout(t2);
    if (filename == "" || filename == null || filename == undefined) {
        filename = mainArray[0][0];
    }

    var ran = Math.floor(Math.random() * 1000000);

    var writeVid = "<object id=\"Object1\" type=\"application/x-shockwave-flash\" data=\"" + fullURL + "videos/player_flv_maxi.swf?id=" + ran + "\" width=\"" + width + "\" height=\"" + height + "\">" +
                   "<param name=\"movie\" value=\"" + fullURL + "videos/player_flv_maxi.swf?id=" + ran + "\" />" +
                   "<param name=\"allowScriptAccess\" value=\"sameDomain\" />" +
                   "<param name=\"allowFullScreen\" value=\"true\" /> " +
                   "<param name=\"FlashVars\" value=\"flv=" + fullURL + "videos/" + filename + "&amp;autoplay=1&amp;autoload=1&amp;width=" + width + "&amp;height=" + height +
                   "&amp;showstop=0&amp;showvolume=1&amp;showtime=1&amp;showplayer=always&amp;showfullscreen=1&amp;playeralpha=100&amp;playercolor=" + playercolor + "&amp;videobgcolor=" + videobgcolor + 
                   "&amp;bgcolor=" + playerBGColor + "&amp;bgcolor1=" + borderColorTop + "&amp;bgcolor2=" + borderColorBottom + "\" /></object>";
    vc.innerHTML = writeVid;
}

//sets size of rotator, vidContainer, and linkContainer elements. position rotator and playbutton elements
function setStyles() {
    var posY = ((height / 2) - 25), posX = ((width / 2) - 25);
    var button = document.getElementById("playButton");
    var r = document.getElementById("rotator");
    vc.style.width = width;
    vc.style.height = height;
    vc.style.position = "relative";
    r.style.position = "absolute";
    r.style.top = "0px";
    r.style.left = "0px";
    button.style.position = "absolute";
    button.style.width = "50px";
    button.style.height = "50px";
    button.style.top = posY;
    button.style.left = posX;
    button.style.zIndex = "100";
    button.style.margin = "0px";
    button.style.padding = "0px";
}

//code for fading images
var d = document, imgs = new Array(), zInterval = null, current = 0, pause = false;

function so_init() {
    if (!d.getElementById || !d.createElement) return;
    var css = d.createElement('link');
    css.setAttribute('href', fullURL + 'styles/slideshow2.css');
    css.setAttribute('rel', 'stylesheet');
    css.setAttribute('type', 'text/css');
    d.getElementsByTagName('head')[0].appendChild(css);

    imgs = d.getElementById('rotator').getElementsByTagName('img');
    for (i = 1; i < imgs.length; i++) imgs[i].xOpacity = 0;
    imgs[0].style.display = 'block';
    imgs[0].xOpacity = .99;

    t = setTimeout(so_xfade, 4000);
}

function so_xfade() {
    cOpacity = imgs[current].xOpacity;
    nIndex = imgs[current + 1] ? current + 1 : 0;
    nOpacity = imgs[nIndex].xOpacity;

    cOpacity -= .05;
    nOpacity += .05;

    imgs[nIndex].style.display = 'block';
    imgs[current].xOpacity = cOpacity;
    imgs[nIndex].xOpacity = nOpacity;

    setOpacity(imgs[current]);
    setOpacity(imgs[nIndex]);

    if (cOpacity <= 0) {
        imgs[current].style.display = 'none';
        current = nIndex;
        t2 = setTimeout(so_xfade, 4000);
        var a = document.getElementById("linkContainer").getElementsByTagName("a");
        //changes color of video links according to which preview image is displaying
        for(var i = 0;i<a.length;i++){
            if (i == current) {
                a[i].className = 'current';
                document.getElementById("playButton").innerHTML = "<a href='javascript:void(0);' onclick='changeVid(\"" + mainArray[i][0] + "\", \"" + mainArray[i][2] + "\")'><img src='" + fullURL + "images//play.png' alt='' border='0' /></a>";
            }
            else {
                a[i].className = 'notCurrent';
            }
        }
    }
    else {
        t2 = setTimeout(so_xfade, 50);
    }

    function setOpacity(obj) {
        if (obj.xOpacity > .99) {
            obj.xOpacity = .99;
            return;
        }

        obj.style.opacity = obj.xOpacity;
        obj.style.MozOpacity = obj.xOpacity;
        obj.style.filter = 'alpha(opacity=' + (obj.xOpacity * 100) + ')';
    }
}
