﻿// Custom js modal box implementation
// Author: Omerz
var _alertBoxIndex = 0, _alertBoxVisibleCount = 0;

function initAlertBox(){
    var srcModalBox = $("#alertBox");
    var tarModalBoxes = $("#alertBoxes");
    var modalBgMask = $("#alertBox_bgmask");
    var zStarter = 1800;
    // 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.alertWindow = function(objMessage,strMsgTitle,customEvents,properties)
    {
        var isObjectiveContent = false, objectiveContent='', strModalMsg = 'N/A';
        if(strMsgTitle==null)
            strMsgTitle = "İsimsiz Pencere " + (_alertBoxVisibleCount+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 = "alert" + _alertBoxIndex;
        var strModalHtml = srcModalBox.html();
        strModalHtml = strModalHtml.replace(/id_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);
            _alertBoxVisibleCount--;
            $(this).remove();
            if(_alertBoxVisibleCount<=0){
                modalBgMask.css("display","none");
                _alertBoxVisibleCount = 0;
                window.onscroll=function(){};
            } else {
            modalBgMask.css("zIndex", $('div#alertBoxes>.alertBox: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(_alertBoxVisibleCount>0){
            newPosx += (10 *_alertBoxVisibleCount);
            newPosy += (10 *_alertBoxVisibleCount);
        }
        
        createdBox.css("left",newPosx);
        createdBox.css("top",newPosy);

        createdBox.draggable({ handle: '.handler', containment: ((properties != null && properties.containment != null) ? properties.containment : 'body')});

        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",_alertBoxIndex+zStarter);
        if(_alertBoxVisibleCount<=0){
            modalBgMask.css("display","block");
            modalBgMask.css("zIndex",_alertBoxIndex+zStarter-1);
        } else {
            modalBgMask.css("zIndex",_alertBoxIndex+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();
        
        _alertBoxIndex++;
        _alertBoxVisibleCount++;
        return createdBox;
    }
}

function GetAlert(ref){
    return $(ref).parents('.alertBox')[0];
}

function CloseActiveAlert(){
    var modalBoxes = $("div[id^=alert].tblPopup");
    modalBoxes[modalBoxes.length-1].Close();
    return true;
}

function CloseAlert(intIndex){
    var modalBoxes = $("div[id^=alert].tblPopup");
    if(intIndex>=modalBoxes.length) return false;
    modalBoxes[intIndex].Close();
}

$().ready(initAlertBox);