
var My0ffice = My0ffice || {version:'xuerersi_1.1.0'};

/* 
  1.title:弹出窗口的标题;
  2.width:弹出窗口的宽度;
  3.height:弹出窗口的高度;
  4.content:弹出窗口的中间内容;
  5.closeAction:弹出窗口是否具有关闭按钮，默认存在，隐藏设置为：closeAction：'none';
  6.backgroundType:弹出窗口是否具有灰色遮罩层，默认存在，隐藏设置为：backgroundType：'none';

  7.具体实现：
  调用此插件，并对相关参数进行设置,如：
  //窗体所需相关参数
  var config = {
	     width:230,
		 height:100,
		 title:'<span>这是一个例子</span>',
		 content:'<div style="padding:10px 0 0 50px;">'+
		               '<div>这是窗体内容</div>'+
			     '</div>'
		 
	}
	
  //创建一个窗体
  var win_new = new My0ffice.Win(config);
  
  //调用该窗体显示方法
  win_new.Show();

*/


My0ffice.Win = function(config){
	if($('#winBack').length==0){
	    $('#content').append('<div style="clear:both;"></div>');
	}
	this.title = config.title || '';
	this.width = config.width || 100;
	this.height = config.height || 100;
	this.content = config.content || '';
	this.closeAction = config.closeAction || 'block';
	this.backgroundType = config.backgroundType || 'block';
	this.browserWidth = $(document).width();
	this.browserHeight = Math.max($('body').height(),$(window).height())+20;
	var showTitle = this.title==''?'none':'block';
	this.winTop = ($(window).height()/2)-(this.height/2)+$(document).scrollTop();
	this.winLeft = Math.abs(this.browserWidth-this.width)/2;
	if(window.XMLHttpRequest){ //Mozilla, Safari, IE7
		var iframe='';
	}else {
		if(config.title!=''){
			var iframeHehght = this.height+45;
		}else{
			var iframeHehght = this.height;
		}
	   
	   var iframe='<iframe class="winHtml"  style=" border:0;width:'+this.width+'px;height:'+iframeHehght+'px;top:'+this.winTop+'px;left:'+this.winLeft+'px; z-index:1000" ></iframe>';
	}
	
	this.winHtml = iframe+
	              '<div class="winHtml" style="width:'+this.width+'px;height:'+this.height+'px;top:'+this.winTop+'px;left:'+this.winLeft+'px;">'+
				       '<table class="roundWin">'+												
						   '<tr>'+	
							  '<td class="round_top_l"></td>'+
							  '<td class="round_top_c"></td>'+														
							  '<td class="round_top_r"></td>'+													
						   '</tr>'+													
						   '<tr>'+														
							  '<td class="round_mid_l"></td>'+														
							  '<td class="round_mid_c winBox">'+
							       '<ul>'+
										'<li style="display:'+showTitle+';" class="winTitle clearboth">'+
											  '<div class="titleStyle floatleft">'+this.title+'</div>'+
											  '<div style="display:'+this.closeAction+';" class="closeBtn"></div>'+
										'</li>'+
										'<li style="height:'+this.height+'px;" class="winBody">'+
										   this.content+
										'</li>'+
								   '<ul>'+
							  '</td>'+														
							  '<td class="round_mid_r"></td>'+													
						  '</tr>'+													
						  '<tr>	'+													
							  '<td class="round_bottom_l"></td>'+
							  '<td class="round_bottom_c"></td>'+
							  '<td class="round_bottom_r"></td>'+
						 '</tr>'+
					  '</table>'+
				   '</div>';
}

My0ffice.Win.prototype.Show = function(){
	if($('#winBack').length==0){
		$('body').append('<div id="winBack" class="winBack" style="display:'+this.backgroundType+';width:'+this.browserWidth+'px;height:'+this.browserHeight+'px"></div>');
	}
	$('body').append(this.winHtml);
	$('.closeBtn').live('click',function(){
		 $('.winBack').remove();
		 $('.winHtml').remove();
	});
}

My0ffice.Win.prototype.Close = function(){
	$('.winBack').remove();
    $('.winHtml').remove();
}

