var scrollMan = new N2EventManager();



if ( !toggleBrandView ) { var toggleBrandView = function() {}; }

if( typeof eventMan != "undefined" ) {

	scrollMan.numberBrands = 0;

	scrollMan.init = function() {
		var widgets = eventMan.aEvents["newSearchResults"];
		for( var i = 0; i < widgets.length; i++ ) {
			if( widgets[i] instanceof Control.Brands ) {
				for( var brand in widgets[i].things ) {
					if( widgets[i].things[brand].selected == true ) {
						scrollMan.onEvent("", "selectBrand", brand);
					}
				}
				break;
			}
		}
	}
	
	scrollMan.onEvent = function(oSrcWidget, sEvent, oData) {
		if( sEvent == "selectBrand" ) {
			if( !($("content-" + oData)) ) {
				scrollMan.numberBrands++;
				var bscroller=new Control.BrandScroller(oData, "brandScrollerView");
				eventMan.subscribe(bscroller, ["newSearchResults"]);
				if( scrollMan.numberBrands > 10 ) {
					toggleBrandView( false );
				} else if (scrollMan.numberBrands == 1 ) {
					toggleBrandView( true );
				}
			} else {
				scrollMan.numberBrands--;
				if( scrollMan.numberBrands == 0 || scrollMan.numberBrands > 10 ) {
					toggleBrandView( false );
				} else if( scrollMan.numberBrands == 10 ) {
					toggleBrandView( true );
				}
				scrollMan.publish("","remove-" + oData,"");
			}
		} else if( sEvent == "updateSearch" && !(oSrcWidget instanceof Control.Brands) ) {
			scrollMan.publish("", "updateSearch", "");
		}
	}
	
	scrollMan.clearAll = function() {
		scrollMan.numberBrands = 0;
		$('brandScrollerView').innerHTML = "";
	}
	
	eventMan.subscribe(scrollMan, ["selectBrand", "updateSearch"]);
}


if( !debgdiv ) {
	var debgdiv = function() {}
}

function queryObject() {
	this.bv = "";	
	this.overrideBrand = "";
	this.colors = "";
	this.sizes = "";
	this.size = "";
	this.widths = "";
	this.heelheights = "";
	this.brands = "";
	this.keywords = "";
	this.nodes = "";
	this.node = "";
	this.sort = "";
	this.priceHigh = "";
	this.priceLow = "";
	this.onsale = "";
	this.newarrival = "";
	this.page = "1";
	this.bpage = "1";
}

if(!Control) var Control = {};
Control.CScroller = Class.create();

