(function() {
  window.MyCMSDialog = {
    'open':function(param,arg,obj){
      var param = param ? param : {};
      var arg   = arg   ? arg   : {};
      var obj   = obj   ? obj   : null;
      var key   = window.MyCMSDialog._key;

      var param = $.extend({
        'url':null,
        'type':'auto',
        'width':'auto',
        'height':'auto',
        'maxwidth':false,
        'maxheight':false,
        'minwidth':false,
        'minheight':false,
        'parent':false,
        'title':'',
        'content':null,
        'notclose':false,
        'open':function(){},
        'close':function(){},
        '_id':''
      },$(obj).data(),param);  

      if(param.parent==true && parent.window){
        parent.MyCMSDialog._num++
        if(param.content==null && param.type=="auto" && obj){
          if($(obj).get(0).tagName=="A"){
            var href = $(obj).attr('href');
            if($(href).size()>0 && href[0]=="#"){
              param.type = "hash";
              param.url  = href;
              param._id  = href.replace("#","");
            }
          }  
        }
        if(param.type!="auto" && param.content){
          var h = param.content;
            if(h[0]=="#" && $(h).size()>0){
              param.type = "hash";
              param.url  = h;
              param._id  = h.replace("#","");
            }
        }
        if(param.type!="hash"){
          param.type = param.type=="auto" ? 'default' : param.type;
          param._id  = parent.MyCMSDialog._num;
        }
        if(param.type=="iframe" && obj){
          var href = '';
          if($(obj).get(0).tagName=="A"){
            href = $(obj).attr('href');
          }
          param.url  = param.url ? param.url : href;
        }
        param._window = window;
        param._id = 'MyCMSDialog_'+param._id;
        parent.MyCMSDialog._active                                 = param._id;  
        parent.MyCMSDialog._arguments[parent.MyCMSDialog._active]  = arg;
        parent.MyCMSDialog._param[parent.MyCMSDialog._active]      = param;
        parent.MyCMSDialog._objects[parent.MyCMSDialog._active]    = obj;
        parent.MyCMSDialog.__shadow();
        parent.MyCMSDialog.__dialog();
      }else{     
        this._num++
        if(param.content==null && param.type=="auto" && obj){
          if($(obj).get(0).tagName=="A"){
            var href = $(obj).attr('href');
            if($(href).size()>0 && href[0]=="#"){
              param.type = "hash";
              param.url  = href;
              param._id  = href.replace("#","");
            }
          }  
        }
        if(param.type!="auto" && param.content){
          var h = param.content;
            if(h[0]=="#" && $(h).size()>0){
              param.type = "hash";
              param.url  = h;
              param._id  = h.replace("#","");
            }
        }
        if(param.type!="hash"){
          param.type = param.type=="auto" ? 'default' : param.type;
          param._id  = this._num;
        }
        if(param.type=="iframe" && obj){
          var href = '';
          if($(obj).get(0).tagName=="A"){
            href = $(obj).attr('href');
          }
          param.url  = param.url ? param.url : href;
        }
        
        param._id = 'MyCMSDialog_'+param._id;
        this._active                   = param._id;  
        this._arguments[this._active]  = arg;
        this._param[this._active]      = param;
        this._objects[this._active]    = obj; 
        this.__shadow();
        this.__dialog();  
      }
    },
    'close':function(id){
      var id     = id ? id : this._active;
      var option = this.__getParam(id);
      if(option.close && typeof option.close ==="function"){
        var arg = [id];
        option.close.apply( $(this._conteiner[id]), Array.prototype.slice.call( arg ));
      } 
      switch(option.type){
        case 'iframe':
        case 'default':
          $(this._conteiner[id]).remove();
          delete(this._conteiner[id]);
          if(this._objects[id]){
            delete(this._objects[id]);
          }
          if(this._arguments[id]){
            delete(this._objects[id]);
          }
          if(this._param[id]){
            delete(this._objects[id]);
          } 
        break;
        case 'hash':
          $('*[mycms-dialog-id="'+id+'"]').hide();
        break;
      }
      var lastDialog = null;
      for (var value in this._conteiner) {
        if($('*[mycms-dialog-id="'+value+'"]').is(':visible')){
          lastDialog = value;           
        }
      }
      if(lastDialog){
        this.__activeDialog(lastDialog);
      }
      
      this.__count();
      this.__shadowRemove();
    },
    'closeAll':function(){ 
      var $this = this; 
      for (var value in this._conteiner) {
        $this.close(value);
      }
    },
    'get':function(type,id){
      if(parent.window){
        return parent.MyCMSDialog._get(type,id);
      }else{
        return this._get(type,id);  
      }
    },
    '_get':function(type,id){
      var type = type ? type : 'default';
      var id   = id ? id : this._active;

      switch(type){
        case 'param':
          if(this._param[id]){
            return this._param[id];
          }  
          return {};
        break;
        case 'arguments':
          if(this._arguments[id]){
            return this._arguments[id];
          }
          return {};
        break;
        case 'objects':
          if(this._objects[id]){
            return this._objects[id];
          }
          return false;
        break;
        case 'id':
          return id;
        break;
      }
      return false;
    },
    '__getArguments':function(id){
      var id = id ? id : this._active;
      if(this._arguments[id]){
        return this._arguments[id];
      }
      return false;
    },
    '__getObjects':function(id){
      var id = id ? id : this._active;
      if(this._objects[id]){
        return this._objects[id];
      }
      return false;
    },
    '__getParam':function(id){
      var id = id ? id : this._active;
      if(this._param[id]){
        return this._param[id];
      }
      return {};
    },
    '__dialog':function(){
      var $this     = this;
      var obj       = $(this.__getObjects());
      var option    = this.__getParam();
      var id        = option._id;
  
      var dialogHtml = {
        'conteiner': $('<div mycms-dialog-id="'+id+'"  class="mycms-dialog-conteiner"></div>'),
        'html':$('<div class="mycms-dialog-content"></div>'),
        'header':$('<div class="header bg2"></div>'),
        'title':$('<span class="title"></span>'),
        'close':$('<span class="close icon-remove"></span>')
      };  
      $(dialogHtml.close).click(function(){
          $this.close(id);
      });  
      
      
         
      switch(option.type){
        case 'iframe':
          var iframe = $('<iframe border="0" style="display:none;border:none;" width="100%" height="99%" src="'+option.url+'"></iframe>');
          var conteiner = dialogHtml.conteiner;
          $(dialogHtml.title).html(option.title);
          $(dialogHtml.html).html(iframe);
          $(dialogHtml.header).append(dialogHtml.title);
          if(!option.notclose){
            $(dialogHtml.header).append(dialogHtml.close);
          }
          
          $(iframe).load(function(){
            $(iframe).show();
            var id        = $this._active;
            if(conteiner){
              if(option.iframe_resize){
									if($('#content',$(this).contents())){
	                  var iframe_width  = $('#content',$(this).contents()).height();
	                  var iframe_height = $('#content',$(this).contents()).height();
									}else{
	                  var iframe_width  = $(this).contents().height();
	                  var iframe_height = $(this).contents().height();
									}
									
                  $(conteiner).width(option.iframe_resize.width);
                  $(conteiner).height(option.iframe_resize.height);
                  $(conteiner).find('.mycms-dialog-content').removeAttr('style');
                  $(iframe).height(iframe_height);
                  $(iframe).parent().height(iframe_height);                  
                $this.__resize(id);
              }
            }
          });
          
          $(conteiner).append(dialogHtml.header);
          $(conteiner).append(dialogHtml.html);
          $(conteiner).css({'position':'absolute'});
          $('body').append(conteiner);   
        break;
        case 'hash':
          if(this._conteiner[id]){
            $('*[mycms-dialog-id="'+id+'"]').show();
            
			$this.__toCenterSize(id);
            $this.__activeDialog(id); 
            return false;
          }   
          var conteiner = $(option.url);
          $(option.url).show();
          $(dialogHtml.title).html(option.title);
          $(dialogHtml.header).append(dialogHtml.title);
          if(!option.notclose){
            $(dialogHtml.header).append(dialogHtml.close);
          }
          $(conteiner).wrap(dialogHtml.conteiner);
          $(conteiner).before(dialogHtml.header);
          $(conteiner).wrap(dialogHtml.html);
          
        break;
        default:  
          var conteiner = dialogHtml.conteiner;
          $(dialogHtml.title).html(option.title);
          $(dialogHtml.html).html(option.content);
          $(dialogHtml.header).append(dialogHtml.title);
          if(!option.notclose){
            $(dialogHtml.header).append(dialogHtml.close);
          }
          $(conteiner).append(dialogHtml.header);
          $(conteiner).append(dialogHtml.html);
          $(conteiner).css({'position':'absolute'});
          $('body').append(conteiner);
      }   
      if(option.open && typeof option.open ==="function"){
        var arg = [id];
        option.open.apply( $('*[mycms-dialog-id="'+id+'"]'), Array.prototype.slice.call( arg ));
      }
      this._conteiner[id] = $('*[mycms-dialog-id="'+id+'"]');
      $this.__toCenterSize(id);
      $this.__resize(id); 
      this.__activeDialog(id);
      this.__count();
    },
    '__toCenterSize':function(id){
		var id = id ? id : this._active;	
        var option    = this.__getParam(id);
        var page_size = this.__pageSize();

        var width     = option.width;
        var height    = option.height;
        var $this     = this;
        
        if(width=="auto" && height=="auto"){
          var width  = (page_size[2]*90)/100;
          if(option.type=="hash"){
            var height = $(option.url).height();
          }else{
            var height = (page_size[3]*90)/100;
          }
        }else if(width=="auto"){
          var width  = (page_size[2]*90)/100;
        }else if(height=="auto"){
          if(option.type=="hash"){
            var height = $(option.url).height();
          }else{
            var height = (page_size[3]*90)/100;
          }
        }    
        if(option.minwidth>0){
          var width  = width<option.minwidth ? option.minwidth : width;
        }
        if(option.minheight>0){
          var height  = height<option.minheight ? option.minheight : height;
        }
        if(option.maxwidth>0){
          var width  = width>option.maxwidth ? option.maxwidth : width;
        }
        if(option.maxheight>0){
          var height  = height>option.maxheight ? option.maxheight : height;
        }
        

        var Left = Math.max(40, parseInt(page_size[2]/2 - width/2)) + 'px';
        var Top  = Math.max(40, parseInt((page_size[3]/2 - height/2)+$(document).scrollTop())) + 'px';
        $('*[mycms-dialog-id="'+id+'"]').find('.mycms-dialog-content').height(height-150);
        $('*[mycms-dialog-id="'+id+'"]').css({'position':'absolute','width':width+'px','height':height+'px','left':Left,'top':Top});
        if(option.width!="auto" && option.height!="auto"){
          
        }        
    },
    '__resize':function(id){
		return ;
      var key = id ? id : false;
      var page_size = this.__pageSize();
      var dd = this._conteiner;     
      if(key && this._conteiner[key]){
        var width   = $('*[mycms-dialog-id="'+id+'"]').width();
        var height  = $('*[mycms-dialog-id="'+id+'"]').height(); 
        var Left = Math.max(40, parseInt(page_size[2]/2 - width/2)) + 'px';
        var Top  = Math.max(40, parseInt((page_size[3]/2 - height/2)+$(document).scrollTop())) + 'px';
        $('*[mycms-dialog-id="'+id+'"]').css({'left':Left,'top':Top});
      }else{
        for (var value in this._conteiner) {
          if(value){
            var width   = $('*[mycms-dialog-id="'+value+'"]').width();
            var height  = $('*[mycms-dialog-id="'+value+'"]').height(); 
            var Left = Math.max(40, parseInt(page_size[2]/2 - width/2)) + 'px';
            var Top  = Math.max(40, parseInt((page_size[3]/2 - height/2)+$(document).scrollTop())) + 'px';
            $('*[mycms-dialog-id="'+value+'"]').css({'left':Left,'top':Top});
          }
        }
      }
    },
    '__shadow':function(){
      if($('#MyCMSDialogShadow').size()>0) return false;
      $('body').append($('<div id="MyCMSDialogShadow"></div>').css({
        'position':'fixed',
        'left':'0px','top':'0px','right':'0px','bottom':'0px',
        'width':'100%','height':'100%',
        'background':'#000','opacity':'0.5',
        'z-index':'999999'
      }).dblclick(function(){
        $this.closeAll();
      }));
      var $this = this;

    },
    '__shadowRemove':function(){
      var i = 0;
      for (var value in this._conteiner) {
        if($('*[mycms-dialog-id="'+value+'"]').is(':visible')){
          i++;
        }
      }
      if(i==0) $('#MyCMSDialogShadow').remove();
    },
    '__activeDialog':function(id){
      var id = id ? id : this._active;
      for (var value in this._conteiner) {
        if($('*[mycms-dialog-id="'+value+'"]')){
            $('*[mycms-dialog-id="'+value+'"]').css('z-index','0');
        }
      }
      var zindex    = (parseInt($('#MyCMSDialogShadow').css('z-index'))+1);
      $('*[mycms-dialog-id="'+id+'"]').css('z-index',zindex);
    },
    '__count':function(){
      var i = 0;
      for (var value in this._conteiner) {
        if(value){
          i++;
        }
      }
      this._count = i;
      return i;
    },
    '__pageSize':function(){
       var xScroll, yScroll;
  
       if (window.innerHeight && window.scrollMaxY) {
                  xScroll = document.body.scrollWidth;
                  yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ 
                  xScroll = document.body.scrollWidth;
                  yScroll = document.body.scrollHeight;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ 
                  xScroll = document.documentElement.scrollWidth;
                  yScroll = document.documentElement.scrollHeight;
       } else {
                  xScroll = document.body.offsetWidth;
                  yScroll = document.body.offsetHeight;
       }
  
  
       var windowWidth, windowHeight;
       if (self.innerHeight) { 
                 windowWidth = self.innerWidth;
                 windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { 
                 windowWidth = document.documentElement.clientWidth;
                 windowHeight = document.documentElement.clientHeight;
       } else if (document.body) {
                 windowWidth = document.body.clientWidth;
                 windowHeight = document.body.clientHeight;
       }
  

       if(yScroll < windowHeight){
          pageHeight = windowHeight;
       } else {
          pageHeight = yScroll;
       }

       if(xScroll < windowWidth){
                 pageWidth = windowWidth;
       } else {
                 pageWidth = xScroll;
       }
  
       return [pageWidth,pageHeight,windowWidth,windowHeight];
    },
    '_num':0,
    '_count':0,
    '_objects': new Array,
    '_conteiner': new Array,
    '_active':null,
    '_arguments': new Array,
    '_param': new Array,
  }
  $(window).resize(function(){
		MyCMSDialog.__toCenterSize();
		MyCMSDialog.__resize();

  });
})();

;(function( $ ){
  $.fn.MyCMSDialog = function(param,arg){ 
    this.click(function(){     
      MyCMSDialog.open(param,arg,this);
      return false;
    }); 
  }
})( jQuery );