/*
  冒泡窗口效果
  1.id:弹出冒泡窗口的id必须唯一值;
  2.width:弹出冒泡窗口的宽度;
  3.height:弹出冒泡窗口的高度;
  4.content:弹出冒泡窗口的中间内容;
  5.btnType:弹出冒泡窗按钮类型，btnType:'confirm'时具备"确定"，"取消"两个按钮；btnType:'alert'时只具备"取消"按钮;
  6.SubmitHandler:点击"确定"按钮后，执行里边的方法;
  7.CancelHandler:点击"取消"按钮后，执行里边的方法;;
  9.winTop:弹出冒泡窗口的top值
  10.winLeft:弹出冒泡窗口的left值
  8.具体实现：
  调用此插件，并对相关参数进行设置,如：
  //窗体所需相关参数
  var config = {
		 id:'confirm',
		 btnType:'confirm',
	     width:240,
		 height:120,
		 winTop:100,
		 winLeft:50,
		 content:'<div>confirmWin</div>',
		 SubmitHandler:function(){
			     alert('确认提交');
		 },
		 CancelHandler:function(){
			    alert('取消提交');
		}
	}
	
  //创建一个窗体
  var win_new = new My0ffice.Win(config);
  
  //调用该窗体显示方法
  win_new.Init();
  
*/

My0ffice.BlebWin = function(config){
	this.id = config.id;
	this.btnType = config.btnType || '';
	this.width = config.width || 100;
	this.height = config.height || 100;
	this.content = config.content || '';
	this.winTop = config.winTop|| 0;
	this.winLeft = config.winLeft|| 0;
	
	//var contentHeight = this.height-4;
	//var contentWidth = this.width-4;
	var contentHeight = 'auto';
	var contentWidth = 'auto';
	if($('#'+this.id).size()==0){
	    var parentTop = ($(window).height()/2)-(this.height/2)+$(document).scrollTop();
	    var parentLeft = Math.abs($(document).width()-this.width)/2;
	}else{
	    var parentTop = $('#'+this.id).offset().top;
	    var parentLeft = $('#'+this.id).offset().left;
	}
	this.winTop = parentTop-this.height;
	this.winLeft = parentLeft-config.winLeft;
	
	if(this.btnType=='confirm'){
	   var btnHtml = '<button id="'+this.id+'_submit" type="submit" class="s-btn btn-apply">确认</button>'+
	                 '<button id="'+this.id+'_cancel" class="s-btn btn-apply" style="margin-left:20px;">取消</button>';
	}else if(this.btnType=='alert'){
	   var btnHtml = '<button id="'+this.id+'_submit" type="submit" class="s-btn btn-apply">确认</button>';
	}else{
	   var btnHtml = '';
	}
	this.winHtml ='<div id="'+this.id+'_winHtml" class="winHtml" style="top:'+parentTop+'px;width:'+this.width+'px;height:0px;left:'+this.winLeft+'px;">'+
				       '<ul class="bleb_Win_back" style="width:'+contentWidth+';height:'+contentHeight+';padding-bottom:10px">'+
							'<li style="height:'+(contentHeight-50)+'px;padding:10px;">'+
							   this.content+
							'</li>'+
							'<li style="text-align: center;">'+
							   btnHtml+
							'</li>'+
					   '</ul>'+
				   '</div>';
	this.Show = function(){
	    if($('#'+this.id+'_winHtml').size()!=0){
		  $('#'+this.id+'_winHtml' ).remove();
		}
		if($('#'+this.id).size()==0){
		   $('body').append(this.winHtml);
		}else{
			$('#'+this.id ).append(this.winHtml);
		}
		$('#'+this.id+'_winHtml').animate({ 
			height: this.height+'px',
			top: this.winTop+'px',
			opacity: '1'
		}, 200 );
		if(this.winTop<$(window).scrollTop()){
		   $(window).scrollTop(this.winTop-6);
		}
	};
	this.SubmitFn = function(){
		var winId = this.id;
	    $('#'+winId+'_submit').click(function(){
			config.SubmitHandler();
			$('#'+winId+'_winHtml' ).remove();
		});
	};
	this.CancelFn = function(){
		var winId = this.id;
	    $('#'+winId+'_cancel').click(function(){
			config.CancelHandler();
			$('#'+winId+'_winHtml' ).remove();
		});
	};
	this.Init = function(){
		this.Show();
		this.SubmitFn();
		this.CancelFn();
	}
}

My0ffice.BlebWin.prototype.Init = function(){
	this.Init();
}



var confirmWin = function(con){
	var config = {
		 id:'confirm',
		 btnType:'confirm',
	     width:240,
		 height:120,
		 winTop:100,
		 winLeft:100,
		 content:con,
		 SubmitHandler:function(){
			    return true;
		 },
		 CancelHandler:function(){
			    return false;
		}
	}
    var win_new = new My0ffice.BlebWin(config);
	win_new.Init();
}

var alertWin = function(con){
	var config = {
		 id:'alert',
		 btnType:'alert',
	     width:240,
		 height:120,
		 winTop:0,
		 winLeft:100,
		 content:con,
		 SubmitHandler:function(){
			     return;
		 }
	}
    var win_new = new My0ffice.BlebWin(config);
	win_new.Init();
}

