(function($) {
	function schoolDialog(el){
		this.nameInput = $(el);
		this.nameInput.attr("readonly", "readonly");
		this.idInput = this.nameInput.prev();
		this.provInput = this.nameInput.prev().prev();
		this.activeProvTab = 0;
		this.dialogLayer = $('<div class="univDialog"></div>');
		this.bindMethodsToObj("show", "hide", "mouseIn", "mouseOut", "hideIfClickOutside", "selectItem");
		this.init();
		this.hide();
	}
	
	schoolDialog.prototype = {
		init: function(){
			//清空按钮
	        this.ClearController = $('<div class="button"><a href="#">关 闭</a></div>').click(this.bindToObj(function(event) {
	        //this.selectItem("", "");
	        this.hide();
	        return false;
	      })); 
	   
	      this.popupController = $('<div class="selecting"></div>').append(this.ClearController);
	      	
			var dialogContent = $('<div class="univDialog-content"></div>');
			var provContent = $('<ul></ul>');
			var prov = this.provInput.val();
			
			if(prov==null || prov==""){				
				$.each(schoolItems, function(){					
					provContent.append('<li><a href="#univ-prov-'+ this.id +'">'+ this.name +'</a></li>');					
					var univContent = $('<div id="univ-prov-'+ this.id +'" class="univlist-content"></div>');
					$.each(this.univ, function(){
						univContent.append('<a href="#" id="'+ this.id +'" title="'+ this.name +'">'+ this.name +'</a>');
					});
					dialogContent.append(univContent);				
				});
			
			}else{
			
			
				$.each(schoolItems, function(){
					if(this.id==prov){
						provContent.append('<li><a href="#univ-prov-'+ this.id +'">'+ this.name +'</a></li>');
						
						var univContent = $('<div id="univ-prov-'+ this.id +'" class="univlist-content"></div>');
						$.each(this.univ, function(){
							univContent.append('<a href="#" id="'+ this.id +'" title="'+ this.name +'">'+ this.name +'</a>');
						});	
						dialogContent.append(univContent);
					}
					
				});
				
			}
			provContent.prependTo(dialogContent);
			
			this.dialogLayer = $('<div class="univDialog"></div>')
				.append(this.popupController,dialogContent)
				.css({ display: "none", position: "absolute", zIndex: 900}).appendTo(document.body);
			
			// ΪIE7以下浮动层的bug-fix
			if ($.browser.msie && $.browser.version < 7) {
				this.ieframe = $('<iframe class="dialog_iframe" frameborder="0" src="about:blank"></iframe>')
					.css({ position: "absolute", display: "none", zIndex: 99 })
					.insertBefore(this.dialogLayer);
				this.dialogLayer = this.dialogLayer.add(this.ieframe);
			};
			
			
			$('.univDialog-content').tabs();
			$(".univlist-content a", this.popupContent).click(this.bindToObj(function(event) {
		    	this.selectItem($(event.target).attr("id"), $(event.target).html());
		    	this.hide();
		    	return false;
		    }));
		},
		mouseIn:function(){
			if(bodyclick!=undefined){
				bodyclick=false;
			}
		},
		mouseOut:function(){
			if(bodyclick!=undefined){
				bodyclick=true;
			}
		},
		selectItem: function(item, text) {
			this.nameInput.val(text);
			this.idInput.val(item);
		},
		show: function() {
			this.setPosition();
			this.dialogLayer.css("display", "block");
			this.nameInput.unbind("focus", this.show);
			$(document.body).click(this.hideIfClickOutside);
		},
		hide: function() {
			this.setPosition();
			this.dialogLayer.css("display", "none");
		    $(document.body).unbind("click", this.hideIfClickOutside);
		    this.nameInput.focus(this.show);
		},
		hideIfClickOutside: function(event) {
			if (event.target != this.nameInput[0] && !this.insideSelector(event)) {
		    	this.hide();
		    };
		},
		insideSelector: function(event) {
			var offset = this.dialogLayer.offset();
			offset.right = offset.left + this.dialogLayer.outerWidth();
		    offset.bottom = offset.top + this.dialogLayer.outerHeight();
		    return event.pageY < offset.bottom &&
		           event.pageY > offset.top &&
		           event.pageX < offset.right &&
		           event.pageX > offset.left;
		},
		setPosition: function() {
		      var offset = this.nameInput.offset();
		      this.dialogLayer.css({
		        top: offset.top + this.nameInput.outerHeight(),
		        left: $(window).width()/2 - this.dialogLayer.outerWidth()/2
		        //left: offset.left
		      });
		      
		      if (this.ieframe) {
		        this.ieframe.css({
		          width: this.dialogLayer.outerWidth(),
		          height: this.dialogLayer.outerHeight()
		        });
		      };
		},
		
		bindToObj: function(fn) {
			var self = this;
			return function() { return fn.apply(self, arguments) };
		},
		bindMethodsToObj: function() {
			for (var i = 0; i < arguments.length; i++) {
				this[arguments[i]] = this.bindToObj(this[arguments[i]]);
		    };
		}
	}
	
	$.fn.schoolDialog = function() {
		return this.each(function() { 
			new schoolDialog(this); 
	    });
	}
})(jQuery);
