	
;(function($) {
	
	$.fn.initForm = function (params) {
		var form = this.get(0);
		if (typeof params != "undefined" && params) {
			for (var name in params) {
				setElementValue(form, name, params[name]);
			}		
		}
	}

	function setElementValue(frm, name, value) {	
		try {
			var item = frm[name];
			if (item) {
			
				if (!item.tagName) {
					//alert(name + ' ' + item.length + " " + value);
					for (var i = 0; i < item.length; i++) {
						var box = item[i];
						
						if (typeof value == 'object') {
							box.checked = value[box.value];
							
						} else {
							box.checked = (value == box.value);
						}
					}
				} else if (item.type == "checkbox") {
					item.checked = (value == item.value);

				} else {
					item.value = value;
				}

			} else {
				item = document.getElementById(name);
				if (item) {
					item.innerHTML = value;
				}
			}
		} catch (e) {
		}
	}

	function JSONRequest(base_url) {
		if (base_url) {
			this.base_url = base_url;
		}
		return this;
	}

	/** 
	 * @param url string
	 * @param done function()
	 * @param timeout number 
	 */
	JSONRequest.prototype.get = function(url, done, timeout) {
		return this.send("GET", url, done, timeout);
	}

	JSONRequest.prototype.del = function(url, done, timeout) {
		return this.send("DELETE", url, done, timeout);
	}

	JSONRequest.prototype.put = function(url, data, done, timeout) {
		return this.send("PUT", url, done, timeout, data);
	}

	JSONRequest.prototype.post = function(url, data, done, timeout) {
		return this.send("POST", url, done, timeout, data);
	}

	JSONRequest.prototype.send = function(method, url, done, timeout, data, type) {
		return $.ajax({
			type	: method,
			url		: (this.base_url || "") + url,
			data	: data,
			timeout : timeout || 5000,
			dataType : type || "json",
			success  : done,
			contentType : "application/x-www-form-urlencoded;charset=UTF-8",
			complete : function(request, status) {
				if ($.json.onComplete) {
					$.json.onComplete(request, status);
				}
			},
			beforeSend : function(request) {				
				request.setRequestHeader("Accept", "application/json");
				if ($.json.onStart) {
					$.json.onStart(request);
				}
			}
		});
	}

	$.json = new JSONRequest(null);

	$.fn.showInfo = function(info) {
		var buttonHtml = '<a class="close-box-button" href="#" onclick="$.hideInfo();" title="Close">&nbsp;</a>';
		$(this).html(buttonHtml + info).fadeIn("slow");
	}

	$.hideInfo = function() {
		$('#warning-box').fadeOut("slow"); 
	}
  
	$.popup = function(content){
		var source = event || window.event;
		if (!source) {
			return;
		}

		var event = $.event.fix(source);
		var $pop = $('#pop');

		if ($pop.css("display") == "block") {
			$pop.hide();

		} else {
			var obj = event.target;
			var offset = $(obj).offset();
			var height = $(obj).height();
			var width = $(obj).width();
			var pop_width = $('#pop').width();
			var top = (offset.top + height) + "px";
			var left = (offset.left - pop_width + width) + "px";
			$pop.css("top", top).css("left", left);
			$pop.html('<div class="pop_content">' + content + '</div>');
			$pop.show();
		}
		event.stopPropagation();
	}

	$.pop = function(options){

        // close pops if user clicks outside of pop
        function closeInactivePop() {
			var $pop = $('#pop');
			if ($pop.css("display") == "block") {
				$pop.hide();
			}
            return false;
        }

        $(document.body).click(function(){ 
			var obj = event.target || event.srcElement;
			var parent = $(obj).parents("#pop");
			if (parent.length < 1) {
				closeInactivePop();
			}
        });

    }


	///////////////////////////////////////////////////////////////////////////////
	// Cookie

	function GetCookieVal(offset) {
		var endstr = document.cookie.indexOf(";", offset);
		if (endstr == -1) {
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	}

	$.delCookie = function (name) {
		var exp = new Date();
		exp.setTime (exp.getTime() - 1);
		var cval = this.getCookie (name);
		document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
	}

	$.setCookie = function (name, value, expires, path, domain, secure) {
		var expires_date = new Date();
		if (expires) {
			expires_date.setTime(expires_date.getTime() + ( expires * 1000 ));
		}

		var cookie = name + "=" + escape (value)
			+ ((expires) ? ';expires=' + expires_date.toGMTString() : '')
			+ ((path) ? ";path=" + path : '')
			+ ((domain) ? ";domain=" + domain : '')
			+ ((secure) ? ";secure" : '');

		document.cookie = cookie;
	};

	$.getCookie = function (name) {
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;

		while (i < clen) {
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg)
				return GetCookieVal (j);

			i = document.cookie.indexOf(" ", i) + 1;
			if (i == 0) 
				break;
		}
		return null;
	};



})(jQuery);

