/** 
 * Help widget view
 */

function HelpView() {
	// Register events
	TSWIDGET.EVENTS.createEvent("evtGetHelpInfo");
	TSWIDGET.EVENTS.createEvent("evtBeShowHelpInfo");
	this.itemID = "balloonHelp";
	this.inStay = false;
	this.inShow = false;
	this.top=0;
	this.currentInstance = "";
} 


/** 
* Initialize
* This call is used typically to register for events post-construction
* 
*/
HelpView.prototype.init = function() {
	TSWIDGET.EVENTS.subscribeEx('evtHelpInfoLoaded', this.show, this, true);
	//YAHOO.util.Event.on("balloonShowHelp", "change", this.isShowHelp, null, this);
	YAHOO.util.Event.on(document.body, "click", this.hideHelp, this, true);
	YAHOO.util.Event.on(this.itemID, "mouseover", this.stayOnContainer, this, true);
	YAHOO.util.Event.on(this.itemID, "mouseout", this.stayOffContainer, this, true);
	this.initTop();
	this.helpInfoRequest();
}
HelpView.prototype.initTop=function(){
	var websearchMessage=$("td#OneColumnContent div.searchWelcomeMsg");
	var filter=$("td#OneColumnContent div.searchBarWrapperLeft");
	var filterparse=$("td#OneColumnContent div.searchBarWrapperLeft_filterParsing");
	var top=0;
	if(websearchMessage.length==1){
		top+=websearchMessage[0].offsetHeight+67;
	}
	if(filter.length==1){
		top+=filter[0].offsetHeight;
	}
	if(filterparse.length==1){
		top+=filterparse[0].offsetHeight;
	}
	if(YAHOO.env.ua.ie>0){
		top=top-25;
	}
	this.top=top+4;
}
HelpView.prototype.helpInfoRequest = function(){	
	TSWIDGET.EVENTS.fireEvent('evtGetHelpInfo', "");
}

HelpView.prototype.show = function(results) {
	if (results.length > 0) {
		if (this.currentInstance == results[0].HELPITEMTAG) { //Just show it
			this.display(true);
			this.inShow = true;
		}
		else {
			this.buildContent(results[0]);
			this.display(true);
			this.inShow = true;
			this.setStyle(results[0].HELPITEMTAG);			
			this.addIFrame();
			this.currentInstance = results[0].HELPITEMTAG;
		}
	}
}
HelpView.prototype.isShowHelp = function(evt){
	var target = evt.target?evt.target : evt.srcElement;
	if(target.checked==true){
		TSWIDGET.EVENTS.fireEvent('evtBeShowHelpInfo', null);
		Analytics.prototype.recordEvent("searchr2_balloonHelp","searchr2_balloonHelp:dismiss",this.currentInstance);
	}
}
HelpView.prototype.hideHelp = function(evt){
	if(this.inStay==false && this.inShow==true){
		this.hide();
	}
}
HelpView.prototype.buildContent = function(helpInfo) {
	var arrHtml = [];
	arrHtml.push('<div class="left">');
	arrHtml.push('<div class="leftTop"></div>');
	arrHtml.push('<div class="border"></div>');
	arrHtml.push('<div class="arrow"></div>');
	arrHtml.push('<div class="border"></div>');
	arrHtml.push('<div class="leftBottom"></div>');
	arrHtml.push('</div>');
	arrHtml.push('<div class="middle">');
	arrHtml.push('<div class="middleTop"></div>');
	arrHtml.push('<div class="content">');
	arrHtml.push('<div class="img"></div>');
	arrHtml.push('<div class="info">');
	arrHtml.push('<div class="top"><div class="title">'+helpInfo.HELPITEMNAME+'</div><div class="close" id="balloonHelpClose"></div></div>');
	arrHtml.push('<div class="message">'+helpInfo.HELPITEMDESCRIPTION+'</div>');
	/*
	if(helpInfo.HELPITEMISDISMISSIBLE==1){
			arrHtml.push('<div class="checkbox">');
			arrHtml.push('<input type="checkbox" id="balloonShowHelp"><label for="balloonShowHelp">Don\'t show this message again</label></div>');
		}
	*/
	arrHtml.push('</div>');
	arrHtml.push('</div>');
	arrHtml.push('<div class="middleBottom"></div>');
	arrHtml.push('</div>');
	arrHtml.push('<div class="right">');
	arrHtml.push('<div class="rightTop"></div>');
	arrHtml.push('<div class="rightBorder"></div>');
	arrHtml.push('<div class="rightBottom"></div>');
	arrHtml.push('</div>');
	get(this.itemID).innerHTML = arrHtml.join("");
	
	YAHOO.util.Event.on("balloonHelpClose", "click", this.hide, null, this);
}
HelpView.prototype.setStyle = function(tag){
	var blAdditionalHeight = get("filterParsingContainer")==null ? false : true; // keyword parsing filter bar tip
	switch(tag){ //search_selectedFilter, search_thumbnailSize // if has other help instances, we need change the logic
		case "search_selectedFilter":
			if ($("#" + this.itemID).hasClass("selectedFilterHelp") == false) {
				get(this.itemID).style.top=this.top+"px";
				if(YAHOO.env.ua.ie==0){
					get(this.itemID).style.top=this.top-30+"px";
				}
				$("#" + this.itemID).addClass("selectedFilterHelp");
				
			}
			if (this.hasSelectedFilter() == false) {
				if(YAHOO.env.ua.ie==0){
					get(this.itemID).style.top=this.top-30+"px";
				}
				if ($("#" + this.itemID).hasClass("selectedFilterHelp_noItem") == false) {
					$("#" + this.itemID).addClass("selectedFilterHelp_noItem");
				}
			}
			if (get("landingMessage") != null) { // is web search
				get(this.itemID).style.top=this.top+"px";
				if ($("#" + this.itemID).hasClass("selectedFilterHelp_isWebSearch") == false) {
					$("#" + this.itemID).addClass("selectedFilterHelp_isWebSearch");
				}
			}
			if (blAdditionalHeight == true) {
				if (get("landingMessage") != null) { // is web search
					get(this.itemID).style.top=this.top+"px";
					if ($("#" + this.itemID).hasClass("selectedFilterHelp_isWebSearch_filterParsing") == false) {
						$("#" + this.itemID).addClass("selectedFilterHelp_isWebSearch_filterParsing");
					}
				}
				else{
					get(this.itemID).style.top=this.top+"px";
					if(YAHOO.env.ua.ie==0){
						get(this.itemID).style.top=this.top-30+"px";
					}
					if ($("#" + this.itemID).hasClass("selectedFilterHelp_filterParsing") == false) {
						$("#" + this.itemID).addClass("selectedFilterHelp_filterParsing");
					}
				}
			}
			break;
		case "search_thumbnailSize":
			if($("#"+this.itemID).hasClass("selectedFilterHelp")==true){
				$("#"+this.itemID).removeClass("selectedFilterHelp");
			}
			if (blAdditionalHeight == true){
				if ($("#" + this.itemID).hasClass("balloonHelp_filterParsing") == false) {
					$("#" + this.itemID).addClass("balloonHelp_filterParsing");
				}
			}
			get(this.itemID).style.top=this.top+"px";
			if(YAHOO.env.ua.ie>0){
				get(this.itemID).style.top=this.top+25+"px";
			}
			break;
		default:
			if($("#"+this.itemID).hasClass("selectedFilterHelp")==true){
				$("#"+this.itemID).removeClass("selectedFilterHelp");
			}
			if (blAdditionalHeight == true){
				if ($("#" + this.itemID).hasClass("balloonHelp_filterParsing") == false) {
					$("#" + this.itemID).addClass("balloonHelp_filterParsing");
				}
			}
			break;
	}
}

