//------------------------------------------------------------------------
// Written by Ray Wilson November 2010
// Copyright Music From Outer Space LLC 2010.
// http://www.musicfromouterspace.com/mfosweb/home.action
//------------------------------------------------------------------------

function adRotator(container)
{
	this.container = container;
	this.imageSrcs = [];
	this.imageUris = [];
	this.imageTitles = [];
	this.scrollPos = 0;
	this.timerRef = null;
	this.scrollingOn = true;
	this.scrollcontroldiv;
	this.holdScrollPos;
	this.form_target = "_blank";
	this.scrollDir = adRotator.VERTSCROLL;
	this.imagePath = "";
	this.SCROLLINC = 1;
	this.SCROLLDELAYMS = 50;
}

adRotator.HORZSCROLL = 0;
adRotator.VERTSCROLL = 1;

adRotator.prototype.setSize = function(w,h)
{
	this.container.style.width = w;
	this.container.style.height = h;
}

adRotator.prototype.setScrollDirection = function(scrollDir)
{
	this.scrollDir = scrollDir;
}

adRotator.prototype.setImagePath = function(imgpath)
{
	this.imagePath = imgpath;
}

adRotator.prototype.callOnclickHandler = function(e)
{
	this.scrollingOn = !this.scrollingOn;
	if(this.scrollingOn)
	{
		if(this.scrollDir == adRotator.VERTSCROLL)
		{
			this.scrollPos = this.container.scrollTop;
		}
		else if(this.scrollDir == adRotator.HORZSCROLL)
		{
			this.scrollPos = this.container.scrollLeft;
		}
	}
	this.setScrollDisplay(this.scrollingOn);
};

adRotator.prototype.setScrollDisplay = function(bScroll)
{
	this.scrollcontroldiv.innerHTML = (bScroll) ? "Click here to scroll manually." : "Click here to auto-scroll.";
	this.roll();
};

adRotator.prototype.roll = function()
{
	if(this.scrollingOn && this.scrollDir == adRotator.VERTSCROLL)
	{
		if(this.scrollPos >= (this.container.scrollHeight - this.container.offsetHeight))
		{
			this.scrollPos = 0;
		}
		this.container.scrollTop = this.scrollPos; 	
		this.scrollPos += this.SCROLLINC;
		var that = this;
		this.timerRef = window.setTimeout(function(){that.roll();}, this.SCROLLDELAYMS);
	}
	else if(this.scrollingOn && this.scrollDir == adRotator.HORZSCROLL)
	{
		if(this.scrollPos >= (this.container.scrollWidth - this.container.offsetWidth))
		{
			this.scrollPos = 0;
		}
		this.container.scrollLeft = this.scrollPos; 	
		this.scrollPos += this.SCROLLINC;
		var that = this;
		this.timerRef = window.setTimeout(function(){that.roll();}, this.SCROLLDELAYMS);
	}
	else
	{
		if(this.timerRef)
		{
			clearTimeout(this.timerRef);
		}
	}
};

