// Js code for link list
dojo.declare("SbLinkList", null, {
	constructor: function(id, size, classNames, selectedIndex, categoryTree){
		if(id == null)
			throw("SbLinkList Constructor parameter id must be non null");
		this.id = id;
		if(size == null)
			throw("SbLinkList Constructor parameter size must be non null");
		if(classNames == null)
			throw("SbLinkList Constructor parameter classNames must be non null");
		this.classNames = classNames;
		this.size = size;
		this.selectedIndex = selectedIndex;
		this.categoryTree = categoryTree;
		/*if (this.selectedIndex != null)
			this.selectItem(this.selectedIndex, true);*/
	},
	onMouseOverOut: function(index, isOver){
		if(index != this.selectedIndex)
			dojo.byId(this.getIdForIndex(index)).className = isOver ? this.classNames.over : this.classNames.normal;
	},
	selectItem: function(index, isInitialSelection){
		if(index != this.selectedIndex || isInitialSelection) {
			this.unselectItem(this.selectedIndex);
			dojo.byId(this.getIdForIndex(index)).className = this.classNames.selected;
			this.selectedIndex = index;
		}
	},
	unselectItem:function(index){
		if(index == null)
			return;
		dojo.byId(this.getIdForIndex(index)).className = this.classNames.normal;
		this.selectedIndex = null;
	},
	unselectAll:function(){
        this.unselectItem(this.selectedIndex);
    },
	getIdForIndex: function(index){
		return this.id + index;
	},
	onClick:function(url, index, target){
		//if(index != this.selectedIndex){
			// closure to reference self in load method
            var self = this;
            var targetElem = target || "programListingsContent";
			dojo.xhrGet({
				indexToSelect:index,
		        url : SbUtil.appendPreventCacheToUrl(url),
		        //Run this function if the request is successful
		        load : function(response, ioArgs) {
			        self.selectItem(ioArgs.args.indexToSelect);
                    SbUtil.innerHTML(targetElem, response);
                    //SbUtil.updateBreadCrumbs("programListingsBreadCrumbs", dojo.byId(self.getIdForIndex(index)).innerHTML);
                    // uncomment when back button is fixed for ajax
		            //dojo.back.addToHistory(SbUtil.createPageState("test"));
		            return response;
		        },
		
		        //Run this function if the request is not successful
		        error : function(response, ioArgs) {
		        	dojo.byId(targetElem).innerHTML = "SbLinkList request failed: " + SbUtil.dojoErrorToString(response);
		            return response;
		        }
		    });
	    //}
	}
});