Control.CScroller.prototype = {
	_initialize: function(sName, sHome, sHeaderMarkup) {
		this.homeBase = sHome;
		this.name = sName;
		this.activeLeft = 0;
		this.activeRight = 0;
			
		var myMarkup = "";
		myMarkup += "<table id=\"table-" + sName + "\">";
		myMarkup += sHeaderMarkup;
		myMarkup += "<tr><td colspan='3'><h1 id=\"no-result-" + sName + "\" style=\"display: none\">No results found. Please try a different search.</h1></td></tr>"
 		myMarkup += "<tr>";
		myMarkup += "<td valign=\"top\">";
		myMarkup += "<br \><img class='scrollArrow' style=\"display: none;\" id=\"left-image-"+sName+"\" src=\""+jsImg.getImagePath("left_arrow_inactive")+"\" onmouseover=\"scrollMan.publish(this, 'start-right-" + this.name + "', this.id);\" onmouseout=\"scrollMan.publish(this, 'stop-right-" + this.name + "', this.id);\">";
		myMarkup += "</td>";
		myMarkup += "<td>";
		myMarkup += "<div class=\"contextMain\" id=\"main-" + sName + "\" >";
		myMarkup += "<table><tbody>";
		myMarkup += "<tr id=\"content-" + sName + "\">";
		myMarkup += "</tr>";
		myMarkup += "</tbody></table>";
		myMarkup += "</div>";
		myMarkup += "</td>";
		myMarkup += "<td valign=\"top\">";
		myMarkup += "<br \><img class='scrollArrow' style=\"display: none;\" id=\"right-image-"+sName+"\" src=\""+jsImg.getImagePath("right_arrow")+"\" onmouseover=\"scrollMan.publish(this, 'start-left-" + this.name + "', this.id);\" onmouseout=\"scrollMan.publish(this, 'stop-left-" + this.name + "', this.id);\">";
		myMarkup += "</td>";
		myMarkup += "</table>";
			
		var createMe = document.createElement("div");
		createMe.innerHTML = myMarkup;
		document.getElementById(this.homeBase).appendChild(createMe);
		this.me = createMe;
		
		scrollMan.subscribe(this, ["start-left-" + sName, "start-right-" + sName, "stop-left-" + sName, "stop-right-" + sName, "left-" + sName, "right-" + sName, "remove-" + sName, "show-list", "updateSearch", "click", "shop-" + sName]);
		
	},
	
	scrollRight: function() {
		if (typeof this._loadImage != 'undefined'){
			this._loadImage();
		}
		if( this.activeRight == 1 ) {
			var beforeInc = $("main-" + this.name).scrollLeft;
			$("main-" + this.name).scrollLeft-=5;
			var afterInc = $("main-" + this.name).scrollLeft;
			if( $("main-" + this.name).scrollLeft == 0 ) {
				$("left-image-" + this.name).src = jsImg.getImagePath("left_arrow_inactive");
			} else {
				$("left-image-" + this.name).src = jsImg.getImagePath("left_arrow");
				setTimeout("scrollMan.publish(null, \"right-" + this.name + "\", null);", 5);
			}
			if( beforeInc != afterInc ) {
				$("right-image-" + this.name).src = jsImg.getImagePath("right_arrow");
			}	
		}
		$('lbound-' + this.name).innerHTML = Math.floor($('main-' + this.name).scrollLeft / this.itemWidth) + 1;
		$('ubound-' + this.name).innerHTML = Math.floor($('main-' + this.name).scrollLeft / this.itemWidth) + this.size;
	},
		
	scrollLeft: function() {
		if (typeof this._loadImage != 'undefined'){
			this._loadImage();
		}
		if( this.activeLeft == 1 ) {
			var beforeInc = $("main-" + this.name).scrollLeft;
			$("main-" + this.name).scrollLeft+=5;
			var afterInc = $("main-" + this.name).scrollLeft;
			if( $("main-" + this.name).scrollLeft == 0 ) {
				$("left-image-" + this.name).src = jsImg.getImagePath("left_arrow_inactive");
			} else {
				$("left-image-" + this.name).src = jsImg.getImagePath("left_arrow");
			}
			if( beforeInc == afterInc ) {
				$("right-image-" + this.name).src = jsImg.getImagePath("right_arrow_inactive");
			} else {
				$("right-image-" + this.name).src = jsImg.getImagePath("right_arrow");
				setTimeout("scrollMan.publish(null, \"left-" + this.name + "\", null);", 5);
			}			
		}
		var before = $('lbound-' + this.name).innerHTML;
		$('lbound-' + this.name).innerHTML = Math.floor($('main-' + this.name).scrollLeft / this.itemWidth) + 1;
		if( typeof this._onEvent != 'undefined' ) {
			this._onEvent("", "scroll-update-" + this.name, $('lbound-' + this.name).innerHTML);
		}
		var after = $('lbound-' + this.name).innerHTML;
		$('ubound-' + this.name).innerHTML = Math.floor($('main-' + this.name).scrollLeft / this.itemWidth) + this.size;
		

	}, 
	
	inMyThings: function( asin ) {
		if( typeof mythingsResponse != "undefined" ) {
			var asins = mythingsResponse.items;
			if( typeof asins != 'undefined' ) {
				for( var i = 0; i < asins.length; i++ ) {
					var item = asins[i];
					if( item.item == asin ) {
						return true;
					}
				}
			}
		}
		return false;
	},
	
	filterImage:function( imgURL ) {
		if(imgURL.indexOf("no-img") >= 0 || imgURL.indexOf("Endless/en_US/images/no_image_large.gif")>=0) {
			imgURL="http://images.amazon.com/images/G/01/Endless/en_US/images/no_image_small.gif";
		}
		return imgURL;
	},
	
	onEvent: function(oSrcWidget, sEvent, oData){
		if( typeof this._onEvent != 'undefined' ) {
			this._onEvent(oSrcWidget, sEvent, oData);
		}
		switch(sEvent){
				case "show-list":
					var row = $("content-" + this.name);
					var contextList = $("main-" + this.name);
					if( row.offsetWidth > contextList.offsetWidth ) {
						document.getElementById("left-image-" + this.name).style.display = "inline";
						document.getElementById("right-image-" + this.name).style.display = "inline";
					}
				break;
				case "left-" + this.name: 
					this.scrollLeft();
				break;
				case "right-" + this.name: 
					this.scrollRight();
				break;
				case "start-right-" + this.name: 
					this.activeRight = 1;
					this.scrollRight();
				break;
				case "start-left-" + this.name: 
					this.activeLeft = 1;
					this.scrollLeft();
				break;
				case "stop-right-" + this.name: 
					this.activeRight = 0;
				break;
				case "stop-left-" + this.name: 
					this.activeLeft = 0;
				break;
				case "remove-" + this.name:
					if( this.request != null ) {
						this.request.transport.abort();
					}
					scrollMan.unsubscribe(this);
					$(this.homeBase).removeChild(this.me);
				break;
		}
	},
	
	getPriceMarkup : function( listprice, price ) {
		var mypriceMarkup = "";
		if( typeof listprice != 'undefined' ) {
			mypriceMarkup = "<span class='xprice'>" + listprice + "</span>&nbsp;<span class='salePrice'>" + price + "</span>";
		} else {
			mypriceMarkup = "<span class='price'>" + price + "</span>";
		}
		return mypriceMarkup;
	},
	
	getMyThingsMarkup : function( asin ) {
		var myThingsMarkUp = "";
		if( this.inMyThings(asin) == true ) {
			myThingsMarkUp = "<span class=\"prodImgSaved\"><img src=\"" + $('mSaved').src + "\"></span>";
		}
		return myThingsMarkUp;
	}
	
};

function initSearchScroller( name, leftEdge, itemWidth ) {
//$("main-" + this.name).scrollLeft = (leftEdge - 1) * this.itemWidth;
$("main-" + name).scrollLeft = (leftEdge - 1) * itemWidth;
if( $("main-" + name).scrollLeft != (leftEdge - 1) * itemWidth ) {
	setTimeout( "initSearchScroller('"+name+"',"+leftEdge+","+itemWidth+");", 10 );
}
}

