dojo.require("dijit.layout.AccordionContainer");
dojo.require("dijit.layout.ContentPane");

CollaborativeFilter = function(containerId){
	dojo.addOnLoad(dojo.hitch(this, function() {
	        this.container = new dijit.layout.AccordionContainer(
	        		{style: 'height: 270px'},
	        		containerId);
	        for (var i=0; i < this.panes.length; i++){
	        	var child = new dijit.layout.ContentPane({
	        					title: this.panes[i].title,
	        					content: this.panes[i].innerHTML()
	 	        				});
	        	this.container.addChild(child);
	        }
	        dojo.subscribe(containerId + '-selectChild', dojo.hitch(this, CollaborativeFilter.prototype.selectChild));
	        this.container.startup();
	        
	        var parentNode = this.container.domNode.parentNode;
	        var topCorners = dojo.query('.topCorners', parentNode)[0];
	    	dojo.style(topCorners, 'display', 'block');
	    	var blc = dojo.query('.bottomLeftCorner', parentNode)[0];
	    	dojo.style(blc, 'display', 'block');
	    	var brc = dojo.query('.bottomRightCorner', parentNode)[0];
	    	dojo.style(brc, 'display', 'block');
	}));
	
	CollaborativeFilter.prototype.panes = [];
	
	CollaborativeFilter.prototype.appendPane = function(collaborativeFilterPane){
		this.panes.push(collaborativeFilterPane);
	} 
}

CollaborativeFilter.prototype.selectChild = function(child){
	// Because of the background gradient we
	// need to swap the rounded bottom corners
	var parentNode = this.container.domNode.parentNode;
	var blc = dojo.query('.bottomLeftCorner', parentNode)[0];
	var brc = dojo.query('.bottomRightCorner', parentNode)[0];
	
	var childs = this.container.getChildren();
	var lastChild = childs[childs.length - 1];
	if (child == lastChild){
		dojo.style(blc, 'backgroundImage', "url(accordionBottomLeftCornerSelected.png)");
		dojo.style(brc, 'backgroundImage', "url(accordionBottomRightCornerSelected.png)");
	}else{
		dojo.style(blc, 'backgroundImage', "url(accordionBottomLeftCorner.png)");
		dojo.style(brc, 'backgroundImage', "url(accordionBottomRightCorner.png)");
	}
}

CollaborativeFilterPane = function(title){
	this.title = title;
	this.links = [];
	
	CollaborativeFilterPane.prototype.addLink = function(linkText, url){
		this.links.push({linkText: linkText,
						 url: url});
	}
	
	CollaborativeFilterPane.prototype.innerHTML = function(){
		var innerHtml = '';
		for (var i=0; i < this.links.length; i++){
			innerHtml += "<ul class='link'>";
			innerHtml += "<li><a href='";
			innerHtml += this.links[i].url;
			innerHtml += "'>";
			innerHtml += this.links[i].linkText;
			innerHtml += "</a></li></ul>";
		}
		return innerHtml;
	}
}
