var LiuYanObj = function (box){
	var This = this;
	this.box = $(box);
	this.timer = null;
	this.texts = $('#mes_name, #mes_mobile, #content_message, #mes_address', box);
	this.txtMobile = $('#mes_mobile', box);
	this.txtEmail = $('#mes_email', box);
	this.txtContent = $('#content_message', box);
	this.btnSend = $('#message_send', box);
	this.addContentDom = $('.addtext', box);
	this.form = $('#msgForm', box);
	this.isError = function (){
		return $('.checkerror:visible', box).length > 0 ? true : false;
	}
	this.defaultCheck = function (){
		$('.checkerror', box).hide();
		$('.required', box).show();
	};
	this.checkCall = function (str){
		return /^1[0-9]{10}$/.test(str);
	};
	this.checkEMail = function (str){
		return $.getRegEmail().test(str);
	};
	this.init = function (){
		
		//载入样式
		var css = document.createElement('link');
		css.setAttribute('rel', 'stylesheet');
		css.setAttribute('type', 'text/css');
		css.setAttribute('href', 'http://css1.qudao.com/common_style/newMsgBoard.css');
		document.getElementsByTagName('head')[0].appendChild(css);
		
		$('.close', '#liuyanSuccessDialog').click(function (e){
			e.preventDefault();
			Dialog.hide($('#liuyanSuccessDialog'));
		});

		$(':text, textarea', box).each(function (){
			this.value = '';
		});
		
		this.defaultCheck();
		this.texts.blur(function (){
			var val = this.value;
			if ($.trim(val) == ''){
				$(this).siblings('.required').hide();
				$(this).siblings('.checkerror').css('display', 'inline-block')
				.find('strong').html('请填写' + this.title);
				return;
			}
			$(this).siblings('.required').show();
			$(this).siblings('.checkerror').hide();
		});
		
		this.txtContent.focus(function (){
			var val = this.value;
			if ($.trim(val) != ''){
				$(this).siblings('.required').show();
				$(this).siblings('.checkerror').hide();
			}
		});
		
		this.txtMobile.blur(function (){
			var val = this.value;
			if ($.trim(val) == '') return;
			if (! This.checkCall(val)){
				$(this).siblings('.required').hide();
				$(this).siblings('.checkerror').css('display', 'inline-block')
				.find('strong').html('手机格式有误');
				return;
			}
			$(this).siblings('.required').show();
			$(this).siblings('.checkerror').hide();
		});
		
		this.txtEmail.blur(function (){
			var val = this.value;
			if ($.trim(val) == ''){
				$(this).siblings('.checkerror').hide();
				return;
			}
			if (! This.checkEMail(This.txtEmail.val())){
				$(this).siblings('.checkerror').css('display', 'inline-block')
				.find('strong').html('邮箱格式有误');
				return
			}
			$(this).siblings('.checkerror').hide();
		});
		
		this.txtContent.hover(function(){
			clearTimeout(This.timer);
			This.addContentDom.show();
		},function(){
			This.timer = setTimeout(function(){
				This.addContentDom.hide();
			},1000);
		});

		this.addContentDom.hover(function(){
			clearTimeout(This.timer);
		},function(){
			This.timer = setTimeout(function(){
				This.addContentDom.hide();
			},1000);
		});
		
		this.addContentDom.find('li').hover(function (){
			$(this).addClass('current');
		}, function (){
			$(this).removeClass('current');
		})
		.click(function (){
			var str = This.txtContent[0].value + $('span', this).html();
			This.txtContent[0].value = str;
			This.txtContent.focus();
			This.addContentDom.hide();
		});
		
		this.btnSend.bind('click',function (){
			This.defaultCheck();
			This.texts.blur();
			This.txtEmail.blur();
			if (This.isError()){
				return;
			}
			if (! This.checkCall(This.txtMobile.val())){
				$(this).siblings('.checkerror').css('display', 'inline-block')
				.find('strong').html('手机格式有误');
				return;
			}
			$('#message_send', box).addClass('dis-btn').unbind('click');
			// ajax 提交留言
			var ITEM_ID = $('#itemid').val();
			var url = This.form.attr('action');
			var prarm = This.form.serialize() + '&time=' + new Date().getTime();
			$.post(url, prarm, function (data) {
				if(data.result == 1){
					if (document.getElementById('liuyanSuccessDialog')){
                        if(window.USERSTATUS){
                            if (window.USERSTATUS['status'] > 0) {
                                $('#login_txt').hide();
                            }
                            if (window.USERSTATUS['status'] == 0){
                                $('#login_txt').show();
                            }
                        }
                        showMsgSuccLayer(data);
                        //SetMsgStat(ITEM_ID,data.result,1);
                        //Dialog.show($('#liuyanSuccessDialog'));
					} else {
						alert('留言成功!');
					}
				}else if(data.result == 0){
					alert('留言失败!');
				}else{
					alert('系统出错，留言失败!');
				}
				$(':text, textarea', This.form).each(function (){
					this.value = '';
				});
			}, 'json');
		});
	};
};
var Dialog = {
	show : function (dom){
		var isIE6 = $.browser.msie && $.browser.version == '6.0';
		if (isIE6){
			dom.css({
				position: 'absolute',
				zIndex: 999,
				top: document.documentElement.scrollTop + (document.documentElement.clientHeight - dom.height()) / 2,
				left: document.documentElement.scrollLeft + (document.documentElement.clientWidth - dom.width()) / 2
			});
		} else {
			dom.css({
				position: 'fixed',
				zIndex: 999,
				top: '50%',
				left: '50%',
				marginTop: 0 - dom.height() / 2,
				marginLeft: 0 - dom.width() / 2
			});
		}
		dom.show();
	},
	hide : function (dom){
		if ($.browser.msie){
			dom.hide();
		} else {
			dom.fadeOut(200);
		}
	}
}