Control.SearchScroller = Class.create();
Control.SearchScroller.prototype = Object.extend({ 
		initialize: function(sName, sHome, sQuery, sAnchorAsin) {
			var myMarkup = "";
			myMarkup += "<tr>";
			myMarkup += "<td colspan='3'><span class=\"contextAmount floatRight\" colspan='2' style=\"display: none;\" id=\"size-bar-" + sName + "\">"+unescape(sName)+": showing <span id=\"lbound-" + sName + "\">1</span>-<span id=\"ubound-" + sName + "\">5</span> of <span id=\"size-" + sName + "\"></span></span>";		
			myMarkup += "<img src=\""+jsImg.getImagePath("continue_shopping")+"\" class=\"contextLink\" onclick=\"scrollMan.publish('', 'shop-" + sName + "', '');\">";
			myMarkup += "<span class=\"contextHeader\"></span>";
			myMarkup += "</td></tr>";
			this._initialize(sName, sHome, myMarkup);
			this.query = sQuery;
			this.queryPageSize = 60;
			this.itemWidth = 93;
			this.request = null;
			this.numberFilled = 0;
			this.numberAsins = 0;
			this.totalSize = 0;
			this.size = 8;
			this.anchorAsin = sAnchorAsin;
			this.anchorAsinFound = false;
			this.anchorAsinIndex = 0;	
			this.anchorAsinLocated = false;
			this.anchorAsinSideSize = 8;
			this.fillAsins();
			this.scrollLeftOccured = false;
		},
		
		fillAsins: function() {
			this.numberFilled++;
			var opt = {
			method: 'post',
			postBody: "lite=1&" + this.queryToString(),
			onSuccess: this.searchReturn.bind(this)
			};
			this.request = new Ajax.Request('/searchliterequest', opt);
		},
		
		searchReturn: function(http_request) {
			var json_data = http_request.responseText;
			var resultSet = {}; 
			resultSet = eval( '(' + json_data + ')' );
			if( this.totalSize == 0 ) {
				var tSize = resultSet.asins.length;
				this.totalSize = tSize; //resultSet.numResults;
				if (this.totalSize == 0){
					Element.show('no-result-'+ this.name);
					Element.hide('size-' + this.name);
					Element.hide('lbound-' + this.name);
					Element.hide('ubound-' + this.name);
					Element.hide('size-bar-' + this.name);
				}
				else{
					Element.hide('no-result-'+ this.name);
					Element.show('size-' + this.name);
					Element.show('lbound-' + this.name);
					Element.show('ubound-' + this.name);
					Element.show('size-bar-' + this.name);					
					$('size-' + this.name).innerHTML = tSize;
					$('lbound-' + this.name).innerHTML = Math.min(1, tSize);
					$('ubound-' + this.name).innerHTML = Math.min(this.size, tSize);
				}
			}
			
			for( var i = 0; i < resultSet.asins.length; i++ ) {
				this.addAsin(resultSet.asins[i].asin, resultSet.asins[i].imgURL, resultSet.asins[i].listprice, resultSet.asins[i].price, resultSet.asins[i].outOfStock);
			}	
			
			if (this.anchorAsinFound && !this.anchorAsinLocated) {
				if (this.anchorAsinIndex > Math.floor(this.size / 2)) {
					var leftEdge = (this.anchorAsinIndex - Math.floor(this.size / 2));
					if (this.anchorAsinIndex > (this.totalSize - this.size) ){
						leftEdge = this.totalSize - this.size + 1;
					}	
					//$("main-" + this.name).scrollLeft = (leftEdge - 1) * this.itemWidth;
					initSearchScroller(this.name, leftEdge, this.itemWidth);
					$('lbound-' + this.name).innerHTML = leftEdge;
					$('ubound-' + this.name).innerHTML = leftEdge + this.size - 1;		
					$("left-image-" + this.name).src = jsImg.getImagePath("left_arrow");	
				}
				
			//	var row = document.getElementById("content-" + this.name);
			//	var asinImages = row.getElementsByTagName("img");
				var asinImages = this.getProdImages();
				for (var i=Math.max(0, this.anchorAsinIndex - this.anchorAsinSideSize); i<Math.min(this.numberAsins, this.anchorAsinIndex + this.anchorAsinSideSize); i++){
					asinImage = asinImages[i];
					asinImage.src = asinImage.id;				
				}
				
				this.anchorAsinLocated = true;
			}
						
			//if (!this.anchorAsinFound && this.numberAsins < this.totalSize ){
				//this.fillAsins();
			//}
		},
		
		addAsin: function( asin, image, listprice, price, outofstock ) {
			var row = document.getElementById("content-" + this.name);
			var newAsin = document.createElement("td");
			
			newAsin.className = "result";
			if( typeof previouscontextasin != "undefined" ) {
				if (asin == previouscontextasin)
					newAsin.className = "resultWithBorder";
			}
			newAsin.valign = "top";
			newAsin.id="contextasin"+asin;
			var asinMarkup = "";
			var myThingsMarkUp = this.getMyThingsMarkup(asin);
			var priceMarkup = "<span class='price'>Unavailable</span>";
			if( outofstock == 0 ) {
				priceMarkup = this.getPriceMarkup(listprice, price);
			} 		
			//asinMarkup += "<div class='result' onclick=\"detailMan.publish('', 'updateDetailPage', '" + asin + "');\"><center><img class=\"prodImg\"  src=\"" + image + "\"><br>" + myThingsMarkUp + priceMarkup + "</center>";
			var center = document.createElement('center');
			var asinImage = new Image();
			asinImage.className = "prodImg";
			asinImage.src = image;
			asinImage.id = image;
			center.appendChild(asinImage);
			center.innerHTML += "<br>" + myThingsMarkUp + priceMarkup;
			asinMarkup += "<div class='result' onclick=\"detailMan.publish('', 'updateDetailPage', '" + asin + "');\">" + center.innerHTML;
			
			this.numberAsins++;
			
			newAsin.innerHTML = asinMarkup;
			row.appendChild(newAsin);
			
			if( this.totalSize >= this.size ) {
				$("left-image-" + this.name).style.display = "inline";
				$("right-image-" + this.name).style.display = "inline";
			}
			
			if (asin == this.anchorAsin){
				this.anchorAsinFound = true;
				this.anchorAsinIndex = this.numberAsins;
			}
			
		},
		
		_loadImage: function() {
				//var row = document.getElementById("content-" + this.name);
				//var asinImages = row.getElementsByTagName("img");
				var asinImages = this.getProdImages();
				for (var i=Math.max(0, $('lbound-' + this.name).innerHTML-this.anchorAsinSideSize); i<Math.min(this.numberAsins, $('ubound-' + this.name).innerHTML); i++){
					asinImage = asinImages[i];
					asinImage.src = asinImage.id;				
				}
		}, 
		
		getProdImages: function(){
			var row = document.getElementById("content-" + this.name);
			var allImages = row.getElementsByTagName("img");
			var prodImages = [];
			for (var i=0; i<allImages.length; i++){
				var image = allImages[i];
				if (image.className == 'prodImg'){
					prodImages.push(image);
				}
			}
			return prodImages;
		},

		queryToString : function( url ) {
			var qStr = "";
			qStr += "colors=" + this.query.colors;
			if( url ) {
				qStr += "&brands=" + this.query.brands;
				qStr += "&dept=" + this.query.dept;
				if( this.query.bv != "" ) {
					qStr += "&bv=" + this.query.bv;
				}
			} else { 
				if( this.query.overrideBrand == "" ) {
					qStr += "&brands=" + this.query.brands;
				} else {
					qStr += "&brands=" + this.query.overrideBrand;
				}
			}
			qStr += "&nodes=" + this.query.nodes;
			qStr += "&node=" + this.query.node;
			qStr += "&priceHigh=" + this.query.priceHigh;
			qStr += "&priceLow=" + this.query.priceLow;
			qStr += "&sort=" + this.query.sort;
			qStr += "&keywords=" + this.query.keywords;
			qStr += "&onsale=" + this.query.onsale;
			qStr += "&widths=" + this.query.widths;
			qStr += "&sizes=" + this.query.sizes;
			qStr += "&heelheights=" + this.query.heelheights.replace(/&/g, "%26");
			qStr += "&newarrivals=" + this.query.newarrivals;
			//if( url ) {
			if( this.query.overrideBrand == "" ) {
				qStr += "&page=" + this.query.page;
				qStr += "&size=" + this.query.size;
			} else {
				qStr += "&page=" + this.query.bpage;
				qStr += "&size=20";
			}
			//} else {
			//	qStr += "&size=" + this.queryPageSize;
			//}
			return qStr;
		},
		
		_onEvent: function(oSrcWidget, sEvent, oData){
			switch(sEvent){
				case "shop-" + this.name:
						window.location = "/s?" + this.queryToString(1);
				break;
			}
		}
		
	}, Control.CScroller.prototype);
	
