// Developed for HorizonHobby, Last Updated: November 12, 2007
// Javascript code to show Recommendations 

// baynote_showGuide
//	Displays Baynote Guide based on the context. guideType is a required parameter and all 
//	other parameters are optional. If pageType cannot be inferred from url, then you must 
//	pass pageType as a parameter. 
//
// parameters:
//  guideType - Following values are supported
//                            Product - shows product recommendation 
//                            Article - shows article recommendation 
//  pageType - Following values are supported. If not specified, pageType is inferred from url. 
//                          StoreFront 
//                          ProductSearch 
//                          ArticleSearch
//                          ProductDetail 
//  category - Category code that the user has narrowed search to. Empty string if no category is selected.                        
//  brand - Brand code that the user has narrowed search to. Empty string if no brand is selected. 
//  site - Site code that the user has narrowed search to. Empty string if no site is selected. 
//  searchTerm - search Term entered by the user. Empty string if no search term is entered. 
//  priceRange - price range if any selected specified in the following format: 
//                        Under $15
//                        $15 - $25
//                        $25 - $50
//                        Over $50 
// 
//  return - none
//
function baynote_showGuide( guideType, pageType, 
   searchTerm, category, brand, site, 
   priceRange)
{
	if (!baynote_isDefined(guideType))
		guideType="Product"; //assume default of "Product"
	if (!baynote_isDefined(pageType))
		pageType = baynote_getPageType(); //infer from page Url. 
	var attrFilter = "";
	
	//For product guides, add category, brand, site and price filters
	if (guideType == "Product") {
	  if (baynote_isDefined(category)) {
	    if (category.length == 1)
	      attrFilter += "FirstLevelCategory:" + category;
	    else //pick the first 2 characters in category 
	      attrFilter += "SecondLevelCategory:" + category.substring(0, 2);
	  }
	  if (baynote_isDefined(brand)) 
	    attrFilter += ",Brand:" + brand;
	  if (baynote_isDefined(site)) 
	    attrFilter += ",Site:" + site;
	  if (baynote_isDefined(priceRange))
	  {
	    attrFilter += ",Price:" + baynote_convertPriceRange(priceRange);
	  }
	  if (attrFilter.charAt(0) == ",")
	    attrFilter = attrFilter.substring(1);	  
	} //end if (guideType == "Product")
	
	//set the class name for the enclosing div based on guideType and pageType. 
	baynote_set_GuideClassName(guideType, pageType);
	baynote_tag.server = "http://horizonhobby-www.baynote.net";
	baynote_tag.customerId = "horizonhobby";
	baynote_tag.code = "www";
	baynote_tag.type = "guide";
	baynote_tag.format = "results-products"
	baynote_tag.guide = pageType + guideType;	
	baynote_tag.attrFilter = attrFilter;
	if (baynote_isDefined(searchTerm))
		baynote_tag.query = searchTerm;

	//If this is in test or dev. environment, set url so that we can see guides on ProductDetail page
	if ( pageType == "ProductDetail" ) {
	  baynote_tag.url = baynote_getCorrectedUrl();
	}	  

	baynote_tag.show();
}

// Returns a string that describes the type of the page.  Used to determine which guide to show
function baynote_getPageType() {
	var pageUrl = baynote_getPageUrl(); 
	if ( (typeof(pageUrl) == "undefined") || (pageUrl == null) || (pageUrl == ""))
	  pageUrl = window.location.href;
	var pageType = "";
	if (pageUrl.indexOf("Products/Default.aspx?ProdID=") > -1) {
		pageType = "ProductDetail"; 
	} else if (pageUrl.indexOf("Stores/Default.aspx?StoreCatId") > -1) {
		pageType = "StoreFront";
	} else if (pageUrl.indexOf("Search/Articles.aspx?") > -1) {
		pageType = "ArticleSearch";
	} else if (pageUrl.indexOf("Search/Default.aspx?") > -1) {
		pageType = "ProductSearch";
	} else if (pageUrl == "http://www.horizonhobby.com" || pageUrl == "http://www.horizonhobby.com/") {
		pageType = "Home";
	} 	
	
	return pageType;
}