adRotator.prototype.initialize = function()
{
	//------------------------------------------------------------------
	// Keep these arrays PARALLEL.
	//
	// To get correct URL find part number and do a search.
	// DO NOT BE LOGGED INTO JAMECO AND...
	// DO NOT GO TO THE SITE WITH AFILLIATE INFO ATTACHED TO URI.
	//------------------------------------------------------------------
	this.imageSrcs.push("EX430_Multimeter_with_freq_counter.jpg");
	this.imageSrcs.push("hobby tool set.jpg");
	this.imageSrcs.push("INSTEK_function_generator.jpg");
	this.imageSrcs.push("mini_drill_and_stand.jpg");
	this.imageSrcs.push("OSCILLOSCOPE_DIGITAL_2-CHANNEL.jpg");
	this.imageSrcs.push("pliers_set.jpg");
	this.imageSrcs.push("SKMI-TAIWAN_OSCILLOSCOPE.jpg");
	this.imageSrcs.push("soldering_station.jpg");
	this.imageSrcs.push("UNDERSTANDING_SIGNALS_TEXT_and_USB_scope.jpg");
	this.imageSrcs.push("breadboard.jpg");
	this.imageSrcs.push("breadboardwire.jpg");
	this.imageSrcs.push("powersupply211705.jpg");
	this.imageSrcs.push("powersupply301955.jpg");
	this.imageSrcs.push("frequencycounter2112840.jpg");
	this.imageSrcs.push("magnifierLight100829.jpg");
	
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D1581177%26catalogId%3D10001%26freeText%3D1581177%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D170069%26catalogId%3D10001%26freeText%3D170069%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D229260%26catalogId%3D10001%26freeText%3D229260%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D2113252%26catalogId%3D10001%26freeText%3D2113252%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D2126969%26catalogId%3D10001%26freeText%3D2126969%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D2120321%26catalogId%3D10001%26freeText%3D2120321%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D685311%26catalogId%3D10001%26freeText%3D685311%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AProductDisplay");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D1950711%26catalogId%3D10001%26freeText%3D1950711%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D387938%26catalogId%3D10001%26freeText%3D387938%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D1942163%26catalogId%3D10001%26freeText%3D1942163%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D19290%26catalogId%3D10001%26freeText%3D19290%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D211705%26catalogId%3D10001%26freeText%3D211705%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D301955%26catalogId%3D10001%26freeText%3D301955%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D2112840%26catalogId%3D10001%26freeText%3D2112840%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	this.imageUris.push("http%3A%2F%2Fwww.jameco.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%3FlangId%3D-1%26productId%3D100829%26catalogId%3D10001%26freeText%3D100829%26app.products.maxperpage%3D15%26storeId%3D10001%26search_type%3Djamecoall%26ddkey%3Dhttp%3AStoreCatalogDrillDownView");
	
	this.imageTitles.push("EXTECH 430 Multimeter");
	this.imageTitles.push("Hobby Tool Set");
	this.imageTitles.push("GW INSTEK Function Generator");
	this.imageTitles.push("Mini Drill And Stand");
	this.imageTitles.push("SKMI 2 Channel 25MHz Digital Oscilloscope");
	this.imageTitles.push("Plier Assortment");
	this.imageTitles.push("SKMI Dual Trace 20MHz Analog Oscilloscope");
	this.imageTitles.push("Soldering Station");
	this.imageTitles.push("Understanding Signals Kit w/USB Oscilloscope");
	this.imageTitles.push("Powered Breadboard");
	this.imageTitles.push("Breadboard Wire Jumper Kit (10 colors)");
	this.imageTitles.push("Bench Supply 2X 30V/2A w/4 Meters");
	this.imageTitles.push("Bench Supply 2X 30V/5A & Fixed 5V/3A w/4 Meters");
	this.imageTitles.push("Frequency Counter 8 Digit 0.2Hz-150MHz 100nHz-10Hz res.");
	this.imageTitles.push("LAMP,ADJ MAGNIFIER,22W,BLACK 120VAC,60HZ,UL APPROVED 4/cs");
	
	this.buildDisplay();
};

adRotator.prototype.buildDisplay = function()
{
	var sb = new top.StringBuffer();
	sb.append("<div id='jameco_roller' style='position:absolute;top:0px;left:0px;'>\n");
	sb.append("<table border='0' cellpadding='2px' cellspacing='0px'>\n");
	if(this.scrollDir == adRotator.VERTSCROLL)
	{
		for(var i=0; i<this.imageSrcs.length; ++i)
		{
			sb.append("<tr><td><img style='cursor:pointer;' src='");
			sb.append(this.imagePath);
			sb.append(this.imageSrcs[i]);
			sb.append("' onclick='adRotator.launchUrl(\"");
			sb.append(this.imageUris[i]);
			sb.append("\");' title='");
			sb.append(this.imageTitles[i]);
			sb.append("'></td></tr>\n");
		}
	}
	else
	{
		sb.append("<tr>");
		for(var i=0; i<this.imageSrcs.length; ++i)
		{
			sb.append("<td><img style='cursor:pointer;' src='");
			sb.append(this.imagePath);
			sb.append(this.imageSrcs[i]);
			sb.append("' onclick='adRotator.launchUrl(\"");
			sb.append(this.imageUris[i]);
			sb.append("\");' title='");
			sb.append(this.imageTitles[i]);
			sb.append("'></td><td>&nbsp;&nbsp;</td>\n");
		}
		sb.append("</tr>\n");
	}
	sb.append("</table></div>\n");
	sb.append("<form method='GET' target='");
	sb.append(this.form_target);
	sb.append("' action='http://www.avantlink.com/click.php' name='jamecolauncher' style='margin:0px;padding:0px;display:inline;'>\n");
	sb.append("<input type='hidden' name='tt' value='cl'>");
	sb.append("<input type='hidden' name='mi' value='10609'>");
	sb.append("<input type='hidden' name='pw' value='25831'>");
	sb.append("<input type='hidden' name='url' value=''>");
	sb.append("</form>\n");
	this.container.innerHTML = sb.toString();
	
	this.scrollcontroldiv = document.getElementById("rotatorscrollcontrols");
	var rotator = this;
	this.scrollcontroldiv.onclick = function () {rotator.callOnclickHandler();};
	this.scrollingOn = false;
	this.setScrollDisplay(false);
};

adRotator.launchUrl = function(url)
{
	if(url.indexOf("DIRECT|") > -1)
	{
		var parts = url.split(/\|/g);
		top.document.location = parts[1];
		return;
	}
	
	var theForm = document.forms["jamecolauncher"];
	if(theForm)
	{
		theForm.elements["url"].value = url;  
		theForm.submit();
	}
};




