﻿// Custom js modal box implementation
// Author: Omerz
var _modalBoxIndex = 0, _modalBoxVisibleCount = 0;

function initModalBox(){
    var srcModalBox = $("#modalBox");
    var tarModalBoxes = $("#modalBoxes");
    var modalBgMask = $("#modalBox_bgmask");
    
    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;
        }
        
        var initHandler,closeHandler;
        if(customEvents!=null && customEvents.onInit != null)
            initHandler = customEvents.onInit;
        
        if(customEvents!=null && customEvents.onClose != null)
            closeHandler = customEvents.onClose;
        
        // 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);
        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.hide();
                _modalBoxVisibleCount = 0;
                window.onscroll=function(){};
            }
            
            //setScroll(true);
            if(closeHandler!=null)
                closeHandler();
        };
        
        $.extend(customEvents,{modal:createdBox[0]});
        createdBox[0].Events = customEvents;
        
        // disable text selections both of moz and ie
//        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(initHandler!=null)
            initHandler();
        
        // set min width
        if(createdBox.width()<300)
        {
            createdBox.find("table").width(300);
        }                   
        
        //set box to center of the screen
        var newPosx = ($(window).width()/2) - (createdBox.width()/2);// - $(document).scrollLeft();
        var newPosy = ($(window).height()/2) - (createdBox.height()/2);// - $(document).scrollTop();
        
        if(_modalBoxVisibleCount>0){
            newPosx += (10 *_modalBoxVisibleCount);
            newPosy += (10 *_modalBoxVisibleCount);
        }
        
        createdBox.css("left",newPosx);
        createdBox.css("top",newPosy);

        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);
            };
        }
        
        //setScroll(false);
        createdBox.show();
        modalBgMask.show();                
        
        _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();
}
