$(document).ready(function(){
	initDropMenu();
	//clearInputs();
	$('select').customSelect();
	//$('.wpsPortletBody table').designTable();
});
function initDropMenu(){
	$("ul.menu a").each(function()
	{
		$(this).click(function() {
			var _parent = $(this).parent();
			var _slider = _parent.find("ul").eq(0);
			if(_parent.find("ul").length > 0) {
				if($(_parent).hasClass("active")) {
					$(_slider).slideUp(300, function(){
						$(_parent).removeClass("active");
					});
				}
				else {
					$(_slider).slideDown(300, function(){
						$(_parent).addClass("active")
					});
				}
			}
		});
	});
}
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}
/*--- custom select function ---*/
jQuery.fn.customSelect = function(_options){
	var _options = jQuery.extend({
		selectStructure: '<div class="selectArea"><div class="left"></div><div class="center"></div><a href="#" class="selectButton">&nbsp;</a><div class="disabled"></div></div>',
		selectText: '.center',
		selectBtn: '.selectButton',
		selectDisabled: '.disabled',
		optStructure: '<div class="selectOptions"><ul></ul></div>',
		optList: 'ul'
	}, _options);
	return this.each(function(){
		var select = jQuery(this);
		if(!select.hasClass('outtaHere')){
			if(select.is(':visible')){
				var replaced = jQuery(_options.selectStructure);
				var selectText = replaced.find(_options.selectText);
				var selectBtn = replaced.find(_options.selectBtn);
				var selectDisabled = replaced.find(_options.selectDisabled).hide();
				var optHolder = jQuery(_options.optStructure);
				var optList = optHolder.find(_options.optList);
				if(select.attr('disabled')) selectDisabled.show();
				select.find('option').each(function(){
					var selOpt = $(this);
					var _opt = jQuery('<li><a href="#">' + selOpt.html() + '</a></li>');
					if(selOpt.attr('selected')){
						selectText.html(selOpt.html());
						_opt.addClass('selected');
					}
					_opt.children('a').click(function(){
						optList.find('li').removeClass('selected');
						select.find('option').removeAttr('selected');
						$(this).parent().addClass('selected');
						selOpt.attr('selected', 'selected');
						selectText.html(selOpt.html());
						select.change();
						optHolder.hide();
						return false;
					});
					optList.append(_opt);
				});
				replaced.width(select.outerWidth());
				replaced.insertBefore(select);
				optHolder.css({
					width: select.outerWidth(),
					display: 'none',
					position: 'absolute'
				});
				jQuery(document.body).append(optHolder);
				
				var optTimer;
				replaced.hover(function(){
					if(optTimer) clearTimeout(optTimer);
				}, function(){
					optTimer = setTimeout(function(){
						optHolder.hide();
					}, 1000);
				});
				optHolder.hover(function(){
					if(optTimer) clearTimeout(optTimer);
				}, function(){
					optTimer = setTimeout(function(){
						optHolder.hide();
					}, 1000);
				});
				selectBtn.click(function(){
					if(optHolder.is(':visible')){
						optHolder.hide();
					}
					else{
						optHolder.css({
							top: replaced.offset().top + replaced.outerHeight(),
							left: replaced.offset().left,
							display: 'block'
						});
					}
					return false;
				});
				select.addClass('outtaHere');
			}
		}
	});
}

jQuery.fn.designTable = function(_options) {
	var _options = jQuery.extend({
		evenClass: 'tr-even',
		oddClass: 'tr-odd'
	}, _options);
	
	return $(this).each(function(){
		$(this).children("tbody")
			.children("tr:even")
				.addClass(_options.evenClass)
			.parent()
			.children("tr:odd")
				.addClass(_options.oddClass);
	});
}
