/**
 * @package Product Comparison
 * @author Keith Bluhm <keithb@solidcactus.com>
 * @copyright Copyright (c) 2007 Solid Cactus
 * @version 1.1
 */

function SC_Compare()
{
	this.compareLimit      = compareLimit;
	this.compareCacheLife  = compareCacheLife;
	this.compareDisclaimer = compareDisclaimer;
	this.compareVars       = compareVars;
	this.useWrite          = false;
	this.submitText        = 'Compare Selected Products';
	this.compareText       = 'Compare'
	this.submitAlert       = 'Please select between 2 and {limit} products to compare.';
	this.compareAlert      = 'You may only compare up to {limit} items.';
}

SC_Compare.prototype.useWrite = function()
{
	this.useWrite = true;
}

SC_Compare.prototype.useReturn = function()
{
	this.useWrite = false;
}

SC_Compare.prototype.countChecked = function()
{
	var inputs     = document.getElementsByTagName( 'input' );
	var inputsLen  = inputs.length;
	var numChecked = 0;
	for ( i = 0; i < inputsLen; i++ )
	{
		if ( inputs[i].className == 'compare-checkbox' )
		{
			if ( inputs[i].checked )
			{
				numChecked++;
			}
		}
	}
	return numChecked;
}

SC_Compare.prototype.submit = function()
{
	var checked = this.countChecked();
	if ( checked < 2 )
	{
		alert( this.submitAlert.replace( '{limit}', this.compareLimit ) );
	}
	else
	{
		document.forms['comparison'].submit();
	}
}

SC_Compare.prototype.limitCheck = function( self )
{
	var checked = this.countChecked();
	if ( checked > this.compareLimit && self.checked )
	{
		this.compareAlert = this.compareAlert.replace( '{limit}', this.compareLimit );
		alert( this.compareAlert );
		self.checked = false;
	}
}

SC_Compare.prototype.formStart = function( action, text )
{
	var text = ( text == null ) ? this.submitText : text;
	var formStart = '';
	if ( this.compareLimit > 1 )
	{
		formStart += '<form name="comparison" id="comparison" action="' + action + '" method="post">\n';
		formStart += '<input type="hidden" name="compareVars" value="' + this.compareVars + '" />\n';
		formStart += '<input type="hidden" name="compareCacheLife" value="' + this.compareCacheLife + '" />\n';
		formStart += '<input type="hidden" name="compareDisclaimer" value="' + this.compareDisclaimer + '" />\n';
		formStart += '<input type="hidden" name="return" value="' + window.location + '" />\n';
		formStart += '<div class="compare-button">';
		formStart += '<a href="javascript:sc_compare.submit();">' + text + '</a>';
		formStart += '</div>\n';
	}
	if ( this.useWrite )
	{
		document.write( formStart );
	}
	else
	{
		return formStart;
	}
}

SC_Compare.prototype.formEnd = function( text )
{
	var text = ( text == null ) ? this.submitText : text;
	var formEnd = '';
	if ( this.compareLimit > 1 )
	{
		formEnd += '<div class="compare-button">';
		formEnd += '<a href="javascript:sc_compare.submit();">' + text + '</a>';
		formEnd += '</div>\n';
		formEnd += '</form>\n';
	}
	if ( this.useWrite )
	{
		document.write( formEnd );
	}
	else
	{
		return formEnd;
	}
}

SC_Compare.prototype.checkbox = function( currID, text )
{
	var currID   = ( currID == null ) ? '' : currID;
	var text     = ( text == null ) ? this.compareText : text;
	var checkBox = '';
	if ( this.compareLimit > 1 && currID != '' )
		checkBox = '<div class="compare-label"><input type="checkbox" class="compare-checkbox" id="compare-' + currID + '" name="compareIDs[]" value="' + currID + '" onclick="sc_compare.limitCheck( this );" /> <a href="javascript:sc_compare.submit();">' + text + '</a></div>';
	if ( this.useWrite )
	{
		document.write( checkBox );
	}
	else
	{
		return checkBox;
	}
}

/**
 * Instantiate class
 */
var sc_compare = new SC_Compare();
