jQuery.fn.mousehold = function(timeout, f) {
	if (timeout && typeof timeout == 'function') {
		f = timeout;
		timeout = 10; /*default 100*/
	}
	if (f && typeof f == 'function') {
		var timer = 0;
		var fireStep = 0;
		return this.each(function() {
			jQuery(this).mousedown(function() {
				fireStep = 1;
				var ctr = 0;
				var t = this;
				timer = setInterval(function() {
					ctr++;
					f.call(t, ctr);
					fireStep = 2;
				}, timeout);
			})

			clearMousehold = function() {
				clearInterval(timer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
			
			jQuery(this).mouseout(clearMousehold);
			jQuery(this).mouseup(clearMousehold);
		})
	}
}
jQuery.fn.slideContent = function(options){
	var defaults = {  
		next: '',
		prev : ''
	}  
	var options =  $.extend(defaults, options);  
	return this.each(function(){
		var o = options;
		var me = this;
		var first_child = $(this).children().eq(0);
		var width = $(first_child).width();
		var height = $(first_child).height();
		var mount = $(this).children().size();
		$(this).css({'height': height, 'width': width, 'overflow': 'hidden'});
		$(this).children().eq(0).clone().appendTo($(this));
		$(this).children().eq(mount - 1).clone().prependTo($(this));
		$(this).children().css('width', width).addClass('fl');
		
		var size = (mount + 2)*width;
		$(this).wrapInner('<div class="fl" style="width:'+size+'px;" />');
		$(me).scrollLeft(width);

		if (o.next != ''){
			$(o.next).click(function(){
				var next_scroll = $(me).scrollLeft() + width;
				if (next_scroll >= size - width){
					$(me).animate({scrollLeft: next_scroll}, 500, function(){$(this).scrollLeft(width);});
				}else{
					$(me).animate({scrollLeft: next_scroll}, 500);
				}
			});
		}
		if (o.prev != ''){
			$(o.prev).click(function(){
				var next_scroll = $(me).scrollLeft() - width;
				if (next_scroll <= 0){
					$(me).animate({scrollLeft: next_scroll}, 500, function(){$(this).scrollLeft(size - width*2);});
				}else{
					$(me).animate({scrollLeft: next_scroll}, 500);
				}
			});
		}
	});
}
jQuery.fn.tabs = function(options, onFunc, offFunc, default_index){
	var me = this;
	me.default_index = default_index;
	$(options).css('display', 'none');
	this.each(function(){
		$(this).click(function(){
			var tam = $(options+':eq('+$(me).index(this)+')');
			var tog;
			if ($(tam).css('display') == 'none'){tog = true;}
			$(options).css('display', 'none');
			if(typeof offFunc == 'function'){
				offFunc.call(me);
			}
			if (tog){
				$(tam).css('display', 'block');
				if(typeof onFunc == 'function'){
					onFunc.call(this);
				}
			}else{
				$(tam).css('display', 'none');
				if (me.default_index != null && me.default_index != undefined){
					$(me).eq(0).click();
				}
			}
			return false;
		});
	});
}
jQuery.fn.dropMenu = function(options, menu){
	var defaults = {
		position: 'vertical',
		rowclass: 'rowclass',
		menuclass: 'menuclass',
		blockHtml: '',
		fixleft: 0,
		fixtop: 0
	}  
	var options =  $.extend(defaults, options);
	return this.each(function(){
		if (menu.length == 0){return false;}
		$(this).css('position', 'relative');
		
		var drop = document.createElement('div');
		$(drop).css({'position':'absolute', 'top':'0px', 'left':'0px'}).addClass(options.menuclass);
		
		if (options.position == 'vertical'){
			$(drop).css({'top':'100%', 'left':options.fixleft});			
		}else{
			$(drop).css({'left': '100%', 'top':options.fixtop});
		}
		var i;
		for (i = 0;  i < menu.length; i++){
			var dropsub = document.createElement('div');
			$(dropsub).addClass(options.rowclass).html(menu[i][0]);
			$(drop).append(dropsub);
			if (typeof(menu[i][2]) == 'object'){
				var o = options; o.position = 'ngang';
				$(dropsub).dropMenu(o, menu[i][2], true);
			}
		}
		if (options.blockHtml != ''){
			$(drop).wrapInner(options.blockHtml);
		}
		$(this).append(drop);
		$(drop).hide();
		$(this).hover(function(){$(drop).show();}, function(){
			if ($.browser.mozilla){
				$(drop).hide(300);
			}else{
				$(drop).hide();
			}
															});
	});
}
$.fn.image = function(src){ 
	return this.each(function(){
		var me = this;
		var i = new Image();
		i.src = src;
		//$(i).load(function(){f.call();});
		if(i.complete != null && i.complete == true){
			//alert(me);
			//f.call();
			$(me).empty().append(i);
		}else{
			//i.onload = function(){alert(me);}
			i.onload = function(){$(me).empty().append(i);}
		}
	}); 
}
jQuery.fn.adwords = function(options){
	var defaults = {  
		keyword: '',
		clas: '',
		content: '',
		element: '',
		url: ''
	}  
	var options =  $.extend(defaults, options);
	function doAddAdwords(node, word){
		if (node.nodeType == 3){
			var regex = new RegExp('('+word+')', 'mig');
			if (node.data.search(regex) !== -1){
				var pa = $(node).parent();
				$(node).replaceWith(node.data.replace(regex, '<a style="cursor:help;" class="ads_word_node '+options.clas+'">$1</a>'));
				var node1 = $(pa).find('.ads_word_node');
				$(node1).removeClass('ads_word_node');
				
				var n_w = $(node1).width();
				var n_h = $(node1).height();
				$(node1).css('position', 'relative');
				
				$(node1).append($('<div style="display:none; position:absolute; bottom:'+n_h+'px; left:0px;"></div>').append($(options.element).clone().show()));
				/*$(node1).unbind('hover');
				$(node1).bind('click', function(){alert(1);});*/
				$(node1).unbind('hover');
				$(node1).hover(function(){$(this).children().show();}, function(){$(this).children().hide();});
			}
		}else if (node.nodeType == 1 && node.childNodes && !(/(script|style)/i).test(node.tagName) && node.tagName != 'a' && node.tagName != 'A'){
			var len = node.childNodes.length;
			for (var i = len - 1; i >= 0; i--){
				doAddAdwords(node.childNodes[i], word);
			}
		}
	}
	return this.each(function(){
		doAddAdwords(this, options.keyword);
	});
};



function validForm(id_form, id_error, callback, style){
	if (!jQuery.isArray(style)){
		var style = ['1px solid gray', '2px solid red'];
	}
	var id_form = '#' + id_form;
	var id_error = '#' + id_error;
	var input = id_form + ' :input';
	$(input).each(function(i, inp){
		valid = inp.getAttribute('valid');
		if (valid !== null && valid !== undefined){
			inp.style.border = style[0];
		}
	});
	$(id_form).submit(function(){
		var pass_all = true, focused = false;
		$(input).each(function(i, inp){
			valid = inp.getAttribute('valid');
			if (valid !== null && valid !== undefined){
				pass = false;
				if (inp.value != ''){
					if (valid.indexOf('string') !== -1){
						pass = doValid(inp.value, 'string');
					}else if (valid.indexOf('basetext') !== -1){
						pass = doValid(inp.value, 'basetext');
					}else if (valid.indexOf('email') !== -1){
						pass = doValid(inp.value, 'email');
					}else if (valid.indexOf('url') !== -1){
						pass = doValid(inp.value, 'url');
					}else if (valid.indexOf('number') !== -1){
						pass = doValid(inp.value, 'number');
					}else if (valid.indexOf('username') !== -1){
						pass = doValid(inp.value, 'username');
					}else if (valid.indexOf('password') !== -1){
						pass = doValid(inp.value, 'password');
					}else if (valid.indexOf('phone') !== -1){
						pass = doValid(inp.value, 'phone');
					}
				}else if (valid.indexOf('require') !== -1){
					pass = false;
				}else{
					pass = true;
				}
				if (!pass){
					pass_all = false;
					if (focused !== true){
						inp.focus();
						focused = true;
					}
					inp.style.border = style[1];
				}else{
					inp.style.border = style[0];
				}
			}
		});
		if (!pass_all){
			$(id_error).css('display', 'block');
		}else{
			$(id_error).css('display', 'none');
			if (jQuery.isFunction(callback)){
				callback($(id_form).serialize());
			}else{
				//$(id_form).submit();
				return true;
			}
		}
		return false;
	});
	function doValid(value, valid){
		if (valid == 'none'){return true;}
		if (valid == 'string'){
			return value != '';
		}else if (valid == 'basetext'){
			reg = /^[a-zA-Z0-9_\.\-]+$/;
			return reg.test(value);
		}else if (valid == 'email'){
			reg = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
			return reg.test(value);
		}else if (valid == 'url'){		
		}else if (valid == 'number'){
			if (value != '' && !isNaN(value)){return true;}else{return false;}
		}else if (valid == 'username'){
			/*first character of your username must be an ascii letter (a-z) or number (0-9). Examples: JSmith, John.Smith. 6-20 length*/
			reg = /^[a-zA-Z0-9\.]+$/;
			reg_a = /\.\./;
			return !reg_a.test(value) && reg.test(value) && (value[0] != '.') && (value.length <= 20) && (2 <= value.length) && (value[value.length - 1] != '.');
		}else if (valid == 'password'){
			/*6-50 char*/
			return (3 <= value.length) && (value.length <= 50);
		}else if (valid == 'phone'){
			reg = /^[0-9\+]+$/;
			return reg.test(value);
		}else{
			return false;
		}
	}
}
