////////////////////////////////VTOURPLAYER OBJECT : BEG////////////////////////////////

/////CONSTRUCTOR : BEG/////
function VTourPlayer(sID)
{
    //Properties
    //Properties : general
    this.ID             = sID;                  //Player ID (cotaining <div> element);
    this.PlayerType     = null;                 //Type of player (c_iPlayerTypeFloat or c_iPlayerTypeInline)
    this.IsInit         = false;                //Has the Player been initialized already?
    this.IsValid        = true;                 //Is Player objected initialzed and valid?
    this.PosTop         = 0;                    //Position of player(if appropriate)
    this.PosLeft        = 0;                    //Position of player(if appropriate)
    this.ImgLeft        = 0;                    //Horizontal position of v-tour image.
    this.Tags           = new Array();          //An array containing references to all player DHTML DOM elements.  Indexed using constants.
    this.ButtonState    = [1,1,0];              //Current state of play backward, pause, and play forward buttons respectively.
    this.SelectedLink   = null;                 //Int indicating ID of currently selected link.

    //this.ImgWindow    = null;                 //Reference to <img> containing tour image.

    //Properties : tour
    this.CurrentTour    = null;                 //Reference to current tour object
    this.PrevVTour      = null;                 //Reference to next tour object (if appropriate)
    this.NextVTour      = null;                 //Reference to previous tour object (if appropriate)
    this.TourNumTotal   = 1;                    //Holds number of tours targeted at player.
    this.TourNumCurr    = 1;                    //Holds index of current tour.


    //Methods
    this.Init           = PlayerInit;           //Initialize player.
    this.Hide           = PlayerHide;           //Hides player
    this.Show           = PlayerShow;           //Shows player
    this.PlayCurrent    = PlayCurrentTour;      //Plays current vtour.
    this.BuildUI        = PlayerBuildAllUI;     //Builds UI
    this.SetNavUI       = SetNavUI;             //Sets tour navigation UI
    this.Validate       = PlayerValidate;       //Validate initialization.
    this.LoadTags       = LoadTags;             //Loads Tags array with player specific tag IDs
    this.LoadTour       = LoadVTour;            //Loads a tour object.
    this.ShowMsg        = ShowPlayerMessage;    //Show message in player window.
    this.Clear          = ClearImage;           //Clear image and scroll interval.
    this.Report         = ReportPlayerState;    //Report tour state information (for test only)
    this.HighlightLink  = HighlightLink;        //Manages highlighting the currently selected link.

    //Initialization
    this.Init();
}
/////CONSTRUCTOR : END/////

/////METHODS : BEG/////

function ClearImage()
{
    ClearVTInterval();
    this.Report();
    this.Tags[cTagTourImg].innerHTML    = "<img src='/s.gif'/>";
}

function HighlightLink(iLnkNum)
{
    if(this.TourNumTotal <= 1) return;

    var sClassOff   = "BODY1GR2";
    var sClassOn    = "BODY1OR1";

    //UN-HIGHLIGHT previously selected link
    if(this.SelectedLink != null) GE(this.SelectedLink).className = sClassOff;
    this.SelectedLink = this.ID + "_lnk_" + iLnkNum;

    //HIGHLIGHT selected link
    GE(this.SelectedLink ).className = sClassOn;

}

function LoadTags()
{
    for(var i = 0; i < g_rgPlayerTags.length; i++)
    {
        this.Tags[i] = GE(this.ID + g_rgPlayerTags[i]);
    }
}

function LoadVTour(oTour)
{
    this.Clear();
    this.CurrentTour    = oTour;
    this.ImgLeft        = oTour.StartPos;
    this.BuildUI();
    return oTour.IsInit;
}

function PlayCurrentTour()
{
    //ENSURE player is showing.
    this.Show();

    if(this.CurrentTour != null && this.CurrentTour.IsValid)
    {
        var sInt = null;

        if(this.CurrentTour.Is360) sInt = "ScrollTour360('" + this.ID + "')";
        else  sInt = "ScrollTour180('" + this.ID + "')";

        SetVTInterval(sInt,g_iIntervalTimer);
    }
    else
    {
        var sMsg = ""
        if(this.CurrentTour == null) Tst("Unable to play tour : \n\nthis.CurrentTour == null");
        else this.CurrentTour.Report("Unable to play tour : \n\nthis.CurrentTour is not a valid tour object");

        //SHOW failure message
        this.ShowMsg(2,null,true);
    }
}

function PlayerBuildAllUI()
{
    var oTagTourImg     = this.Tags[cTagTourImg];
    if(!this.IsValid)
    {
        this.Report();
        return false;
    }
    if(this.CurrentTour.IsInit)
    {
        var sTourImg;
        if(g_bTst)
        {
            sTourImg    = "<img style='border-left:1px solid red;border-right:1px solid blue;' src='" + this.CurrentTour.ImgSrc + "'/>";
            this.CurrentTour.Report();
        }
        else sTourImg                           = "<img src='" + this.CurrentTour.ImgSrc + "'/>";

        if(this.CurrentTour.Is360)  sTourImg    = "<NOBR>" + sTourImg + sTourImg + "</NOBR>";
        oTagTourImg.innerHTML                   =  sTourImg;

        this.Tags[cTagCaption].innerHTML        = this.CurrentTour.Caption;
        this.SetNavUI();

        //SHOW load wait message
        //NOTE: this call to ShowMsg() may not be needed.. already calling in ShowVTour().
        this.ShowMsg(1,null,false);
    }
    else
    {
        oTagTourImg.src = "/s.gif";
        this.Report("Problem Building UI");
        this.ShowMsg(2,null,true);
    }
}

