function getL(id){
	if (typeof(siteLocal)!='undefined'&&siteLocal[id]) return siteLocal[id]; else return '';
}

function askQuestion(msg){
	return confirm(msg);
}

function redirect(url){
	location.href = url;
}

function showMessage(msg,err){
	if (!err) err = false;
	var $dlg = $('#messageDialog'), $lay = $('#messageOverlay');
	$lay.remove();
	$dlg.remove();
	var cl = err?' error':'';
	
	$(document).find('body').prepend('<div id="messageOverlay"></div><div id="messageDialog"><div class="box'+cl+'"><a href="#close" class="close"></a>'+msg+'</div></div>');

	$dlg = $('#messageDialog');
	$lay = $('#messageOverlay');
	$lay.css('opacity',0.4).show();
	$dlg.show().css('top',($(document).scrollTop()+parseInt($dlg.css('top'),10))+'px');
	$dlg.unbind('close').bind('close',function(){
		$('#messageOverlay').remove();
		$dlg.remove();
	});
	$dlg.unbind('click').click(function(){
		$dlg.trigger('close');
	});
	$dlg.find('a.close').click(function(){
		$dlg.trigger('close');
		return false;
	});
}

function showDebugInfo(txt){
	var $dlg = $('#debugDialog');
	$dlg.remove();
	$(document).find('body').append('<div id="debugDialog"><pre>'+txt+'</pre></div>');
	$dlg = $('#debugDialog');
	$dlg.show();
	
	$dlg.unbind('click').click(function(){
		$dlg.remove();
		return false;
	});
	
	$dlg.find('a[href$="#queries"]').unbind('click').click(function(){
		if ($dlg.find('.queries').is(':visible')) $dlg.find('.queries').hide(); else $dlg.find('.queries').show();
		return false;
	});
	
	$dlg.find('.queries').unbind('click').click(function(){
		$(this).hide();
		return false;
	});
}

(function($){

	$.fn.smartField = function(){
		return this.each(function(){
			if (this.tagName!='INPUT'&&this.tagName!='TEXTAREA') return false;
			var $this = $(this);
			var value = $this.val();
			
			$this.focus(function(){
				if (value==$this.val()) $this.val('');
			});
			$this.blur(function(){
				if ($this.val()=='') $this.val(value);
			});
		});
	}

	$.fn.zebra = function(){
		return this.each(function(){
			var $this = $(this);
			var ch = '*';
			switch (this.tagName){
				case 'UL':
				case 'OL':
					ch = '> li';
				break;
				case 'TABLE':
					ch = '> tbody > tr';
				break;
				case 'THEAD':
				case 'TBODY':
				case 'TFOOT':
					ch = '> tr';
				break;
			}
			$this.find(ch+':even').addClass('even');
			$this.find(ch+':odd').addClass('odd');
		});
	}
	
	$.fn.checkField = function(fn){
		var _expr = {
			'string'		: /^\S+.*$/i,
			'email'			: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2,4})$/i,
			'password'		: /^.{6,20}$/i,
			'phone'			: /^[0-9]{8,15}$/i,
			'zip'			: /^[\w\d- ]{3,12}$/i
		};
		return this.each(function(){
			if (this.tagName!='INPUT'&&this.tagName!='TEXTAREA'&&this.tagName!='SELECT') return false;
			var $this = $(this);
			if (!$this.hasClass('required')) return false;
			var id = $this.attr('name');
			
			function _validate(){
				var expr = _expr[$this.attr('alt')] || /.*/i;
				if ($this.attr('disabled')||$this.hasClass('disabled')){
					return true;
				}
				var res = false;
				if ($this.get(0).tagName=='SELECT'){
					res = $this.val()!='0' && $this.val()!='';
				} else {
					res = expr.test($this.val());
				}
				$this.removeClass('error');
				if (!res) $this.addClass('error');
				return res;
			}
			
			if (this.tagName=='SELECT'){
				$this.unbind('change').change(function(){
					_validate.call(this);
				});
			} else {
				$this.unbind('click').click(function(){
					_validate.call(this);
				});
			}
			
			$this.unbind('blur').blur(function(){
				_validate.call(this);
			});
			
			if (!$this.is('.js-no-type-verify')){
				$this.unbind('keyup').keyup(function(){
					if ($this.val().length<200&&this.tagName=='INPUT') _validate.call(this);
				});
			}
			
			if (fn&&typeof fn=='function'){
				fn.call(this,_validate());
			}
		})
	}
	
	$.fn.checkForm = function(){
		return this.each(function(){
			if (this.tagName!='FORM') return false;
			var $this = $(this);
			var $fields = $this.find('input.required,select.required,textarea.required');
			
			$fields.checkField();
			
			$this.submit(function(){
				var count = 0;
				$fields.checkField(function(result){
					if (result) count++;
				});
				if (count<$fields.size()) return false;
				return true;
			});
			
		});
	}
	
	$.fn.hrefDelete = function(){
		return this.each(function(){
			var $this = $(this);
			$this.click(function(){
				$.post($this.attr('href'),{ajax:1,method:'get'},function(data){
					var a = eval(data);
					if (a.result&&a.msg&&askQuestion(a.msg)){
						$.post($this.attr('href'),{ajax:1,method:'post'},function(data){
							var a = eval(data);
							if (a.result){
								$this.parents('li:first').remove();
							}
						});
					}
					if (!a.result&&a.msg){
						showMessage(a.msg,true);
					}
				});
				return false;
			});
		});
	}
	
	$.fn.tableColumns = function(){
		return this.each(function(){
			if (this.tagName!='TABLE') return false;
			var $this = $(this);
			var found = false;
			$this.find('tr').each(function(){
				if (found) return false;
				var span = 0;
				$(this).find('> td, > th').each(function(){
					if ($(this).attr('colspan')) span++;
				});
				if (span == 0){
					var i = 0;
					$(this).find('> td, > th').each(function(){
						$(this).addClass('column'+(++i));
					});
					found = true;
					return false;
				}
			});
		});
	}
	
	$.fn.lengthLimit = function(len){
		return this.each(function(){
			var $this = $(this);
			var $span = $('#'+$this.attr('name')+'_counter');
			
			function _cut(){
				if ($this.val().length<=len) return false;
				$this.val($this.val().substr(0,len));
			}
			
			function _check(){
				if ($this.val().length>len) _cut();
				$span.text(len-$this.val().length);
			}
			
			$this.change(function(){
				_check();
			});
			
			$this.keypress(function(){
				_check();
			});
			
			$this.keydown(function(){
				_check();
			});
			
			$this.keyup(function(){
				_check();
			});
			
			$this.blur(function(){
				_check();
			});
			
			_check();
		})
	}

})(jQuery)

