/* Contents
      popup function
      Minqty functions (add, clean up)
	  Leftnav visibility toggle function
	  Items added to cart functions
	  Minicart Function
	  Google custom search
*/

// Function for poupup window (posters, ig, oe, dr_d)
function popup(url,w,h) {
  window.open('http://www.woodburnpress.com'+url, 'foo', 'width='+w+',height='+h+',left=50,top=50,status=no,resizable=yes,scrollbars=yes')
}

// Toggle visibility of leftnav menu items
function togglevis(id){
  var x = document.getElementById(id);
  if(x.style.display=="none"){
    x.style.display="block";
    x.backgroundPosition="top";
  } else { x.style.display="none"; }
}

// Functions for "Item(s) added to cart." message.
function compareurl() {
	addedurl = get_cookie ( "addedurl" );
	if (addedurl != location.href) { document.cookie = "addedurl=blank;path=/;"; }
}

function addurl() { document.cookie = "addedurl=" + location.href + ";path=/;"; 
}

function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

// Minicart Function
function DisplayMiniCart(name,style) {
  var cookies=document.cookie;  //read in all cookies
  var start = cookies.indexOf(name + "=");  //set start to beginning of ss_cart cookie
  var cartvalues = "";
  var linecount = 0;
  var tmp;

  // Start Output
  document.write("<a href=\"http://www.woodburnpress.com/cgi-woodburnpress/sb/order.cgi?storeid=*120de3ac4fd106768c50f2&function=show\">");
  document.write("<img src=\"http://www.woodburnpress.com/media/themesmedia/cart-white.gif\" border=\"0\" name=\"cart\" align=\"top\">");
  document.write("<\/a>&nbsp;");
  document.write("<a href=\"http://www.woodburnpress.com/cgi-woodburnpress/sb/order.cgi?storeid=*120de3ac4fd106768c50f2&function=show\">");
  document.write("View Cart/Checkout");
  if (start == -1) {document.write(": $0");}  //No cart cookie - $0
  else   //cart cookie is present
  {
    start = cookies.indexOf("=", start) +1;  
    var end = cookies.indexOf(";", start);  
    if (end == -1)
    {
      end = cookies.length;
    }
    cartvalues = unescape(cookies.substring(start,end)); //read in just the cookie data

    start = 0;
    while ((start = cartvalues.indexOf("|", start)) != -1)
    {
      start++;
      end = cartvalues.indexOf("|", start);
      if (end != -1)
      {
        linecount++;
        if (linecount == 3)  // Product Subtotal
        {
          if ((style == "Subtotal") || (style == "Summary"))
          {
            tmp = cartvalues.substring(start,end);
            colon = tmp.indexOf(":", 0);
			document.write(": ");
			document.write(tmp.substring(colon+1,end - start));
          }
        }
        start = end;
      }
      else
        break;
    } // end while loop
  }
  document.write("<\/a>");
}

// Google Watermark Funtionality on/off
// http://www.handymanhowto.com/2009/01/03/google-custom-search-integration-with-woo-themes-freshnews/ 
// http://www.google.com/cse/docs/cref.html
// Turn off onfocus
function blankfield() {
  document.searchform.q.style.backgroundImage = "none";
}
// Turn on onblur
function watermark() {
  if (document.searchform.q.value == '') {
  document.searchform.q.style.background = "rgb(255, 255, 255) url(/images/cse-watermark.gif) no-repeat scroll left center";
}}
