var patientVideos = new Array();

patientVideos[patientVideos.length] = {
    actorName: "millie",
    icon: "/web/img/videos/millie/millie_icon.jpg",
    videoURL: "/web/flash/videos/millie/motivation_to_test.flv",
    imgURL: "/web/img/videos/millie/pre_test_counseling.jpg",
    titleML: "<h3>Pre-Test Counseling:</h3>Millie Discusses Testing with a Genetic Counselor",
    titlePos: "bottom",
    topic: 'Is Hemochromatosis<br>Hereditary?',
    moreVideoUrls: ['What was counseling like?|/web/flash/videos/millie/pre_test_counseling.flv',
                    'What were my concerns?|/web/flash/videos/millie/concerns_about_testing.flv',
                    'My doctor\'s reaction|/web/flash/videos/millie/doctor_reacts_to_testing.flv',
                    'Did testing change my life?|/web/flash/videos/millie/the_power_of_knowing.flv',
                    'Would I recommend testing?|/web/flash/videos/millie/glad_she_tested.flv'

                    ]
};

patientVideos[patientVideos.length] = {
    actorName: "lisa",
    icon: "/web/img/videos/lisa/lisa_icon.jpg",
    videoURL: "/web/flash/videos/lisa/decides_to_test.flv",
    imgURL: "/web/img/videos/lisa/decides_to_test.jpg",
    titleML: "<h3>Making the Decision:</h3>Lisa Decides to Test for Breast Cancer Risk",
    titlePos: "top",
    topic: 'At risk for Breast &<br>Ovarian Cancer?',
    moreVideoUrls: ['How did testing work?|/web/flash/videos/lisa/testing_process.flv',
                    'What did testing tell me?|/web/flash/videos/lisa/learns_test_results.flv',
                    'What did my doctor think?|/web/flash/videos/lisa/doctors_respond_to_results.flv',
                    'Did testing change my healthcare?|/web/flash/videos/lisa/chooses_preventive_surgery.flv',
                    'What testing did for me|/web/flash/videos/lisa/genetic_testing_saved_life.flv'

                    ]
};
/*
patientVideos[patientVideos.length] = {
    actorName: "Ken Graham",
    icon: "/web/img/videos/elcamino/k_graham_icon.jpg",
    videoURL: "/web/flash/videos/elcamino/k_graham_GMI.flv",
    imgURL: "/web/img/videos/elcamino/k_graham_BG.jpg",
    titleML: "<h3>El Camino Hospital&#153;:</h3> The Genomic Medicine Institute",
    titlePos: "bottom",
    topic: 'El Camino Hospital CEO',
    moreVideoUrls: []
};
*/
patientVideos[patientVideos.length] = {
    actorName: "Brad Popovich",
    icon: "/web/img/videos/brad/brad_icon.jpg",
    videoURL: "/web/flash/videos/brad/homepage.flv",
    imgURL: "/web/flash/videos/brad/board_still.jpg",
    titleML: "<h3>Genetic Medicine:</h3>",
    titlePos: "bottom",
    topic: 'DNA Direct Board Member',
    moreVideoUrls: []
};
patientVideos[patientVideos.length] = {
    actorName: "dee",
    icon: "/web/img/videos/dee/dee_icon.jpg",
    videoURL: "/web/flash/videos/dee/decides_to_preserve.flv",
    imgURL: "/web/img/videos/dee/decides_to_preserve.jpg",
    titleML: "<h3>Choosing DNA Archive&#153;:</h3>dee Decides to Preserve Her Mother&apos;s DNA",
    titlePos: "bottom",
    topic: 'Preserves Parent\'s DNA',
    moreVideoUrls: ['My family history|/web/flash/videos/dee/dont_know_all_history.flv',
                    'Why did I store my mom\'s DNA?|/web/flash/videos/dee/mom_brca_positive.flv',
                    'How did the process work?|/web/flash/videos/dee/using_dna_archive_kit.flv',
                    'What my mom would think|/web/flash/videos/dee/mom_would_be_proud.flv',
                    'Would I recommend it?|/web/flash/videos/dee/mom_would_be_alive.flv'
                    ]
};

patientVideos[patientVideos.length] = {
    actorName: "Sharon Terry",
    icon: "/web/img/videos/sharon/sharon_icon.jpg",
    videoURL: "/web/flash/videos/sharon/integrating_genetics640x360.flv",
    imgURL: "/web/img/videos/sharon/integrating_genetics.jpg",
    titleML: "<h3>Integrating Genetics Into Medicine:</h3>",
    titlePos: "bottom",
    topic: 'President & CEO, Genetic Alliance',
    moreVideoUrls: []
};

