﻿// Custom js modal box implementation
// Author: Omerz
var _modalBoxIndex = 0, _modalBoxVisibleCount = 0;

function initModalBox(){
    var srcModalBox = $("#modalBox");
    var tarModalBoxes = $("#modalBoxes");
    var modalBgMask = $("#modalBox_bgmask");
    var zStarter = 800;
    // Property parameter options
    // disableSelection: disables text selection
    // disableClose: hides close button
    // containment: Constrains dragging to within the bounds of the specified element. default: body
    window.modalWindow = function(objMessage, strMsgTitle, customEvents, properties) {
        var isObjectiveContent = false, objectiveContent = '', strModalMsg = 'N/A';
        if (strMsgTitle == null)
            strMsgTitle = "İsimsiz Pencere " + (_modalBoxVisibleCount + 1);

        if ((typeof objMessage) == "string" && !objMessage.isHtml()) {
            strModalMsg = objMessage;
            strModalMsg = strModalMsg.replace(/\r\n/g, "<br/>");
            strModalMsg = strModalMsg.replace(/\r/g, "<br/>");
            strModalMsg = strModalMsg.replace(/\n/g, "<br/>");
        } else if ((typeof objMessage) == "object") {
            isObjectiveContent = true;
            objectiveContent = objMessage.html();
            objMessage.empty();
            strModalMsg = objectiveContent;
        }

        // Create new instance of modalBox
        var strNewID = "modal" + _modalBoxIndex;
        var strModalHtml = srcModalBox.html();
        strModalHtml = strModalHtml.replace(/modalid_replacement/g, strNewID);
        tarModalBoxes.append(strModalHtml);

        var strCreatedBox = "#" + strNewID + "_box";
        var createdBox = $(strCreatedBox);
        if (properties != null && properties.CssClass != null) {
            createdBox.removeClass("tblPopup").addClass(properties.CssClass);
        }
        var createdBox_title = $(strCreatedBox + "_title");
        var createdBox_content = $(strCreatedBox + "_content");

        if (properties != null && properties.disableClose == true) {
            createdBox.find('.btn_mn_kapat').empty();
        }

        createdBox[0].Close = function() {
            if (isObjectiveContent)
                objMessage.html(objectiveContent);
            _modalBoxVisibleCount--;
            $(this).remove();
            if (_modalBoxVisibleCount <= 0) {
                modalBgMask.css("display", "none");
                _modalBoxVisibleCount = 0;
                window.onscroll = function() { };
            } else {
                modalBgMask.css("zIndex", $('div#modalBoxes>.modalBox:last').css('zIndex') - 1);
            }

            if (createdBox[0].Events != null && createdBox[0].Events.onClose != null)
                createdBox[0].Events.onClose();
        };

        $.extend(customEvents, { modal: createdBox[0] });
        createdBox[0].Events = customEvents;

        // disable text selections both of moz and ie
        if (properties != null && properties.disableSelection == true) {
            createdBox[0].onselectstart = function(e) {
                return false;
            };
            createdBox.css("-moz-user-select", "none");
        }

        createdBox_title.text(strMsgTitle);
        createdBox_content.html(strModalMsg);

        //onselectstart="return false;"
        //createdBox_content.find('input').bind('selectstart',function(){ return tu });

        if (createdBox[0].Events != null && createdBox[0].Events.onInit != null)
            createdBox[0].Events.onInit();

        // set min width
        if (createdBox.width() < 300) {
            createdBox.find("table").width(300);
        }

        var newPosx = 0, newPosy = 0;
        var $window = $(window), $document = $(document);

        //set box to center of the screen
        newPosx = ($window.width() / 2) - (createdBox.width() / 2); // - $(document).scrollLeft();
        newPosy = ($window.height() / 2) - (createdBox.height() / 2); // - $(document).scrollTop();

        if (createdBox.height() > $window.height())
            newPosy = 0;

        if (_modalBoxVisibleCount > 0) {
            newPosx += (10 * _modalBoxVisibleCount);
            newPosy += (10 * _modalBoxVisibleCount);
        }

        createdBox.css("left", newPosx);
        createdBox.css("top", newPosy);
        //draggable
        if (properties != null && properties.disableDragging == true) {

        }
        else {
            //createdBox.draggable({ handle: '.handler', containment: ((properties != null && properties.containment != null) ? properties.containment : 'body')});
            createdBox.draggable({ handle: '.handler' });
        }
        if ($.browser.msie && ($.browser.version.indexOf("6.") > -1 ? true : false)) {
            modalBgMask.height($document.height());
            createdBox.css("top", $document.scrollTop() + newPosy);
            window.onscroll = function(evt) {
                createdBox.css("top", $document.scrollTop() + newPosy);
            };
        }

        // setting up Z order 
        createdBox.css("zIndex", _modalBoxIndex + zStarter);
        if (_modalBoxVisibleCount <= 0) {
            modalBgMask.css("display", "block");
            modalBgMask.css("zIndex", _modalBoxIndex + zStarter - 1);
        } else {
            modalBgMask.css("zIndex", _modalBoxIndex + zStarter - 1);
        }
        createdBox.css("display", "block");
        //setScroll(false);
        //createdBox.show();
        //modalBgMask.show();                

        if (createdBox[0].Events != null && createdBox[0].Events.onShow != null)
            createdBox[0].Events.onShow();

        _modalBoxIndex++;
        _modalBoxVisibleCount++;
        return createdBox;
    }
}

function GetModal(ref){
    return $(ref).parents('.modalBox')[0];
}

function CloseActiveModal(){
    var modalBoxes = $("div[id^=modal].tblPopup");
    modalBoxes[modalBoxes.length-1].Close();
    return true;
}

function CloseModal(intIndex){
    var modalBoxes = $("div[id^=modal].tblPopup");
    if(intIndex>=modalBoxes.length) return false;
    modalBoxes[intIndex].Close();
}