/**
 * Add a iframe to avoid a div covered by select item in IE6!
 **/
HelpView.prototype.addIFrame = function(){
	var s = navigator.userAgent;
	if(s.indexOf("MSIE 6.")!=-1){	// is IE6
		var helpID = this.itemID;
		var helpItem = get(helpID);
		var iframeClassName = helpItem.className;
		if (get("helpIFrame") == null) {
			var div = document.createElement("DIV");
			div.id = "helpIFrame";
			div.style.textAlign = "right";
			div.style.verticalAlign = "middle";
			div.style.paddingTop = "7px";
			div.style.marginRight = "5px";
			div.className = iframeClassName;
			div.style.width = helpItem.clientWidth - 5;
			var iframe = document.createElement("IFRAME");
			iframe.name = "completionFrame";
			iframe.width = helpItem.clientWidth - 43;
			iframe.height = helpItem.clientHeight - 20;
			div.appendChild(iframe);
			YAHOO.util.Dom.insertBefore(div, helpItem);
		}
		else{
			var div = get("helpIFrame");
			div.className = iframeClassName;
			div.style.width = helpItem.clientWidth - 5;
			var iframe = div.firstChild;
			iframe.width = helpItem.clientWidth - 43;
			iframe.height = helpItem.clientHeight - 20;
		}
		this.display(false);
		this.display(true);
	}
}

HelpView.prototype.hide = function(){
	this.display(false);
	this.inShow = false;
	Analytics.prototype.recordEvent("searchr2_balloonHelp","searchr2_balloonHelp:close",this.currentInstance);
}

HelpView.prototype.hasSelectedFilter = function(){
	return get("selectedFilterEmptyContent") == null ? true : false;
}
HelpView.prototype.hasShowThumbnailSize = function(){
	return get("ResultControlTopBarThumbnailSize") == null ? false : true;
}

HelpView.prototype.display = function(blShow){
	blShow ? YAHOO.util.Dom.replaceClass(this.itemID,"hidden","show") : YAHOO.util.Dom.replaceClass(this.itemID,"show","hidden");
	blShow ? get(this.itemID).style.display="" : get(this.itemID).style.display="none";
	var s = navigator.userAgent;
	if (s.indexOf("MSIE 6.") != -1) { // is IE6
		blShow ? YAHOO.util.Dom.replaceClass("helpIFrame","hidden","show") : YAHOO.util.Dom.replaceClass("helpIFrame","show","hidden");
	}
}
HelpView.prototype.stayOnContainer = function(){
	this.inStay=true;
}
HelpView.prototype.stayOffContainer = function(){
	this.inStay=false;
}