﻿function isIE(){ return $.browser.msie; }
function isIE6(){ return  isIE() && $.browser.version == '6.0'; }
function U(url, lan){ return url.indexOf('http://') === 0 ? url : baseUrl.replace('{lan}', lan || language)+'/'+url.trim('/'); }
function T(url){ return themeRoot+'/'+url.trim('/'); }
function B(url){ return baseRoot+'/'+url.trim('/'); }
function reload(){window.location.reload(true); }
function redirect(url) {window.location.href = url;}
function strToInt(str) {return isNaN(str = parseInt(str)) ? 0 : str;}

$.extend(Array.prototype, {
	filter : function(func) { var i = 0, j = this.length; for (;i<j; i++) { if (func) { isFunction(func) ? func.apply(this[i], [this[i]]) === false && delete this[i] :	this[i] === func && delete this[i];	} else if (!this[i]) { delete this[i];}} return this; }
});

$.extend(String.prototype, {
	trim : function(t){ var re = new RegExp('(^'+(t || ' ')+'*)|('+(t || ' ')+'*$)', 'g'); return this.replace(re,''); },
	ucfirst : function(){ return this.substr(0, 1).toUpperCase()+this.substr(1)},
	repeat : function(i){ var s ='';for(var j=0;j<i;j++) s+=this;return s; }
});

jQuery.fn.allval = function(){
	var val = '';
	this.each(function(){
		val += $(this).val();
	});
	return val;
}

//图片自动大小
$.fn.autoSize = function(width, height, func) {
	return this.each(function(){
		var $this = $(this);
		if (!$this.is('img')) return;
		this.complete && $.fn.autoSize.func.apply(this, [{data:{width:width, height:height, center:func}}]);
		$this.unbind('load', $.fn.autoSize.func).bind('load', {width:width, height:height, center:func}, $.fn.autoSize.func)
	});
}

$.fn.autoSize.func = function(e){
	var $this = $(this), img = $('<img/>').attr({src:$this.attr('src')})[0], width = e.data.width, height = e.data.height,
		_width = height / img.height * img.width, _height = width / img.width * img.height;
	if (!img.width || !img.height) return; 
	width < _width && (height = _height) || (width = _width);
	$this.css({width:parseInt(width), height:parseInt(height)});
	isFunction(e.data.center) && e.data.center.apply($this, [width, height]);
}

//模拟img标签
$.fn.likeimg = function(url){
	isIE()
		&& this.css({filter:'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=bEnabled,sizingMethod=scale,src='+url+')'})
		|| this.css({background:'url('+url+')','-moz-background-size':'100% 100%','background-size':'100% 100%'});
	return this;
}

//设置层级在节点前面
$.fn.beforeLayer = function($node){
	var zIndex = 0
	while(($node = $node.offsetParent()) && !$node.is('body,html')) {
		if (strToInt($node.css('zIndex')) > zIndex) {
			zIndex = strToInt($node.css('zIndex'));
		}
	}
	return this.css('zIndex',++zIndex);
}

$.fn.nearLayer = function(layer){
	return this.each(function(){
		var $this = $(this), elPos = layer.offset(), x, y;
		WIN.height() / 2 > elPos.top - $(document).scrollTop() ?
			y = elPos.top + layer.outerHeight() :
			y = elPos.top - $this.outerHeight();
		WIN.width() / 2 > elPos.left - $(document).scrollLeft() ?
			x = elPos.left: 
			x = elPos.left - $this.outerWidth() + layer.outerWidth();
		$this.css({ position: 'absolute', left: x, top: y})
	});
}

$.fn.childrenSize = function(type) {
	var size = 0;
	this.children().each(function(){
		size += $(this)['outer'+type.ucfirst()]();
	});
	return this[type](size);
}

