
//################ Layer Object ################

function fillobject() {
    if (UseMainSeq == true) {

        this.url = urlary[MainSeqNum];
        this.type = typeary[MainSeqNum];
        this.nomc = nomcary[MainSeqNum];
        
        this.width = widthary[MainSeqNum];
        this.height = heightary[MainSeqNum];
        this.transition = transary[MainSeqNum];
        this.duration = durary[MainSeqNum];
        this.thumbnail = thumbary[MainSeqNum];

        this.title = titleary[MainSeqNum];
        this.textbody = txtary[MainSeqNum];
        this.link = linkary[MainSeqNum];
        this.extref = extrefary[MainSeqNum];  //- holds the URL of the external reference
        this.intref = intrefary[MainSeqNum];  //– Use as sub selector with External Referance
        this.catalogid = catidary[MainSeqNum]; // The catalog ID if 0 no catalog

        this.loaded = true; // set when Object is loaded cleared when it's layer is cleared 
        this.running = false; // set when a player is runing or when the duration time has not been met
        this.ready = false; // set for the players after the run, pause detetion
        this.main_seq = MainSeqNum; //from  sequencer to see if on a mouseovr if its filled
        this.sub_seq = SubSeqNum; //from  sequencer to see if on a mouseovr if its filled
       
    }

    if (UseMainSeq == false) {
        this.url = sub_urlary[SubSeqNum];
        this.type = sub_typeary[SubSeqNum];
        this.nomc = sub_nomcary[MainSeqNum];
        this.width = sub_widthary[SubSeqNum];
        this.height = sub_heightary[SubSeqNum];
        this.transition = sub_transary[SubSeqNum];
        this.duration = sub_durary[SubSeqNum];
        this.thumbnail = sub_thumbary[SubSeqNum];
        this.title = sub_titleary[SubSeqNum];
        this.textbody = sub_txtary[SubSeqNum];
        this.link = sub_linkary[SubSeqNum];
        this.extref = sub_extrefary[SubSeqNum];  //- holds the URL of the external reference
        this.intref = sub_intrefary[SubSeqNum];  //– Use as sub selector with External Referance
        this.catalogid = sub_catidary[SubSeqNum]; // The catalog ID if 0 no catalog

        this.loaded = true; // set when Object is loaded cleared when it's layer is loaded it finishes
        this.running = false; // set when a player is runing or when the duration time has not been met
        this.ready = false; // set for the players after the run, pause detetion
        this.main_seq = MainSeqNum; //from  sequencer to see if on a mouseovr if its filled
        this.sub_seq = SubSeqNum; //from  sequencer to see if on a mouseovr if its filled
    }
}

// Layer Prototypes
function layerobj() {
    this.url = '';
    this.type = 'ltype';
    this.width = 0;
    this.height = 0;
    this.transition = '';
    this.duration = 0;
    this.thumbnail = '';
    this.title = '';
    this.bodytext = '';
    this.link = '';
    this.extref = '';
    this.intref = '';
    this.catalogid = 0;
    this.loaded = false;
    this.running = false;
    this.ready = false;
    this.main_seq = 0;
    this.sub_seq = 0;
    this.fillobj = fillobject; // fills the object from the ctl.js file
}

//Make Objects
var OGP1 = new layerobj();
var OGP2 = new layerobj();
var OMedia = new layerobj();
var OFlash = new layerobj();

function get_object(LayerName) {
    switch (LayerName) {
        case 'MediaPlayer':
            MyObject = OMedia;
            break;
        case 'FlashPlayer':
            MyObject = OFlash;
            break;
        case 'GP1':
            MyObject = OGP1;
            break;
        case 'GP2':
            MyObject = OGP2;
            break;
    }
    return MyObject
}

//################# State Object ############
function stateobj() {
    this.auto_play = false;  // The sub counter is auto sequencing

    this.transrunning = false;  // A transition is running
    this.detectrunning = false;  // Detecting the play pause set when entered cleared by detect.
    this.nextready = false;  // Only for players The next layer is ready it's paused
    this.readycounter = 0;  // how many one second timers have passed waiting for nextready
    this.enddetecting = false;  // When a player has finished playing.
    this.enddetect = false;  // When a player has finished playing.
    this.durtimerset = false;  // the duration timeout is set
    this.durtimeout = false;   // it's timed out
    this.preload = false;   // set by preload
    this.load_check = false; // set from check_loaded   
    this.load_ctl_counter = 0 // the mumber of 1 sec intervals trying to load ctl 
}
var OState = new stateobj();

function statereset() {
    OState.detectrunning = false;
    OState.nextready = false;
    OState.enddetecting = false;
    OState.enddetect = false;
    OState.durtimerset = false;
    OState.durtimeout = false;
    OState.load_check = false; // set from check_loaded
    OState.load_ctl_counter = 0; 

    //clear the timers
    clearTimeout(DurTimer); //DurTimer
    clearTimeout(StateTimer); //StateTimer
    clearTimeout(PlayerWaitTimer); //PlayerWaitTimer
    clearTimeout(PreLoadTimer); //PreLoadTimer
    clearTimeout(EndDetectTimer); // player end detect
    clearTimeout(PlayerStateTimer);
    clearTimeout(CtlLoadTimer); 
    PlayerStateCounter = 0;
    PlayerStateDetecting = false;

}