Control.BrandScroller = Class.create();
Control.BrandScroller.prototype = Object.extend({ 
		initialize: function(sName, sHome) {
			var myMarkup = "";
			myMarkup += "<tr>";
			myMarkup += "<td id=\"logo-" + sName + "\" colspan='3'></td>";
			myMarkup += "</tr><tr>";
			myMarkup += "<td colspan='3'>";
			myMarkup += "<span class=\"contextAmount floatRight\" style=\"display: none;\" id=\"size-bar-" + sName + "\">showing <span id=\"lbound-" + sName + "\">1</span>-<span id=\"ubound-" + sName + "\">5</span> of <span id=\"size-" + sName + "\"></span></span>";
			myMarkup += "<span class=\"contextLink\" onclick=\"scrollMan.publish('', 'shop-" + sName + "', '');\">shop this brand</span>";
			myMarkup += "</td></tr>";
			this._initialize(sName, sHome, myMarkup);
			this.request = null;
			this.itemWidth = 93;
			this.numberFilled = 0;
			this.numberAsins = 0;
			this.totalSize = 0;
			this.size = 6;
			this.fillAsins();
		},
		
		fillAsins: function() {
			this.numberFilled++;
			var postString = "";
			for (var i = 0; i < searchMan.widgets.length; i++){
					var oWidget = searchMan.widgets[i];
					var serial = oWidget.serialize();
					if (serial != "" && !(oWidget instanceof Control.Pager) && !(oWidget instanceof Control.Brands)){
						if(i != 0){ postString += "&" }
						 postString += serial;
					}
			}
			postString +="&brands="+escape(this.name);
			postString +="&page=" + this.numberFilled;
			postString +="&lite=1&size=20";

			if( $('hSearchCount').value == "" || $('hSearchCount').value == "1" ) {
				postString += "&sizes=" + sSizes;
				postString += "&widths=" + sWidths;
				postString += "&heelheights=" + sHeelHeights.replace(/&/g, "%26");
			}			

			// now we have a valid post string - fire off another search
			var opt = {
			method: 'post',
			postBody: postString,
			onSuccess: this.searchReturn.bind(this)
			};
			debgdiv('brand scroller: /searchliterequest?' + opt.postBody);
			if ((this.request)&&(this.callInProgress(this.request.transport))) {
				debgdiv("aborted previous call");
				this.request.transport.abort();
			}
			this.request = new Ajax.Request('/searchliterequest', opt); // search request - get initial result set
		},
		
		emptyAsins : function() {
			var row = document.getElementById("content-" + this.name);
			while( row.childNodes.length > 0 ) {
				row.removeChild( row.childNodes[0] );
			}
			this.numberFilled=0;
			this.numberAsins=0;
			this.totalSize=0;
			Element.hide('size-' + this.name);
			Element.hide('lbound-' + this.name);
			Element.hide('ubound-' + this.name);
			Element.hide('size-bar-' + this.name);
			$("left-image-" + this.name).style.display = "none";
			$("right-image-" + this.name).style.display = "none";
		},
		
		searchReturn: function(http_request) {
			var json_data = http_request.responseText;
			var resultSet = {}; 
			resultSet = eval( '(' + json_data + ')' );
			if( this.totalSize == 0 ) {
				this.totalSize = resultSet.numResults;
				if (this.totalSize == 0){
					Element.show('no-result-'+ this.name);
					Element.hide('size-' + this.name);
					Element.hide('lbound-' + this.name);
					Element.hide('ubound-' + this.name);
					Element.hide('size-bar-' + this.name);
					Element.hide('main-' + this.name);
				}
				else{
					Element.hide('no-result-'+ this.name);
					Element.show('size-' + this.name);
					Element.show('lbound-' + this.name);
					Element.show('ubound-' + this.name);
					Element.show('size-bar-' + this.name);			
					Element.show('main-' + this.name);		
					$('size-' + this.name).innerHTML = resultSet.numResults;
					$('lbound-' + this.name).innerHTML = Math.min(1, resultSet.numResults);
					$('ubound-' + this.name).innerHTML = Math.min(this.size, resultSet.numResults);
				}
			}
			
			for( var i = 0; i < resultSet.asins.length; i++ ) {
				this.addAsin(resultSet.asins[i].asin, resultSet.asins[i].imgURL, resultSet.asins[i].title, resultSet.asins[i].listprice, resultSet.asins[i].price, resultSet.asins[i].outOfStock);
			}	
			if( this.numberFilled == 1 && typeof(resultSet.brandLogo) != 'undefined' && resultSet.brandLogo != "" ) {
				$('logo-' + this.name).innerHTML = resultSet.brandLogo;
			} else if( this.numberFilled == 1 ) {
				$('logo-' + this.name).innerHTML = unescape(this.name);
			}
		},
		
		addAsin: function( asin, image, title, listprice, price, outofstock ) {
			var row = document.getElementById("content-" + this.name);
			var newAsin = document.createElement("td");
			
			newAsin.className = "result";
			if( typeof previouscontextasin != "undefined" ) {
				if (asin == previouscontextasin)
					newAsin.className = "resultWithBorder";
			}
			newAsin.valign = "top";
			newAsin.id="contextasin"+asin;
			var asinMarkup = "";
			var myThingsMarkUp = this.getMyThingsMarkup(asin);
			var priceMarkup = "<span class='price'>Unavailable</span>";
			if( outofstock == 0 ) {
				priceMarkup = this.getPriceMarkup(listprice, price);
			}
			var linkString = this.buildAsinLink(this.name, [asin, this.numberFilled]);
			asinMarkup += "<a href=\"" + linkString + "\"><img class=\"prodImg\" src=\"" + image + "\"><br><span class=\"title\">" + title + "</span><br>" + myThingsMarkUp + priceMarkup + "</a>";		
			
			this.numberAsins++;
			
			newAsin.innerHTML = asinMarkup;
			row.appendChild(newAsin);

			if( this.totalSize >= this.size ) {
				$("left-image-" + this.name).style.display = "inline";
				$("right-image-" + this.name).style.display = "inline";
			}
		},	
		
		updateSearch : function() {
			$("main-" + this.name).scrollLeft = 0;
			$("left-image-" + this.name).src = jsImg.getImagePath("left_arrow_inactive");
			$("right-image-" + this.name).src = jsImg.getImagePath("right_arrow");
			this.emptyAsins();
			this.fillAsins();
		},
		
		buildAsinLink: function(oSrcWidget, oData){
			var postString = "/dp/" + oData[0] +"?";
			for (var i = 0; i < searchMan.widgets.length; i++){
					var oWidget = searchMan.widgets[i];
					var serial = oWidget.serialize();
					if (serial != "" && !(oWidget instanceof Control.Pager) ){
						 postString += "&";
						 postString += serial;
					}
			}
			postString +="&overrideBrand="+escape(oSrcWidget);
			postString +="&bv=1";
			postString +="&bpage="+oData[1];
			postString +="&fromPage=search";
			return postString;
		},
		
		_onEvent: function(oSrcWidget, sEvent, oData){
			switch(sEvent){
				case "updateSearch":
					this.updateSearch();
				break;
				case "click":
					var postString = "/dp/" + oData[0] +"?";
					for (var i = 0; i < searchMan.widgets.length; i++){
							var oWidget = searchMan.widgets[i];
							var serial = oWidget.serialize();
							if (serial != "" && !(oWidget instanceof Control.Pager) ){
								 postString += "&";
								 postString += serial;
							}
					}
					postString +="&overrideBrand="+escape(oSrcWidget);
					postString +="&bv=1";
					postString +="&bpage="+oData[1];
					postString +="&fromPage=search";
					window.location = postString;
					break;
				break;
				case "shop-" + this.name:
					var postString = "";
					for (var i = 0; i < searchMan.widgets.length; i++){
							var oWidget = searchMan.widgets[i];
							var serial = oWidget.serialize();
							if (serial != "" && !(oWidget instanceof Control.Pager) && !(oWidget instanceof Control.Brands)){
								if(i != 0){ postString += "&" }
								 postString += serial;
							}
					}
					postString +="&brands="+escape(escape(this.name));
					postString +="&boutique=1";
					if( typeof( $('mDept') ) != 'undefined' && typeof( $('mDept').innerHTML ) != 'undefined' && $('mDept').innerHTML.length > 0 ) {
						postString += "&dept=" + $('mDept').innerHTML;
					}
					window.location = "/s/ref=sr_bb/?" + postString;
				break;
				case "scroll-update-" + this.name:
					if( oData == ((this.numberFilled-1)*20)+5 && this.numberAsins < this.totalSize ) {
						this.fillAsins();
					}
				break;
				case "newSearchResults":
					var myBins = oData["bins"];
					var inbrandslist=false;
					for( var i = 0; i < myBins.length; i++ ) {
						var myName = escape(myBins[i].name);
						if (myName==this.name){
							inbrandslist=true;
							break;
						}
					}
					if (inbrandslist==false) 
						scrollMan.publish("","remove-" + this.name,"");
					break;
			}
		},
		
		callInProgress: function(xmlhttp) {
			switch (xmlhttp.readyState) {
			case 1: case 2: case 3:
			return true;
			break;
			// Case 4 and 0
			default:
			return false;
			break;
			}
		}
		
	}, Control.CScroller.prototype);