patientVideos[patientVideos.length] = {
    actorName: "Ryan Phelan",
    icon: "/web/img/videos/ryan/ryan_icon.jpg",
    videoURL: "/web/flash/videos/ryan/personalized_medicine_with_title.flv",
    imgURL: "/web/img/videos/ryan/still.jpg",
    titleML: "<h3>Why I started DNA Direct</h3>",
    titlePos: "bottom",
    topic: 'President, DNA Direct',
    moreVideoUrls: []
};

patientVideos[patientVideos.length] = {
    actorName: "Trish Brown",
    icon: "/web/img/videos/trish/trish_icon.jpg",
    videoURL: "/web/flash/videos/trish/power_of_genetics.flv",
    imgURL: "/web/img/videos/trish/still.jpg",
    titleML: "<h3>Using Genetics in Medicine:</h3>",
    titlePos: "bottom",
    topic: 'Vice President of Clinical Affairs',
    moreVideoUrls: []
};

var currVideos = new Array();

function chooseVideos() {
    //eventually add random choice
    currVideos.push(patientVideos[5]);
    currVideos.push(patientVideos[4]);
    currVideos.push(patientVideos[6]);
    currVideos.push(patientVideos[1]);
    currVideos.push(patientVideos[3]);
    currVideos.push(patientVideos[0]);
    currVideos.push(patientVideos[2]);

}

function initFirstVideo() {
    preparePlayer(0, false);
    removeFlashWinOnclick();
}

function directClickPlay() {
    preparePlayer(0, true);
    removeFlashWinOnclick();
}

function removeFlashWinOnclick() {
    if (document.getElementById('flash')) {
        document.getElementById('flash').onclick = null;
    }
}

function populateVideoLinks(whichVideo) {

    chooseVideos();

    if (whichVideo == 0 || whichVideo != '') {

        var startAt = 0;
        var endAt = 0;

        if (whichVideo == 'all') {
            endAt = 3;
            writeVideoLinks(startAt, endAt);
        } else {
            startAt = whichVideo;
            endAt = whichVideo + 1;
            writeVideoLinks(startAt, endAt);
            writeMoreVideoLinks(whichVideo);
        }
        
    }

}

function writeVideoLinks(startAt, endAt) {
    var vidNavML = '';
    for (var i=startAt;i<endAt;i++) {
        vidNavML += '<a id="vid_link_' + i + '" href="javascript:preparePlayer('+ i +', true);">' +
                           '<img src="' + currVideos[i].icon + '" alt="' + currVideos[i].actorName + '">' +
                           '<span class="actorName">' + currVideos[i].actorName.substring(0,1).toUpperCase() + currVideos[i].actorName.substring(1) + '</span>' +
                           '<span class="vidTopic">' + currVideos[i].topic + '</span>' +
                    '</a>';
    }
    document.getElementById('video_nav_links').innerHTML = vidNavML;
}

function writeMoreVideoLinks(which) {
    var moreVidsML = '';

    var moreVidsTitleDiv = '<div class="more_vids_title">Listen to ' + currVideos[which].actorName.substring(0,1).toUpperCase() + currVideos[which].actorName.substring(1) + '\'s story</div>';

    var moreVidsArray = currVideos[which].moreVideoUrls;

    for (var i=0;i<moreVidsArray.length;i++) {
        vidDataBits = moreVidsArray[i].split('|');
        moreVidsML += '<a id="more_vid_link_' + i + '" href="javascript:playOtherVideo(' + which + ', \'' + vidDataBits[1]  + '\', ' + i + ')" class="text_vid_link" href="">' + vidDataBits[0] + '</a>';    
    }


    document.getElementById('video_nav_links').innerHTML += moreVidsTitleDiv + moreVidsML;

}

function showVideoTitle(videoIndex) {
    doHide = false;
    document.getElementById('video_title').style.display = 'none';
    document.getElementById('preview_video_titleML').innerHTML = currVideos[videoIndex].titleML;
    document.getElementById('preview_video_title').style.top = (videoIndex*50) + (videoIndex==2?10:(videoIndex==3?15:0)) + 'px';
    document.getElementById('preview_video_title').style.display = 'block';
}

var doHide = true;

function callHideVideoTitle() {
    doHide = true;
    setTimeout('hideVideoTitle()', 70);
}

function hideVideoTitle(force) {
    if (document.getElementById('video_titleML') && (doHide || force)) {
        if ( document.getElementById('video_titleML').innerHTML != '' && !isPlaying) {
             document.getElementById('video_title').style.display = 'block';
        }
        document.getElementById('preview_video_title').style.display = 'none';
    }
}

var on_link_index = -1;