//设置居中
$.fn.center = function(offset){
	return this.each(function(){
		var $this = $(this), top, left;
		if (offset === true) {
			offset = $this.offsetParent();
			top   = (offset.height() - $this.height()) / 2,
			left  = (offset.width() - $this.width()) / 2;
		} else {
			top   = (WIN.height() - $this.height()) / 2,
			left  = (WIN.width() - $this.width()) / 2;
		}
		if (left < 0) left = 0;
		if (top < 0) top = 0;
		top  += $(offset || document).scrollTop();
		left += $(offset || document).scrollLeft();
		$(this).css({top : top + 'px',left : left + 'px'});
	});
}

//设置具有定位属性
$.fn.pos = function(type){
	return this.each(function(){
		type ?
			$(this).css('position','absolute') :
			$(this).css('position') == 'static' ? $(this).css('position','relative') : '';
	})
}

$.fn.enter = function(func){
	return this.bind('keydown',function(e){
		if (e.keyCode == 13 && isFunction(func)) {
			return func.apply(this, [e]);
		}
	});
}

$.fn.stopPropagation = function(type){
	return this.bind(type || 'click', function(e){
		e.stopPropagation();
	});
}

$.fn.active = function(cls, siblings){
	!cls && (cls = 'active');
	return this.each(function(){
		siblings ? 
			siblings.removeClass(cls) :
			$(this).siblings().removeClass(cls);
		$(this).addClass(cls)
	});
}

//禁用鼠标选择
$.fn.selectNone = function(bool){
	if (bool !== false) {
		return this.css({'-moz-user-select':'-moz-none'}).each(function(){
			this.onselectstart = function(){return false;}
		})
	} else {
		return this.css({'-moz-user-select':''}).each(function(){
			this.onselectstart = function(){}
		})
	}
}

$.fn.move = function (setting){
	setting = $.extend({
		cursor     : 'move',             //可移动元素的鼠标样式
		notEqual   : 'a',                //不参加移动的元素的选择器
		virtualBox : false,              //是否以虚线框移动
		direction  : 'auto',             //可移动方向
		startLen   : 10,                 //开始移动步长
		rect       : false,              //矩型泛围
		autoZindex : true,
		start      : function(){},
		move       : function(){},
		stop       : function(){}
	}, setting || {});

	return this.each(function(){
		var $this  = $(this),
			virtualBox,
			rect   = setting.rect,
			header = setting.header ? $this.find(setting.header) : $this,
			target = $(setting.target || $this);
		var isMouseDown, isDraw, targetLeft, targetTop, clientX, clientY, left, top;
		$this.mousedown(function(e){
			if (setting.autoZindex) {
				jQuery.fn.move.prototype.zIndex = (jQuery.fn.move.prototype.zIndex || 1000) + 1;
				target.css({zIndex : jQuery.fn.move.prototype.zIndex});
			}
		}).mousedown();
		header.css({cursor : setting.cursor}).mousedown(function(e){
			if (!$(e.target).is(setting.notEqual)) {
			//	e.preventDefault();
				isMouseDown = true;
				clientX     = e.clientX;
				clientY     = e.clientY;
				targetLeft  = strToInt(target.offsetLeft || target.css('left'));
				targetTop   = strToInt(target.offsetTop || target.css('top'));
				ofpar = target.offsetParent();
				ofpar.is('body') && (ofpar = $(document));
				setting.rect === true && (rect = [0, 0, ofpar.width()-target.width(), ofpar.height()-target.height()]);
				return false;
			}
		}).selectNone();
		
		if (setting.virtualBox) {
			virtualBox = $this.clone().data('clone', $this)
				.css({position:'absolute',cursor:setting.cursor, zIndex:999999}).appendTo('body').hide();
		} else {
			target.pos('abs');
		}
		$(document).bind('mousemove',function(e){
			if (!isMouseDown) return;
			if (isDraw) {
				left = e.clientX - clientX + targetLeft;
				top  = e.clientY - clientY + targetTop;
				setting.direction == 'x' && (top  = targetTop);
				setting.direction == 'y' && (left = targetLeft);
				if (isArray(rect)) {
					left < rect[0] && (left = rect[0]);
					top  < rect[1] && (top  = rect[1]);
					left > rect[2] && (left = rect[2]);
					top  > rect[3] && (top  = rect[3]);
				}
				var p = {left:left,top:top};
				if (setting.move.apply(target, [e, p, rect]) !== false) {
					(virtualBox || target).css(p); 
				}
			} else if(Math.abs(e.clientX - clientX) > setting.startLen || Math.abs(e.clientY - clientY) > setting.startLen) {
			//	isIE() && header[0].setCapture();
				isDraw = true;
				setting.start.apply(target, [e, {left:left,top:top}, virtualBox, rect]);
				if (setting.virtualBox) {
					virtualBox.show()
						.css({zIndex:jQuery.fn.move.prototype.zIndex+1});
				}
			}
		}).bind('mouseup',function(e){
			if (!isMouseDown) return;
			if (isDraw) {
				if (setting.virtualBox) {
					virtualBox.hide();
					target.css({left:virtualBox.css('left'),top:virtualBox.css('top')});
				}
				setting.stop.apply(target, [e, {left:left,top:top}, virtualBox, rect]);
			}
		//	isIE() && header[0].releaseCapture();
			isMouseDown = isDraw = false;
		});
	});
}