/* Chinese initialisation for the jQuery UI date picker plugin. */
/* Written by Cloudream (cloudream@gmail.com). */
jQuery(function($){
try {
	if (!$.datepicker) return ;

	$.datepicker.regional['zh-cn'] = {
		closeText: '关闭',
		prevText: '&#x3c;上月',
		nextText: '下月&#x3e;',
		currentText: '今天',
		monthNames: ['一月','二月','三月','四月','五月','六月',
		'七月','八月','九月','十月','十一月','十二月'],
		monthNamesShort: ['一','二','三','四','五','六',
		'七','八','九','十','十一','十二'],
		dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
		dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],
		dayNamesMin: ['日','一','二','三','四','五','六'],
		dateFormat: 'yy-mm-dd', firstDay: 1,
		isRTL: false};
	$.datepicker.regional['zh-tw'] = {
		closeText: '關閉',
		prevText: '&#x3c;上月',
		nextText: '下月&#x3e;',
		currentText: '今天',
		monthNames: ['壹月','二月','三月','四月','五月','六月',
		'七月','八月','九月','十月','十壹月','十二月'],
		monthNamesShort: ['壹','二','三','四','五','六',
		'七','八','九','十','十壹','十二'],
		dayNames: ['星期日','星期壹','星期二','星期三','星期四','星期五','星期六'],
		dayNamesShort: ['周日','周壹','周二','周三','周四','周五','周六'],
		dayNamesMin: ['日','壹','二','三','四','五','六'],
		dateFormat: 'yy-mm-dd', firstDay: 1,
		isRTL: false};

    $.datepicker.regional['en'] = {
		closeText: 'Close',
		prevText: '&#x3c;Back',
		nextText: 'Next&#x3e;',
		currentText: 'Today',
		monthNames: ['Jan','Feb','Mar','Apr','May','Jun',
		'Jul','Aug','Sep','Oct','Nov','Dec'],
		monthNamesShort: ['Jan','Feb','Mar','Apr','May','Jun',
		'Jul','Aug','Sep','Oct','Nov','Dec'],
		dayNames: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
		dayNamesShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
		dayNamesMin: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
		dateFormat: 'yy-mm-dd', firstDay: 1,
		isRTL: false};

	$.datepicker.setDefaults($.datepicker.regional['zh-cn']);

} catch (e) {}
});


if (!window.localStorage) {
	window.localStorage  = [];
}

function getErrorInfo(text) {
	try {
		var info = JSON.parse(text);
		var html = [];
		html.push("<h4>发生 ");
		html.push(info.type);
		html.push(" 错误.</h4><p>");
		html.push(info.message);
		html.push("</p><p>");
		html.push(info.trace);
		html.push("</p>");

		return html.join("");
	} catch (e) {
		return text;
	}
}


