
/* exten jquery function */
function clean_whitespace( text ) {
	return (text || "").replace(/\s/g, "" );
}

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

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

function validateField(field) {
	var error = false;

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

	// username field
	if ( $(field).hasClass("username") ) {
		$(field).val( clean_username( $(field).val() ) );
	}
	// email field
	if ( $(field).hasClass("email") ) {
		$(field).val( clean_email( $(field).val() ) );
	}
	// url field
	if ( $(field).hasClass("url") ) {
		$(field).val( clean_email( $(field).val() ) );
	}

	// required fields
	if ($(field).attr("class").indexOf("required") != -1) {
		if (!$(field).val().length)
			error = true;
	}
	// numeric fields
	//console.log( $(field).attr("name") + " : " + $(field).val())
	if ($(field).val().length && $(field).hasClass("numeric") ) {
		if (!/^[0-9]*$/.test($(field).val()))
			error = true;
	}
	// emails
	if ($(field).val().length && $(field).hasClass("email") ) {
		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).hasClass("url") ) {
		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 topMenu_DoFSCommand(command, args) {

	//$("div#swfMenu").css("border", "1px solid red");
	//console.log(command)
	if (command == "menu_open") {
		$("div#swfMenu embed").attr("height", "200");
		//$("div#swfMenu").css("overflow", "").css("height", "200px");

	} else if (command == "menu_close") {
		$("div#swfMenu embed").attr("height", "55");
		//$("div#swfMenu").css("overflow", "hidden").css("height", "55px");
	}
}


$(function(){
	// Global Element
	$("a").each( function() {
		if ($(this).attr("href") == undefined || $(this).attr("href") == "#" || $(this).attr("href") == "") {
			$(this).attr({href:"#"});
			//$(this).css("cursor", "default");
			$(this).click( function(e) { e.preventDefault(); });
		}
		$(this).focus( function() { $(this).blur(); });
	});
	$("input[type=submit], input[type=reset], input[type=button], input[type=select], input[type=image], input[type=radio], input[type=checkbox]" ).each( function() {
		$(this).focus( function() { $(this).blur(); });
	});

	// handle select text
	$("input[type=text], input[type=password], textarea").each( function() {
			if ( $(this).hasClass("username") || $(this).hasClass("number") ) {
				$(this).attr("autoComplete", "Off");
			}
			$(this).focus( function() {
				$(this).select();
				if ( !$(this).hasClass("focus") ) $(this).addClass("focus");
			});

			if( $(this).hasClass("username") ) {
				$(this).blur( function() {
					$(this).val( clean_whitespace($(this).val()) );
					$(this).removeClass("focus");
				});
			} else {
				$(this).blur( function() {
					$(this).val( jQuery.trim( $(this).val() ) );
					$(this).removeClass("focus");
				});
			}
	});


	// Top Menu
	if ( $("div#swfMenu").length > 0 ) {
		/*$("div#swfMenu").flash({
			id: "topMenu",
			name: "topMenu",
			swf: "flash/menu.swf",
			width: 950,
			height: 200,
			params: {
				wmode: "transparent",
				flashvars: {

				}
			},
			hasVersion: 10, // optional and temporary
			expressInstaller: "flash/expressInstall.swf" // optional and temporary
		});*/


		//var meta = $.metadata.get(this);
	}

	$("div.flash").each(function() {
		var meta = $(this).metadata();
		//wmode = ( meta.wmode != undefined ? meta.wmode :  "opaque" ); // opaque, transparent;
		/*var title = (meta.flashvars.title || "");
		var title_th = (meta.flashvars.title_th || "");
		var title_en = (meta.flashvars.title_en || "");*/

		$(this).empty();
		$( this ).flash({
				id: (meta.id || ""),
				name: (meta.id || ""),
				src: meta.swf,
				width: meta.width,
				height: meta.height,
				wmode: (meta.wmode || "opaque"),
				menu: "false",
				quality: "high",
				swliveconnect: true,
				flashvars: {  }
			}, { version: 8 }
		);
	});

	/*$("div.flash_youtube").each(function() {
		var meta = $(this).metadata();

		$(this).empty();
		$( this ).flash({
				width: meta.width,
				height: meta.height,
				allowFullScreen: "true",
				allowscriptaccess: "always"
			}, { version: 8 },
			function(htmlOptions) {
				$this = $(this);
				htmlOptions.src = meta.url;
				$this.before($.fn.flash.transform(htmlOptions));
			}
		);
	});*/

});


/* set height flas menu*/
$("#swfMenu").height(60);
$("#swfMenu").mouseover(function() {
	$(this).height(175);
});
$("#swfMenu").mouseout(function() {
	$("#swfMenu").height(60);
});


