$(document).ready(function(){

		// IMAGE
		$("div.Preview a").click(function() {
			return false;
		});

		$("div.ListImage div.Thumbnail a").not(".delete").each(function() {
			$(this).css({opacity: 0.5});
			$(this).mouseover(function() {
				if ( $(this).attr("href").length > 0 ) {
					$(this).not(".active").animate( {opacity: 1 }, "fast" );
					$(this).click();
				}
			});
			$(this).mouseout(function() {
				if ( $(this).attr("href").length > 0 ) {
					$(this).not(".active").animate( {opacity: .5 }, "fast" );
				}
			});

			$(this).click(function(e) {
				e.preventDefault();
				if ( $(this).attr("href").length > 0 ) {
					$("div.ListImage div.Thumbnail a").removeClass("active");
					$("div.ListImage div.Thumbnail a").css( {opacity: 0.5} );
					$(this).addClass("active");
					$(this).css( {opacity: 1} );

					if ( $("div.ImgDesc").length == 0 ) {
						$('<div id="ImgDesc"></div>').insertAfter("div.Preview")
					}
					$("div.ImgDesc").empty();
					$("div.ImgDesc").hide();
					$("div.ImgDesc").css( {opacity: 0.7} );

					$("div.Preview a").hide();
					var img = new Image();
					$(img).hide();
					$(img).load(function() {
						$("div.Preview a").attr("href", $(this).attr("src"));
						$("div.Preview a").css("background", "url('"+$(this).attr("src")+"') no-repeat center center");
						// fade our image in to create a nice effect

						if ($("div.ImgDesc").length > 0 && $(this).attr("title").length > 0 ) {
							$("div.ImgDesc").html( $(this).attr("title") );
							$("div.ImgDesc").fadeIn();
							//alert("Show")
							$("div.Preview a").fadeIn("normal", function() {$("div.ImgDesc").fadeIn();} );
						} else {
							$("div.Preview a").fadeIn();
						}
						//$("div.ListImage div.Preview a").lightbox( {} );
					});
					$(img).attr("title", $(this).attr("title"));
					$(img).attr("src", $(this).attr("href"));
				}
				return false;
			});
		});
		if ( $("div.ListImage div.Thumbnail a").length > 0 ) { // Active Attachment
			$("div.ListImage div.Thumbnail a:first").click();
		}

});

function clean_username (username) {
	username = username.toLowerCase();
	return (username || "").replace(/\s/g, "" );
}

function clean_whitespace( text ) { // remove all space
	return (text || "").replace(/\s/g, "" );
}

function clean_url( url ) { // add url protocal
	if(url) {
		var regexp = /(ftp|http|https):\/\/?/;
		if (!regexp.test(url)) {
			url = "http://"+url;
		}
	}

	return (url || "").replace(/\s/g, "" );
}

function clean_email( email ) {
	email = email.toLowerCase();
	return (email || "").replace(/\s/g, "" );
}

// mini jQuery plugin that formats to two decimal places
function formatCurrency (val) {
	if( isNaN( parseFloat(val) ) ) return "0";
	return parseFloat(val).toFixed(2);
}

function validateField(field) {
	var error = false;

	// remove whitespace
	$(field).val( jQuery.trim( $(field).val() ) );

	// required fields
	if ($(field).attr("class").indexOf("required") != -1) {
		if (!$(field).val().length)
			error = true;
	}
	// numeric fields
	if ($(field).val().length && $(field).attr("class").indexOf("numeric") != -1) {
		if (!/^[0-9]*$/.test($(field).val()))
			error = true;
	}
	// float fields
	if ($(field).val().length && $(field).attr("class").indexOf("float") != -1) {
		$(field).val( formatCurrency( $(field).val() ) );
		if (!/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/.test($(field).val()))
			error = true;
	}
	// emails
	if ($(field).val().length && $(field).attr("class").indexOf("email") != -1) {
		if (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test($(field).val()))
			error = true;
	}
	// url
	if ($(field).val().length && $(field).attr("class").indexOf("url") != -1) {
		if (!/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test($(field).val()))
			error = true;
	}

	if (error) {
		$(field).addClass("focus");
	} else {
		$(field).removeClass("focus");
	}

	return !error;
}

function validPhone(value) {
	try {
		var rs_tel = (/^[0]{1}[0-79]{1}[0-9]{7}$/.test(value));
		var rs_mobile = (/^[0]{1}[8]{1}[0-9]{8}$/.test(value));
		return (rs_tel || rs_mobile);
	} catch (e) {}
	return false;
}

$.fn.clearForm = function() {

	// iterate each matching form
	return this.each(function() {
		// iterate the elements within the form
		$(":input", this).each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();
			if (type == "text" || type == "password" || tag == "textarea")
				this.value = "";
			else if (type == "checkbox" || type == "radio")
				this.checked = false;
			else if (tag == "select")
				this.selectedIndex = -1;
		});
	});
};

function set_img( obj, width, height )
{
var i = new Image(); i.src = obj.src;
if(i.width > i.height)
	{
		if(i.width > width)
			{
				obj.style.width = width;
			}
		else
			{
				obj.style.width = i.width;
			}
	}
else
	{
		if(i.height > height)
			{
				obj.style.height = height;
			}
		else
			{
				obj.style.height = i.height;
			}
	}
}


