﻿var IE = (navigator.userAgent.toLowerCase().indexOf('msie') > -1);

var TIMETOWAIT = 1000; // 1 second

var as3Ready = false;
var animHtml1 = null;
var animHtml2 = null;
var numAnims = 0;
var currentAnim = 0;
var activeAnim = 0;
var timerID = -1;
var activeDiv = 1;

function flashReady(value)
{
    if (currentAnim == 1)
        preloadAnim();
}

function initializeConvo(autoplay)
{
    numAnims = Number(getObject("numconvos").value);
    animHtml1 = getObject("divAnimation1");
    animHtml2 = getObject("divAnimation2");
    
    var id = getObject("convoanim1").value;
    activeDiv = 1;
    currentAnim = 1;
    activeAnim = 1;
    var toon2 = getEmbedObject("swfToon2");
    if (toon2 != null)
    {
        if (IE)
            toon2.style.height = 0;
        else
            toon2.height = 0;
    }
    animHtml1.innerHTML = getSwf("swfToon1", "getcustomtoon.ashx?face=1&cs=" + (autoplay ? "0" : "1") + "&id=" + id + "&v=swf&dl=0", CONVOWIDTH, CONVOHEIGHT, true, "direct", 9);
}

function loadNextAnim()
{
    timerID = setTimeout("loadAnim();", TIMETOWAIT);
}

function loadAnim()
{
    timerID = -1;
    if (activeAnim == numAnims) return;

    activeAnim++;
    
    if (activeDiv == 1)
    {
        activeDiv = 2;
        if (IE)
        {
            getEmbedObject("swfToon1").style.height = 0;
            getEmbedObject("swfToon2").style.height = CONVOHEIGHT;
        }
        else
        {
            getEmbedObject("swfToon1").height = 0;
            getEmbedObject("swfToon2").height = CONVOHEIGHT;
        }
        setTimeout("playAnim(2);", 500);
    }
    else
    {
        activeDiv = 1;
        if (IE)
        {
            getEmbedObject("swfToon2").style.height = 0;
            getEmbedObject("swfToon1").style.height = CONVOHEIGHT;
        }
        else
        {
            getEmbedObject("swfToon2").height = 0;
            getEmbedObject("swfToon1").height = CONVOHEIGHT;
        }
        setTimeout("playAnim(1);", 500);
    }
    
    preloadAnim();
}

function playAnim(num)
{
    try
    {
        getEmbedObject("swfToon" + num).playToon();
    }
    catch (err){}
}

function preloadAnim()
{
    if (currentAnim == numAnims) return;
    
    currentAnim++;
    var id = getObject("convoanim" + currentAnim).value;

    var html = getSwf("swfToon" + (activeDiv == 1 ? "2" : "1"), "getcustomtoon.ashx?face=1&cs=1&id=" + id + "&v=swf&dl=0", CONVOWIDTH, 0, true, "direct", 9);

    if (activeDiv == 1)
    {
        animHtml2.innerHTML = html;
        //animHtml2.style.height = "0px";
    }
    else
    {
        animHtml1.innerHTML = html;
        //animHtml1.style.height = "0px";
    }
}

function play()
{
    try
    {
        if (timerID > -1) clearInterval(timerID);

        if (activeAnim == 1)
            getEmbedObject("swfToon1").playToon();
        else
            initializeConvo(true);
    }
    catch (err){}
}

function rewind()
{
    try
    {
        if (timerID > -1) clearInterval(timerID);
        initializeConvo(false);
    }
    catch (err){}
}

function dialogFinished()
{
    loadNextAnim();
}