Control.AsinListScroller = Class.create();
Control.AsinListScroller.prototype = Object.extend({ 
		initialize: function(sName, sHome, sSize) {
			var myMarkup = "";
			this.size = 8;
			if( sSize < 8 ) {
				this.size = sSize;
			}
//			myMarkup += "<tr><td align='right' colspan='3' id=\"size-bar-" + this.name + "\"><span class=\"contextAmount floatRight\">"+unescape(sName)+" Showing <span id=\"lbound-" + sName + "\">1</span>-<span id=\"ubound-" + sName + "\">"+this.size+"</span> of <span id=\"size-" + sName + "\">"+sSize+"</span></span></td></tr>";	
			myMarkup += "<tr><td align='right' colspan='3' id=\"size-bar-" + this.name + "\"><span class=\"transparentText\"><span id=\"lbound-" + sName + "\">1</span>-<span id=\"ubound-" + sName + "\">"+this.size+"</span> of <span id=\"size-" + sName + "\">"+sSize+"</span></span></td></tr>";	
			this._initialize(sName, sHome, myMarkup);
			this.itemWidth = 150;
			this.numberFilled = 0;
			this.numberAsins = 0;
			this.totalSize = sSize;	
			if( this.totalSize >= this.size ) {
				$("left-image-" + this.name).style.display = "inline";
				$("right-image-" + this.name).style.display = "inline";
			}
			this.fillAsins();
		},
		
		fillAsins : function() {
			if (typeof asinList != "undefined") {			
				for( var i = 0; i < asinList.length; i++ ) {
					this.addAsin(asinList[i].asin, asinList[i].thumbnailImageURL, asinList[i].listprice, asinList[i].buyingPrice); 
				}
			}
		},
		
		addAsin: function( asin, image, listprice, price ) {
			var row = document.getElementById("content-" + this.name);
			var newAsin = document.createElement("td");
			
			newAsin.className = "result";
//			if( typeof previouscontextasin != "undefined" ) {
//				if (asin == previouscontextasin)
//					newAsin.className = "resultWithBorder";
//			}
			newAsin.valign = "top";
			newAsin.id="contextasin"+asin;
			var asinMarkup = "";
			var myThingsMarkUp = this.getMyThingsMarkup(asin);
			var priceMarkup = this.getPriceMarkup(listprice, price);
			asinMarkup += "<div class='result' onclick=\"javascript:changeezpage(" + asin + ");\"><center><img class=\"prodImg\" src=\"" + image + "\" width='90' height='60'><br>" + myThingsMarkUp + priceMarkup + "</center>";		
			
			this.numberAsins++;
			
			newAsin.innerHTML = asinMarkup;
			row.appendChild(newAsin);
			
		}
		
	}, Control.CScroller.prototype);

