KT.Note = new Class({
	Implements: Options,

	options : {
		theme :				"ktnote home_land",
		autoClose :			false,				//是一段时间后自动关闭，默认为需要操作关闭
		autoCloseView: 'morph',			//自动关闭时的效果，因为有时渐隐会影响到其他控件
		centerLocation:	false,
		width :				314,
		height :			172,
		locationId :		"",					//可以设置在其他控件边弹出对话框
		x :					0,
		y :					0,
		disX :				30,					//弹出对话框的边界的错位，如果x值完全相等不美观
		title :				"标题",
		content :			"",					//content,eid为2种格式，如果同时出现，优先级为eid,content
		eid:                "",
		pointer :			true,				//是否有小三角
		autoScrollV :		true,				//当在底部是否自动滚动，默认true
		autoScrollH :		false,				//当在右边是否自动滚动，默认false
		closeBtn:           true,				//现在隐藏了这个按钮，仅用来判断能否关闭
		EB:                 false,
		EBCaption:          "确定",
		EBCallback:         null,
		CB:                 false,
		CBCaption:          "取消",
		CBCallback:         null,
		scrollTop:			0				//修正页面中还有下拉框的情况
	},

	initialize : function(element, options){
		this.setOptions(options);
		this.options.y -= this.options.scrollTop;
		if(Browser.Engine.trident4)
		{
			this.options.height = this.options.height+9;
			this.options.width = this.options.width+9;
		}
		this.options.width = $chk(this.options.width) && this.options.width.toInt() > 0 ? this.options.width.toInt() : 314;
		this.options.height = $chk(this.options.height) && this.options.height.toInt() > 0 ? this.options.height.toInt() : 172;
		this.options.width += 28;
		this.options.height += 28;

		this.element = element;

		this.initNote();
		this.initLocation();
		
		if(this.options.centerLocation){
			this.options.x = (document.documentElement.clientWidth - this.options.width)/2;
			this.options.y = document.documentElement.scrollTop + 150;
			this.noteDiv.setStyles({'left' : this.options.x,'top' : this.options.y});
		}
		
		if(this.options.autoClose){
			if(this.options.autoCloseView == 'morph'){
				KT.RemoveElement(this.noteDiv, {duration:3000});
			}else{
				setTimeout(function(){this.noteDiv.dispose()}.bind(this),	1000);
			}
		}else{
			(function(){
				this.initEvent();
			}).delay(100, this);
		}
	},
	initNote : function(){
		// 算div大小
		var innerWidth1 = this.options.width - 8;
		var innerHeight1 = this.options.height - 8;
		var innerWidth2 = innerWidth1 - 20;
		var innerHeight2 = innerHeight1 - 20;
		
		this.noteDiv = new Element('div', {
			'class' : this.options.theme,
			'styles' : {
			//'left' : this.options.x,
			//'top' : this.options.y,
			'width' : this.options.width,
			'height' : this.options.height
		}}).inject(document.body, 'bottom');

		// 插入小三角
		if(this.options.pointer){
			this.pointer = new Element('img',{
				'src' : 'http://img.kuantu.com/images/home_land/hl_duihuatop.gif',
				'styles' : {'margin-left' : '30px'}
			}).inject(new Element('div', {
				'class' : 'hl_duihuatop'
			}).inject(this.noteDiv, 'top'), 'top');
		}
		// 插入外部div
		this.mainDiv = new Element('div', {'class' : 'note_text'}).inject(this.noteDiv, 'bottom');
		// 插入外部边框
		new Element('div', {'class' : 'duihua_left1'}).injectInside(this.mainDiv);
		new Element('div', {'class' : 'duihua_center1', 'styles' : {'width' : innerWidth1}}).injectInside(this.mainDiv);
		new Element('div', {'class' : 'duihua_right1'}).injectInside(this.mainDiv);
		new Element('div', {'class' : 'duihua_left2', 'styles' : {'height' : innerHeight1}}).injectInside(this.mainDiv);
		this.contentDiv = new Element('div', {
			'class' : 'duihua_center2', 
			'styles' : {
				'height' : innerHeight2, 
				'width' : innerWidth2,
				'overflow-x' :'hidden',
				'overflow-y' : 'auto'
		}}).injectInside(this.mainDiv);
		new Element('div', {'class' : 'duihua_right2', 'styles' : {'height' : innerHeight1}}).injectInside(this.mainDiv);
		new Element('div', {'class' : 'duihua_left3'}).injectInside(this.mainDiv);
		new Element('div', {'class' : 'duihua_center3', 'styles' : {'width' : innerWidth1}}).injectInside(this.mainDiv);
		new Element('div', {'class' : 'duihua_right3'}).injectInside(this.mainDiv);
		// 插入内容
		if(this.options.eid!="")
		{
			new Element('div', {'html' : $(this.options.eid).get("html")}).injectInside(this.contentDiv);
		}
		else
		{
			new Element('div', {'html' : this.options.content}).injectInside(this.contentDiv);
		}
		// 插入链接
		/*
		if(this.options.url!="")
		{
			this.setContentUrl(this.options.url);
		}
		else if(this.options.eid!="")
		{
			this.setContentEid(this.options.eid);
		}
		else
		{
			this.setContent(this.options.content);
		}
		*/
		if(this.options.EB === true || this.options.CB === true ){
			this.linksDiv = new Element('div', {
				'class' : 'btngray btngray_mar1'
			}).injectInside(this.contentDiv);
		}
		// 关闭链接,与取消不同，暂时不用
		/*
		if(this.options.closeBtn){
			new Element('a', {
				'html':'关闭',
				'href':'#'
			}).injectInside(this.linksDiv).addEvent('click', function(event){
				this.close(); 
				event.stopPropagation();
				return false;
			}.bind(this));
		}
		*/
		// 确认链接
		if(this.options.EB){
			//this.linksDiv.innerHTML += " ";
			this.enterButton = new Element('a', {'html':this.options.EBCaption, 'href':'javascript:void(0)'}).injectInside(this.linksDiv);
			if(this.options.EBCallback!=null){
				this.enterButton.addEvent('click', function(event){
														this.options.EBCallback(); 
														this.close(); 
														//event.stopPropagation();
														return false;
													}.bind(this));
			}else{
				this.enterButton.addEvent('click', function(event){
														this.close(); 
														//event.stopPropagation();
														return false;
													}.bind(this));
			}
			
		}

		// 取消链接
		if(this.options.CB){
			//this.linksDiv.innerHTML += " ";
			this.cancelButton = new Element('a', {'html':this.options.CBCaption, 'href':'javascript:void(0)'}).injectInside(this.linksDiv);
			if(this.options.CBCallback!=null){
				this.cancelButton.addEvent('click', function(event){
														this.options.CBCallback(); 
														this.close(); 
														//event.stopPropagation();
														return false;
													}.bind(this));
			}else{
				this.cancelButton.addEvent('click', function(event){
														this.close(); 
														//event.stopPropagation();
														return false;
													}.bind(this));
			}
			
		}
		/*
		var inner = ['',
				'  <div class="hl_duihuatop"><img src="http://img.kuantu.com/images/home_land/hl_duihuatop.gif" style="margin-left:30px" /></div>',
				'  <div class="note_text">',
				'		<div class="duihua_left1"></div>',
				'		<div class="duihua_center1" style="width:',innerWidth1,'px;"></div>',
				'		<div class="duihua_right1"></div>',
				'		<div class="duihua_left2" style="height:',innerHeight1,'px;"></div>',
				'		<div class="duihua_center2" style="height:',innerHeight2,'px;width:',innerWidth2,'px;">',
							this.options.content,
				'			<div  id="duihua_links" class="btngray btngray_mar1">asdf</div>',
				'		</div>',
				'		<div class="duihua_right2" style="height:',innerHeight1,'px;"></div>',
				'		<div class="duihua_left3"></div>',
				'		<div class="duihua_center3" style="width:',innerWidth1,'px;"></div>',
				'		<div class="duihua_right3"></div>',	
				'  </div>'].join('');
		*/
	},
	initEvent : function(){
		//this.noteDiv.addEvent('mouseenter', function(event){this.mouseenter = true;event.stopPropagation();$("aaaaaa").innerHTML +=this.mouseenter +",";}.bind(this));
		//this.noteDiv.addEvent('mouseleave', function(event){this.mouseenter = false;event.stopPropagation();$("aaaaaa").innerHTML +=this.mouseenter +",";}.bind(this));
		this.ml = this.mouseClickListener.bindWithEvent(this);
		this.kl = this.keyboardListener.bindWithEvent(this);
		document.addEvent('click', this.ml);
		document.addEvent('keydown', this.kl);
	},
	mouseClickListener : function(event){
		this.mouseenter = event.page.x > this.options.x && event.page.x < (this.options.x + this.options.width) && event.page.y > this.options.y && event.page.y < this.options.y + this.options.height;
		if(this.mouseenter == false && this.options.closeBtn){
			this.close();
		}
	},
	keyboardListener : function(event){
		if(event.key=="esc" && this.options.closeBtn){
			this.close();
		}
	},
	close : function(){
		// 事件没有被移除？
		document.removeEvent('click', this.ml);
		document.removeEvent('keydown', this.kl);
		this.noteDiv.dispose();
	},
	initLocation : function(){
		/* todo 进行document.documentElement.* 和document.body.*的判断 */
		/* todo 垂直为false，水平为true的功能 */

		var browserNowTop = document.documentElement.scrollTop;
		var browserHeight = document.documentElement.clientHeight;
		
		var browserNowLeft = document.documentElement.scrollLeft;
		var browserWidth = document.documentElement.clientWidth;

		var e = this.options.locationId=="" ? this.element : $(this.options.locationId);
		do{
			this.options.x += e.offsetLeft;
			this.options.y += e.offsetTop;
		}while (e = e.offsetParent) 

		this.elementX = this.options.x;

		if(this.options.x + this.options.width > browserWidth){
			//this.options.x = browserNowLeft + browserWidth - this.options.width - 6;
			this.options.x = this.options.x + this.element.offsetWidth - this.options.width + this.options.disX;
		}else{
			this.options.x = this.options.x - this.options.disX;
		}
		if($chk(this.element.offsetHeight)){
			this.options.y +=this.element.offsetHeight;
		}
		this.noteDiv.setStyles({'left' : this.options.x,'top' : this.options.y});
		//小三角位置
		if(this.options.pointer){
			var pointerX = 30;
			var tmpX = Math.max(this.elementX, this.options.x);
			var tmpW = Math.min(this.elementX + this.element.offsetWidth, this.options.x + this.options.width);
			//取中间点，减6是因为箭头本身的宽度为11
			pointerX = (tmpW + tmpX)/2 - this.options.x - 6;
			this.pointer.setStyles({'margin-left' : pointerX});
		}
		
		// 算是否到底部，如果是，有两种方案：自动往上滚，在上部显示
		if(this.options.autoScrollV){
			// 自动往上滚
			if(browserNowTop + browserHeight < this.options.y + this.options.height + 12){
				self.scroll(browserNowLeft, this.options.y + this.options.height - browserHeight + 12); 
			}
		}
	},
	autoSize : function(){
	
	},
	leave : function(){
		// 判断离开的标准：在他处点击过鼠标
	}
});

KT.Note.note = function(message){
	new KT.Note(this,{autoScrollV:false, autoClose:true,centerLocation:true,width:180,height:36,pointer:false,content:'<center>'+message+'</center>'});
}