/* *
 * For input[text], textarea has default value and sign
 * Author: Deo 2010.01.03
 * Param:
 * 	1. selector: 
 *		exp: demo > input.demo1 input.abcd textarea
 *	2. config:
 *		{sign: ['<', '>'], color: '#ccc'}
 * 2010.01.07
 * */
function textDefault(selector, config) {
	var doMap = {}, mapLen = 0, matcher = /(\w+)\s*>\s*(.+)/.exec( selector );
	var boxId = matcher[1], tagNameArr = matcher[2].split(' ');
	var start = config.sign[0], end = config.sign[1], color = config.color;
	
	// Reset Sign
	if (/\$|\(|\)|\[|\]|\{|\}|\*|\+|\.|\?|\\|\/|\^|\|/gi.test( start )) { start = '\\'+ start; }
	if (/\$|\(|\)|\[|\]|\{|\}|\*|\+|\.|\?|\\|\/|\^|\|/gi.test( end ))   { end 	= '\\'+ end; }
	
	Each(tagNameArr, function(i, tag){
		// tn: match tagName, cn: match className
		var tn = tag, cn = null,  matchClass = /^(\w+)\.(\w+)$/.exec( tn );
		if ( matchClass ) { tn = matchClass[1]; cn = matchClass[2]; }
		
		var elemArr = document.getElementById( boxId ).getElementsByTagName( tn );
		
		Each(elemArr, function(index, elem){
			if ((/input/i.test( elem.tagName ) && /text/i.test( elem.type )) || /textarea/i.test( elem.tagName )) {
				var regValue = RegExp('^'+ start +'.*?'+ end +'$'), regClass = RegExp(cn, 'g');
				
				if (regValue.test( elem.value ) && (matchClassName(elem.className, cn) || cn == null)) {
					elem.style.color = color;
					doMap[ mapLen ] = [];
					doMap[ mapLen ].push( elem );
					doMap[ mapLen ].push( elem.value );
					mapLen ++;
				}	
			}
		});
	});
	
	// Element events
	Each(doMap, function(i, key, value){
		var oElem = value[0], oVal = value[1];
		oElem.onfocus = function() {
			if (Trim( this.value ) === doMap[ key ][1] || Trim( this.value ) == "") {
				this.value = '';
				this.style.color = '';
			}
		}
		oElem.onblur = function(){
			if (Trim( this.value ) === doMap[ key ][1] || Trim( this.value ) == "") {
				this.value = doMap[ key ][1];
				this.style.color = color;
			}
		}
	});
	
	function Each(arr, func) {
		if (typeof arr === 'object' && typeof func === 'function') {
			var mapLen = 0, len = arr.length;
			if (len !== undefined)  for (var i = 0; i < arr.length; i ++) func(i, arr[i]);
			else 					for (var key in arr) func(mapLen ++, key, arr[ key ]);
		}
	}
	function Trim( str ) { return str.replace(/^\s*|\s*$/g, ''); };
	function matchClassName(str, matcher) {
		var strArr = str.split(/\s+/);
		for (var i = 0; i < strArr.length; i ++)
			if (strArr[i] == matcher) return true;
		return false;
	}
}
			$(document).ready(function() {
		 
            //auto resize image
            $(".thickbox img").each(function() {
                var img = $(this);
                var newWidth = $(this).width();
                var newHeight = $(this).height();
                var imageLoader = new Image();
                imageLoader.onload = function() {
                    imageLoader.onload = null;
                    var imageWidth = imageLoader.width;
                    var imageHeight = imageLoader.height;
                    if (newWidth / newHeight > imageWidth / imageHeight) {
						var imgWidth = parseInt(newHeight * imageWidth / imageHeight);
						if (!isNaN(imgWidth) && imgWidth > 0)
						{
							img.width(imgWidth);
						}
                    } else {
						var imgHeight = parseInt(newWidth * imageHeight / imageWidth);
						if (!isNaN(imgHeight) && imgHeight > 0)
						{
							img.height(imgHeight);
						}
                    }
                }
                imageLoader.src = img.attr("src");

            });
            //auto resize image
            $(".resize img").each(function() {
                var img = $(this);
                var newWidth = $(this).width();
                var newHeight = $(this).height();
                var imageLoader = new Image();
                imageLoader.onload = function() {
                    imageLoader.onload = null;
                    var imageWidth = imageLoader.width;
                    var imageHeight = imageLoader.height;
                    if (newWidth / newHeight > imageWidth / imageHeight) {
                        img.width(newHeight * imageWidth / imageHeight);

                    } else {
                        img.height(newWidth * imageHeight / imageWidth);

                    }
                }
                imageLoader.src = img.attr("src");

            });
        });
