﻿//--------------------------------------------------------
// dialog operations:
// includes: 
//  <script type="text/javascript" src="/javascript/jquery/jquery-1.4.2.min.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/bgiframe/v2.1.1/jquery.bgiframe-2.1.1.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/jquery-ui-1.8.2/jquery.ui.core.min.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/jquery-ui-1.8.2/jquery.ui.widget.min.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/jquery-ui-1.8.2/jquery.ui.mouse.min.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/jquery-ui-1.8.2/jquery.ui.draggable.min.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/jquery-ui-1.8.2/jquery.ui.position.min.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/jquery-ui-1.8.2/jquery.ui.resizable.min.js"></script>
//  <script type="text/javascript" src="/javascript/jquery/jquery-ui-1.8.2/jquery.ui.dialog.min.js"></script>       
//  <script type="text/javascript" src="/javascript/jquery/easing/v1.3/jquery.easing.1.3.js"></script>
//--------------------------------------------------------

// gets confirm for postback command.
function agahSystems_dialog_getCommandConfirm(commandTarget,commandArgument,message)
{
    agahSystems_dialog_show({
            title: 'توجه',
            bodyHtml: message,
            icon: { type: "confirmation"},
            modal:true,
            backgroundOpacity:.2,
            buttons:{
                        'بلی': function(){
                            $(this).dialog('close');
                            __doPostBack(commandTarget,commandArgument);        
                        },
                        'خیر': function(){
                            $(this).dialog('close');
                        }
                     }
        });
}


function agahSystems_dialog_show(params)
{
    //
    var href=params.href;
    var bodyHtml=params.bodyHtml;
    var panelId=params.panelId;
    var scrolling=params.scrolling;    
    var panelIdSet= (params.panelId!=null);
    var showEffect=params.showEffect;
    var icon=params.icon;
    var backgroundOpacity=params.backgroundOpacity;
    
    if (scrolling==null)
        scrolling='auto';
    
    if (panelId==null)
        panelId='agahSystems_dialog_panel';    
    
    // deleting extera parameters
    delete params.href;
    delete params.scrolling;
    delete params.bodyHtml;
    delete params.panelId;
    delete params.showEffect;
    delete params.icon;
    delete params.backgroundOpacity;

    
    //
    if (icon!=null)
    {
        if (typeof(icon)=='string')
            icon={href:icon};        
    } 

    // 
    if (icon!=null)
    {
        switch(icon.type)
        {
            case 'success': 
                icon.href='/icon/general/48x48/dialog_success.jpg';
                break;
            case 'information': 
                icon.href='/icon/general/48x48/dialog_information.jpg';
                break;
            case 'warning':
                icon.href='/icon/general/48x48/dialog_warning.png';
                break;
            case 'error':
                icon.href='/icon/general/48x48/dialog_error.png';
                break;
            case 'confirmation':
                icon.href='/icon/general/48x48/dialog_confirmation.png';
                break;
            default:
                // nothing needed here.
        }
    }      
    
    // creating dialog panel
    var s;
    var panel;
    var dlg;
    var panelCreated=false;
    var oldPanelParent;
    
    if (href!=null || bodyHtml!=null)
    {            
        if (bodyHtml==null)
            bodyHtml="<iframe FRAMEBORDER='0' scrolling='[[scroll]]'  ALLOWTRANSPARENCY='true' width='100%' height='100%' src='[[href]]'/>";
            
        s="";
        s+="<div id='[[panelId]]' style='display:none;'>";
        if (icon!=null)
        {
            s+="<img src='[[icon]]' align='right' hspace='5' />";
        }
        s+=bodyHtml;
        s+="</div>";
            
        //    
        s=s.replace("[[href]]",href)
           .replace("[[scroll]]",scrolling)
           .replace("[[panelId]]",panelId);

        if (icon!=null)           
           s=s.replace("[[icon]]",icon.href);

        //    
        panelCreated=true;
        $(document.body).prepend(s);
        panel=$("#"+panelId);                
    } else if (panelIdSet)
    {
        panel=$("#"+panelId);
        oldPanelParent=panel.parent();       
    }  else {
        throw "Invalid dialog box parameters. href or bodyHtml or panelId should be set for dialog.";
    }

    //-----------------------------------------------------------
    // auto hide
    //-----------------------------------------------------------
    var autoHide=params.autoHide;
    var autoHideTimer=null;
    
    if (autoHide==false)
        autoHide=null;
    if (autoHide==true)
        autoHide={duration:8000};    
    
    if (autoHide)
    {
        autoHideTimer=window.setTimeout(function(){
            if (!panel.dialog('isOpen'))
                return;

            var back=$('.ui-widget-overlay');
            
            back.animate({
                opacity :0
                },1200
            );
                            
            //             
            dlg.animate({
                height:0                    
                },1000,'easeInExpo',function(){
                        panel.dialog('close');
                        }
                );                    
                                                        
            },autoHide.duration);
    }                

    //-----------------------------------------------------------
    // close handle
    //-----------------------------------------------------------        
    //
    var oldCloseHandler=params.close;
    params.close= function ()
    {
        $(this).remove();        
        if (!panelCreated)
        {            
            oldPanelParent.append(panel);
        }
        
        //
        if (autoHideTimer!=null)
            window.clearTimeout(autoHideTimer);
        
        //
        if (oldCloseHandler)
            oldCloseHandler();
    };


    //-----------------------------------------------------------
    // show dialog
    //-----------------------------------------------------------                
    //
    panel.dialog(params);
    dlg=panel.dialog('widget');    
    
    if (backgroundOpacity!=null)
    {
        $('.ui-widget-overlay').css({opacity:backgroundOpacity});
    }
    
    //-----------------------------------------------------------
    // show effect
    //-----------------------------------------------------------                    
    //    
    if (showEffect!=null && params.show==null)
    {
        agahSystems_dialog_effect(panel,showEffect);
    }
      
    //
    return panel;
}


