/*! http://www.tcreator.info/cnbootstrap/cndocs/solutions/placeholder.html/placeholder v1.0 by @sofish */
// placeholder ¹¦ÄÜÀ©Õ¹ Ô­ÉúJSÀ©Õ¹·½·¨
window.onload = function(){
    var doc = document,
    inputs = doc.getElementsByTagName('input'),
	
    // ¼ì²â placeholder Ö§³Ö
    supportPlaceholder = 'placeholder' in doc.createElement('input'),
    supportPlaceholdertextarea = 'placeholder' in doc.createElement('textarea'),
	
/* 
 * ´´½¨ placeholder
 * @param {ELEMENT} input ´«ÈëµÄ input ¶ÔÏó
 * @return {VOID}
 * @author: sofish Lin http://sofish.de
 */
    placeholder = function(input){
        var text = input.getAttribute('placeholder'),
            defaultValue = input.defaultValue;
        if(input.value=="" || input.value==text){
            // ÖÆ×÷Ò»¸ö¼Ù placeholder
            // ÐèÒª css ÅäºÏ£¬ÔÚ JS ÖÐÉèÖÃ style ²¢²»Ì«ºÃ
            input.value=text;
            // input.style.color = 'gray';
            input.className = 'placeholder';
        }
        
        // ¾Û½¹È¥µô¼Ù placeholder
        input.onfocus = function(){
            if(input.value === text){
                this.value = '';
                // this.style.color = '';
                input.className = '';
            }
        }
        
        // µ±Ê§½¹ÖµÎª¿ÕÊ±£¬²¹»Ø placeholder µÄÖµ
        input.onblur = function(){
            if(input.value === ''){
                // input.style.color = 'gray';
                input.className = 'placeholder';
                this.value = text;
            }
        }
        input.onkeydown = function(){
            this.style.color = '';
        }
    };
	
    if(!supportPlaceholder){
    	for(var i = 0, len = inputs.length; i < len; i++){
    	    var input = inputs[i], text = input.getAttribute('placeholder');
    	    
    	    // µ± input[type=text] ²¢ÇÒ placeholder Öµ²»Îª¿ÕÊÇÖ´ÐÐ
    	    if(input.type === 'text' && text){
    	    	placeholder(input);
    	    }
    	}    
    }
};
