if (typeof(objYtimes)=='undefined')
  objYtimes = new Object();

objYtimes.showCompareButtonWhen = ""; // if set to always, the compare button is always shown.
if (typeof(objYtimes.compareSelected)=='undefined')
	objYtimes.compareSelected = "<img border=0 src=/lib/yhst-17116217969881/objYtimes.compareSelected.png>";

jQuery(window).unload( function() {
  var compare = GetCookie("compare");
  if (compare == null)
    compare = "";

  if (compare == "")
    compare = new Array();
  else  
    compare = compare.split(";");
  
  jQuery(".compare").each( function() {
    if (this.checked)
    {
        if (jQuery.inArray(this.value,compare) == -1)
        {
            compare[compare.length] = this.value;
        }
    }
    else
    {
        var j = inArray(this.value,compare);
        if (j > -1)
        {
            compare.splice(j,1);
        }
    }
  });
  if (compare.length > 0)
  {
    compare = compare.join(";");
    if (compare != "")
        SetCookie("compare",compare);
    else
        DeleteCookie("compare");
  }
  else
  {
    DeleteCookie("compare");
  }
});

jQuery(document).ready( function() {
    setCompareButton();
    if (objYtimes.showCompareButtonWhen == "always")
        showCompareButton();
        
    jQuery(".compare").bind("click",function() {
        // if either the compare cookie is set or there is a compare checkbox on the page thaht's checked
        // then show the compare button
        var compare = GetCookie("compare");
        compare = (compare != null && compare != "");
        
        var oneChecked = false;
        jQuery(".compare").each( function() {
            if (this.checked)
            {
                oneChecked = true;
                return;
            }
        });
        
        compare |= oneChecked;
        
        if (compare || objYtimes.showCompareButtonWhen == "always")
        {
            showCompareButton();
        }
        else
        {
            jQuery("#compButton").remove();
        }
    });
});

function inArray(val, arr)
{
  for (var i = 0; i < arr.length; i++)
  {
    if (arr[i] == val)
    {
        return(i);
    }
  }
  return(-1);
}

function showCompareButton()
{
    if (jQuery(".compButton").length == 0 && jQuery(".compare").length > 0)
    {
        if (jQuery(".prodCatHead").length > 0)
        {
            jQuery(".prodCatHead").append("<a href=compare.html class=compButton>" + objYtimes.compareSelected + "</a>")
                                  .css("background-color", "#d5d5d5")
                                  .css("vertical-align", "middle")
                                  .css("padding", "5px");
        }
        else
            jQuery("#section-contenttitle:first").before("<a href=compare.html class=compButton>" + objYtimes.compareSelected + "</a>");
    }

    jQuery(".compButton").click( function() {
        var compare = GetCookie("compare");
        compare = (compare != null && compare != "");
        
        var oneChecked = false;
        jQuery(".compare").each( function() {
            if (this.checked)
            {
                oneChecked = true;
                return;
            }
        });
        
        compare |= oneChecked;
        
        if (compare)
        {
            return(true);
        }
        else
        {
            alert("Checkmark one or more items and click \"Compare Selected\" to show them side by side.");
            return(false);
        }
        
    });
}

function setCompareButton()
{
  var compare = GetCookie("compare");
  if (compare != null && compare != "")
  {
    compare = compare.split(";");
    if (compare.length > 0)
    {
        jQuery(".compare").each( function() {
            if (inArray(this.value,compare) > -1)
            {
                this.checked = true;
            }
        });
        showCompareButton();
    }
  }
}
