/*

	Class: SSAUtils
		...
	
	
	Parameters:
		None
		
	Returns:
		Nothing
		
		
	Todo:
		even out the selection container across both the selection-box and selection-drag

*/


SSAUtilsFrontend = Class.create({
	
	initialize:function(){
	},
	
	querySuggestion:function(suggestionStr, updateTag){
		if(suggestionStr==""){
			updateTag.setStyle({display:"none"});
			return;
		}
		
		var requestURL = language.x_frontend_search_suggestions+"/"+suggestionStr;
		
		new Ajax.Request(requestURL, {
			method: "GET",
			onFailure: this.onQuerySuggestionResult.bind(this, false, updateTag),
			onSuccess: this.onQuerySuggestionResult.bind(this, true, updateTag)
		})
	},
	
	onQuerySuggestionResult:function(succes, updateTag, transport){
		updateTag.update("");
		var projectNodes = XPath.selectNodes("steam/feedback/projects/project", transport.responseXML);

		if(projectNodes.length > 0){
			updateTag.setStyle({display:"block"});
		}else{
			updateTag.setStyle({display:"none"});
		}
		
		var projectNode, suggestionLabel, projectUnixName;
		var aTag, suggestionTag;
		for(var a=0; a<projectNodes.length; ++a){
			projectNode = projectNodes[a];
			suggestionLabel = XPath.selectNodes("highlight", projectNode)[0];
			projectUnixName = XPath.selectNodes("_unixname", projectNode)[0];
			
			aTag = new Element("a", {href:"#"});
			suggestionTag = new Element("div").update(suggestionLabel.firstChild.nodeValue);
			suggestionTag.observe("mouseover", this.onSuggestionOver.bindAsEventListener(this, suggestionTag));
			suggestionTag.observe("mouseout", this.onSuggestionOut.bindAsEventListener(this, suggestionTag));
			suggestionTag.observe("click", this.onSuggestionClick.bindAsEventListener(this, projectUnixName.firstChild.nodeValue));
			suggestionTag.setStyle({cursor:"pointer"});
			
			aTag.appendChild(suggestionTag);
			updateTag.appendChild(aTag);
		}

	},
	
	onSuggestionOver:function(mouseEV, suggestionTag){
		//suggestionTag.addClassName("suggestion-over");
	},
	
	onSuggestionOut:function(mouseEV, suggestionTag){
		//suggestionTag.removeClassName("suggestion-over");
	},
	
	onSuggestionClick:function(mouseEV, projectUnixName){
		document.location = language.x_frontend_viewproject+"/"+projectUnixName;
	},
	
	
	
	
	/*
	
		Function: initInfoLabels
			Initializes a set of DOM tags to display a description at mouseover.
	
	
		Parameters:
			infoItems	-	list of DOM tags with popover_fg and popover_bg classes representing the descriptions
			
		Returns:
			Nothing
	
	*/
	initInfoLabels:function(infoItems){
		var fgItem, bgItem, itemStateObj;
		for(var itemIndex = 0; itemIndex < infoItems.length; ++itemIndex){
			
			if(infoItems[itemIndex].select(".info").length==0){
				continue;	
			}
			
			fgItem = infoItems[itemIndex].select(".popover_fg")[0];
			bgItem = infoItems[itemIndex].select(".popover_bg")[0];
			
			itemStateObj = {animating:false};
			itemStateObj.orgHeight = bgItem.getHeight();
			
			if(infoItems[itemIndex].hasClassName("small")){
				itemStateObj.orgHeight+=5;
			}
			infoItems[itemIndex].observe("mouseover", this.onItemHover.bindAsEventListener(this, fgItem, bgItem, itemStateObj));
			infoItems[itemIndex].observe("mouseout", this.onItemOut.bindAsEventListener(this, fgItem, bgItem, itemStateObj));
			
			fgItem.setStyle({height:"0px", marginBottom:"-10px"});
			bgItem.setStyle({height:"0px", marginBottom:"-10px"});
			
		}
	},
	
	onItemHover : function(mouseEV, fgItem, bgItem, itemStateObj) {
		var sContainer = fgItem.select(".s_container")[0];
		jQuery(fgItem).stop();
		jQuery(bgItem).stop();
		jQuery(sContainer).stop();
		
		jQuery(fgItem).animate({ 
			height:itemStateObj.orgHeight
		}, {duration:150});
		
		jQuery(bgItem).animate({ 
			height:itemStateObj.orgHeight
		}, {duration:150});
		

		jQuery(sContainer).animate({ 
			height:itemStateObj.orgHeight
		}, {duration:150});
	},
	
	onItemOut : function(mouseEV, fgItem, bgItem, itemStateObj) {
		var sContainer = fgItem.select(".s_container")[0];
		jQuery(fgItem).stop();
		jQuery(bgItem).stop();
		jQuery(sContainer).stop();
		
		jQuery(fgItem).animate({ 
			height:0
		}, {duration:250});
		
		jQuery(bgItem).animate({ 
			height:0
		}, {duration:250});
		
		
		sContainer.setStyle({height:"auto"});
	}
});

jQuery.noConflict();
var SSAUtilsFrontend = new SSAUtilsFrontend();