function searchElement(element,target,value) {
	var keyword = new RegExp(value, "i");
	var isodd = true;
	jQuery(element, target).each(function(){
		if(keyword.test(jQuery(this).html())) {
			if(isodd) {
	    		isodd = false;
	    		addClass = 'odd';
	    		removeClass = 'even';
	    	} else {
	    		isodd = true;
	    		addClass = 'even';
	    		removeClass = 'odd';
	    	}
			jQuery(this).fadeIn("slow").addClass(addClass).removeClass(removeClass);
		} else {
			jQuery(this).fadeOut("slow");
		}
    });
}
function searchAttributes(element,target,attr,value) {
	var isodd = true;
	jQuery(element, target).each(function(){
		var thisItem = jQuery(this);
        if(jQuery(this).attr(attr) == value) {
			if(isodd) {
        		isodd = false;
        		addClass = 'odd';
        		removeClass = 'even';
        	} else {
        		isodd = true;
        		addClass = 'even';
        		removeClass = 'odd';
        	}
			thisItem.fadeIn("slow").addClass(addClass).removeClass(removeClass);
        } else {
        	thisItem.fadeOut("slow");
        }
    });
}

/*
Used to figure out whether to drop something up or down

DOESN'T WORK IT WOULD NEED TO BE CALLED EVERY TIME SOMETHING APPEARS.
*/
function appendTopBottom(appendTo,appendThis) {
	var pos = appendTo.offset();
	bottomGap = jQuery(window).height() - (pos.top + appendTo.outerHeight());
	//Checks if the element is bigger than the gap at the bottom.
	//If there's not enough room at the top either we drop down anyway.
	if(bottomGap < appendThis.outerHeight() && pos.top >= appendThis.outerHeight()) {
		appendTop(appendTo,appendThis);
	} else {
		appendBottom(appendTo,appendThis);
	}
}
function appendTop(appendTo,appendThis) {
	var pos = appendTo.offset();
	var top  = pos.top - appendThis.outerHeight();
	posAbsolute(appendThis,pos.left,top,appendTo.outerWidth());
}
function appendBottom(appendTo,appendThis) {
	var pos = appendTo.offset();
	var top  = pos.top + appendTo.outerHeight();
	posAbsolute(appendThis,pos.left,top,appendTo.outerWidth());
}
function appendRight(appendTo,appendThis) {
	var pos = appendTo.offset();
	var left = pos.left + appendTo.outerWidth();
	posAbsolute(appendThis,left,pos.top,appendTo.outerWidth());
}
function appendLeft(appendTo,appendThis) {
	var pos = appendTo.offset();
	var left = pos.left - appendThis.outerWidth();
	posAbsolute(appendThis,left,pos.top,appendTo.outerWidth());
}
function posAbsolute(element,left,top,minwidth) {
	element
		.css({'left':left+'px','top':top+'px','min-width':minwidth+'px','position':'absolute'})
		.appendTo("body");
	//jQuery("body").append(element);
}

function addMessage(message,type) {
	var newmsg = jQuery('<div style="display:none;" class="'+type+' message">'+message+'</div>');
	jQuery("#messages").append(newmsg);
	jQuery('html,body').animate({scrollTop: 0}, 500, 'linear',function(){
		newmsg.slideDown("slow");
	});

}

function inputCharacter(c) {
	if ( (c == 8) || (c == 46) || (c == 109 || c == 189) || (c >= 65 && c <= 90) || (c >= 48 && c <= 57) ) {
		return true;
	} else {
		return false;
	}
}

function convertSelect(thisItem,callbackFunction) {
	var title = thisItem.attr('title');
	var input = jQuery('<input type="text" class="whiteInput dropDown" id="'+thisItem.attr('id')+'" value="'+title+'" />');

	thisItem.after(input);
	thisItem.hide();

	var options = jQuery('<div class="dropDownOptions"></div>');
	appendBottom(input,options);
	thisItem.find('option').each(function(){
		options
			.append('<span value="'+jQuery(this).val()+'">'+jQuery(this).html()+'</span>');
	});
	input
		.focus(function(){
			options.find("span").show();
			options.slideDown("normal");
			input.select()
		})
		.blur(function(){
			if(input.val() != '' && input.val() != title) {
				input.val(options.find('span:visible:first').html());
				callbackFunction(input,options.find('span:visible:first').attr('value'));
			} else {
				input.val(title);
			}
			options.slideUp("normal");
		})
		.keyup(function(event){
			if (inputCharacter(event.keyCode)) {
				searchElement("span",options,input.val());
			}
		});
	options.find('span').click(function(){
		input.val(jQuery(this).html());
		thisItem.val(jQuery(this).attr('value'));
		//searchAttributes('tbody tr',$("#catTable"),'nat_id',jQuery(this).attr('value'));
		callbackFunction(input,jQuery(this).attr('value'));
		return false;
	});
}

jQuery(document).ready(function(){
	jQuery(".js_hover")
		.hover(
			function(){
				jQuery(this).addClass("onhover");
			},
			function(){
				jQuery(this).removeClass("onhover");
			}
		)
		.click(function(){
			window.location.href = jQuery(this).find("a:first").attr('href');
		});
	jQuery("textarea:empty, input[value='']").each(function(){
		jQuery(this).val(jQuery(this).attr('title')).addClass("inputFaded").focus(function(){
			if(jQuery(this).val() == jQuery(this).attr('title')) {
				jQuery(this).val("").removeClass("inputFaded");
			}
		}).blur(function(){
			if(jQuery(this).val() == '') {
				jQuery(this).val(jQuery(this).attr('title')).addClass("inputFaded");
			}
		});
	});

/*
	jQuery(".dropDown").each(function(){
		var thisItem = jQuery(this);
		var title = thisItem.attr('title');
		var input = jQuery('<input type="text" class="whiteInput dropDown" id="'+thisItem.attr('id')+'" value="'+title+'" />');

		thisItem.after(input);
		thisItem.remove();

		var options = jQuery('<div class="dropDownOptions"></div>');
		appendBottom(input,options);
		thisItem.find('option').each(function(){
			options
				.append('<span value="'+jQuery(this).val()+'">'+jQuery(this).html()+'</span>');
		});
		input
			.focus(function(){
				options.find("span").show();
				options.slideDown("normal");
				input.select()
			})
			.blur(function(){
				if(input.val() != '' && input.val() != title) {
					input.val(options.find('span:visible:first').html());
				} else {
					input.val(title);
				}
				options.slideUp("normal");
			})
			.keyup(function(event){
				if (inputCharacter(event.keyCode)) {
					searchElement("span",options,input.val());
				}
			});
		options.find('span').click(function(){
			input.val(jQuery(this).html());
			thisItem.val(jQuery(this).attr('value'));
			searchAttributes('tbody tr',$("#catTable"),'nat_id',jQuery(this).attr('value'));
			return FALSE;
		});
	});
*/
});