/*
Copyright (c) 2009, Jotta AS. All rights reserved.
Jotta namespace, based on YAHOO global object
*/
/*
Copyright (c) 2009, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.7.0
*/

if (typeof JOTTA == "undefined" || !JOTTA) {
    var JOTTA = {};
}

JOTTA.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=JOTTA;
        for (j=(d[0] == "JOTTA") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

JOTTA.log = function(msg, cat, src) {
    var l=YAHOO.widget.Logger;
    if(l && l.log) {
        return l.log(msg, cat, src);
    } else {
        return false;
    }
};

JOTTA.register = function(name, mainClass, data) {
    var mods = JOTTA.env.modules, m, v, b, ls, i;

    if (!mods[name]) {
        mods[name] = { 
            versions:[], 
            builds:[] 
        };
    }

    m  = mods[name];
    v  = data.version;
    b  = data.build;
    ls = JOTTA.env.listeners;

    m.name = name;
    m.version = v;
    m.build = b;
    m.versions.push(v);
    m.builds.push(b);
    m.mainClass = mainClass;

    for (i=0;i<ls.length;i=i+1) {
        ls[i](m);
    }
    if (mainClass) {
        mainClass.VERSION = v;
        mainClass.BUILD = b;
    } else {
        JOTTA.log("mainClass is undefined for module " + name, "warn");
    }
};

JOTTA.env = JOTTA.env || {
    modules: [],
    listeners: []
};
JOTTA.env.getVersion = function(name) {
    return JOTTA.env.modules[name] || null;
};

JOTTA.env.ua = YAHOO.env.ua;

(function() {
    JOTTA.namespace("util", "profile", "pub", "widget");
    if ("undefined" !== typeof JOTTA_config) {
        var l=JOTTA_config.listener,ls=JOTTA.env.listeners,unique=true,i;
        if (l) {
            for (i=0;i<ls.length;i=i+1) {
                if (ls[i]==l) {
                    unique=false;
                    break;
                }
            }
            if (unique) {
                ls.push(l);
            }
        }
    }
})();

(function (){
	var JU = JOTTA.util,
		DOM = YAHOO.util.Dom,
		Lang = YAHOO.lang,
		GATRCK = false, 
		TS_REGEX = new RegExp(/\/$/);
	
	JU.JDom = {
		getCSS: function(style, clazz){
			var css = false;
			var styles = document.styleSheets;
			if(styles != null && styles.length > 0){
				for(var i = 0; i < styles.length; i++){
					if(styles[i].href.search(style) != -1){
						css = styles[i];
						break;
					}
				}	
			}
			if(css){
				var x = (css.cssRules ? css.cssRules.length : css.rules.length);
				for(var i = 0; i < x; i++){
					var c = (css.cssRules ? css.cssRules[i] : css.rules[i]);
					if(c && c.selectorText.toLowerCase().search(clazz) != -1){
						return c;
					}
					
				}
			}
			return false;
		},
		inputFocus: function(id){
			var el = DOM.get(id);
			if(el != null){
				el.focus();
				if(el.value != null && el.value != ""){
					el.select();
				}
			}
		},
		ignoreInputKey: function(event){
			var key = event.keyCode;
			switch(key){
				case 9: // tab
					return true;
				case 13: // enter
					return true;
				case 16: // shift
					return true;
				case 18: // alt
					return true;
				case 20: // CAPS
					return true;
				case 27:
					return true;
				case 37: // left arrow
					return true;
				case 38: // up arrow
					return true;
				case 39: // right arrow
					return true;
				case 40: // down arrow
					return true;
				default:
					return false;
			}
		},
		clearSetText: function(text, node){
			if(node){
				while(node.hasChildNodes()){
					node.removeChild(node.childNodes[0]);
				}
				if(text){
					node.appendChild(document.createTextNode(text));
				}
			}
		},
		/* Update validation icons */
		/* Common images */
		LOAD_IMG: "/img/common/ajax_loader_white_16x16.gif",
		OK_IMG: "/img/common/validation_ok_16x16.gif",
		ERR_IMG: "/img/common/validation_failed_16x16.gif",
		setValidating: function(id, reset, others){
			if(reset == "all" && others != null && others.length > 0){
				for(var i = 0; i < others.length; i++){
					DOM.setStyle(others[i]+"Validation", "visibility", "hidden");
				}
			}
			else if(reset == "submit"){
				DOM.setStyle("submitValidation", "visibility", "hidden");
			}
			var img = DOM.get(id);
			if(img != null){
				img.alt = "";
				img.title = "";
				img.src = CTXP + JU.JDom.LOAD_IMG;
				DOM.setStyle(img, "visibility", "visible");
			}
		},
		setValidated: function(id, validation, changeBox){
			if(validation != null){
				var img = DOM.get(id);
				if(img != null){
					img.alt = validation.msg;
					img.title = validation.msg;
					img.src = CTXP + (validation.ok ? JU.JDom.OK_IMG : JU.JDom.ERR_IMG);
					DOM.setStyle(img, "visibility", "visible");
				}
				if(changeBox){
					var msg = DOM.get("errorMessage");
					if(msg != null){
						var ul = DOM.getFirstChild(msg);
						if(ul == null && !validation.ok){
							ul = document.createElement("ul");
							msg.appendChild(ul);
						}
						if(ul == null){
							msg.className = "hidden";
							return;
						}
						
						var li = DOM.getFirstChildBy(ul, function(node){
							if(node.id == "errorMessage_"+id){
								return node;
							}
						return null;});
						if(li == null && !validation.ok){
							li = document.createElement("li");
							li.id = "errorMessage_"+id;
							var txt = document.createTextNode(validation.msg);
							li.appendChild(txt);
							ul.appendChild(li);
						}
						if(li != null && validation.ok){
							ul.removeChild(li);
						}else if(li != null && !validation.ok){
							li.className = "bad";
						}
						if(validation.ok && ul.childNodes.length == 0){
							msg.className = "hidden";
						}
						if(!validation.ok || ul.childNodes.length > 0){
							msg.className = "errorMessage";
						}
					}	
				}else{
					var li = DOM.get("errorMessage_"+id);
					if(li != null){
						li.className = (validation.ok ? "ok" : "bad");
						if(!validation.ok){
							var txt = document.createTextNode(validation.msg);
							while(li.childNodes.length > 0){
								li.removeChild(li.childNodes[0]);
							}
							li.appendChild(txt);
						}
					}
				}
			}
		},
		/* GA */
		gaTrack: function(url, skipParams){
			setTimeout(function(){JU.JDom._gaTrack(url, skipParams);}, 10);
		},
		_gaTrack: function(url, skipParams){
			try {
				JU.JDom._initGA();
				if(url){
					var page = JU.JDom.gaURI(CTXP + url, skipParams);
					GATRCK._trackPageview(page);
				}else{
					GATRCK._trackPageview();
				}
			} catch(err) {}
		},
		gaEvent: function(category, action, label){
			JU.JDom._gaEvent(category, action, label);
		},
		_gaEvent: function(category, action, label){
			try {
				JU.JDom._initGA();
				GATRCK._trackEvent(category, action, label);
			} catch(err) {}
		},
		gaURI: function(url, skipParams){
			if(!skipParams){
				var qs = null;
				try{
					qs = window.top.location.search;
				}catch(e){
					qs = null;
				}
			}
			var ret = url;
			if(ret){
				while(ret != "/" && ret.match(TS_REGEX)){
					ret = ret.replace(TS_REGEX, "");
				}
				if(qs != null && qs != ""){
					if(!ret.match(TS_REGEX)){
						ret = ret + "/";
					}
					ret = ret + qs;
				}
			}
			return ret;
		},
		_initGA: function(){
			if(!GATRCK){
				try{
					GATRCK = _gat._getTracker(JX_GAID);
				}catch (e) {
					GATRCK = false;
				}	
			}
		}
	};
})();

JOTTA.register("jdom", JOTTA.util.JDom, {version: "1.0-SNAPSHOT", build: "1"});