Control.MyThingsScroller = Class.create();
Control.MyThingsScroller.prototype = Object.extend({ 
		initialize: function(sName, sHome, sSize) {
			var myMarkup = "";
			this.size = 8;
			if( sSize < 8 ) {
				this.size = sSize;
			}
			myMarkup += "<tr><td colspan='3' id=\"size-bar-" + sName + "\"><span class=\"contextAmount floatRight\">"+unescape(sName)+" showing <span id=\"lbound-" + sName + "\">1</span>-<span id=\"ubound-" + sName + "\">"+this.size+"</span> of <span id=\"size-" + sName + "\">" + sSize + "</span></span><a class=\"contextLink\" href=\"/mythings\"><img src=\""+jsImg.getImagePath("return_to_saved")+"\"></a></td></tr>";
			this._initialize(sName, sHome, myMarkup);
			this.itemWidth = 93;
			this.numberFilled = 0;
			this.numberAsins = 0;
			this.totalSize = sSize;
			
			if( this.totalSize > this.size ) {
				$("left-image-" + this.name).style.display = "inline";
				$("right-image-" + this.name).style.display = "inline";
			}
			this.fillAsins();
		},
		
		addAsin: function( asin, image, listprice, price ) {

			var row = document.getElementById("content-" + this.name);
			var newAsin = document.createElement("td");
			
			newAsin.className = "result";
			if( typeof previouscontextasin != "undefined" ) {
				if (asin == previouscontextasin)
					newAsin.className = "resultWithBorder";
			}
			newAsin.valign = "top";
			newAsin.id="contextasin"+asin;
			var asinMarkup = "";
			var myThingsMarkUp = this.getMyThingsMarkup(asin);
			var priceMarkup = this.getPriceMarkup(listprice, price);
			asinMarkup += "<div class='result' onclick=\"detailMan.publish('', 'updateDetailPage', '" + asin + "');\"><center><img class=\"prodImg\" src=\"" + image + "\"><br>" + priceMarkup + "</center>";		

			this.numberAsins++;
			
			newAsin.innerHTML = asinMarkup;
			row.appendChild(newAsin);
			
		},

		fillAsins : function() {
			this.numberFilled++;
			for( var i = 0; i < mythingsResponse.items.length; i++ ) {
				this.addAsin(mythingsResponse.items[i].item, this.filterImage(mythingsResponse.items[i].image), mythingsResponse.items[i].listprice, mythingsResponse.items[i].price); 
			}				
		}	
		
	}, Control.CScroller.prototype);

