var css_ref_id;
if (css_ref_id == null) css_ref_id=1;

function ShopWidget()
{
	this.mode = "none";
	this.linkin = 0;
	this.width = 0;
	this.height = 0;
	this.backgroundColor = "#ffffff";
	this.linkColor = "#0000ff";
	this.fontColor = "#606060";
	this.borderColor = "#ff9b00";
	this.font = "Arial";
	this.url = "http://widgets.shopping.com/widget";

	// Keyword Mode
	this.keyword = "";

	// Top Products Mode
	this.categoryId = -1;

	this.isValid = false;
}

ShopWidget.prototype.validate = function()
{
	// Is this a valid component
	this.isValid = false;

	try
	{
		// Size Validations
		var validSize = false;
		if ((this.width == 120) && (this.height == 600)) validSize = true;
		if ((this.width == 160) && (this.height == 600)) validSize = true;
		if ((this.width == 728) && (this.height == 90)) validSize = true;
		if (!validSize)
			throw "Invalid Size: " + this.width + "x" + this.height;

		// Validate each modes parameters
		var validMode = false;
		if (this.mode == "kw")
		{
		    // Keyword
		    validMode = true;
		    if (this.keyword.length > 80) this.keyword = this.keyword.substr(0,80);
		}
		else if (this.mode == "tp" || this.mode == "sdctp")
		{
		    // Top Products
		    validMode = true;
		}
		else {
		    validMode = true;
		    this.mode = "kw";
		}		    

		if (!validMode)
			throw "Invalid Mode: " + this.mode;

		// Linkin ID will be handled in the API

		// If we have a size and mode, we can render an ad.  
		this.errorMessage = null;
		this.isValid = true;
		return true;
	}
	catch(e)
	{
		this.errorMessage = e;
		this.isValid = false;
		return false;
	}
}

ShopWidget.prototype.render = function()
{
	this.validate();
	if (!this.isValid) return;

	var widgetUrl = this.url;
	widgetUrl += "/linkin_id-" + encodeURIComponent(this.linkin);

	if (this.mode == "kw")
	{
		if (this.categoryId > 0)
		{
			widgetUrl += "/SF-7";
			widgetUrl += "/BEFID-" + encodeURIComponent(this.categoryId);
		}

		if ((this.keyword != null) && (this.keyword.length > 0))
			widgetUrl += "/keyword-" + encodeURIComponent(this.keyword);
	}

	if (this.mode == "tp" || this.mode == "sdctp")
	{
		if (this.mode == "tp")
		    widgetUrl += "/TP-3";
		else
		    widgetUrl += "/TP-4";

		// Append the category
		if ((this.categoryId != null) && (this.categoryId != -1))
			widgetUrl += "/BEFID-" + encodeURIComponent(this.categoryId);
	}

	widgetUrl += "/width-" + encodeURIComponent(this.width);
	widgetUrl += "/height-" + encodeURIComponent(this.height);
	widgetUrl += "/cssprefix-shopwidget" + encodeURIComponent(css_ref_id++);
	
	widgetUrl += "/bordercolor-" + encodeURIComponent(this.borderColor);
	widgetUrl += "/linkcolor-" + encodeURIComponent(this.linkColor);
	widgetUrl += "/fontcolor-" + encodeURIComponent(this.fontColor);
	widgetUrl += "/bgcolor-" + encodeURIComponent(this.backgroundColor);
	widgetUrl += "/font-" + encodeURIComponent(this.font);
	if (this.language != null) {
	    widgetUrl += "/language-" + encodeURIComponent(this.language);
	}

	document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + widgetUrl + "\" charset=\"UTF-8\">  <" + "/sc" + "ript>");

}

