// JavaScript Document
// set some default values
var catalog_page;
var products = new Array();
var firstload = 1;
// Fetch random
var rand_list = new Array();

// get a cookie val
function getCookieVal(name)
{
	var cookieFound = false;
	var start = 0;
	var end = 0;
	var i = 0;
	while(i <= document.cookie.length)
	{
		start = i;
		end = start + name.length;
		if (document.cookie.substring(start,end) == name)
		{
			cookieFound = true;
			break;
		}
		++i;
	}
	if (cookieFound)
	{
		start = end + 1;
		end = document.cookie.indexOf(';', start);
		if (end < start)
		{
			end = document.cookie.length;
		}
		return document.cookie.substring(start,end);
	}
	return '';
}
// the object for offsite catalogs
function product(id, n, p, i, merch_link, merch_name)
{
	this.pid = id;
	this.name = n;
	this.price = p;
	this.imageURL = i;
	this.image = new Image();
	this.image.src = this.imageURL;
	this.merchant_link = merch_link;
	this.merchant_name = merch_name;
}

//display a catalog
function displayCatalog(pg)
{
	if(rand_list.length == 0)
	{
		rand_list = random_array(products.length);
	}
	catalog_page = getCookieVal('catPage');
	if (catalog_page == '')
	{
		catalog_page = 1;
	}
	if ((pg != null) && (pg != ''))
	{
		calcpage = catalog_page;
		switch(pg)
		{
			case 9:
				calcpage = document.pager.CATALOGPAGE.selectedIndex + 1;
				break;
			case 1:
				++calcpage;
				if (calcpage > catPages)
				{
					calcpage = catPages;
				}
				break;
			case -1:
				--calcpage;
				if (calcpage < 1)
				{
					calcpage = 1;
				}
				break;
		}
		catalog_page = calcpage;
	}
	document.cookie = 'catPage=' + catalog_page;
	var i = (catalog_page - 1) * (col * row);
	var cellID = 0;
	var outstr = '';
	var htmlText = '';
	var cc = 0;
	if (firstload)
	{
		// Set div width (get div width from style_os)
		var div_width = col * 190;
    	htmlText = '<div name="main_block" id="main_block" style="width: ' + div_width + 'px;">';
	}
	var productLink;
	var imgLink;
	var nameLink;
	var productPrice;
	var productMerchantLink;
	var key;
	
	while (cellID < (row * col))
	{
		if (i < products.length)
		{	
			key = rand_list[i];
			productLink = '"http://www.clickshopsave.com/results.php?productId=' + products[key].pid + '"';
			imgLink = '<a href=' + productLink + '><img border="0" src="' + products[key].imageURL + '"></a>';
			nameLink = '<a href=' + productLink + '>' +products[key].name + '</a>';
			productPrice = '$' + products[key].price;
			if(typeof products[key].merchant_link != 'undefined') {
				productMerchantLink = '<a href="' + products[key].merchant_link +'">' + products[key].merchant_name + "</a><br />";
			}
			else {
				productMerchantLink = '';
			}
		}
		else
		{
			productLink = '&nbsp;';
			imgLink = '&nbsp;';
			nameLink = '&nbsp;';
			productPrice = '&nbsp;';
			productMerchantLink = '&nbsp;';
		}
		outstr = '<div class="prod_thumb_os"><div class="prod_thumb_img_os">';
		outstr += imgLink;
		outstr += '</div>';
		outstr += '<div class="prod_thumb_descr_os"><div class="prod_thumb_title_os">';
		outstr += nameLink;
		outstr += '</div>';
		outstr += productMerchantLink;
		outstr += '<div class="prod_price_os">';
		outstr += productPrice;
		outstr += '</div>';
		outstr += '<br /></div></div>';
		htmlText += outstr;
		++cc;
		if (cc == col)
		{
			htmlText += '<br clear="all" /><hr size="1" color="#E1E2E8" />';
			cc = 0;
		}
		++i;
		++cellID;
	}
	
	// bla
	if(firstload)
	{
		document.write(htmlText);
		htmlText = '';
	}

	if (catPages > 1)
	{
		var selectText = '<select name="CATALOGPAGE" id="CATALOGPAGE" onChange="displayCatalog(9)">';
		for (i=1; i < catPages + 1; i++)
		{
			selectText += '<option value="' + i + '"';
			if (i == catalog_page)
			{
				selectText += ' selected="selected"';
			}
			selectText += '>' + i + '</option>';
		}
		selectText += '</select>';
		htmlTextNav = '<div id="pagectrl" name="pagectrl" class="page_ctrl_os"><form name="pager" id="pager">';
		htmlTextNav += '<a id="prevLink" class="linkBox_os" href="javascript:displayCatalog(-1)">&#171;Previous</a>';
		htmlTextNav += selectText;
		htmlTextNav += '<a id="nextLink" class="linkBox_os" href="javascript:displayCatalog(1)">Next&#187;</a>';
		htmlTextNav += '</form></div>';
		htmlText += htmlTextNav;
	}

	// Add logo text
	// This is where we insert the logo
	if(typeof merchant_link == 'undefined')	{merchant_link = 'http://www.clickshopsave.com'}
	var logoText = '<div style="width: 100%; background-color: #E0E0E0; height: 31px; text-align: right;"><a href="' + merchant_link + '"><img src="http://www.clickshopsave.com/images/css_logo_js_incl.gif" width="203" height="31" border="0" alt="Click Shop Save" /></a></div>';
	htmlText += logoText;
	
	if (firstload)
	{
		htmlText += '</div>';
		document.write(htmlText);
	}
	else
	{
		document.getElementById("main_block").innerHTML=htmlText;
	}
	
	
	firstload = 0;

	
}

// Create an array of random digits, used to make displayCatalog random
function random_array(size)
{
	var numbers = new Array();
	var rand;
	var add = true;
	while(numbers.length < size)
	{
		rand=Math.floor(Math.random()*size);
		add = true;
		for(var i=0; i < numbers.length; i++)
		{
			if(numbers[i] == rand) add = false;
		}
		if(add == true)
			numbers.push(rand);
	}
	return numbers;
}
