/*
 * 
 * @author: jack@softwe.net
 * @version
 * @copyright
 */
function verifyEmail(obj) 
{ 
	var email = obj; 
	var pattern = /^[_\.0-9a-z-]+@[0-9a-z-]+(\.[a-z]{2,4})+$/i; 
	flag = pattern.test(email); 
	if(flag) 
	{  
		return true; 
	} 
	else 
	{ 
		return false; 
	} 
}

 $(document).ready(function(){
	$("#contact").submit(function(){
		var cid = $("#cid").val();
		var ty = $("#ty").val();
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		var email = $("#email").val();
		var phone = $("#phone").val();
		var contacttime = $("#contacttime").val();
		var comments = $("#comments").val();
		$("#contactResult").html(waitImg+' 數據處理中...').css({color:'red'}).show();
		if(cid<1 || firstname=='' || email=='' || phone=='' || contacttime=='' || lastname=='')
		{
			$("#contactResult").text("請填寫所有必填項。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
		}
		else
		{
			if(!verifyEmail(email))
			{
				$("#contactResult").text("請填寫正確格式的郵箱地址。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
				return false;
			}
			$.post(appUrl+'Index/send',{cid:cid,ty:ty,firstname:firstname,email:email,phone:phone,contacttime:contacttime,lastname:lastname,comments:comments,ajax:'1'},function(xml){
				if($("result",xml).text()=='1')
				{
					$("#contactResult").text("郵件發送成功。").css({color:'green',fontWeight:'bold'}).show();
					$("#firstname").val("");
					$("#lastname").val("");
					$("#email").val("");
					$("#phone").val("");
					$("#contacttime").val("");
					$("#comments").val("");
				}
				else
				{
					$("#contactResult").text("郵件發送失敗。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
				}
			});
		}
		return false;
	});

	$("#contactUs").submit(function(){
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		var email = $("#email").val();
		var phone = $("#phone").val();
		var contacttime = $("#contacttime").val();
		var comments = $("#comments").val();
		$("#contactUsResult").html(waitImg+' 數據處理中...').css({color:'red'}).show();
		if(firstname=='' || email=='' || phone=='' || contacttime=='' || lastname=='')
		{
			$("#contactUsResult").text("請填寫所有必填項。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
		}
		else
		{
			if(!verifyEmail(email))
			{
				$("#contactUsResult").text("請填寫正確格式的郵箱地址。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
				return false;
			}
			$.post(appUrl+'Index/doContactus',{firstname:firstname,email:email,phone:phone,contacttime:contacttime,lastname:lastname,comments:comments,ajax:'1'},function(xml){
				if($("result",xml).text()=='1')
				{
					$("#contactUsResult").text("郵件發送成功。").css({color:'green',fontWeight:'bold'}).show();
					$("#firstname").val("");
					$("#lastname").val("");
					$("#email").val("");
					$("#phone").val("");
					$("#contacttime").val("");
					$("#comments").val("");
				}
				else
				{
					$("#contactUsResult").text("郵件發送失敗。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
				}
			});
		}
		return false;
	});

	$("#contacts").submit(function(){
		var bid = $("#bid").val();
		var firstname = $("#firstname").val();
		var lastname = $("#lastname").val();
		var email = $("#email").val();
		var phone = $("#phone").val();
		var contacttime = $("#contacttime").val();
		var comments = $("#comments").val();
		$("#contactsResult").html(waitImg+' 數據處理中...').css({color:'red'}).show();
		if(bid<1 || firstname=='' || email=='' || phone=='' || contacttime=='' || lastname=='')
		{
			$("#contactsResult").text("請填寫所有必填項。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
		}
		else
		{
			if(!verifyEmail(email))
			{
				$("#contactsResult").text("請填寫正確格式的郵箱地址。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
				return false;
			}
			$.post(appUrl+'Index/sends',{bid:bid,firstname:firstname,email:email,phone:phone,contacttime:contacttime,lastname:lastname,comments:comments,ajax:'1'},function(xml){
				if($("result",xml).text()=='1')
				{
					$("#contactsResult").text("郵件發送成功。").css({color:'green',fontWeight:'bold'}).show();
					$("#firstname").val("");
					$("#lastname").val("");
					$("#email").val("");
					$("#phone").val("");
					$("#contacttime").val("");
					$("#comments").val("");
				}
				else
				{
					$("#contactsResult").text("郵件發送失敗。").css({color:'red',fontWeight:'bold'}).show().fadeOut(3000);
				}
			});
		}
		return false;
	});
});
 (function($){

$.fn.kxbdMarquee = function(options){
	var opts = $.extend({},$.fn.kxbdMarquee.defaults, options);
	
	return this.each(function(){
		var $marquee = $(this);//滚动元素容器
		var _scrollObj = $marquee.get(0);//滚动元素容器DOM
		var scrollW = $marquee.width();//滚动元素容器的宽度
		var scrollH = $marquee.height();//滚动元素容器的高度
		var $element = $marquee.children(); //滚动元素
		var $kids = $element.children();//滚动子元素
		var scrollSize=0;//滚动元素尺寸
		var _type = (opts.direction == 'left' || opts.direction == 'right') ? 1:0;//滚动类型，1左右，0上下
		
		//防止滚动子元素比滚动元素宽而取不到实际滚动子元素宽度
		$element.css(_type?'width':'height',10000);
		//获取滚动元素的尺寸
		if (opts.isEqual) {
			scrollSize = $kids[_type?'outerWidth':'outerHeight']() * $kids.length;
		}else{
			$kids.each(function(){
				scrollSize += $(this)[_type?'outerWidth':'outerHeight']();
			});
		}
		//滚动元素总尺寸小于容器尺寸，不滚动
		if (scrollSize<(_type?scrollW:scrollH)) return; 
		//克隆滚动子元素将其插入到滚动元素后，并设定滚动元素宽度
		$element.append($kids.clone()).css(_type?'width':'height',scrollSize*2);
		
		var numMoved = 0;
		function scrollFunc(){
			var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft':'scrollTop';
			if (opts.loop > 0) {
				numMoved+=opts.scrollAmount;
				if(numMoved>scrollSize*opts.loop){
					_scrollObj[_dir] = 0;
					return clearInterval(moveId);
				} 
			}

			if(opts.direction == 'left' || opts.direction == 'up'){
				var newPos = _scrollObj[_dir] + opts.scrollAmount;
				if(newPos>=scrollSize){
					newPos -= scrollSize;
				}
				_scrollObj[_dir] = newPos;
			}else{
				var newPos = _scrollObj[_dir] - opts.scrollAmount;
				if(newPos<=0){
					newPos += scrollSize;
				}
				_scrollObj[_dir] = newPos;
			}
		}
		//滚动开始
		var moveId = setInterval(scrollFunc, opts.scrollDelay);
		//鼠标划过停止滚动
		$marquee.hover(
			function(){
				clearInterval(moveId);
			},
			function(){
				clearInterval(moveId);
				moveId = setInterval(scrollFunc, opts.scrollDelay);
			}
		);
		
	});
};
$.fn.kxbdMarquee.defaults = {
	isEqual:true,//所有滚动的元素长宽是否相等,true,false
	loop: 0,//循环滚动次数，0时无限
	direction: 'left',//滚动方向，'left','right','up','down'
	scrollAmount:1,//步长
	scrollDelay:40//时长

};
$.fn.kxbdMarquee.setDefaults = function(settings) {
	$.extend( $.fn.kxbdMarquee.defaults, settings );
};
})(jQuery);