Date.prototype.setISO8601 = function(dString) {
	var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;

	if (dString.toString().match(new RegExp(regexp))) {
		var d = dString.match(new RegExp(regexp));
		var offset = 0;

		this.setUTCDate(1);
		this.setUTCFullYear(parseInt(d[1],10));
		this.setUTCMonth(parseInt(d[3],10) - 1);
		this.setUTCDate(parseInt(d[5],10));
		this.setUTCHours(parseInt(d[7],10));
		this.setUTCMinutes(parseInt(d[9],10));
		this.setUTCSeconds(parseInt(d[11],10));

		/*if (d[12])
			etUTCMilliseconds(parseFloat(d[12]) * 1000);
		else
			this.setUTCMilliseconds(0);*/

		if (d[13] != 'Z') {
			offset = (d[15] * 60) + parseInt(d[17],10);
			offset *= ((d[14] == '-') ? -1 : 1);
			this.setTime(this.getTime() - offset * 60 * 1000);
		}
	} else {
		this.setTime(Date.parse(dString));
	}
	return this;
};

function padTime(value) {
	return value < 10 ? '0' + value : value;
}

Date.prototype.toISOString = function() {
	var text = "";
	text += this.getFullYear();
	text += "-";
	text += padTime(this.getMonth());
	text += "-";
	text += padTime(this.getDate());
	text += " ";
	text += padTime(this.getHours());
	text += ":";
	text += padTime(this.getMinutes());
	text += ":";
	text += padTime(this.getSeconds());
	return text;
}

$util = {
	formatDate : function(iso8601Date) {
		try {
			var date = new Date();
			date.setISO8601(iso8601Date);
			return date.toLocaleString();
		} catch (e) {
			return iso8601Date + "@" + e;
		}
	}
};

function changeImg(obj, w)
{
	var img = new Image();
	img.src = obj.src;
	var imgw = img.width;
	var imgh = img.height;
	if(imgw > w){
		obj.style.width = w + "px";
	}else{
		obj.style.width = imgw + "px";
	}
}

function init_dialog(id, form, w, h, servcie, done) {
	var params = { bgiframe: true, autoOpen: false, width: w, height: h, modal: true };
	params.buttons = {};
	params.buttons[CANCEL] = function() { $(this).dialog('close'); };
	params.buttons[OK] = function() { servcie[done || "submit"](); };
	params.close = function() { $(form).clearForm(); };
	$(id).dialog(params);
}

///////////////////////////////////////////////////////////////////////////////

var XVisionUtils = {};

/** 显示分页导航条 */
XVisionUtils.showPager = function(pagerContainer, curPage, pageSize, totalCount) {
	var startIndex = 0;
	var endIndex = 0;
	var pageCount = 0;

	pageSize = pageSize || 20;
	curPage  = curPage || 1;

	if (totalCount > 0) {
		startIndex	= (curPage - 1) * pageSize + 1;
		endIndex	= startIndex + pageSize - 1;
		pageCount	= parseInt((totalCount - 1) / pageSize) + 1;
		if (curPage == pageCount) {
			endIndex = totalCount;
		}

	} else {
		totalCount = 0;
	}

	var html = '';

	// 起始位置 - 结束位置 / 总共的数目.
	html += '<div class="lf"> &nbsp; ' + startIndex + " - " + endIndex + " / " + totalCount + '</div>';
	html += '<div class="grid-page-nav">';

	if (pageCount > 1) {

		// 转到第一页.
		if (pageCount > 0) {
			html += '<a href="#" onclick="onGotoPage(1, ' + pageSize + ');">'
				+ '<img src="images/grid/page-first.gif" align="absmiddle" title="First Page"></a>';
		}

		var offset = 5;
		var list = [];

		// 计算要显示的页码号. 一般显示当前页前后五页的链接.
		if (curPage - offset > 10) {
			list.push(curPage - offset - 10);
		}

		for (var k = curPage - offset; k < curPage + offset - 1; k++) {
			if (k >= 0 && k < pageCount) {
				list.push(k);
			}
		}

		if (curPage + offset + 8 < pageCount) {
			list.push(curPage + offset + 8);
		}

		var length = list.length;
		var page = 0;

		for (var k = 0; k < length; k++) {
			page = list[k] + 1;
			if (page == curPage) {
				html += '<span class="grid-page-current">' + page + "</span>";
				continue;
			}
			html += "<a href='#' onclick='onGotoPage(" + page + ", " + pageSize + ");'>" + page + "</a>";
		}

		// 转到最后一页
		if (pageCount > 0) {
			html += '<a href="#" onclick="onGotoPage(' + pageCount + ', ' + pageSize + ');">'
				+ '<img src="images/grid/page-last.gif" align="absmiddle" title="Last Page"></a>';
		}
	}

	html += '</div>';
	$('#' + pagerContainer).html(html);
}

