﻿var demoframeobject = document.getElementById('demoframe');
var demoList = new Array();
var relativeDemoPath = "";
var selectedClassName = "demo-nav-selected";
var regularClassName = "";
var menuName = "my_menu";
var endPage = "Centerprise3DemoEnd.htm";

fillDemoList();

function fillDemoList()
{
     var datatransferDemoList = new Array();
    //AddChapter(datatransferDemoList, "Intro","DataMapperIntro_demo.htm","Introduction");
    AddChapter(datatransferDemoList, "BuildingDataMaps", "Building Data Maps_demo.htm", "Building Data Maps");
    AddChapter(datatransferDemoList, "OtherMapTypes", "Other Map Types_demo.htm", "Other Map Types");
    AddChapter(datatransferDemoList, "RunningATransfer", "Running a Transfer_demo.htm", "Running a Transfer");
    AddChapter(datatransferDemoList, "QueryTool", "Query_demo.htm", "Using the Query Tool");
    AddChapter(datatransferDemoList, "BatchTransfers", "Transfer Batch_demo.htm", "Batch Transfers");
    AddChapter(datatransferDemoList, "DBCopyBatch", "Database Copy Tables_demo.htm", "Copying Multiple Tables");
    AddChapter(datatransferDemoList, "SchedulingTransfers", "Scheduler_demo.htm", "Using the Query Tool");
    AddChapter(datatransferDemoList, "ServerManager", "ServerManagerDemo_demo.htm", "Server Manager");
    AddChapter(datatransferDemoList, "WorkingWithFiles", "Working with Files_demo.htm", "Text File Options");
    AddChapter(datatransferDemoList, "COBOL", "Cobol Demo_demomini.htm", "Cobol Data Files");
    AddChapter(datatransferDemoList, "WorkingWithDB", "Working with DB_demo.htm", "Database Options");
    AddChapter(datatransferDemoList, "FileDrop", "FileDrop_demo.htm", "File Drop Triggers");
    AddChapter(datatransferDemoList, "XML", "XML_demo.htm", "XML");
    AddChapter(datatransferDemoList, "DB2XML", "XMLDemo2_demorevmini.htm", "Database to XML");
    AddChapter(datatransferDemoList, "MappingGroups", "mappinggroups_demo.htm", "File Drop Triggers");
    AddChapter(datatransferDemoList, "LayoutSpec", "Layout from spec_demo.htm", "Layout Specifications");
    AddChapter(datatransferDemoList, "DataModels", "DataModel_demo.htm", "Data Models");
    AddChapter(datatransferDemoList, "DataModelTransfers", "TransferDataModel_demo.htm", "Data Model Transfers");
    AddChapter(datatransferDemoList, "DataQualityRules", "ValidationRules_demo.htm", "Data Quality");
    AddChapter(datatransferDemoList, "DataProfiling", "Profiling Demo_demomini.htm", "Data Profiling");
     AddChapter(datatransferDemoList, "SalesForce", "Salesforce_demo.htm", "Salesforce");
    
    AddDemo(0,"Data Transfer",datatransferDemoList);
}


function BuildMenu()
{
    var menu = document.getElementById(menuName);
    for(var index = 0; index < demoList.length; index++)
    {
        var demo = demoList[index];
        var divElement = document.createElement("div");
        divElement.setAttribute("class","collapsed");
        var titleElement = document.createElement("span");
        titleElement.innerText = demo.title;
        titleElement.textContent = demo.title;
        divElement.appendChild(titleElement);
        menu.appendChild(divElement);
        AddSubMenus(divElement,demo);
    }
}

function AddSubMenus(div,demo)
{
    for(var chapterListName in demo.chapterList)
    {
        var chapter = demo.chapterList[chapterListName];
        var anchor = document.createElement("a");
        anchor.setAttribute("href","javascript:playDemo('" + chapter.name + "');");
        anchor.setAttribute("id",chapter.name);
        anchor.innerText = chapter.title;
        anchor.textContent = chapter.title;
        div.appendChild(anchor);
    }
}

function AddChapter(chapterList,chapterName,chapterURL,chapterTitle)
{
    var demoChapter = new DemoChapter(chapterName, chapterURL,chapterTitle);
    chapterList[chapterName] = demoChapter;
}

