

/* tagSearch.js */

var hub, slug, storyType, searchTerm, objStatus, objSrchText, objResponse;
var url, pars, req, title;

function tagSearchInit() {
	var allLinks = $("chewy").getElementsByTagName("A");	
	for(var i=0; i<allLinks.length; i++) {
		if(allLinks[i].className != "keyword") {
			continue;
		} else {
			addEvent(allLinks[i], "click", tagSearch);						
		}
	}
	/* grab the ldbd object that contains hub name */
	if(sURL_hub != null) {
		hub = sURL_hub;
	} else {
		hub = window.location.href.split("Story/")[1];
		hub = hub.split("/")[1];
		
	}
	storyType = location.href.split("/")[6];
	if( (storyType == "undefined") || (storyType == "") || (storyType == null) ) { storyType = "BNStory"; }
}

function tagSearch(e) {		
	e = (!e) ? window.event : e;
		
	searchTerm = this.firstChild.nodeValue;
	slug = this.href.split("&")[2].split("=")[1];
	
	//console.log(searchTerm);
	//console.log(slug);
	
	objStatus = $("status");
	objResponse = $("response");
	
	if(objStatus.style.display == "none") {
		objStatus.style.display = "block";
	}
	// remove the "Searching..." text
	if(objStatus.hasChildNodes) {
		while(objStatus.firstChild) {
			objStatus.removeChild(objStatus.firstChild);
		}
	}
	//remove the response if one is present 
	if($("responseMsg")) {
		if($("responseMsg").hasChildNodes) {
			while ($("responseMsg").firstChild) { 
   				$("responseMsg").removeChild($("responseMsg").firstChild);
			}
		}		
	}	
	
	var msg = createElement("p");
	var imgSpin = createElement("img","spinner","Searching...","http://images.theglobeandmail.com/v5/images/icon/icon-spinner.gif","Searching...",16,16);

	srchText = d.createTextNode("Searching for other articles tagged '" + searchTerm + "'");
	msg.appendChild(imgSpin);
	msg.appendChild(srchText);
	objStatus.appendChild(msg);
	
	new Effect.Highlight(objStatus);	
	
	// dummy var for cache control
	var ord = new Date().getTime();
	
	url = "http://www.lisashore.com/servlet/HTMLTemplate?tf=tgamv3/v5/hub/layouts/articleTags.html"
	pars = "hub=" + hub + "&slug=" + slug + "&storyType=" + storyType + "&tag=" + encodeURI(searchTerm) + "&ord=" + ord + "&tmp=1";
	
	//console.log("URL: " + url + pars);
	
	req = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: showSearchResults
		});	
		
	return false;
}

function showSearchResults(origRequest) {	
	if($("spinner")) { $("spinner").style.display = "none"; }
	objStatus.style.display = "none";
	objResponse.innerHTML = origRequest.responseText;
	
	// handle max-height of the response div in IE
	if(objResponse.hasChildNodes && document.all) {
		if(objResponse.childNodes.length > 5) {		
			objResponse.style.height = "340px";
		}
	}
	// objClose is the "Close" link in the response
	var objClose = $("close");
	objClose.onclick = function() {
		new Effect.BlindUp(objResponse);
		return false;
	}
	// toggle blurb on and off
	var objToggle = objResponse.getElementsByTagName("A");
	for(var g=0; g<objToggle.length; g++) {
		if(objToggle[g].className == "rSumm") {
			objToggle[g].onclick = function() { 
				var objBlurb = this.nextSibling.nextSibling;
				new Effect.toggle(objBlurb, "appear", { duration: 1.0 });
				return false;
			}
		}
	}
	new Effect.BlindDown(objResponse);
}

addEvent(window, "load", tagSearchInit);