/* Custom <select> tag object */
var selects = [];
var inputs = [];

var products = ['Aero-3','2oz Miracle','Triad','Smart Arch','Super Sport','Bottom of the Foot Cushion','Gel Toe Beds','Halo Heel','Callus Cushions',
'Corn Removers','Corn Cushions','Moleskin','Corn Wraps','Toe Relief Pad','Toe Protector','Toe Spacer','Toe Beds','Toe Bandages','Heel Rescue Cream',
'Toenail Nipper','Toenail Nipper with Rubber Handle','Corn & Callus Trimmer','Pro-Clearz','Flex-Tastic','Pump Pouches','Synovium','Ultra Gel Energizing Insoles','Goodnight Bunion','Callus Blaster'];

var online = [
{ store: "Walgreens",link: "http://www.walgreens.com/search/search_results.jsp?_dyncharset=ASCII&term=profoot&x=0&y=0" },
{ store: "Amazon", link: "http://www.amazon.com/s/qid=1272903855/ref=sr_pg_1?ie=UTF8&keywords=PROFOOT&bbn=3760901&rh=n%3A3760901%2Cn%3A!3760931%2Ck%3APROFOOT&page=1" },
{ store: "Drugstore", link: "http://www.drugstore.com/search/search_results.asp?N=0&Ntx=mode%2Bmatchallpartial&Ntk=All&Ntt=profoot&srchtree=1" },
{ store: "Medichest", link: "http://health-and-beauty.medichest.com/search?asug=&w=profoot" },
{store: "Rite Aid", link: "http://www.riteaidonlinestore.com/search/search.asp?searchtype=1&trx=28198&trxp1=10784&ipp=20&srchtree=5&search=profoot&Go.x=0&Go.y=0"} ];

var Select = function(obj,options)
{
	this.node = (typeof obj == 'object') ? obj : document.getElementById(obj);
	if(!this.node) return false;
	this.options = options;
	if(!this.options.type) this.options.type = 'online';
	if(!this.options.width) this.options.width = '204';

	/*modify variable 'online' for alternate parameters*/

	if(this.options.type == 'online') online.splice(4,1);
	if(this.options.type == 'online1') online = new Array(online[2]);
	if(this.options.type == 'online2') online = new Array(online[1], online[2], online[3], online[4]);
	if(this.options.type == 'online3') online = new Array(online[1], online[2], online[3], online[4]);
	if(this.options.type == 'online4') online = new Array(online[1], online[2], online[4]);
	if(this.options.type == 'online5') online = new Array(online[1]);

	/*-------------------------------------------------*/

	this.node.onclick = this.toggleSelect(this);
	this.node.style.width = this.options.width+"px";
	this.defaultValue = this.node.value;
	this.el = document.createElement('select');
	this.el.className='';
	this.el.style.width = this.options.width+5+"px";
	this.el.size = '6';
	this.el.style.display='none';
	this.el.id = this.node.id + "_select";
	this.node.parentNode.appendChild(this.el);
	selects.push(this.el);
	inputs.push(this.node);
	this.el.onchange = this.copyValue(this);
	
	switch(this.options.type)
	{
		case 'years':
			this.writeYears();
		break;
		case 'products':
			this.writeProducts();
		break;
		case 'online':
		case 'online1':
		case 'online2':
		case 'online3':
		case 'online4':
		case 'online5':
		case 'online_all':
			this.el.size = online.length + 1;
			this.writeOnline();
		break;
	}
}


Select.prototype.toggleSelect = function(obj)
{
	return function(e)
	{
		closeMenus(e);
		obj.el.style.display = obj.el.style.display == "none" ? "block" : "none";
	}
}

Select.prototype.copyValue = function(obj)
{	
	return function()
	{
		if(obj.options.type.match(/online/))
		{
			var sel = online[obj.el.selectedIndex-1];
			if(sel)
			{
				urchinTracker('/Retailers/'+sel.store+'.com');
				window.open(sel.link);
			}
			var value = obj.el[obj.el.selectedIndex].innerHTML;
		}
		else var value = obj.el[obj.el.selectedIndex].value;
		obj.node.value = value;
		if(!value) obj.node.value = obj.defaultValue;
		obj.el.style.display = 'none';
	}
}

Select.prototype.writeYears = function()
{
	var option = document.createElement('option');
	option.value = '';
	option.innerHTML = 'YYYY';
	this.el.appendChild(option);
	
	for(var i=1900; i<=2008; i++) 
	{
		option = document.createElement('option');
		option.value = option.innerHTML = i+'';
		this.el.appendChild(option);
	}
}

Select.prototype.writeProducts = function()
{
	var option = document.createElement('option');
	option.value = '';
	option.innerHTML = 'Select a product';
	this.el.appendChild(option);
	
	for(var i=0; i<products.length; i++)
	{
		option = document.createElement('option');
		option.value = option.innerHTML = products[i];
		this.el.appendChild(option)
	}
}

Select.prototype.writeOnline = function()
{
	var option = document.createElement('option');
	option.value = '';
	option.innerHTML = 'Select one';
	this.el.appendChild(option);
	for(var i=0; i<online.length; i++)
	{
		option = document.createElement('option');
		option.value = online[i].link;
		option.innerHTML = online[i].store;
		this.el.appendChild(option);
	}
}

document.onclick = closeMenus;

function closeMenus(e)
{
	var event = e || window.event;
	var target = event.target || event.srcElement;
	if (target.nodeType == 3) target = target.parentNode; // defeat Safari bug

	//alert(event.target.id == selects[0].id || event.target.id == selects[0].parentNode.id);
	for(var i=0;i<selects.length;i++)
	{
		if(target.id != selects[i].id && target.id != inputs[i].id) selects[i].style.display = "none";
	}
}