$.each(['Object','Array','Function','String','Boolean','Number'], function(i, type){
	window['is'+type] = function( check ){
		return Object.prototype.toString.call(check) == '[object '+type+']';
	}
});

function filterCode(str){
	return (str || '').toString().replace(/\\/g,'\\\\').replace(/\"/g,'\\"').replace(/\n/g,'\\n').replace(/\r\n/g,'\\r\\n').replace(/\r/g,'\\r').replace(/\t/g,'\\t');
}

function jsonEncode(object){
	var jsonArray = [],	jsonStr = '';
	if (isArray(object)) {
		$.each(object || [],function(i,val){
			if (isObject(val) || isArray(val)) {
				jsonArray.push(jsonEncode(val));
			} else if (isNumber(val)) {
				jsonArray.push(val);
			} else {
				jsonArray.push('"'+filterCode(val)+'"');
			}
		});
		return '['+jsonArray.toString()+']';
	} else if (isObject(object) && object !== undefined) {
		$.each(object,function(key,val){
			jsonStr = '"'+key+'":';
			if (val !== undefined && isObject(val) || isArray(val)) {
				jsonArray.push(jsonStr + jsonEncode(val));
			} else if (isNumber(val)) {
				jsonArray.push(jsonStr + val);
			} else {
				jsonArray.push(jsonStr + '"'+filterCode(val)+'"');
			}
		});
		return '{'+jsonArray.toString()+'}';
	}
}

function jsonDecode(str){
	try{
		str = (str || '').replace(/\\u[A-z0-9]{4}/g,function($1){ return unescape($1.replace('\\','%')); }).trim();
		if (str.indexOf('{') === 0 || str.indexOf('[') === 0) return eval("("+str+")");
	}catch(e){
		return '';
	}
}

function cookie(name, value, expires){
	if (value !== undefined) { 
		if (value === null) { 
			value = ''; 
			expires = -1; 
		} 
		var path = '', domain = '', secure = ''; 
		if (expires && (typeof expires == 'number' || expires.toUTCString)) { 
			var date = expires; 
			if (typeof expires == 'number') { 
				date = new Date(); 
				date.setTime(date.getTime() + (expires * 24 * 60 * 60 * 1000)); 
			}
			expires = '; expires=' + date.toUTCString(); 
		} 
		document.cookie = [name, '=', encodeURIComponent(value), expires, path && '; path='+path, domain && '; domain='+domain, secure && '; secure'].join(''); 
	} else { 
		var cookieValue = null; 
		if (document.cookie && document.cookie != '') { 
			var cookies = document.cookie.split(';'); 
			for (var i = 0; i < cookies.length; i++) { 
				var cookie = jQuery.trim(cookies[i]); 
				if (cookie.substring(0, name.length + 1) == (name + '=')) { 
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 
					break; 
				} 
			} 
		} 
		return cookieValue; 
	}
}

var DOC = $(document),BODY = $('body'), WIN = $(window);
//创建KIT组件时进行扩展

$.util = {};
$.kits = {};
$.kitGroup = {};

$.ajax.ajax = '';
$.ajax.queues = [];
$.ajax.server = function(url, data, cb, type, fn){
	//this.ajax && this.ajax.abort();
	var app = $, args = []
	
	if (isFunction(data) || isArray(data)) {
		type = cb; cb = data; data = {};
	}
	
	data.fn = 'func'+$.util.randomName();
	
	if (data.extend) {
		data.extend.apply && (app = data.extend.apply);
		data.extend.args && (args = data.extend.args);
		delete data.extend;
	}
	
	if (isArray(cb)) {
		fn = cb[1];cb = cb[0]; 
	}
	
	$.ajax.bool = false;
	
	return this.ajax = $.ajax({
		'type' : type || 'POST',
		'data' : data,
		'cache' : false,
		'url' : url,
		'success' : function(text, response) {
			$.ajax.ajax = '';
			response = jsonDecode(text);
			if (response) {
				response.msg && callback('user.msg', response.msg);
				callback(response.fn, response.fndata) !== false && ($.ajax.bool = true) && cb && cb.apply(this, [response, text]);
				isFunction(window[data.fn]) && window[data.fn].apply(app, args);
			} else {
				alert('request is error:' + text);
			}
		},
		'complete' : function(a){
			$.ajax.ajax = '';
			isFunction(fn) && fn(a.responseText, $.ajax.bool);
			$.ajax.queues.length && $.ajax.server.apply($.ajax, [$.ajax.queues.shift()]);
		},
		'error' : function(response, textStatus){
			$.ajax.ajax = '';
			alert('请求失败!'+textStatus);
		}
	});
}

$.ajax.queue = function(){
	if (this.ajax) return this.queues.push(arguments);
	return this.server.apply(this, arguments);
}

$.ajax.abort = function(){
	return this.ajax && this.ajax.abort();
}

$.util.setHomePage = function(){
	if ($.browser.msie) {
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(location.href);
	}else {
		alert('您的浏览器不支持该操作!');
	}
}

$.util.co = function(){
	var object = {};
	$.each(arguments, function(i, ary){
		object[ary[0]] = ary[1];
	});
	return object;
}

$.util.addFavorite = function(url, title){
	url = url || location.href;
	title = title || document.title;
	try{
		if ($.browser.msie) {
			if ($.browser.version >= 8){
				external.AddToFavoritesBar(url, title, 'slice');
			} else {
				window.external.addFavorite(url, title);
			}
		} else {
			window.sidebar.addPanel(title, url, "");
		}
	} catch(e) {alert('您的浏览器不支持此操作')}
}

$.util.randomName = function(name) {
	return (name || '')+parseInt(Math.random()*10000).toString();
}

$.apply = function(fn, ext){
	var oldPrototype = fn.prototype,
		newFn = function(){};
	newfn.prototype = oldPrototype;
	fn = new newfn();
	y.extend(fn, ext);
	fn.superClass = dp;
	return fn;
}
	
$.kitExtendend = {
	width : function(width){
		return this.dom.css({width:width}), this;
	},
	height : function(height){
		return this.dom.css({height:height}), this;
	},
	bind : function(evt, func){
		var This = this;
		return this.dom.bind(evt, function(e){
			return func.apply(This, [e]);
		}), this;
	}
}

$.kit = function(kitName, extend){
	if (!this.kit[kitName]) {
		var func = new Function();
		func.extend = function(ext){
			return $.extend(func.prototype, ext);
		}
		func.apply = function(ext){
			return $.apply(func, ext);
		}
		$.kit[kitName] = func;
		$.kitGroup[kitName] = [];
		func.extend($.kitExtendend);
		func.extend(extend);
	} else {
		extend = extend || {};
		if (extend.id && $.kits[extend.id]) {
			return $.kits[extend.id];
		}
		var kit = new this.kit[kitName]();
		kit.isKit = true;
		kit.settings = extend;
		if (kit.initComplete && kit.initComplete(extend, kitName, kit) !== false) {
			$.each(extend, function(key, val){
				isFunction(kit[key]) && kit[key](val);
			});
		}
		extend.id && ($.kits[extend.id] = kit);
		this.kitGroup[kitName].push(kit);
		var destory = kit.destory;
		kit.destory = function(){
			if (!isFunction(destory) || destory.apply(kit) !== false) {
				this.dom && this.dom.remove();
			}
			if (extend.id && $.kits[extend.id]) {
				delete $.kits[extend.id]	;
			}
			$.each($.kitGroup[kitName],function(k,v){
				v === kit && delete $.kitGroup[kitName][k];
			});
		}
		return kit;
	}
}

$.kit('mast', {
	initComplete : function(settings){
		if (!settings) settings = {};
		settings.content && settings.content.pos();
		this.dom = $('<div/>').hide()
			.css({zIndex:100,left:0,top:0,background:'#000',opacity:0.4}).pos('abs');
	},
	show : function(content){
		if (content) {
			var width = content[0].scrollWidth, height = content[0].scrollHeight;
		} else {
			var content = $('body'), width = DOC.width(), height = DOC.height(); 
		}
		if (isIE()) {
			if (height > content.height()) width -= 21;
		}
		return this.dom.show().appendTo(content).css({height:height, width:width}), this;
	},
	hide : function(){
		this.dom.remove();
	}
});

$.kit('window',{
	initComplete : function(settings, kitName, kthis){
		this.dom = $.tpl.skin('window').appendTo('body').pos('abs').hide();
		this.$title = kthis.dom.find('.title');
		this.$content = kthis.dom.find('.content').pos();

		this.dom.find('.close').click(function(){
			kthis.destory();
		});

		kthis.dom.move({header : '.title:eq(0)',not : '.close'});
	},
	button : function(btn){
		btn.appendTo(this.$button);
		this.$button.show();
		return this;
	},
	title : function(str) {
		return this.$title.find('.h3').empty().append(str);
	},
	content : function(html) {
		return this.$content.empty().append(html);
	},
	show : function(){
		if (isIE6()) {
			this.hideSelect = $('select:visible').not(this.dom.find('select')).hide();
		}
		this.settings.layer && $.kit('mast', {id:'window'}).show();
		this.dom.is(':hidden') && this.dom.show().center();
		return this;
	},
	destory : function(){
		this.hideSelect && this.hideSelect.show();
		this.settings.layer && $.kit('mast', {id:'window'}).hide();
		this.dom.find('iframe').attr({src:''});
	}
});

$.tpl = function(strings, rp){
	if (rp && isObject(rp)) {
		strings = strings.replace(/\{(.+?)\}/g,function(v, k){
			return rp[k] !== undefined ? rp[k] : '';
		});
	}
	return strings;
}

$.tpl.skins = {};

$.tpl.skin = function(name, val){
	if (val === undefined || isObject(val)) {
		return $($.tpl(this.skins[name], val || {}));
	}
	this.skins[name] = val;
}

$.tpl.skin('window', '<div class="float" style="width:290px;"><div class="title"><span class="h3">文件上传中</span><span class="close"></span></div><div class="content"></div></div>');

$.fn.route = function(){
	if (this.data('routing')) return false;
	var route = $(this).attr('href').replace('route://', ''), p = route.split('/'), This = this,
		controller = p[0], action = p[1].split('?').shift(), data = call(p.join('.'));
	if (data === false) return;
	this.data('routing', 1);
	$.ajax.server(U(route), data, [function(){}, function(){ This.removeData('routing'); }]);
}

$.fn.formEncode = function(data){
	return this.each(function(){
		var $this = $(this).html('');
		$.fn.formEncode.encode(data, '', function(key, val){
			val !== undefined && $('<input/>').appendTo($this).val(val || '').attr('name', key);
		});
	});
}

$.fn.formEncode.encode = function(object, keys, fn) {
	$.each(object || {}, function(key, val){
		if (val !== undefined && (isObject(val) || isArray(val))) {
			$.fn.formEncode.encode(val, keys?keys+'['+key+']':key, fn);
		} else {
			fn && fn(keys?keys+'['+key+']':key, val);
		}
	});
}

$.fn.rButton = function(fn){
	return this.bind('contextmenu', function(){return false;}).bind('mousedown', function(evt){
		if (evt.button == 2) {
			return (fn || function(){}).apply(this, [evt]);
		}
	});
}

var $contentMenu = $('<div/>').pos('abs'), $hideMenu = $('<div/>');
$(function(){
	DOC.bind('mousedown', function(){ $contentMenu.hide() });
});

$.fn.contextMenu = function(menu){
	return this.rButton(function(evt){
		$contentMenu.children().appendTo($hideMenu);
		menu.show().appendTo($contentMenu.appendTo('body').show());
		$contentMenu.css({left:evt.clientX+3, top:evt.clientY+3});
		return false;
	});
}

$.fn.contextMenu.addItem = function(){
	return $('<div/>').selectNone().css({padding:2, paddingLeft:10, cursor:'default'}).hover(function(){
		$(this).css({background:'#eee'});
	}, function(){
		$(this).css({background:''});
	});
}

$.fn.selectList = function(settings){
	return this.each(function(){
		if (!$(this).find(':text').length) return;
		var $this = $(this).pos(), $text = $this.find(':text').attr('autoComplete', 'off'), oldval = $text.val(), timer, geting = false, index = 0,
			wrap = $('<div/>').hide().pos('abs')
				.bind('mousedown', function(){ return false; }).addClass('text_list').appendTo($this).css({zIndex:10,left:$text[0].offsetLeft,top:$text[0].offsetTop+$text.outerHeight(),width:$text.width()});
		$text.focus(function(){
			timer && clearInterval(timer);
			timer = setInterval(function(){
				if (oldval == $text.val() || geting) return;
				if (!(oldval = $text.val())) return wrap.hide();
				geting = true;
				$.ajax.server(settings.url, {data:oldval}, function(response){
					geting = false;
					if (!response.result.length) return wrap.hide();
					wrap.empty().show();
					$.each(response.result, function(i, row){
						$('<div/>').pos().css({width:'100%',zIndex:50}).attr({unselectable:'on'}).appendTo(wrap).html(row[settings.key]).mouseover(function(){ index = $(this).active().index(); }).click(function(){
							$text.val(oldval = row[settings.key]);
							wrap.hide();
						});
					});
					wrap.children('div').eq(index = 0).active();
				});
			}, 200);
		}).keydown(function(e){
			if (wrap.is(':visible')) {
				if (e.keyCode == 32) {
					wrap.children('div').eq(index).click();
					return false;
				} else if (e.keyCode == 38 || e.keyCode == 40){
					if ((index += e.keyCode - 39) < 0) {
						index = wrap.children('div').length - 1;
					} else if (index >= wrap.children('div').length) {
						index = 0;
					}
					wrap.children('div').eq(index).active();
					return false;
				}
			}
		}).blur(function(){
			timer && clearInterval(timer);
			timer = '';
			wrap.hide();
		}).blur();
	});
}

$.fn.formUpload = function(option){
	option = option || {};
//	$('<form action="'+option.url+'" />').submit();
	return this.each(function(){
		var $this = $(this).pos().css({overflow:'hidden'}), form = $this.data('form');
		if (!form) {
			var iframeName = $.util.randomName('iframe'), div = $('<div/>').appendTo($this).pos('abs')
				.css({left:0,top:0,width:$this.outerWidth(),height:$this.outerHeight(),opacity:0});
			if (option.form === false) {
				$('<div class="form" />').appendTo(div);
			} else {
				$('<form class="form" enctype="multipart/form-data" action="'+option.url+'" method="post" target="'+iframeName+'" ><iframe name="'+iframeName+'" id="'+iframeName+'"></iframe></form>').appendTo(div);
			}
			$this.data('form', form = div.find('.form'));
			$('<input name="'+(option.name||'Filedata')+'" type="file"/>').appendTo(form).pos('abs').css({right:0,top:0,opacity:0})
				.css({height:150, fontSize:900});
		}
		if (option.success) {
			form.unbind('submit').bind('submit', function(){
				$(this).find('iframe').unbind('load').bind('load',function(){
					var body = $(this).contents().find('body'), text = body.html(), response = jsonDecode(text);
					if (response) {
						response.msg && callback('user.msg', response.msg);
						callback(response.fn, response.data) !== false && option.success.apply($this, [response, text]);
					} else {
						alert('error:'+text);
					}
				});
			});
		}

		form.find('input').unbind('change').bind('change', function(){
			option.auto && form.submit();
			return option.onSelect && option.onSelect.apply($(this), [$this]);
		})

		if (option.submit && !option.auto) {
			form.submit();
		}
	}).stopPropagation();
}

$.fn.round = function(option) {
	var $this = this, index = 0, timer, len = this.length;
	option.otherObj && option.otherObj.bind(option.stop, function(){
		timer && clearInterval(timer);
		timer = '';
	}).bind(option.start, function(){
		timer && clearInterval(timer);
		timer = setInterval(function(){
			if (++index >= len) index = 0;
			$this.eq(index).triggerHandler(option.run);
		}, option.time || 3000);
	});
	this.each(function(i){
		$(this).bind(option.stop, function(){
			index = i;
			timer && clearInterval(timer);
			timer = '';
		}).bind(option.start, function(){
			timer && clearInterval(timer);
			timer = setInterval(function(){
				if (++index >= len) index = 0;
				$this.eq(index).triggerHandler(option.run);
			}, option.time || 3000);
		}).triggerHandler(option.start);
	});
}

function call(route, func){
	if (isFunction(func)) {
		return call[route] = func;
	}
	var args = route.split('.'), route = args.shift()+'.'+args.shift();
	if (call[route]) {
		return call[route].apply(this, args);
	}
	return '';
}

function callback(route, func){
	if (isFunction(func)) {
		if (!callback[route]) callback[route] = [];
		return callback[route].push(func);
	} 
	if (callback[route]) {
		for(var i=0,j=callback[route].length-1; i<=j; j--) {
			if (callback[route][j](func) === false) return false;
		}
	}
};

$('form[isform] input').live('keypress', function(e){
	if (e.keyCode == 13) {
		return $(this).parents('form:eq(0)').submit(), false;
	}
});

$('form[isform] textarea').live('keydown', function(e){
	if (e.ctrlKey && e.keyCode == 13) {
		return $(this).parents('form:eq(0)').submit(), false;
	}
});

$('a[href^=route://]').live('click', function(){ return $(this).route(), false;});

$('form[isform] .button,form[isform] :button').live('click', function(){
	return $(this).parents('form:eq(0)').submit(), false;
});

function l(key) {
	return lan[key] ? lan[key] : key;
}

function appendScroll($src, $to) {
	$to = $($to);
	$src.appendTo($to);
	$to.scrollTop($to[0].scrollHeight+50);
}

