/**
 * slin zhao
 * dotcoo@163.com
 */

// console
if(!window.console){
	console = window._console = window.console = {};
	console.loginfo = [];
	console.dirinfo = [];
	console.log = function(info){ console.loginfo.push(info); };
	console.dir = function(info){ console.dirinfo.push(info); };
}

// jquery ext
if($){
	jQuery.valid = {
		regexps: {
			email:/^[a-z0-9_\.]+@[a-z0-9-]+\.[a-z\.]+$/i,
			username:/^[a-z]\w{4,10}[a-z0-9]$/i,
			password:/^.{6,16}$/,
			cnid: /^\d{17}[\dx]$/i,
			mobile:/^1[358]\d{9}$/,
			number:/^\d+$/
		},
		func: {
			empty: function(event){
				this.validresult = $.trim(this.value).length==0;
				if(this.validresult){
					$(this).tip('', '');
					event.stopImmediatePropagation();
				}else{
					$(this).tip('', '');
				}
				return this.validresult;
			},
			notempty: function(event){
				this.validresult = $.trim(this.value).length>0;
				if(this.validresult){
					$(this).tip(0, '');
				}else{
					$(this).tip(1, this.title+'不能为空!');
					event.stopImmediatePropagation();
				}
				return this.validresult;
			},
			regexp: function(event){
				var regexp = event.data[0];
				var pass = event.data[1]?event.data[1]:'';
				var notpass = event.data[2]?event.data[2]:'请填写正确的信息!';
				
				if(regexp.charAt(0) == '/'){
					regexp = regexp.replace(/；/g, ';');
					regexp = regexp.replace(/，/g, ',');
					regexp = eval(regexp);
				}else{
					regexp = jQuery.valid.regexps[regexp];
				}
				this.validresult = regexp.test(this.value);
				if(this.validresult){
					$(this).tip(0, pass);
				}else{
					$(this).tip(1, notpass);
					event.stopImmediatePropagation();
				}
				return this.validresult;
			},
			equal:function(event){
				var input = event.data[0];
				var pass = event.data[1]?event.data[1]:'';
				var notpass = event.data[2]?event.data[2]:'两次输入不一致!';
				
				this.validresult = this.value == $(input).val();
				if(this.validresult){
					$(this).tip(0, pass);
				}else{
					$(this).tip(1, notpass);
					event.stopImmediatePropagation();
				}
				return this.validresult;
			}
		}
	}
}

jQuery.fn.extend({
	// valid
	tip: function(color, message){
		var colors = ['', 'red'];
		$.each(this, function(){
			$(this).nextAll('span.Jtip').css('color', colors[color]).text(message);
		});
		return this;
	},
	valid: function(valids){
		this.valids = valids;
		for(var name in valids){
			var $input = this.find('[name='+name+']');
			
			var vals = valids[name].split(';');
			for(var i=0; i<vals.length; i++){
				var ps = vals[i].split(',');
				var func = jQuery.valid.func[ps.shift()];
				$input.bind('valid', ps, func);
			}
			
			$input.blur(function(){
				$(this).trigger('valid');
			});
		}
		this.submit(function(){
			var pass = true;
			this.valids = valids;
			for(var name in valids){
				var $input = $(this).find('[name='+name+']').trigger('valid');
				if($input.get(0).validresult === false){
					pass = false;
				}
			}
			return pass;
		});
	},
	validfunc: function(func, pass, notpass){
		this.bind('valid', function(event){
			this.validresult = func.apply(this, []);
			if(this.validresult){
				$(this).tip(0, pass);
				event.stopImmediatePropagation();
			}else{
				$(this).tip(1, notpass);
			}
			return this.validresult;
		});
		return this;
	},
	
	del: function(){
		$(this).click(function(){
			if(!confirm('确定要删除！')){
				return false;
			}
			var that = this;
			$.getJSON(this.href, function(resp){
				if(resp.status == 0){
					$(that).parent().parent().remove();	
				}else{
					alert(resp.message);
				}
			});
			return false;
		});
		return this;
	},
	msg: function(){
		$(this).click(function(){
			var that = this;
			$.getJSON(this.href, function(resp){
				if(resp.status == 0){
					if(resp.show != false){
						alert(resp.message);
					}
				}else{
					alert(resp.message);
				}
				if(resp.text){
					$(that).text(resp.text);
				}
				if(resp.reload){
					location.href = location.href
				}
				if(resp.url){
					location.href = resp.url;
				}
				if(resp.login){
					location.href = resp.loginurl+'?src='+encodeURIComponent(location.href);
				}
				if(resp.noclick){
					$(that).before('<span>'+$(that).text()+'</span>').remove();
				}
			});
			return false;
		});
		return this;
	}
});

// jqueryui ext
if(jqueryui){
	jQuery.fn.extend({
		dialogFrame: function(options) {
			var optionsDefault = {autoOpen:false, modal:true, close: function(event, ui){
				$('#dialogframe').attr('src', 'about:blank');
			}};
			options = $.extend(optionsDefault, options);
			if(!window.dialogframe){
				$('body').append('<div id="dialogdiv" style="display:none;"><iframe id="dialogframe" name="dialogframe" src="about:blank" frameborder="0" marginwidth="0" marginheight="0" width="100%" height="100%"></iframe><div>');
				window.dialogframe = $('#dialogdiv').dialog(optionsDefault);
			}
			$(this).click(function(){
				$('#dialogframe').attr('src', this.href);
				$('#dialogdiv').dialog(options).dialog('open');
				return false;
			});
			return this;
		}
	});
}

// function
function imagezoom(new_w, new_h){
	var img = document.createElement('img');
	img.src = this.src;
	var src_w = img.width;
	var src_h = img.height;

	var zoom_w = 0, zoom_h = 0;
	if(src_w > new_w || src_h > new_h){
		var scale_w = new_w / src_w;
		var scale_h = new_h / src_h;
		var b = scale_w < scale_h;
		
		if(b){
			zoom_w = src_w * scale_w;
			zoom_h = src_h * scale_w;
		}else{
			zoom_w = src_w * scale_h;
			zoom_h = src_h * scale_h;
		}
	}else{
		zoom_w = src_w;
		zoom_h = src_h;
	}
	
	this.style.marginLeft = Math.abs(new_w-zoom_w)/2+'px';
	this.style.marginTop = Math.abs(new_h-zoom_h)/2+'px';
	
	this.width = zoom_w;
	this.height = zoom_h;
}
