// ---------------------------------------------------------------------------
//  cbLocTopix.js
//
//  the Loc object as it is used and needed for Topix style Locs
// ---------------------------------------------------------------------------
"use strict";
/*global LBPLoc, isDef */

function Loc( seqno, tlc, name, type, area, ow_o, ow_d, rt_o, rt_d, from,
          until, parent, hex, brand ) {

    this.seqno    = seqno;
    this.tlc      = tlc;
    this.name     = name;
    this.type     = type;
    this.area     = area;
    this.ow_o     = ow_o;
    this.ow_d     = ow_d;
    this.rt_o     = rt_o;
    this.rt_d     = rt_d;
    this.from     = from;
    this.until    = until;
    this.parent   = parent;
    this.min      = from;
    this.max      = until;
    this.str      = "";
    this.hex      = hex;
    this.brand    = brand;

    //-------------------------------------------------------------------------
    // explode hex compressed to '0'/'1' binary data
    //-------------------------------------------------------------------------

    function hex_to_bin( hstr ) {
        var b = "",
            i;
        for ( i = 0; i < hstr.length; i += 1 ) {
            b += LBPLoc.HB[ hstr.charAt( i ) ];
        }
        return b;
    }

    if ( this.hex && this.hex.length ) {
        this.str  = hex_to_bin( this.hex );
    }

    this.init();
}

Loc.prototype = LBPLoc;

Loc.fitting_origins = function( locs, type ) {
    var i, fitting_locs = [];

    for ( i = 0; i < locs.length; i += 1 ) {
        if ( locs[ i ].ow_o === "O" || locs[ i ].rt_o === "O" ) {
            if ( !type || locs[ i ].type === type ) {
                fitting_locs.push( locs[ i ] );
            }
        }
    }
    return fitting_locs;
};

Loc.fitting_origins_to = function( loc, locs, mode ) {
    var ind = ( loc && isDef(loc.seqno) ) ? loc.seqno : -1,
        i, l, fitting_locs = [];

    for ( i = 0; i < locs.length; i += 1 ) {
        l = locs[ i ];
        if ( ind < 0 || l.str.substr( ind, 1 ) === "1" ) {
            if ( ( !mode && ( l.ow_o === "O" || l.rt_o === "O" ) ) ||
                 ( mode === "OW" && l.ow_o === "O" ) ||
                 ( mode === "RT" && l.rt_o === "O" ) ) {
                fitting_locs.push( l );
            }
        }
    }

    return fitting_locs;
};

Loc.fitting_destinations = function( locs, type ) {
    var i, fitting_locs = [];

    for ( i = 0; i < locs.length; i += 1 ) {
        if ( locs[ i ].ow_d === "D" || locs[ i ].rt_d === "D" ) {
            if ( !type || locs[ i ].type === type ) {
                fitting_locs.push( locs[ i ] );
            }
        }
    }
    return fitting_locs;
};

Loc.fitting_destinations_from = function( loc, locs, mode ) {
    var i, l, fitting_locs = [];

    for ( i = 0; i < locs.length; i += 1 ) {
        l = locs[ i ];
        if ( !loc  || loc.str.substr( l.seqno, 1 ) === '1' ) {
            if ( ( !mode && ( l.ow_o === "D" || l.rt_o === "D" ) ) ||
                 ( mode === 'OW' && l.ow_d === 'D' ) ||
                 ( mode === 'RT' && l.rt_d === 'D' ) ) {
                fitting_locs.push( l );
            }
        }
    }
    return fitting_locs;
};


// ---------------------------------------------------------------------------
//  ... the end
// ---------------------------------------------------------------------------