var pathsArray = new Array();
	pathsArray[pathsArray.length] = 'testing-for-genetic-disorders/breast-and-ovarian-cancer-risk/15/who-should-consider-testing';
	pathsArray[pathsArray.length] = 'dna-storage/dna-archive-home-storage/382/who-should-consider-dna-archive';
    pathsArray[pathsArray.length] = 'testing-for-genetic-disorders/breast-and-ovarian-cancer-risk/15/who-should-consider-testing';

function preparePlayer(videoIndex, autostart) {
    isPlaying = false;
    hideVideoTitle(true);
    if (document.getElementById('vid_link_' + on_link_index)) {
        document.getElementById('vid_link_' + on_link_index).className = '';
        on_link_index = videoIndex;
    } else {
        on_link_index = videoIndex;
    }
    document.getElementById('vid_link_' + videoIndex).className = 'vidSelected';
    document.getElementById('player').innerHTML = '<a href="javascript:playVideo();"><img src="' + currVideos[videoIndex].imgURL + '"></a>';
    /*
    if (currVideos[videoIndex].titlePos == 'bottom') {
        document.getElementById('video_title').style.top = '157px';
    } else {
        document.getElementById('video_title').style.top = '7px';
    }

    document.getElementById('video_titleML').innerHTML = currVideos[videoIndex].titleML;
    document.getElementById('video_title').style.display = 'block';
    */
    //document.getElementById('play_button').style.display = 'block';
    //document.getElementById('flash').style.backgroundImage = 'url( \'' + currVideos[videoIndex].imgURL + '\' )';
    document.getElementById('flash').style.visibility = 'visible';

    if (document.getElementById('hearTheRest')) {
        var pathEnd = pathsArray[videoIndex];
        document.getElementById('hearTheRest').innerHTML = '<a href="/web/article/' + pathEnd + '">Click here to watch the rest of ' + currVideos[videoIndex].actorName.substring(0,1).toUpperCase() + currVideos[videoIndex].actorName.substring(1) + '\'s story &#187;</a>';
        hideRestLink();
    }
    initPlayer(currVideos[videoIndex].videoURL, currVideos[videoIndex].imgURL, autostart);
    playVideo(videoIndex);
}

function hidePlayer() {
    document.getElementById('flash').style.visibility = 'hidden';
    document.getElementById('player').innerHTML = '';
}

var oldMoreVidLinkIndex = '';

function playOtherVideo(mainVisArrayIndex, vidURL, moreVidArrayIndex) {
    if (oldMoreVidLinkIndex) {
        document.getElementById(oldMoreVidLinkIndex).className = 'text_vid_link';
        oldMoreVidLinkIndex = 'more_vid_link_' + moreVidArrayIndex;
    } else {
        oldMoreVidLinkIndex = 'more_vid_link_' + moreVidArrayIndex;    
    }
    document.getElementById('more_vid_link_' + moreVidArrayIndex).className = 'text_vid_link text_vidSelected';
    initPlayer(vidURL, currVideos[mainVisArrayIndex].imgURL, true);
    document.getElementById('flash').style.visibility = 'visible';
    playVideo();
}

function hideRestLink() {
    if (document.getElementById('hearTheRest')) {
        document.getElementById('hearTheRest').style.display = 'none';
    }
}

function fadeInRestLink() {
    setOpacity(document.getElementById('hearTheRest'), 0);
    document.getElementById('hearTheRest').style.display = 'block';
    setTimeout('fadeIn(\'hearTheRest\', 0, 50, 10, true)', 35000);
}


function initPlayer(vidURL, imgURL, autostart) {
    var autoStartParm = '';

    if (autostart) {
        autoStartParm = '&autostart=true';
    }

    so = new SWFObject('/web/flash/player.swf','mpl','432','258','9');
    so.addParam('allowscriptaccess','always');
    so.addParam('wmode','transparent');
    so.addParam('allowfullscreen','true');
    so.addParam('wmode','transparent');
    so.addParam('flashvars','&file=' + vidURL + '&image=' + imgURL + autoStartParm + '&bufferlength=5&icons=false');
}

function writePlayer() {
    so.write('player');
}

var isPlaying = false;
function playVideo(videoIndex) {
    isPlaying = true;
    if (document.getElementById('video_title')) {
        document.getElementById('video_title').style.display = 'none';
    }
    if (document.getElementById('play_button')) {
        document.getElementById('play_button').style.display = 'none';
    }

    writePlayer();

    if (document.getElementById('hearTheRest') && videoIndex > 2) {
        fadeInRestLink();
    }
}

$.ready(populateVideoLinks(whichVideo));
//we only want to init on corp and consumer home - which is also the places where hearTheRest is
if (document.getElementById('hearTheRest')) {
    $.ready(initFirstVideo());
}