/**
 * message framework
 * @author jonathan
 * 
 * @todo 현재 한개의 div 를 사용하나 여러개의 메세지를 띄울 수 있도록 변경할것.
 */

if (typeof SayCastMsg == "undefined") {
  
  SayCastMsg = {
     
    divNum : 1,
    
    init: function(){
      if( $("#_SayMsg").length <= 0  ) 
      {
        var div = '<DIV id="_SayMsg" style="display:none;position:absolute;background-color:#eee;z-index:255"></DIV>';
        $("body").append(div); 
      }    
    },
    
    basic : function( msg, p , callback1 , callback2 ){ // 기본.     
      
      if (typeof p == 'undefined') {
        p = { x : '' , y : '' , w : '', h : ''};
      }  
     
      this.init();
     
      var o  = $("#_SayMsg");   
      if(p.w){o.css("width", p.w + 'px');}      
      if(p.h){o.css("height",p.h + 'px');}
      
      o.html(msg);
      this.show();
      this.location(o, p.x , p.y);
       
      $("#_SayMsgYes").click(callback1);
      $("#_SayMsgYes").click(function(){SayCastMsg.hide()});
      
      $("#_SayMsgNo").click(callback2);
      $("#_SayMsgNo").click(function(){SayCastMsg.hide()});
      
      return o;      
    },
    
    location : function( o, x , y){ // 위치 옮겨주기. (위치 정보가 없으면 가운데.)      
      if(!x  || !y){
         
         if (!x) {
           
           var width = o.css("width");
           if( width == 'auto')
             width = 200;
           else
             width = parseInt(width);  
             
           var left = ( $(window).width() / 2 ) - ( width / 2) + 'px';
           o.css('left', left );
         }
         if (!y) {
           
           var height = o.css("height");
           if( height == 'auto')
             height = 150;
           else
             height = parseInt(height);           
           
           var top =  ( $(window).height() / 2) - ( height / 2) + 'px' ;
           o.css('top', top);
         }         
      }
      
      if(x)
        o.css('left', x + 'px');
        
      if(y)
        o.css('top', y + 'px');
                    
    },
    
    hide : function(){
      $('#_SayMsg').hide();
      $('#_SayMsgModal').hide();
    },
    
    show : function (){
      $('#_SayMsg').show();
    },
    
    alert: function( msg, p, callback){      
      msg = msg + "<br /><br /><p><a href='#' id='_SayMsgYes'>확인</a></p>" ;
      this.basic( msg, p, callback);
    },
    
    confirm: function(msg, p, callback1, callback2 ){
      msg = msg + "<br /><br /><p><a href='#' id='_SayMsgYes'>예</a> <a href='#' id='_SayMsgNo'>아니오</a></p>" ;      
      this.basic( msg, p , callback1, callback2 );
    },

    loading: function(msg , p ){
      if(!msg)
        msg = "로딩중...";
        
      this.basic( msg, p );
    },
    
    mLoading: function(msg, p){ //로딩중 화면 여러개 띄울때 , return 받은 object hide() method 호출하면  사라짐.
     
     if (typeof p == 'undefined') {
        p = { x : '' , y : '' , w : '', h : ''};
      }  
      
      if(!msg)
        msg = "로딩중...";
      
      $("body").append("<DIV></DIV>");
      o = $("div:last");
      
      o.css("position","absolute");
      o.css("background-color","#eee");
      
      if(p.w){o.css("width",p.w + 'px');}      
      if(p.h){o.css("height",p.h + 'px');}
      
      o.html(msg);      
      o.show();      
      SayCastMsg.location(o, p.x , p.y);
      
      return o ;      
    },   
    modalInit : function (){
       if( $("#_SayMsgModal").length <= 0  ) 
      {
        var div = '<DIV id="_SayMsgModal" style="display:none;filter:alpha(opacity=10);opacity:0.1;-moz-opacity:0.1;position:absolute;top:0px;left:0px;width:100%;height:100%;background-color:black;"></DIV>';
        $("body").append(div); 
      }    
    },
    modalBasic : function(){
      SayCastMsg.modalInit();
      $('#_SayMsgModal').show();
    },
    modalAlert : function(msg, p, callback){
      SayCastMsg.modalBasic();
      SayCastMsg.alert(msg, p, callback);
    },
    modalConfirm : function(msg, p, callback1 , callback2){
      SayCastMsg.modalBasic();
      SayCastMsg.confirm(msg, p, callback1, callback2);
    },
    modalLoading : function(msg,p){
      SayCastMsg.modalBasic();
      SayCastMsg.loading(msg,p);
    }            
  };  
}
