$(document).ready(function(){
	initTooltip();
	initFormEl();
	initMiddleP();
});
function initMiddleP(){
	$('div.racelist .mid').each(function(){
		$(this).css({"marginTop": ($(this).parent().height() - $(this).height())/2 + "px"});
	});
}
/*--- form elements function ---*/
function initFormEl(){
	$('ol.form input:text, ol.form textarea, ul.form input:text, ul.form textarea').each(function(){
		if(!this.val) this.val = this.value;
		this.onmouseover = function(){
			$(this).addClass('hover');
		}
		this.onmouseout = function(){
			$(this).removeClass('hover');
		}
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
			$(this).parent().addClass('active');
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
			$(this).parent().removeClass('active');
		}
	});
}
/*--- Tooltip function ---*/
function initTooltip(){
    $('.gallist > li').each(function(){
		var _el = $(this);
		var _popup = _el.find('.popup');
		if(_popup.length){
			var _t;
			var _text = _popup.find('.pophold');
			_el.mouseenter(function(){
				if(_t) clearTimeout(_t);
				_t = setTimeout(function(){
					inPopup(true);
				}, 3000);
			}).mouseleave(function(){
				if(_t) clearTimeout(_t);
				inPopup(false);
			});
			_popup.mouseenter(function(){
				if(_t) clearTimeout(_t);
				inPopup(true);
			}).mouseleave(function(){
				if(_t) clearTimeout(_t);
				inPopup(false);
			});	
			var _h = 0;
			function inPopup(_f){
				if(_f){
					if (($.browser.msie && $.browser.version <= 7) || (navigator.userAgent.indexOf('Chrome') != -1)){
						_popup.show();
					}
					else{
						if(!_popup.is(':animated')){
							_popup.css({opacity: 0, display: 'block'});
							_h = _text.height();
							_text.height(0);
						}
						_popup.stop().animate({opacity: 1}, 400);
						_text.stop().animate({height: _h}, 400, function(){
							$(this).height('auto');
						});
					}
				}
				else{
					if (($.browser.msie && $.browser.version <= 7) || (navigator.userAgent.indexOf('Chrome') != -1)){
						_popup.hide();
					}
					else{
						_popup.stop().animate({opacity: 0}, 400, function(){
							$(this).hide();
						});
						_text.stop().animate({height:0}, 400, function(){
							$(this).height('auto');
						});
					}
				}
			}
		}
	});
	$('.textblock .list .popbox').each(function(){
		var _btn = $(this).find('.open-close');
		var _popup = $(this).find('.popup');
		if(_popup.length){
			var _t;
			var _text = _popup.find('.pophold');
			_btn.mouseenter(function(){
				if(_t) clearTimeout(_t);
				_t = setTimeout(function(){
					inPopup(true);
				}, 0);
			}).mouseleave(function(){
				if(_t) clearTimeout(_t);
				inPopup(false);
			});
			_popup.mouseenter(function(){
				if(_t) clearTimeout(_t);
				inPopup(true);
			}).mouseleave(function(){
				if(_t) clearTimeout(_t);
				inPopup(false);
			});	
			var _h = 0;
			function inPopup(_f){
				if(_f){
					if (($.browser.msie && $.browser.version <= 7) || (navigator.userAgent.indexOf('Chrome') != -1)){
						_popup.show();
					}
					else{
						if(!_popup.is(':animated')){
							_popup.css({opacity: 0, display: 'block'});
							_h = _text.height();
							_text.height(0);
						}
						_popup.stop().animate({opacity: 1}, 300);
						_text.stop().animate({height: _h}, 300, function(){
							$(this).height('auto');
						});
					}
				}
				else{
					if (($.browser.msie && $.browser.version <= 7) || (navigator.userAgent.indexOf('Chrome') != -1)){
						_popup.hide();
					}
					else{
						_popup.stop().animate({opacity: 0}, 300, function(){
							$(this).hide();
						});
						_text.stop().animate({height:0}, 300, function(){
							$(this).height('auto');
						});
					}
				}
			}
		}
	});
}
