try {document.execCommand('BackgroundImageCache', false, true);} catch(err) {}


/*
 * Namespace Manager
 */
if (typeof Namespace == 'undefined') var Namespace = {};
if (!Namespace.Manager) Namespace.Manager = {};
Namespace.Manager = {
 Register:function(namespace){
  namespace = namespace.split('.');

  if(!window[namespace[0]]) window[namespace[0]] = {};
  
  var strFullNamespace = namespace[0];
  for(var i = 1; i < namespace.length; i++)
  {
   strFullNamespace += "." + namespace[i];
   eval("if(!window." + strFullNamespace + ")window." + strFullNamespace + "={};");
  }
 }
};
Namespace.Manager.Register("Utility.Class"); 
Namespace.Manager.Register("Forms.Class"); 
Namespace.Manager.Register("Widget.Class");



/*
 * Utility
 */

Utility.Class.trim = function(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};




/*
 * Forms 
 */
Forms.Class.prettyExamples = function(form) {
    this.init(form);
};
Forms.Class.prettyExamples.prototype = {


    init : function(form) {
    
        if(YAHOO.lang.isString(form)) {
            var form = YAHOO.util.Dom.get(form);
        }
        
        // get all the inputs that have examples
        this.inputs = YAHOO.util.Dom.getElementsByClassName('inputText','input',form,function(el) {
            if(el.title) {
                return true;
            }
            return false;
        });
        
        
        for(var i in this.inputs) {
            if (YAHOO.lang.hasOwnProperty(this.inputs, i)) {
                var el = this.inputs[i];
                if(Utility.Class.trim(el.value) == '') {
                    if(YAHOO.env.ua.ie && YAHOO.util.Dom.hasClass(el,'inputPassword')) {
                    
                        var passwordInput = el.cloneNode(false); 
                        passwordInput.type = 'password';
                        YAHOO.util.Dom.setStyle(passwordInput,'display','none');
                        YAHOO.util.Dom.insertAfter(passwordInput,el);
                        
                        el.value = el.title;
                        YAHOO.util.Dom.addClass(el,'inputSample');
                        YAHOO.util.Dom.addClass(el,'removeMeBeforeSubmit');
                        YAHOO.util.Event.on(el,'focus',function() {
                            this.blur();
                            YAHOO.util.Dom.setStyle(this,'display','none');
                            YAHOO.util.Dom.setStyle(YAHOO.util.Dom.getNextSibling(this),'display','block');
                            YAHOO.util.Dom.getNextSibling(this).focus();
                        });
                        
                        YAHOO.util.Event.on(passwordInput,'blur',function() {
                            if(Utility.Class.trim(this.value) == '') {
                                YAHOO.util.Dom.setStyle(this,'display','none');
                                YAHOO.util.Dom.setStyle(YAHOO.util.Dom.getPreviousSibling(this),'display','block');
                            }
                        });
                        
                    }
                    else {
                    
                        el.value = el.title;
                        YAHOO.util.Dom.addClass(el,'inputSample');
                        
                        YAHOO.util.Event.on(this.inputs[i],'focus',function() {
                            YAHOO.util.Dom.removeClass(YAHOO.util.Dom.getAncestorByTagName(this,'li'),'error');
                            if(Utility.Class.trim(this.value) == this.title) {
                                this.value = '';
                                YAHOO.util.Dom.removeClass(this,'inputSample');
                                if(YAHOO.util.Dom.hasClass(this,'inputPassword')) {
                                    this.type = 'password';
                                }
                            }
                        });
                        
                        YAHOO.util.Event.on(this.inputs[i],'blur',function() {
                            if(Utility.Class.trim(this.value) == '') {
                                if(YAHOO.util.Dom.hasClass(el,'inputPassword')) {
                                    el.type = 'text';
                                }
                                YAHOO.util.Dom.addClass(this,'inputSample');
                                this.value = this.title;
                            }
                            else {
                                YAHOO.util.Dom.removeClass(this,'inputSample');
                            }
                        });
                    }
                }
            }
        }
        
        
        // get all the selects that have examples
        this.selects = YAHOO.util.Dom.getElementsByClassName('selectExample','select',form,function(el) {
            if(el.title) {
                return true;
            }
            return false;
        });
        
        YAHOO.util.Event.on(this.selects,'change',function() {
            if(Utility.Class.trim(this.value) != '' && YAHOO.util.Dom.hasClass(this,'selectExample')) {
                YAHOO.util.Dom.removeClass(this,'selectExample');
                this.remove(0);
            }
        });
        
        
        
        
        YAHOO.util.Event.on(form,'submit',this.removeExamples,this,true);
    
    },
    
    removeExamples : function(e) {
    
        var passed = true;
        
        for(var i in this.inputs) {
            if (YAHOO.lang.hasOwnProperty(this.inputs, i)) {
                var el = this.inputs[i];
                if((Utility.Class.trim(el.value) == el.title || Utility.Class.trim(el.value) == '') && YAHOO.util.Dom.hasClass(el,'inputRequired')) {
                    passed = false;
                    YAHOO.util.Dom.addClass(YAHOO.util.Dom.getAncestorByTagName(el,'li'),'error');
                }
                else {
                    YAHOO.util.Dom.removeClass(YAHOO.util.Dom.getAncestorByTagName(el,'li'),'error');
                }
            }   
        }
    
        if(passed) {
            for(var i in this.inputs) {
                if (YAHOO.lang.hasOwnProperty(this.inputs, i)) {
                    var el = this.inputs[i];
                    if(!YAHOO.util.Dom.hasClass(el,'removeMeBeforeSubmit')) {
                        if(Utility.Class.trim(el.value) == el.title) {
                            el.value = '';
                        }
                    }
                    else {
                        YAHOO.util.Event.purgeElement(el,true);
                        el.parentNode.removeChild(el);
                    }
                }
            }
        }
        else {
            YAHOO.util.Event.stopEvent(e);  
        }
    }
    
};




// preloads images
Widget.Class.preLoadImgs = function(currentImg,totalImages,imgPath,fn) {
            
    var preLoaded = 0;

    var getImgPath = function(pointer) {
        if(YAHOO.lang.isArray(imgPath)) {
            return imgPath[pointer];
        }
        else {
            return imgPath.replace("{0}", pointer);
        }
    };
    
    var loadNext = function() {
        preLoaded++;
        if(preLoaded < totalImages) {
            currentImg++;
            preload(currentImg);
        }
        else {
            fn.call();
        }
    };
    
    var preload = function(pointer) {
        var preloadImg = document.createElement('img');
        document.getElementsByTagName("body")[0].appendChild(preloadImg);
        YAHOO.util.Dom.setStyle(preloadImg,'visibility','hidden');
        YAHOO.util.Dom.setStyle(preloadImg,'width','1px');
        YAHOO.util.Dom.setStyle(preloadImg,'height','1px');
        YAHOO.util.Dom.setStyle(preloadImg,'position','absolute');
        YAHOO.util.Dom.setStyle(preloadImg,'top','0');
        YAHOO.util.Dom.setStyle(preloadImg,'left','0');
        
        YAHOO.util.Event.addListener(preloadImg,'load',loadNext);
        preloadImg.src = getImgPath(pointer);
    };

    preload(currentImg);

}