function getLoadingHtml() { 
	var tip = window.LOADING || "Loading, please wait...";
	return '<img src="/images/blue_loading.gif" class="absmiddle"/> <label>' + tip + '</label>';
}

function onSelectLanguage(lang) {
	//$.setCookie("ui_lang", lang, 3600 * 24 * 100);
	$.setCookie("ui_lang", lang, 3600 * 24 * 100, '/');
	location.reload();
} 

  function SelectLanguage(lang) {
	$.setCookie("ui_lang", lang, 3600 * 24 * 100);
      // location.reload();

}
function SlidePlayer() {
	this.curr = 0;
	this.next = 0;
	this.count = 0;

	this.init = function() {
		this.count = $('#slide-buttons a').size();	
		var timer = setInterval('playNextSlide()', 5000);

		// 鼠标移动到图片或导航上停止播放，移开后恢复播放	
		$('#slide-box li, #slide-buttons a').hover(
			function() { clearInterval(timer); }, 
			function() { timer = setInterval('playNextSlide()', 5000); });
		
		var player = this;

		//点击导航播放到相应的图片		
		$('#slide-buttons a').click(function() {
			// index()函数返回当前导航的下标
			var index = $('#slide-buttons a').index(this);
			if (player.curr != index) {
				player.play(index);
				player.curr = index;
			};
			return false;
		});
	}

	this.playNext = function() {
		this.next = this.curr + 1;
		// 若当前图片播放到最后一张，这设置下一张要播放的图片为第一张图片的下标
		if (this.curr == this.count - 1) {
			this.next = 0;
		}

		this.play(this.next);
		this.curr++;

		// 在当前图片的下标加1后，若值大于最后一张图片的下标，则设置下一轮其实播放的图片下标为第一张图片的下标，而next永远比curr大1
		if (this.curr > this.count - 1) { 
			this.curr = 0; 
			this.next = this.curr + 1; 
		}
	}

	// 控制播放效果的函数
	this.play = function(next) {
		$('#slide-buttons a').siblings('a').removeClass('active').end().eq(next).addClass('active');
		$('#slide-box li').eq(this.curr).css({'top': '0px', 'left':'0px'}).animate({'top': '-300px'}, 'slow', 
			function() { $(this).css({'left': '0px', 'display': 'none'}); 
		}).end().eq(next).css({'top': '280px', 'left':'0px', 'display': 'block'}).animate({'top': '0px'}, 'slow', function() {});
	}
}

 
// 播放图片的函数
var playNextSlide = function() {
	if (slidePlayer) {
		slidePlayer.playNext();
	}
}

$(document).ready(function() {
	$('#foot_lang').hover(function() {
		$('#foot_lang_menu').show();
	}, function(event) {
		var target = event.relatedTarget || {};
		if (target.id != 'foot_lang_menu') {
			$('#foot_lang_menu').hide();
		}
	});

	$('#foot_lang_menu').hover(null, function() {
		$(this).hide();
	});
});

var now = new Date();
var gmtseconds = now.getTimezoneOffset()*60;
$.setCookie("time_zone", gmtseconds, 36000*24*360);