function PlayerHide()   {this.Tags[cTagPlayer].style.display='none';}
function PlayerShow()   {this.Tags[cTagPlayer].style.display='';}

//Initialize Player
function PlayerInit()
{
    if(this.IsInit) return;
    this.PlayerType = (this.ID.indexOf("Float") >= 0) ? c_iPlayerTypeFloat : c_iPlayerTypeInline;
    this.LoadTags();
    this.IsValid    = this.Validate();
    this.IsInit     = this.IsValid;
}

function PlayerValidate()
{

    var sErrMsg = '';
    var sINF    = 'ID not found:';

    for(var i = 0; i < this.Tags.length; i++)
    {
        var sTagID =  this.ID + g_rgPlayerTags[i];
        if(this.Tags[i] == null) sErrMsg += "- "+ sINF + " '" + sTagID + "'\n";
    }

    if(sErrMsg.length > 0)
    {
        sErrMsg = "Problem(s) Initializing Player:"
                + "\n-------------------------------"
                + "\n"
                + sErrMsg;

        Tst(sErrMsg,null,c_iTstMsgsObjReports);
        return false;
    }
    else
    {
        return true;
    }
}

//REPORTS state of current tour object
function ReportPlayerState(sMsg)
{
if(!g_bTst) return;
sMsg += "";
if(sMsg.length > 0) sMsg = "\n\n[NOTE]\n" + sMsg
var sCaller = "\n\n[CALLING FUNCTION]\n" + ((ReportPlayerState.caller != null) ? ReportPlayerState.caller.toString().slice(0,ReportPlayerState.caller.toString().indexOf(")")+1) : "No Calling Function");

var sState  = "VTourPlayer Object("    + this.ID + ") State:"
            + sMsg
            + sCaller
            +"\n\n[PROPERTIES : GENERAL]"
            +"\nID ["           + typeof(this.ID)           + "]\t\t= " + this.ID
            +"\nPlayerType ["   + typeof(this.PlayerType)   + "]\t= "   + this.PlayerType
            +"\nIsInit ["       + typeof(this.IsInit)       + "]\t\t= " + this.IsInit
            +"\nIsValid ["      + typeof(this.IsValid)      + "]\t\t= " + this.IsValid
            +"\nPosTop ["       + typeof(this.PosTop)       + "]\t\t= " + this.PosTop
            +"\nPosLeft ["      + typeof(this.PosLeft)      + "]\t\t= " + this.PosLeft
            +"\nImgLeft ["      + typeof(this.ImgLeft)      + "]\t\t= " + this.ImgLeft
            +"\nTags ["         + typeof(this.Tags)         + "]\t\t= " + this.Tags
            +"\nInterval ["     + typeof(this.Interval)     + "]\t\t= " + this.Interval
            +"\nButtonState ["  + typeof(this.ButtonState)  + "]\t= "   + this.ButtonState


            +"\nSelectedLink [" + typeof(this.SelectedLink)  + "]\t= " + this.SelectedLink

            +"\n\n[PROPERTIES : TOUR]"
            +"\nCurrentTour ["  + typeof(this.CurrentTour)  + "]\t= "   + this.CurrentTour
            +"\nPrevVTour ["    + typeof(this.PrevVTour)    + "]\t\t= " + this.PrevVTour
            +"\nNextVTour ["    + typeof(this.NextVTour)    + "]\t\t= " + this.NextVTour
            +"\nTourNumTotal ["  + typeof(this.TourNumTotal)  + "]\t= " + this.TourNumTotal
            +"\nTourNumCurr ["  + typeof(this.TourNumCurr)  + "]\t= "   + this.TourNumCurr;


    Tst(sState,1,c_iTstMsgsObjReports);
}

//SETS UI elements. Should be called whenever UI state changes.
function SetNavUI()
{
    this.HighlightLink(this.TourNumCurr);

    SetNextPrevNavUI(this);
    SetButtonUI(this);
}

//WRITES message to player console window.
function ShowPlayerMessage(iMsg,sMsg,bClear)
{
    if(sMsg == null && iMsg != null)
    {
        var iDefaultMsg = 0;

        var rgMsg   = ["General error","Loading tour, one moment please...","We were unable to load that tour, please choose another.",""];
        if(iMsg < 0 || iMsg > rgMsg.length) iMsg = iDefaultMsg;
        sMsg        = rgMsg[iMsg];
    }
    if(bClear) this.Clear();

    this.Tags[cTourMsg].innerHTML = sMsg;
}

/////METHODS : END/////

////////////////////////////////VTOURPLAYER OBJECT : END////////////////////////////////
