﻿var boards = {
    //--autoRepublish
    autoRepublish: {
        currentBoardId: null,
        data: {
            boardId: null,
            occursInterval: null,
            timeFrom: null,
            timeTo: null,
            days: null
        },
        boardsSettings: {}, //isUsing this.data structure
        //--
        showSettings: function(boardId) {
            this.currentBoardId = boardId;
            var repubData = this.boardsSettings[boardId];

            $j('#occurInterval').val(this.boardsSettings[boardId].occursInterval);

            $j('#repubSettings .dd input:checked').each(function() { $j(this).click(); });
            $j('#repubTimeFrom').val(repubData.timeFrom);
            $j('#repubTimeTo').val(repubData.timeTo);

            $j(repubData.days.split(',')).each(function() { $j('#repubDay' + this).click(); });

            boards.autoRepublish.checkTimeInterval();
            $j('#repubSettings').show();
        },
        showSettingsBoardsMy: function(boardId) {
            $j('#repubSettings').css('top', findPosY($j('#moneyOpers_' + boardId).get(0)) + 220 + 'px');
            $j('#repubSettings').css('left', findPosX($j('#moneyOpers_' + boardId).get(0)) + 202 + 'px');

            this.showSettings(boardId);
        },
        showSettingsLevelFour: function(boardId) {
            $j('#repubSettings').css('top', '50%');
            $j('#repubSettings').css('left', '50%');

            this.showSettings(boardId);
        },
        hideSettings: function() {
            $j('#repubSettings').hide();
            boards.autoRepublish.setTimesOccursPerDay();
            this.currentBoardId = null;
        },
        applySettings: function() {
            if ($j('#applySettings').attr('disabled'))
                return;
            this.saveSettings();
            //--set manual[visibility] and leuPerDay[data+visibility]
            var isAuto = $j('#occurInterval').attr('selectedIndex') > 0;
            $j('#mrepub_' + this.currentBoardId).get(0).className = isAuto ? "mrepub0" : "mrepub1";
            if (isAuto) {
                $j('#leuPerDay_' + this.currentBoardId).html($j('#leuPerDay').html());
                $j('#leuPerDayContainer_' + this.currentBoardId).show();
                $j('#mrepub_' + this.currentBoardId).removeAttr('href');
            }
            else {
                $j('#leuPerDayContainer_' + this.currentBoardId).hide();
                $j('#mrepub_' + this.currentBoardId).attr('href', $j('#mrepub_' + this.currentBoardId).attr('link'));
            }
            this.hideSettings();
        },
        saveSettings: function() {
            var occurIntervalObj = $j('#occurInterval');
            if (occurIntervalObj.attr('selectedIndex') > 0)
                this.boardsSettings[this.currentBoardId].occursInterval = occurIntervalObj.val();

            Private.ChangeAutoRepublishSettings(
                    this.currentBoardId,
                    occurIntervalObj.val(),
                    this.boardsSettings[this.currentBoardId].days = this.getDays(),
                    this.boardsSettings[this.currentBoardId].timeFrom = $j('#repubTimeFrom').val(),
                    this.boardsSettings[this.currentBoardId].timeTo = $j('#repubTimeTo').val());
        },
        checkDay: function(me) {
            $j(me).parent().parent().toggleClass('ddis');
            boards.autoRepublish.setTimesOccursPerDay();
        },
        checkTimeInterval: function() {
            if ($j('#repubTimeFrom').attr("selectedIndex") >= $j('#repubTimeTo').attr("selectedIndex")) {
                $j('#repubErrMsg').show();
                $j('#applySettings').attr('class', base.isRo ? 'roOff' : 'ruOff');
                $j('#applySettings').attr('disabled', 'disabled');
            }
            else {
                $j('#repubErrMsg').hide();
                $j('#applySettings').attr('class', base.isRo ? 'roOn' : 'ruOn');
                $j('#applySettings').removeAttr('disabled');
            }
            this.setTimesOccursPerDay();
        },
        getDays: function() { var days = ''; $j('#repubSettings .dd input:checked').each(function() { days += (days != '' ? ',' : '') + $j(this).attr('value'); }); return days; },
        setTimesOccursPerDay: function() {
            var count = 0;
            var occurIntervalObj = $j('#occurInterval');
            if (this.getDays() != '' && occurIntervalObj.attr('selectedIndex') > 0) {
                var hoursToAdd = occurIntervalObj.val().substr(0, 2) * 1;
                var minsToAdd = occurIntervalObj.val().substr(3, 2) * 1;

                var dateFromVal = $j('#repubTimeFrom').val();
                var dateFrom = new Date(0, 0, 0, dateFromVal.substr(0, 2) * 1, dateFromVal.substr(3, 2) * 1, 0, 0);
                var dateToVal = $j('#repubTimeTo').val();
                var dateTo = new Date(0, 0, 0, dateToVal.substr(0, 2) * 1, dateToVal.substr(3, 2) * 1, 0, 0);

                while (dateFrom < dateTo) {
                    dateFrom.setHours(dateFrom.getHours() + hoursToAdd);
                    dateFrom.setMinutes(dateFrom.getMinutes() + minsToAdd);
                    count++;
                }
            }
            $j('#leuPerDay').html(count);
        }
    },
    //--upload [add || adit] board + vip
    upload: {
        boardId: null,
        photoUploadId: null,
        videoUploadId: null,
        photoCount: 0,
        maxPhotoCount: 5,
        hasVideo: false,
        uploaderUrl: null,
        mediaTypes: {
            photo: 0,
            video: 1
        },
        commands: {
            getPath: 0,
            remove: 1,
            getAll: 2
        },
        uploadStatus: {
            succesfull: 0,
            processing: 1,
            failed: 2
        },
        photoItemTemplate: "<div class='ld' id='-SessionId-'><div class='i'><div></div><img src='-FilePath-'/></div><div style='float: left;'><span class='fileno'>-FileNo-</span><br/>-FileSize-<br/><a href='javascript:void(0);' onclick='boards.upload.removePhoto(this,\"-FileName-\");'>-ActionText-</a></div></img></div>",
        initialize: function(boardId) {
            this.boardId = boardId;
            HttpRequest.get(this.uploaderUrl + 'synchronizer.ashx', { cmdid: this.commands.getAll, boardid: this.boardId });
        },
        populate: function(boardsInfo) {
            $j(boardsInfo).each(function() { if (this.MediaType == boards.upload.mediaTypes.photo) boards.upload.addPhoto(this); else boards.upload.addVideo(this); });
            this.changeVisibility();
        },
        changeVisibility: function() {
            if (this.photoCount == 0) {
                $j('#photoPreview').hide();
                $j('#photoDescription').show();
            }
            else {
                $j('#photoDescription').hide();
                $j('#photoPreview').show();
            }
            if (this.hasVideo) {
                ; //todo:implement for video
            }
            if (this.photoCount >= this.maxPhotoCount) {
                $j('#photoUploader').css('z-index', 0);
                $j('#addPhotoLnk').css('color', '#9A9DA1').css('cursor', 'default');
            }
            else {
                $j('#photoUploader').css('z-index', 2);
                $j('#addPhotoLnk').css('color', '').css('cursor', '');
            }
        },
        addPhoto: function(boardInfo) {
            var adding = this.photoItemTemplate;
            for (var key in boardInfo) {
                var regExp = new RegExp('-' + key + '-', 'g');
                adding = adding.replace(regExp, boardInfo[key]);
            }
            adding = adding.replace(new RegExp('-FileNo-', 'g'), '№' + ++this.photoCount)
                           .replace(new RegExp('-SessionId-', 'g'), base.newGuid().substr(0, 6))
                           .replace(new RegExp('-ActionText-', 'g'), 'Delete');
            $j('#photoPreview').append(adding);
        },
        addVideo: function(boardInfo) {
            //todo: implement for video
        },
        reIdentPhotos: function() {
            var photoNo = 0;
            $j('#photoPreview .fileno').each(function() { $j(this).html('№' + ++photoNo); });
        },
        setPhotoPath: function(fileInfo) {
            if (fileInfo.uploadStatus == this.uploadStatus.succesfull) {
                $j('#' + fileInfo.sessionId + ' img:first').attr('src', fileInfo.filePath);
            }
            else if (fileInfo.uploadStatus == this.uploadStatus.failed) {
                //todo:implement errors
                $j('#' + fileInfo.sessionId).remove();
            }
            else {
                var timerId = window.setInterval(
                    function() {
                        boards.upload.callGetPath(fileInfo.fileName, fileInfo.sessionId);
                        window.clearInterval(timerId);
                    }, 1000);
            }
        },
        prepareForUpload: function(event, ID, fileObj) {
            var adding = this.photoItemTemplate
                .replace(new RegExp('-SessionId-', 'g'), ID)
                .replace(new RegExp('-FileNo-', 'g'), '№' + ++this.photoCount)
                .replace(new RegExp('-FilePath-', 'g'), 'http://999.md/images/no_foto_med.gif')
                .replace(new RegExp('-FileSize-', 'g'), base.getSizeText(fileObj.size))
                .replace(new RegExp('-FileName-', 'g'), fileObj.name)
                .replace(new RegExp('-ActionText-', 'g'), 'Cancel');
            $j('#photoPreview').append(adding);
            this.changeVisibility();
        },
        onPhotoProgress: function(event, queueID, fileObj, data) { $j('#' + queueID + ' div:empty').css('width', data.percentage + '%'); },
        onPhotoUploadComplete: function(event, queueID, fileObj, response, data) {
            $j('#' + queueID + ' div:empty').remove();
            $j('#' + queueID + ' a:first').html('Delete');
            this.callGetPath(fileObj.name, queueID);
        },
        callGetPath: function(fileName, queueID) {
            HttpRequest.get(this.uploaderUrl + 'synchronizer.ashx', { cmdid: this.commands.getPath, muid: this.photoUploadId, filename: fileName, sessionId: queueID });
        },
        removePhoto: function(me, fileName) {
            this.photoCount--;
            $j(me).parent().parent().remove();
            this.reIdentPhotos();
            this.changeVisibility();
            HttpRequest.get(this.uploaderUrl + 'synchronizer.ashx', { cmdid: this.commands.remove, muid: this.photoUploadId, filename: fileName });
        }
    }
}