/* -----------------------------------
vn_experience_classes.js
(class defs for vn_piece and vn_selected)
for use on any experience (deal or read)
pages needs to have vn_image (tarot, or 
other, etc.) defined 

(c) 2001, visionary networks
any questions? ask jewel mlnarik 
(jewel@tarot.com)
--------------------------------------- */ 

if( IMAGE_BASE_PATH == "" ) {
    var IMAGE_SRC = "/";
    //document.write("base path: / <BR>");
} else {
    var IMAGE_SRC = IMAGE_BASE_PATH;
    //document.write("base path: "+IMAGE_SRC+" <BR>");
}

/////////////////////////////////////
//     vn_piece             //
////////////////////////////////////

//---------------------------------------------
// constructor for vn_piece object (e.g. card)
//---------------------------------------------
function vn_piece( num, strDeck ) {
    //sk071504
    //reset to -1 as default value-- 0 is Fool card
    if(num || (num==0) )
        this.picked = num; // if selected, set to piece ref number
    else
        this.picked = -1; // else, set to -1
    
    // properties
    this.image = new vn_image( num, strDeck );
}



/////////////////////////////////////
//             vn_selected        //
////////////////////////////////////



///////////////////////////////////////
//
// selected_insert() -- inserts a "piece" (e.g. card) into the picked_pieces[i] array
//
// numPositionToBeFilled parameter is optional and used only when it is known in advance which position will be filled
//
// if numPositionToBeFilled not specified, finds next open position and returns the index
//
function selected_insert( refNum, numPositionToBeFilled, groupObj ) {
    var numThisPosition = numPositionToBeFilled;
    // if numPositionToBeFilled not specified, find next reading position for the picked card (loop through the array)
    if (!numPositionToBeFilled) {
        for (var i=1; i <= this.total_pieces; i++) {
            // see if this position is "open"-- if the obj exists
            if (!this.picked_pieces[i]) {
                // found the open position
                numThisPosition = i;
                // stop looking for next open position
                i = this.total_pieces + 1;
            } else {
//                alert("exp_class. selected_insert pp value "+i+": "+this.picked_pieces[i].picked);
            }
        }
    }
    
    // insert the card
    this.picked_pieces[numThisPosition] = new vn_piece( refNum, this.deck );
    // check for option to turn the card over
    
    // increment picked cards counter
    this.pick_no ++;
    
    if( groupObj ){
        // when inserting a card, we must mark the card as picked so we can't re-pick it

        // get the position of the card
        pnum = groupObj.find( refNum );
    
        // erase the card so it can't be picked again
        groupObj.group_array[pnum] = -1;
    }

    
    // if numPositionToBeFilled not specified, return the new position that was filled
    if (!numPositionToBeFilled) {
        return numThisPosition;
    } else {
        // need this to prevent "function does not always return a value" error in some JavaScript versions
        return null;
    }
}

function selected_setFormValue( posNo ) {
     this.form.elements["picked_pieces[" + posNo + "]"].value = this.picked_pieces[posNo].picked;
}


function selected_callReading( posNo ) {
    //sk071904
    //double check final card was set
    if( this.pick_no >this.total_pieces ){
        if (!this.picked_pieces[this.total_pieces] ||
        (this.picked_pieces[this.total_pieces].picked<0) ) {
            this.pick_no -= 1;
        }
    }

    if( this.pick_no <= this.total_pieces ){
        alert("You have not chosen all your cards.  Please select " + ( (this.total_pieces - this.pick_no) + 1 ) + " more before consulting Tarot.");
    }else{
        if(this.page_action != "saved") {  
            // need to set picked vals in form to pass on (if saved, these are already set)
            for (var i=1; i <= this.total_pieces; i++) {
                this.set_form_value( i );
            }
        }

        this.form.deck_ID.value = this.deck;
        this.form.position.value = posNo;

        // alert(this.form.action);
        this.form.submit();
    }
}

//checks to see cards have been picked
function selected_checkForm( posNo ) {
    //sk071904
    //double check final card was set
    if( this.pick_no >this.total_pieces ){
        if (!this.picked_pieces[this.total_pieces] ||
        (this.picked_pieces[this.total_pieces].picked<0) ) {
            this.pick_no -= 1;
        }
    }

    if( this.pick_no <= this.total_pieces ){
        alert("You have not chosen all your cards.  Please select " + ( (this.total_pieces - this.pick_no) + 1 ) + " more before consulting Tarot.");
        return 0;
    }
    return 1;
}

// for this to work, ChangeGhost must be included!!!!!
//
function selected_changeDeck(objDeckField, objHiddenField) {
    // get value of selected deck
    var idNewDeck = objDeckField.options[objDeckField.selectedIndex].value;
    // if a deck is selected (value not null)
    if (idNewDeck != '') {
        // update the selected deck, including hidden form field
        this.deck = idNewDeck;
        objHiddenField.value = idNewDeck;
        // now get the deck folder name (jsDeckFolders array comes from PHP Deck.class)
        var strNewDeck = jsDeckFolders[idNewDeck];
        // loop through cards to find next "open slot"
        for (var i=1; i <= this.total_pieces; i++) {
            // if card is picked, set face to selected deck and update document image
            if (this.picked_pieces[i]) {
                this.picked_pieces[i].image.set_face(idNewDeck);
                turnOver(i, this);
            // else set ghost image to selected deck
            } else {
                ChangeGhost(i, strNewDeck);
            }
        }
    }
}

function selected_clear() {
    // erase picked pieces
    this.picked_pieces = new Array();
//    for (i=1;i<12;i++) {
//        this.picked_pieces[i] = -1;
//    }
    // reset pick num
    this.pick_no = 1;
}

function vn_selected( total_pieces, form, action, deck ) {
// constructor for vn_selected object, 
// a container class for selected pieces (or pieces to be selected)
// like the scrabble holder -- can hold up to 7 scrabble pieces
//
// total_pieces is the max size of vn_selected -- the total num of peices it can hold
// form is the form object on the page -- the one needing to be submitted or altered, etc.
// read_loc is the url of the appropriate reading page (different "readings" have different 
//        reading pages, i.e. sample readings, expert readings, look up any card)
// action is either new (none selected), half-saved (middle of reading), or saved
// deck is the deck being used, i.e. rider, sacred_rose
//

    // properties
    
    this.form = form;
    
    // page action may be saved, half-saved, or new
    if( action ) this.page_action = action;
    else this.page_action = 'new';
    
    if (deck) this.deck = deck;
    else this.deck = 23; // default deck NUMBER
    
    this.total_pieces = total_pieces;
    this.picked_pieces = new Array(total_pieces);
    this.pick_no = 1; // pick_no incremented after every card selected
    
    
    //methods
    this.insert = selected_insert;
    this.set_form_value = selected_setFormValue;
    this.call_reading = selected_callReading;
    this.check_form = selected_checkForm;
    this.change_deck = selected_changeDeck;
    
    this.clear = selected_clear;
}

function ChangeGhost(numPosition, strDeck) {
    var strPieceName = 'cardimg' + numPosition;
    document[strPieceName].src = IMAGE_SRC + "images/decks/blank.gif";
    
    var nameImg = "name" + numPosition;
    document[nameImg].src = IMAGE_SRC + "images/blank.gif";
}