//################ Sequencer ##################
function state_ctl(callby) {

    switch (callby) {
        case 'move_it':
            statereset();
            // Resets OState.load_check = false
            // counters are adjusted already

            // #############################
            // Why is Move It called if not finished ?? 
            // Back to back same player problem

            // If current is a player and next is the same player
            // Can't load next -- Have to wait until finished Why is Move It called if not finished ??
            // If back to back don't transition load it.

            // if it's currently a remote flash, kill it

            if (CurLayer == 'FlashPlayer') {
                //kill_f()
                var MyFile = OFlash.url;
                MyFile = MyFile.toLowerCase()
                var i = MyFile.indexOf('www.');
                if (i != -1) {
                    kill_f()
                }
            }


            check_loaded();
            if (OState.load_check == true) {
                check_scroll();
            }
            // Only force clear if not an in use player 
            else {
                force_clear_next_type(); // Forced clears a layer for the next type Clears loaded
                preloadnext();  // sets next layer // Size box in the load
                check_scroll(); // CAUSES TRANSITION
            }
            break;

        case ('check_scroll'):
            // If back to back din't transition just load the layer
        
            transition(); // Has position_slidebox()
            break;



        case ('transition'):
            transition_cleanup();
            change_listing_border();  // NEEDS AN EARLY OUT

            // ################ IE PROBLEM - Half boxes
            //load_layer(NextLayer); // Fixed it
            //make_z(NextLayer, TopLayerZ);
            //showit(NextLayer);
            //make_size();
            //showit(NextLayer);
            //position_slidebox();

            setTimeout('advance_ctr()', 100);

            //advance_ctr(); // May load a new array - Has a timer 
            break;

        case ('advance_ctr'):
            MyClear = pre_clear_next_type(); // clears a layer for the next type Clears loaded
            // What if the type is being used??? Can't preload
            // If it's a remote flash don't preload 
            if (MyClear == true) preloadnext();

            if (CurLayer == 'MediaPlayer') play_m();
            //if (CurLayer == 'MediaPlayer') playit(CurLayer);
            if (CurLayer == 'FlashPlayer') play_f();
            //if (CurLayer == 'FlashPlayer') playit(CurLayer);

            if (OState.auto_play == true) {
                // start timers 
                //if (CurLayer == 'MediaPlayer' || CurLayer == 'FlashPlayer') {

                if (CurLayer == 'MediaPlayer') {
                    OState.enddetecting = true;
                    enddetect_m();
                }
                if (CurLayer == 'FlashPlayer') {
                    // ####################
                    // Check if it's remote if it is don't set end detect, use the timer
                    var MyFile = OFlash.url;
                    MyFile = MyFile.toLowerCase()
                    var i = MyFile.indexOf('www.');
                    if (i != -1) {
                        // it's a remote flash -
                        MyTime = get_object(CurLayer).duration;
                        if (MyTime > 0) DurTimer = setTimeout('durtimerend()', MyTime);
                    }
                    else { 
                        OState.enddetecting = true;
                        enddetect_f();
                    }
                }

                // #################### New one here
                // playerenddetect();
                //}
                if (CurLayer != 'MediaPlayer' && CurLayer != 'FlashPlayer') {
                    // get the time from the object not 0
                    MyTime = get_object(CurLayer).duration;
                    if (MyTime > 0) DurTimer = setTimeout('durtimerend()', MyTime);
                }
            }
            break;
    }
}

function durtimerend() {
    OState.durtimeout = true  //
    state_ctl('move_it');
}


// ############  ADVANCE ############
// If CatList == true - Need to show only slides with that catalog ID
// The listing also

//var i = 0;
//var j = 0;
//var MyCatID = 0;
//MainCatID       ;

function advance_ctr() {
    // Copy layer Next to Cur

    //CatListItem = true;
    if (CatListItem == true) {
        MainSeqNum = CatListAryItem;
    }
    else {
        // increment it
        CurLayer = NextLayer;
        CurType = NextType;
        // If UseMainSeq = true and the current main sequence is no gallery extrefary[] != 'None'
        //      increment MainSeqNum use it
        if (UseMainSeq == true && extrefary[MainSeqNum] == 'None') {
            CurSeqNum = MainSeqNum;  // ???
            MainSeqNum = (MainSeqNum < urlary.length - 1) ? MainSeqNum + 1 : 0;
            NextSeqNum = MainSeqNum;
            GalCur = (extrefary[MainSeqNum] == 'None') ? false : true;
            state_ctl('advance_ctr');
        } // Its a gallery
        else if (UseMainSeq == true && extrefary[MainSeqNum] != 'None') {
            UseMainSeq = false;
            //First time going into a sub turn its auto on
            OState.sub_auto = true;
            SubSeqNum = 1;
            GalCur = (extrefary[MainSeqNum] == 'None') ? false : true;

            load_url = 'parts/galleries/gallery' + intrefary[MainSeqNum] + '/ctl.xml';

            //load_url = extrefary[MainSeqNum]

            load_arrays();  // let it state_ctl('advance_ctr');
        }
        else { //Use the sub and check for end
            SubSeqNum = (SubSeqNum < sub_urlary.length - 1) ? SubSeqNum + 1 : 0;
            if (SubSeqNum == 0) {
                MainSeqNum = (MainSeqNum < urlary.length - 1) ? MainSeqNum + 1 : 0;
                UseMainSeq = true;
                GalCur = (extrefary[MainSeqNum] == 'None') ? false : true;
            }
            state_ctl('advance_ctr');
        }
    }
}