function baynote_getSearchTerm() {
  var searchTerm = baynote_getUrlParam("Ntt");
  return unescape(searchTerm);
}

// Set the class name for the enclosing div with id="bn_product" or id="bn_article". 
function baynote_set_GuideClassName(guideType, pageType)
{
   var className = "bn_g_" + pageType.toLowerCase() + guideType.toLowerCase();
   var elementId = "bn_" + guideType.toLowerCase();
   var element = document.getElementById(elementId);
   if (element)
     element.className = className;
}

// Helper function to get the value of a parameter from URL. 
function baynote_getUrlParam(name) {
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var pageUrl = baynote_getPageUrl();
  var results = regex.exec( pageUrl );
  if( results == null )
    return "";
  else
    return results[1];
}

//Helper function to get current page url 
function baynote_getPageUrl() {
	var pageUrl = baynote_tag.url; 
	if ( !baynote_isDefined(pageUrl) )
	  pageUrl = window.location.href;
	return pageUrl;
}

// Helper function to determine whether a parameter is defined or not. 
function baynote_isDefined(param) {
  return (typeof(param) != "undefined") && (param != null) && (param != ""); 
}

function baynote_getCorrectedUrl() {
  var pageUrl = baynote_getPageUrl();
  //Repleace test and dev urls with www
  if (pageUrl.indexOf("http://www.hh.dv") == 0) 
    pageUrl = pageUrl.replace("http://www.hh.dv", "http://www.horizonhobby");
  else if (pageUrl.indexOf("http://www.hh.tst") == 0) 
    pageUrl = pageUrl.replace("http://www.hh.tst", "http://www.horizonhobby");
  
  //Replace url of Tabs with Main tab url. 
  if (pageUrl.indexOf("TechnicalSpecs.aspx") > 0)
    pageUrl = pageUrl.replace("TechnicalSpecs.aspx", "Default.aspx");
  else if (pageUrl.indexOf("Support.aspx") > 0)
    pageUrl = pageUrl.replace("Support.aspx", "Default.aspx");
  else if (pageUrl.indexOf("PhotoGallery.aspx") > 0)
    pageUrl = pageUrl.replace("PhotoGallery.aspx", "Default.aspx");  
  else if (pageUrl.indexOf("RelatedPartsSummary.aspx") > 0)
    pageUrl = pageUrl.replace("RelatedPartsSummary.aspx", "Default.aspx");
  return pageUrl;
}

function baynote_convertPriceRange(priceRange)
{
  var priceFrom;
  var priceTo;
  var index; 
  if (priceRange.indexOf("Under") != -1)
  {//starts with Under 
    index = priceRange.indexOf("$");
	if (index > 0)
      priceTo = priceRange.substring(index+1);
  }
  else if (priceRange.indexOf("Over") != -1)
  {
    index = priceRange.indexOf("$");
	if (index > 0)
      priceFrom = priceRange.substring(index+1);
  }
  else if (priceRange.indexOf("-") != -1)
  {
    index = priceRange.indexOf("-");
	var index2 = priceRange.indexOf("$");
	if (index2 != -1 && index2 < index)
	  priceFrom = priceRange.substring(index2+1, index);
	index2 = priceRange.indexOf("$", index);
	if (index2 != -1 && index2 > index)
	  priceTo = priceRange.substring(index2+1);
  }
  if (!baynote_isDefined(priceFrom) && !baynote_isDefined(priceTo))
    return "";
  var priceFilter="";
  if (baynote_isDefined(priceFrom))
  {
    priceFrom = baynote_trim(priceFrom);
    priceFilter = priceFrom;
  }
  priceFilter += "..";  
  if (baynote_isDefined(priceTo)) 
  {
    priceTo = baynote_trim(priceTo);
    priceFilter += priceTo;
  }
  return priceFilter; 
}

// Trim all white spaces from left and right. 
function baynote_trim(str)
{
	for(var k = 0; k < str.length && baynote_isWhitespace(str.charAt(k)); k++);
	for(var j=str.length-1; j>=0 && baynote_isWhitespace(str.charAt(j)) ; j--);
	return str.substring(k,j+1);
}
// Is the given character a white space? 
function baynote_isWhitespace(ch) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(ch) != -1);
}