function AddDemo(index, title, chapterList)
{
    //demoList[index] = chapterList;
    demoList[index] = new Demo(title);
    demoList[index].chapterList = chapterList;
}



function raiseNextClicked(currentDemoChapter)
{
try
    {
    var nextDemoChapter = getNextChapter(currentDemoChapter);
    if(nextDemoChapter == null)
    {
        alert("This is the last chapter.");
        triggerEvent("Demos", "Next Clicked", currentDemoChapter);
        return;
     }
     playDemo(nextDemoChapter.name);
     triggerEvent("Demos", "Next Clicked", currentDemoChapter);
    }
    catch(err)
    {
        alert(err);
    }
}

function raisePreviousClicked(currentDemoChapter)
{
    try
    {
     var nextDemoChapter = getPreviousChapter(currentDemoChapter);
        if(nextDemoChapter == null)
        {
            alert("This is the first chapter");
            triggerEvent("Demos", "Previous Clicked", currentDemoChapter);
            return;
            }
            playDemo(nextDemoChapter.name);
            triggerEvent("Demos", "Previous Clicked", currentDemoChapter);
    }
    catch(err)
    {
        alert(err);
    }
}

function raiseEnded(currentDemoChapter)
{
   var nextDemoChapter = getNextChapter(currentDemoChapter);
   if(nextDemoChapter == null)
   {
        window.location = endPage;
        return;
   }
   playDemo(nextDemoChapter.name);
   triggerEvent("Demos", "Completed", currentDemoChapter);
}

function getChapterList(demoChapterName)
{
    for(var demo in demoList)
    {
       for(demoChapter in demoList[demo].chapterList)
        {
            if(demoChapter == demoChapterName)
                return demoList[demo].chapterList;
        }  
    }
    return null;
}

function getNextChapter(currentDemoChapter)
{
    var chapterList = getChapterList(currentDemoChapter);
    if(chapterList == null)
        throw currentDemoChapter + " was not found in the chapter list.";
  
    var currentChapterFound = false;
    for(demoChapter in chapterList)
    {
        if(currentChapterFound)
            return chapterList[demoChapter];
        if(demoChapter == currentDemoChapter)
            currentChapterFound = true;
    } 
    return null;
}

function getPreviousChapter(currentDemoChapter)
{
    var chapterList = getChapterList(currentDemoChapter);
    if(chapterList == null)
        throw currentDemoChapter + " was not found in the chapter list.";
  
    var currentChapterFound = false;
    
   
    var i = 0;
    var previousChapter = null;
    for(demoChapter in chapterList)
    {
        if(demoChapter == currentDemoChapter)
        {
            if(i <= 0)
                return null;
             return chapterList[previousChapter];
        }
        previousChapter = demoChapter;
        i++;
    } 
    return null;
}

function playDemo(demoKey)
{
    var chapterList = getChapterList(demoKey);
    if(chapterList == null)
        throw demoKey + " was not found in the chapter list.";
    var demoChapter = chapterList[demoKey];
    playLocation(demoChapter.url);
    //triggerEvent("Demos","Play", demoKey);
    //highLightChapter(demoKey)
}


function playLocation(url)
{
	demoframeobject.src = relativeDemoPath + "/" + url;
}

function openURL(url)
{
    alert("opening " + url);
}




function DemoChapter(name, url,title)
{
    this.name = name;
    this.url = url;
    this.title = title;
}

function Demo(title)
{
    this.title = title;
    this.chapterList = null;
}


function highLightChapter(key)
{
    var anchor = document.getElementById(key);
    var parentDiv = anchor.parentElement;
    if(parentDiv.className == "collapsed")
        myMenu.expandMenu(parentDiv);
    var root = parentDiv.parentElement;
    var allanchors = root.getElementsByTagName("a");
    for(var i = 0; i< allanchors.length; i++)
    {
        allanchors[i].className = regularClassName;
    }
    
    anchor.className = selectedClassName;
}

function triggerEvent(category, action, label, value) {
    pageTracker._trackEvent(category, action, label);
}
