
var ImageSelector = {
    index: 1,
    rows: 3,
    cols: 3,
    x: 0,
    y: 0,
    page: 1,

    init: function(_currImgIndex, _total) {
        this.index = _currImgIndex;
        this.page = Math.ceil(_currImgIndex / (this.cols * this.rows));
        this.total = _total;
        this.setCurrentCoords();
        this.mark(this.index, true);
        this.initEvents();
    },

    initEvents: function() {
        $J(document).bind("keydown", function(e) { ImageSelector.OnKeyDown(e); if (e.keyCode == 32) e.preventDefault(); });
        //$J(document).bind("keydown", function(e){  {ImageSelector.OnKeyDown(e); e.preventDefault();} });
    },

    blockEvents: function() {
        $J(document).unbind("keypress");
        $J(document).unbind("keydown");
    },

    setCurrentCoords: function() {
        var pageIndex = this.index - (this.page - 1) * this.cols * this.rows;

        if (pageIndex > this.cols * this.rows) pageIndex = this.cols * this.rows;
        this.y = Math.ceil(pageIndex / this.cols);
        var xt = (pageIndex % this.cols);
        this.x = xt != 0 ? xt : this.cols;
    },

    mark: function(_index, _cancelFireEvent) {
        this.index = _index;
        $J($J(".media-thumb")[this.index - 1]).addClass("media-thumb-selected");
        if (_cancelFireEvent !== true) {
            changeViewMediaFile($J($J(".media-thumb")[this.index - 1]).find(".mediaViewFileInAlbum")[0]);
        }
    },

    unmark: function(_index) {
        $J($J(".media-thumb")[_index - 1]).removeClass("media-thumb-selected");
    },

    setIndexByCoords: function() {
        this.unmark(this.index);
        //jQuery.Console.writeLn(this.page);
        this.mark(((this.y - 1) * this.cols + this.x) + (this.page - 1) * this.cols * this.rows);
    },

    ChangePage: function(_page, _isShowLastOnThePage) {
        var nIndex;
        if (_isShowLastOnThePage) {
            nIndex = _page * this.cols * this.rows;
            if (nIndex > this.total) nIndex = this.total;
        } else {
            nIndex = (_page - 1) * this.cols * this.rows + 1;
        }
        if (nIndex != this.index) {
            this.page = _page;
            if (onKeyboardChangePage) {
                this.unmark(this.index);
                this.mark(nIndex);
                this.index = nIndex;
                this.setCurrentCoords();
            }
            else {
                this.x = 1;
                this.y = 1;
                this.setIndexByCoords();
            }
        }
    },

    OnKeyDown: function(e) {
        switch (e.keyCode) {
            case 38: case 104:
                this.moveUp();
                e.preventDefault();
                e.stopPropagation();
                break;
            case 40: case 98:
                this.moveDown();
                e.preventDefault();
                e.stopPropagation();
                break;
            case 37: case 100:
                this.movePrev();
                e.preventDefault();
                e.stopPropagation();
                break;
            case 39: case 102:
                this.moveNext();
                e.preventDefault();
                e.stopPropagation();
                break;
            case 32:
                this.moveNext();
                e.preventDefault();
                e.stopPropagation();
                break;
            /*case 8:
            this.movePrev();
            e.preventDefault();
            e.stopPropagation();
            break;*/ 
            default:
                break;
        }
    },

    moveUp: function() {
        if (this.y - 1 <= 0) return;
        this.y--;
        this.setIndexByCoords();
    },
    moveDown: function() {
        if (this.y + 1 > this.rows || this.index + 1 > this.total) return;
        this.y++;
        this.setIndexByCoords();
    },

    moveNext: function() {
        var pageIndex = this.index - (this.page - 1) * this.cols * this.rows;
        if (pageIndex < 9) {
            var ti = this.index + 1;
            if (ti <= this.total) {
                this.unmark(this.index);
                this.mark(ti);
                this.setCurrentCoords();
            }
        } else {
            var tp = this.page + 1;
            if (this.index + 1 <= this.total) {
                onPageClick(tp, true, true);
                //this.ChangePage(tp);	
                this.setCurrentCoords();

            }
        }
    },
    movePrev: function() {
        var pageIndex = this.index - (this.page - 1) * this.cols * this.rows;
        if (pageIndex > 1) {
            this.unmark(this.index);
            var ti = this.index - 1;
            this.mark(ti);
            this.setCurrentCoords();
        } else {
            var tp = this.page - 1;
            if (tp > 0) {
                onPageClick(tp, true);
                //this.ChangePage(tp, true);
                this.setCurrentCoords();
            }
        }
    },
    moveTo: function(_index) {
        this.unmark(this.index);
        this.mark(_index);
        this.setCurrentCoords();
    }
}
