/** 
 * controller for Help
 */

function HelpController() {
	this.model=null;
	this.helpView = new HelpView();
	this.helpInstance = ["search_selectedFilter","search_thumbnailSize"];
	this.filterCookie = new FilterCookie();
	this.cookieName = "helpInstance";
	this.cookieValue = null;
	this.currentInstance = "";
}

/** 
* Initialize
*/
HelpController.prototype.init = function() {
	this.model = new HelpModel();
	this.model.init();
	TSWIDGET.EVENTS.subscribeEx('evtGetHelpInfo', this.onGetHelp, this, true);
	TSWIDGET.EVENTS.subscribeEx('evtBeShowHelpInfo', this.beShowHelp, this, true);
	this.helpView.init();
}


/** 
* Handle Help load submit 
*/
HelpController.prototype.onGetHelp = function(helpItem, obj) {
	if (helpItem == "") {
		helpItem = this.getHelpInstance();
	}
	if (this.helpItem != "") {
		if (this.currentInstance == helpItem) {	// just show it
			this.model.fetch(this.currentInstance,true);
		}
		else {									// get help message first.
			this.currentInstance = helpItem;		
			this.model.fetch(this.currentInstance);
		}
	}
}

HelpController.prototype.beShowHelp = function(){
	if(this.cookieValue!=null){
		this.cookieValue[this.currentInstance]["noShow"] = 1;
		this.filterCookie.SaveJSONCookie(this.cookieName,this.cookieValue);
	}
}

HelpController.prototype.getHelpInstance = function(){
	if (navigator.userAgent.indexOf("MSIE 7.") != -1) { // is IE7
		return "";
	}
	if(this.helpView.hasShowThumbnailSize()==false){ // empty search result page
		return "";
	}
	if(get("landingMessage")!=null){		// is web search
		return "";
	}
	var cookieValue = this.filterCookie.GetJSONCookie(this.cookieName);
	var len = this.helpInstance.length;
	if(cookieValue==null){
		cookieValue = new Object();
		for(var i=0;i<len;i++){
			cookieValue[this.helpInstance[i]] = new Object();
			cookieValue[this.helpInstance[i]]["Count"] = 0;
		}		
	}
	this.cookieValue = cookieValue;
	var startIndex = 1;
	/*if(this.helpView.hasSelectedFilter()==false){
		startIndex = 1;
	}*/
	for(var i=startIndex;i<len;i++){
		if(this.cookieValue[this.helpInstance[i]]["Count"] == 0){
			this.cookieValue[this.helpInstance[i]]["Count"] = 1;
			this.filterCookie.SaveJSONCookie(this.cookieName,this.cookieValue);
			return this.helpInstance[i];
		}
	}
	return "";
}