Control.RecommendationScroller = Class.create();
Control.RecommendationScroller.prototype = Object.extend({

		initialize: function(sName, sHome, asins) {
			this.asins = asins;
			var wSize = 5;
			if( recommendationResponse.asinList.length < 5 )
				wSize = recommendationResponse.asinList.length;
			var myMarkup = "";
			myMarkup += "<tr><td colspan='3'  id=\"size-bar-" + sName + "\"><span class=\"contextAmount floatRight\">"+unescape(sName)+" showing <span id=\"lbound-" + sName + "\">1</span>-<span id=\"ubound-" + sName + "\">" + wSize + "</span> of <span id=\"size-" + sName + "\">"+recommendationResponse.asinList.length+"</span></span></td></tr>";
			this._initialize(sName, sHome, myMarkup);
			this.itemWidth = 120;
			this.numberFilled = 0;
			this.numberAsins = 0;
			this.totalSize = recommendationResponse.asinList.length;
			this.size = wSize;
			if( this.totalSize > this.size ) {
				$("left-image-" + this.name).style.display = "inline";
				$("right-image-" + this.name).style.display = "inline";
			}
			this.fillAsins();
		},
		
		fillAsins : function() {
			this.numberFilled++;
			for( var i = 0; i < recommendationResponse.asinList.length; i++ ) {
				this.addAsin(recommendationResponse.asinList[i].asin, recommendationResponse.asinList[i].thumbnailImageURL, recommendationResponse.asinList[i].title, recommendationResponse.asinList[i].buyingPrice); 
			}		
		},
		
		addAsin: function( asin, image, title, price ) {
			var row = document.getElementById("content-" + this.name);
			var newAsin = document.createElement("td");
			
			newAsin.className = "result";
			if( typeof previouscontextasin != "undefined" ) {
				if (asin == previouscontextasin)
					newAsin.className = "resultWithBorder";
			}
			newAsin.valign = "top";
			newAsin.id="contextasin"+asin;
			var asinMarkup = "";
			var myThingsMarkUp = this.getMyThingsMarkup(asin);
			asinMarkup += "<div class='result' onclick=\"window.open('/dp/"+asin+"?fromPage=asinlist&contextTitle=Recommendation&asins="+this.asins+"','_self');\"><center><img class=\"prodImg\"  src=\"" + image + "\"><br>" + myThingsMarkUp + "<span class=\"title\">" + title + "</span><br><span class=\"price\">" + price + "</span></center>";			
			
			this.numberAsins++;
			
			newAsin.innerHTML = asinMarkup;
			row.appendChild(newAsin);
			
		}
		
	}, Control.CScroller.prototype);
	
