/** 
 * History widget view
 */

function HistoryView() {
	// Register events
	TSWIDGET.EVENTS.createEvent("evtDropDownRequest");	
	this.blShow = false;	
	this.stgContentId = "HistoryContent";
	this.stgItemsId = "historyItems";
	this.blSetItems = false;
	this.blStay=false;
	this.blStayHistory=false;
} 


/** 
* Initialize
* This call is used typically to register for events post-construction
* 
*/
HistoryView.prototype.init = function() {
	TSWIDGET.EVENTS.subscribeEx('evtHistoryLoaded', this.show, this, true);
	YAHOO.util.Event.on("History", "click", this.loadHistorySubmit, this, true);
	YAHOO.util.Event.on("History", "mouseover", this.stayOnButton, this, true);
	YAHOO.util.Event.on("History", "mouseout", this.stayOffButton, this, true);
	YAHOO.util.Event.on(this.stgContentId, "mouseover", this.stayOnContainer, this, true);
	YAHOO.util.Event.on(this.stgContentId, "mouseout", this.stayOffContainer, this, true);
	YAHOO.util.Event.on(document.body, "click", this.ControlShow, this, true);
	YAHOO.util.Event.on("historyTopLeft", "click", this.hide, this, true);
	YAHOO.util.Event.on("historyTopRight", "click", this.hide, this, true);
	this.setSafariStyle();
}
HistoryView.prototype.initClickHistoryEvt=function(){
	var hisItems = YHDom.getElementsByClassName("HistoryItem","div","HistoryContent");	
	YAHOO.util.Event.on(hisItems, "click", this.trackEvent,["Search_Filter_SearchBar","Search_Filter_SearchBar:select_history_item"], this);
}
HistoryView.prototype.trackEvent=function(evt,args){
	var node = evt.target?evt.target: evt.srcElement;
	var getParent=function(obj){
		var item = YAHOO.util.Dom.getAncestorByClassName(obj,"HistoryItem");
		if(item!=null){
			return item.id;
		}
		return "";
	}
	var label=getParent(node);
	Analytics.prototype.recordEvent(args[0],args[1],label);
}
HistoryView.prototype.stayOnContainer= function(){
	this.blStay=true;
}
HistoryView.prototype.stayOffContainer= function(){
	this.blStay=false;
}
HistoryView.prototype.stayOnButton= function(){
	this.blStayHistory=true;
}
HistoryView.prototype.stayOffButton= function(){
	this.blStayHistory=false;
}
HistoryView.prototype.ControlShow= function(){
	if(this.blShow){
		if(!this.blStay&&!this.blStayHistory){
			this.hide();
			}
		}
}
HistoryView.prototype.show = function(results) {
	this.blShow = true;
	var divHistoryContent = document.getElementById(this.stgItemsId);
	if (this.blSetItems == false) {
		this.buildContent(results, divHistoryContent);
		this.blSetItems = true;
	}
	//divHistoryContent.style.display = "";
	this.display(true);
}
HistoryView.prototype.buildContent = function(results,dom) {
	var len = results.length;
	if(len>0){
		var html = [];
		var price = "";
		for(var i=0;i<len;i++){
			if(results[i].PRICE!="0"){
				price = FormatDollarValue(parseFloat(results[i].PRICE)/100);
			}
			else{
				price = "Free";
			}
			var name = results[i].NAME;
			var title = "";
			if(name.length>16){
				title = name;
				name = name.substr(0,14) + "...";
			}
			var format = results[i].FORMAT_NAME;
			var formatTitle = "";			
			var reg = /,+/g;
			format = format.replace(reg," ");
			reg = /\s+/g;
			format = format.replace(reg," ").trim();
			var splitChar = " ";
			var arrFormat = format.split(splitChar);
			var subFormat = "";
			var lenFormat = arrFormat.length;
			for (var j = 0; j < lenFormat; j++) {
				arrFormat[j] = "." + arrFormat[j];
			}
			format = arrFormat.join(" ");
			if(format.length>22){
				formatTitle = format;
				for(var j=0;j<lenFormat;j++){
					if((subFormat + arrFormat[j]).length >19){
						break;
					}
					subFormat =subFormat + arrFormat[j] + " ";
				}
				if(subFormat.charAt(subFormat.length-1)==" "){
					subFormat = subFormat.substr(0,subFormat.length-1);
				}
				format = subFormat + "...";
			}
			if (i != len - 1) {
				html.push("<div class='HistoryItem' id='history" + i + "'><div class='itemContent'><a href='" + results[i].URL + "'>");
			}
			else {
				html.push("<div class='HistoryItem HistoryItemEnd' id='history" + i + "'><div class='itemContent'>");
			}
			html.push("<div class='left'><table cellspacing='0' cellpadding='0'><tr><td><a href='" + results[i].URL + "'><img src='" + results[i].THUMBNAIL + "'></a></td></tr></table></div>");
			html.push("<div class='right'><a href='" + results[i].URL + "'><div title='"+title+"' class='product'>" + name + "</div></a>");
			html.push("<div class='author'>by "+results[i].ARTIST+"</div>");
			html.push("<div class='format' title='"+formatTitle+"'>" + format + "</div>");
			html.push("<div class='price'>" + price + "</div></div></div>");
			if(i!=len-1){
				html.push("<div class='splitLine'>&nbsp;</div>");
			}
			html.push("</div>");
		}
		dom.innerHTML = html.join("");
		YAHOO.util.Dom.addClass(dom,"contentPadding");
		this.initClickHistoryEvt();
	}
}
HistoryView.prototype.hide = function(){
	this.blShow = false;
	this.display(false);
}

HistoryView.prototype.loadHistorySubmit = function() {
	if (this.blShow == true) {
		this.hide();
	}
	else {
		TSWIDGET.EVENTS.fireEvent('evtDropDownRequest', null);
	}
}
HistoryView.prototype.display = function(blShow){
	blShow ? YAHOO.util.Dom.replaceClass(this.stgContentId,"hidden","show") : YAHOO.util.Dom.replaceClass(this.stgContentId,"show","hidden");
	blShow ? Analytics.prototype.recordEvent("Search_Filter_SearchBar","Search_Filter_SearchBar:select_history"):Analytics.prototype.recordEvent("Search_Filter_SearchBar","Search_Filter_SearchBar:close_history");
}

/*
 * only safari version 3.2.1 has some display problem.
 */
HistoryView.prototype.setSafariStyle = function(){
	if (window.openDatabase){	// is Safari
		var ua = navigator.userAgent.toLowerCase();
		if (ua.indexOf("version/3.2.1") != -1||ua.indexOf("version/3.2.3") != -1||ua.indexOf("version/3.2.2") != -1) {
			$("#HistoryContent").addClass("Safari");
		}
	}
}

