/*
 *** This javascript library contains the methods for dealing with
 *** the product counter on the product lister and search results display
 *** pages 
 *
 *  author: Mark B
*/

// default barWidth
var barWidth = "120";


// function to dictate the display bar width - allows for different
// pages to use different bar widths.
function setWidthOfBar(width) {
	barWidth = width;
}


// function to write the counter cookie with the current number of
// product results.
function WriteCounterCookie( name, value, path ) {
  document.cookie = name + "=" + escape (value);
  + ( ( path ) ? "; path=" + path : "" );
}


// function to read the previous number of product results from the
// counter cookie
function ReadCounterCookie ( pCookieName ) {
  var returnValue = "";
  var cookies = document.cookie.split(";");
  for (var i=0; i<cookies.length; i++) {
    var cookieCrumbs = cookies[i].split("=");
    var cookieName = cookieCrumbs[0];
    var cookieValue = cookieCrumbs[1];
    // if the first character of the cookie name is a space, strip it
    if (cookieName.indexOf(" ") == 0) {
	  cookieName = cookieName.substring(1, cookieName.length);
	}
    if (cookieName.toUpperCase() == pCookieName.toUpperCase()) {
      returnValue = cookieValue;		
    }
  }  
  return returnValue;
}


// function to construct the html content that makes the product counter
// bar and text
function getInnerHtml(filledAmount, currentNumberOfRecords, previousNumberOfRecords) {
  var gifToLoad = "100.gif";
  var innerHtml = "";
  // determine which animated gif to display based on the filledAmount value
  if (filledAmount < 15) { gifToLoad = "10.gif"
  } else if (filledAmount < 25) { gifToLoad = "20.gif"
  } else if (filledAmount < 35) { gifToLoad = "30.gif"
  } else if (filledAmount < 45) { gifToLoad = "40.gif"
  } else if (filledAmount < 55) { gifToLoad = "50.gif"
  } else if (filledAmount < 65) { gifToLoad = "60.gif"
  } else if (filledAmount < 75) { gifToLoad = "70.gif"
  } else if (filledAmount < 85) { gifToLoad = "80.gif"
  } else if (filledAmount < 95) { gifToLoad = "90.gif"
  }   
  innerHtml = "<div style='width:"+(barWidth)+"px; float:left; background:#C881A3; padding:0 2px 0 0;'>" 
   			  + "<div style='float:right; width:"+barWidth+"px; background:url(/wcsstore/ConsumerDirect/images/counterBar/"+gifToLoad+") no-repeat top left;"
   			  + " text-align:right;'>"+currentNumberOfRecords+"</div></div>";
   // innerHtml = "<div style='width:"+barWidth+"px; float:left; background:#f2f2f2;'>"
   //		+ "<div style='float:right; background:#C881A3; text-align:right; padding:0 2px 0 2px; width:"+filledAmount+"%;'>"+currentNumberOfRecords+"</div></div>";
  if (previousNumberOfRecords.length > 0) {
  	innerHtml = innerHtml + "<div style='float:left;'>&nbsp;of&nbsp;"+previousNumberOfRecords+" products</div>" ;
  } else {
	innerHtml = innerHtml + "<div style='float:left;'>&nbsp;products</div>" ;
  }
  return innerHtml;
}


// function to determine the length of the 'products returned' bar
// conditionals added to ensure the bar is always long enough to contain
// the number of products returned text.
function getFilledAmount(currentNumberOfRecords, previousNumberOfRecords) {
	var filled = (currentNumberOfRecords / previousNumberOfRecords * 100);
	if (filled > 100) { filled = 100; }
	if (filled < 12) {
		if (currentNumberOfRecords > 999) { 
			filled = 10 * 4; 
		} else if (currentNumberOfRecords > 99) { 
			filled = 10 * 3; 
		} else if (currentNumberOfRecords > 9) {
			filled = 10 * 2;
		} else {
			filled = 12;
		}
	}
	return filled;
}

