// starbak js utility class
dojo.declare(
        "SbDojoTreeAdvanced", dijit.Tree,{
        postCreate: function(){
            this.inherited("postCreate", arguments);
            this.afterPostCreate();
        },
        afterPostCreate: function(){
        }
});

dojo.declare("SbDojoTree", null, {
        constructor: function(id, linkList, target, classNames){
        	this.id = id;
        	this.linkList = linkList;
        	this.target = target || "programListingsContent"
        	this.classNames = classNames || {baseName:"dijitTreeIsRoot", odd:"Odd", even:"Even", selected:"Selected"};
        },
        onTreeClick: function(item, node){
            // select as current node
            this.evenOddStyle();
            this.setNodeBgClass(node.domNode, 
                    new Array(this.classNames.baseName + this.classNames.selected),
                    new Array(this.classNames.baseName + this.classNames.selected));
        	// closure
        	var self = this;
        	// There is a bug in dojo that causes a js error to be thrown 
        	// when using preventCache in IE. The error is:
        	// 		"object doesn't support this property or method"
        	// Generate our own preventCache param
		    dojo.xhrGet({
		    	//preventCache : true,
		        url : SbUtil.appendPreventCacheToUrl(item.url),
		        //Run this function if the request is successful
		        load : function(response, ioArgs) {
		        	self.unselectLinkList();
		            SbUtil.innerHTML(self.target, response);
		            SbUtil.backBtnHelper(null, ioArgs.args.url);
		            return response;
		        },
		
		        //Run this function if the request is not successful
		        error : function(response, ioArgs) {
		        	dojo.byId(self.target).innerHTML = "SbDojoTree request failed: " + SbUtil.dojoErrorToString(response);
		            return response;
		        }
		    });
		},
		unselectLinkList:function(){
			if(this.linkList != null){
				this.linkList.unselectAll();
			}
		},
		refreshData: function(url, responseElem){
			responseElem = responseElem || "catTreeContainer";
			// closure
            var self = this;
			dojo.xhrGet({
		        url : url,
		        preventCache : true,
                responseElem : responseElem,
                self: self,
		        //Run this function if the request is successful
		        load : function(response, ioArgs) {
                    // Need to destroy any previously created widgets.
                    // This code may cause some bugs, I'd prefer a method of query for registered widgets,
                    // but have not been able to find one.
                    if(dijit.byId(self.id)) {
	                    ioArgs.args.self.destroy();
	                }
                    SbUtil.innerHTML(ioArgs.args.responseElem, response);
                    try{
                        dojo.parser.parse(dojo.byId(ioArgs.args.responseElem), true);
                        ioArgs.args.self.evenOddStyle();
                    } catch(e){
                        dojo.byId(ioArgs.args.responseElem).innerHTML = "SbDojoTree error displaying categories tree: " + SbUtil.dojoErrorToString(e);
                    }
		            return response;
		        },
		        //Run this function if the request is not successful
		        error : function(response, ioArgs) {
		        	dojo.byId(ioArgs.args.responseElem).innerHTML = "SbDojoTree request failed: " + SbUtil.dojoErrorToString(response);
		            return response;
		        }
		    });
		},
		destroy: function(){
			dijit.byId(this.id).destroy();
		},
        evenOddStyle: function(initialLoad){
        	// function to color tree rows different styles every other row
            var treeNodes = dojo.query("." + this.classNames.baseName, dojo.byId(this.id));
            
            if (treeNodes.length > 1){
	            for (var i = 1; i < treeNodes.length; i++){
                    // root nodes
                    var addClassNames = (initialLoad && i == 1) ? new Array(this.classNames.baseName + this.classNames.selected) :
                    		new Array(this.classNames.baseName + (i % 2 == 0 ? this.classNames.odd : this.classNames.even)); 
                    
                    this.setNodeBgClass(treeNodes[i], 
                            new Array(this.classNames.baseName + this.classNames.odd, 
                                    this.classNames.baseName + this.classNames.even, 
                                    this.classNames.baseName + this.classNames.selected),
                            addClassNames);
                    // leaf nodes
                    var leafNodes = dojo.query(".dijitTreeNode", treeNodes[i]);
                    for (var j = 0; j < leafNodes.length; j++){
                    	// Fix IE 6 rendering bug, first leaf node alignment is off by about -3 pixels on the x axis
                        if (initialLoad && dojo.isIE == 6 && j == 0){
                            leafNodes[j].style.position = "relative";
                            leafNodes[j].style.left = "-3px";
                        }
                        
                        var rmClasses = new Array(this.classNames.baseName + this.classNames.odd, 
                            this.classNames.baseName + this.classNames.even, 
                            this.classNames.baseName + this.classNames.selected);
                        var addClasses = null;
                            
                        this.setNodeBgClass(leafNodes[j], 
                                rmClasses,
                                addClasses);
                    }
	            }
	        }
        },
        setNodeBgClass:function(node, rmClassNames, addClassNames){
            var newClassName = new String(node.className);
            if(rmClassNames != null){
                for (var i = 0; i < rmClassNames.length; i++){
                    // use replace to avoid duplication of class name
                    var regExp = new RegExp("(^\\s*|\\s+)" + rmClassNames[i] + "(\\s+|\\s*$)", "g");
                    newClassName = newClassName.replace(regExp, " ");
                }
            }
            
            // place classes at the end of the classNames String
            if (addClassNames != null && addClassNames.length > 0)
                newClassName += " " + addClassNames.join(" ");
            node.className = newClassName;
        }
});

SbDojoTree.TREE_TYPE_CAT = "category";
SbDojoTree.TREE_TYPE_CHANNEL = "channel";