function agahSystems_dialog_close(params) {

    if (params == null) {
        params = { panelId: null };
    }    
    
    var panelId = params.panelId;
    if (panelId == null)
        panelId = 'agahSystems_dialog_panel';

    $("#" + panelId).dialog('close');
}

function agahSystems_dialog_effect(panel,params)
{
    var effect=params.effect;
    var dlg=panel.dialog('widget');    
    
    //-------------------------------------------------
    // bounce drop        
    //-------------------------------------------------
    if (effect=='bounceDrop')
    {
        //
        if (params.duration==null)
            params.duration=800;
            
        if (params.backgroundDuration==null)
            params.backgroundDuration=Math.max(params.duration-300,0);        
            
        if (params.backgroundOpacity==null)
            params.backgroundOpacity=.3;
    
        // start poistion
        dlg.offset( { top:$(document).scrollTop(),
                     left:$(document).scrollLeft()+($(window).width()-dlg.outerWidth())/2 });
        
        // animate                            
        dlg.animate({
            top       : ( $(document).scrollTop() + ($(window).height() - dlg.outerHeight()) / 2 )
            },{
            duration  : params.duration,
            easing    : 'easeOutBack'
         });
     
        // background
        var back=$('.ui-widget-overlay');
        back.css({'opacity':0});       
        $(back).animate({
            opacity : params.backgroundOpacity
            },params.backgroundDuration);
        
    } else
    {
        throw "Unknown effect. effect:"+effect;
    }
        
}



// shows message
function agahSystems_dialog_showMessageBox(params)
{  
    var title='';
    var iconType=null;
    
    // finding icon and title
    switch(params.messageType)
    {
        case 0: // success
            title='اطلاعات';
            iconType='success';            
            break;
        case 1: // information
            title='اطلاعات';
            iconType='information';
            break;
        case 2: // warning
            title='هشدار';
            iconType='warning';
            break;
        case 3: // error
            title='خطا';
            iconType='error';
            break;
        default:
            // nothing needed here.
    }      
        
    delete params.messageType;
    
    //    
    if (params.title==null)
        params.title=title;        
    
    if (params.icon==null)
        params.icon={type:iconType};
        
    if (params.resizable==null)
        params.resizable=false;
    
    if (params.draggable==null)
        params.draggable=true;
     
    if (params.modal==null)
        params.modal=true;
        
    if (params.width==null)
        params.width=350;        
        
    if (params.showEffect==null)
        params.showEffect={effect:'bounceDrop'};
    
    //
    agahSystems_dialog_show(params);    
}