Control.AccessoryScroller = Class.create();
Control.AccessoryScroller.prototype = Object.extend({

		initialize: function(sName, sHome, asins) {
			this.asins = asins;
			this.itemWidth = 93;
			this.numberFilled = 0;
			this.numberAsins = 0;
			this.totalSize = jsonText.accessory.accessaries.length;
			var myMarkup = "";
			myMarkup += "<tr><td colspan='3' style=\"display: none;\" id=\"size-bar-" + sName + "\"><span style=\"display: none;\" id=\"lbound-" + sName + "\"></span><span style=\"display: none;\" id=\"ubound-" + sName + "\"></span><span style=\"display: none;\" id=\"size-" + sName + "\"></span></td></tr>";		
			this._initialize(sName, sHome, myMarkup);
			this.size = 3;
			if( this.totalSize >= this.size ) {
				$("left-image-" + this.name).style.display = "inline";
				$("right-image-" + this.name).style.display = "inline";
			}
			this.fillAsins();
		},
		
		fillAsins : function() {
			this.numberFilled++;		
			for( var i = 0; i < jsonText.accessory.accessaries.length; i++ ) {
				this.addAsin(jsonText.accessory.accessaries[i].asin, jsonText.accessory.accessaries[i].image, jsonText.accessory.accessaries[i].title, jsonText.accessory.accessaries[i].listprice); 
			}		
		
		},
		
		addAsin: function( asin, image, title, price ) {

			var row = document.getElementById("content-" + this.name);
			var newAsin = document.createElement("td");
			
			newAsin.className = "result";
			if( typeof previouscontextasin != "undefined" ) {
				if (asin == previouscontextasin)
					newAsin.className = "resultWithBorder";
			}
			newAsin.valign = "top";
			newAsin.id="contextasin"+asin;
			var asinMarkup = "";
			var myThingsMarkUp = this.getMyThingsMarkup(asin);
			
			asinMarkup += "<div class='result' onclick=\"window.open('/dp/"+asin+"?fromPage=asinlist&contextTitle=Accessory&asins="+this.asins+"&ref=" + detailController.jsonText.currentAsin +"','_self');\"><center><img class=\"prodImg\"  src=\"" + image + "\"><br>" + myThingsMarkUp + "<span class=\"title\">" + title + "</span><br><span class=\"price\">" + price + "</span></center>";			
			
			this.numberAsins++;
			
			newAsin.innerHTML = asinMarkup;
			row.appendChild(newAsin);
			
		}
				
	}, Control.CScroller.prototype);
	
	Control.CartScroller = Class.create();
Control.CartScroller.prototype = Object.extend({ 
		initialize: function(sName, sHome, sSize) {
			var myMarkup = "";
			this.size = 8;
			if( sSize < 8 ) {
				this.size = sSize;
			}
			myMarkup += "<tr><td colspan='3' id=\"size-bar-" + sName + "\"><span class=\"contextAmount floatRight\">"+unescape(sName)+" showing <span id=\"lbound-" + sName + "\">1</span>-<span id=\"ubound-" + sName + "\">"+this.size+"</span> of <span id=\"size-" + sName + "\">" + sSize + "</span></span><a class=\"contextLink\" href=\"/shoppingcart\"><img src=\""+jsImg.getImagePath("return_to_cart")+"\"></a></td></tr>";
			this._initialize(sName, sHome, myMarkup);
			this.itemWidth = 93;
			this.numberFilled = 0;
			this.numberAsins = 0;
			this.totalSize = sSize;
			
			if( this.totalSize > this.size ) {
				$("left-image-" + this.name).style.display = "inline";
				$("right-image-" + this.name).style.display = "inline";
			}
			this.fillAsins();
		},
		
		addAsin: function( asin, image, listprice, price ) {

			var row = document.getElementById("content-" + this.name);
			var newAsin = document.createElement("td");
			
			newAsin.className = "result";
			if( typeof previouscontextasin != "undefined" ) {
				if (asin == previouscontextasin)
					newAsin.className = "resultWithBorder";
			}
			newAsin.valign = "top";
			newAsin.id="contextasin"+asin;
			var asinMarkup = "";
			var CartMarkUp = this.getMyThingsMarkup(asin);
			var priceMarkup;
			if ( listprice!=price) 
				priceMarkup = this.getPriceMarkup(listprice, price);
			else
				priceMarkup = "<span class='price'>$" + price + "</span>";
			asinMarkup += "<div class='result' onclick=\"detailMan.publish('', 'updateDetailPage', '" + asin + "');\"><center><img class=\"prodImg\" src=\"" + image + "\"><br>"+ CartMarkUp + priceMarkup + "</center>";		

			this.numberAsins++;
			
			newAsin.innerHTML = asinMarkup;
			row.appendChild(newAsin);
			
		},

		fillAsins : function() {
			this.numberFilled++;
			for( var i = 0; i < cartResponse.length; i++ ) {
				this.addAsin(cartResponse[i].asin, this.filterImage(cartResponse[i].image), cartResponse[i].listprice.toFixed(2), cartResponse[i].price.toFixed(2)); 
			}				
		}	
		
	}, Control.CScroller.prototype);
