// globals

var ipos = null;
var curralbum = null;
var clickx = -1;
var clicky = -1;
var trans = null;
var add = (Browser.Engine.trident) ? 2 : 0;
var wraptablerow = 1;

window.addEvent('domready', function() {
	// Add Javascript-only features
	// none at the moment
	
	// Swap out long Google-friendly FELLERS text for shorter text
	
	if ($defined($('textswapout'))) {
			$('textswapout').set('html','FELLERS is the largest supplier of wrap supplies to the companies who wrap vehicles. FELLERS Certified Wrap Providers are FELLERS customers who can help you with your vehicle wrap from design concept to professional printing and installation.');
	}
	
	// Accordion
	
	if ($defined($('accordion'))) {
		var homeAccordion = new Fx.Accordion($$('h3'), $$('div.shrinker'), $('accordion'), { opacity: false, fixedHeight: 370 });
	}
	
	// Topbar functionality
	
	var tb = $$('#topbar ul li.tbli a').each( function(it, ix) {
		var el = it.getParent().getChildren();
		if (el.length > 2) {
			it.addEvents({
				'mouseenter': function() {
					var px = this.getParent();
					// px.setStyles({
					// 	'backgroundImage': 'url(/images/designapr09/topbardroptopb.gif)'
					// });
					var em = px.getChildren()[2];
					em.position({relativeTo: px, position: 'bottomLeft'});
					em.setStyle('display', 'block');
					px.addEvent('mouseleave', function() {
						// this.setStyle('backgroundImage','none');
						this.getChildren()[2].setStyle('display','none');
					});
				}
			});
		}
	});
	
/* Old minimap, vetoed by Frank for being too cool for this world

	// Minimap functionality
	// Note that the div "minimap" MUST be followed by divs "mres" and "aftermap" in the HTML
	if ($defined($('minimap'))) {
		var mm = $('minimap');
		var mr = new Element('div', { id: 'mres' }).inject( mm, 'after' );
		var am = new Element('div', { id: 'aftermap' }).inject( mm, 'after' );
		trans = new Fx.Morph(mr);
		if ($defined($('central'))) {
			$('central').addEvent('click', function() {
				if ($('mres').getStyle('display') == 'block') {
					$('mres').setStyle('display','none');
					$('mresx').setStyle('display','none');
					$('flagout').setStyle('display','block');
				}
			});
		}
		mm.empty();
		mm.setStyles({
			position: 'relative',
			backgroundImage: 'url(/images/designapr09/ussuperminimap.gif)',
			height: '149px',
			width: '220px',
			marginBottom: '10px'
		});
		var mx = new Element('div', { 
			id: 'xline',
			styles: {
				position: 'absolute',
				backgroundColor: '#69e',
				zIndex: 3
			}
		}).inject(mm);
		var my = new Element('div', {
			id: 'yline',
			styles: {
				position: 'absolute',
				backgroundColor: '#69e',
				zIndex: 3
			}
		}).inject(mm);
		var mb = new Element('div', {
			id: 'clickbox',
			styles: {
				position: 'absolute',
				display: 'none',
				border: '1px solid white',
				height: 9,
				width: 9,
				zIndex: 4
			}
		}).inject(mm);
		var fo = new Element('div', {
			id: 'flagout',
			styles: {
				position: 'absolute',
				display: 'none',
				height: 15,
				width: 15,
				zIndex: 6,
				top: 0,
				left: 0,
				backgroundImage: 'url(/images/designapr09/flagout.gif)',
				cursor: 'pointer'
			},
			events: {
				'click': function(e) {
					e.stop();
					$('mres').setStyle('display','block');
					$('mresx').setStyle('display','block');
					$('flagout').setStyle('display','none');
				}
			}
		}).inject(mm);
		am.set('html','<p>Click the map to find FELLERS Certified providers within 250 miles of your location.</p>');
		ipos = mm.getPosition();
		mm.addEvents({
			'mouseenter': function() {
				$('yline').setStyles({
					'height':	'149px',
					'width':	'1px',
					'display':	'block'});
				$('xline').setStyles({
					'height':	'1px',
					'width':	'220px',
					'display':	'block'});
			},
			'mouseleave': function() {
				if (clickx >= 0) {
					$('xline').setStyle('top', clicky);
					$('yline').setStyle('left', clickx);
				} else {
					$('xline').setStyle('display','none');
					$('yline').setStyle('display','none');
				}
			},
			'mousemove': function(ev) {
				if (clickx < 0) {
					var mousex = ev.page.x - ipos.x - add;
					var mousey = ev.page.y - ipos.y - add;
					$('xline').setStyle('top', mousey);
					$('yline').setStyle('left', mousex);
				}
			},
			'click': function(ev) {
				var mousex = ev.page.x - ipos.x - add;
				var mousey = ev.page.y - ipos.y - add;
				clickx = mousex;
				clicky = mousey;
				$('xline').setStyle('top', mousey);
				$('yline').setStyle('left', mousex);
				$('clickbox').setStyles({ 'top': mousey - 5, 'left': mousex - 5, 'backgroundImage': 'url(/images/designapr09/spinball.gif)','display': 'block', 'border': 'none' });
				$('mres').setStyles({
					'top': ipos.y - 50,
					'left': ipos.x - 182,
					'display': 'block'
				});
				$('mres').set('html', '<p>One moment please ...</p>');
				if (!($defined($('mresx')))) {
					var mclose = new Element('div', {
						'id': 'mresx',
						'events': {
							'click': function() {
								$('mres').setStyle('display', 'none');
								$('mresx').setStyle('display', 'none');
								$('flagout').setStyle('display','block');
							}
						},
						'styles': {
							'top': ipos.y - 66,
							'left': ipos.x - 182,
							'display': 'block',
							'cursor': 'pointer'
						}
					}).inject($('rightbar'));
				} else {
					$('mresx').setStyle('display','block');
				}
				$('flagout').setStyle('display','none');
				var JSON = new Request.JSON({
					url: "/json/returnInstallers.cfm",
					onSuccess: function(obj,txt) {
						var arlen = obj.RES.length;
						var outtext = '';
						for (i = 0; i < arlen; i++) {
							var spid = obj.RES[i][0];
							var comp = obj.RES[i][1];
							var addr = obj.RES[i][2];
							var dist = obj.RES[i][3];
							var punch = '<p><a href="#" onclick="getInstDetail(\'' + spid + '\'); return false;"><b>' + comp + '</b></a><br />' + addr + '</p>';
							outtext = outtext + punch;
							$('clickbox').setStyles({'backgroundImage': 'none', 'border': '1px solid white'});
							addBrightDot(spid);
						}
						if (outtext != '') {
							$('mres').set('html',outtext);
						} else {
							$('mres').set('html','<p>No FELLERS Certified providers were found within 250 miles of your selected location.</p>');
						}
					}
				}).post({'mousex': mousex, 'mousey': mousey, 'mapwidth': 220}).send();
			}
		});
	}
	// End minimap
*/

	// Minimap 2.0, incredibly puny, mundane, boring version
	
	if ($defined($('minimap'))) {
		var mm = $('minimap');
		mm.set('html','<a href="/index.cfm/spKey/find"><img src="/images/designapr09/ussuperminimap.gif" height="149" width="220" border="0"></a><p>Click the map to find FELLERS Certified providers within 250 miles of your location.</p>');
		
	}

	// Megamap
	if ($defined($('mapholder'))) {
		var mh = $('mapholder');
		
		mh.empty(); // Clears out the anchor tag and img, giving us a nice clean slate
		
		var cb = new Element('div', { 'id': 'clickbox' }).inject(mh);
		var px = new Element('div', { 'id': 'xline' }).inject(mh);
		var py = new Element('div', { 'id': 'yline' }).inject(mh);
		
		// Adding points to the map, speedy style (I hope)
		var loadingdiv = new Element('div', {
			'id': 'loadingdiv',
			'styles': {
				'position': 'absolute',
				'top': '240px',
				'left': '305px',
				'height': '19px',
				'width': '220px',
				'color': 'white',
				'backgroundImage': 'url(/images/designapr09/loadbar.gif)'
			}
		}).inject(mh);
		var mapJSON = new Request.JSON({
			url: "/json/installersMap.cfm",
			onSuccess: function(obj, txt) {
				mapj = obj;
				var arlen = obj.RES.length;
				var outtext = '';
				$('loadingdiv').setStyle('display','none');
				for (i = 0; i < arlen; i++) {
					var spid = obj.RES[i][0];
					var xpos = obj.RES[i][1] - 28;
					var ypos = obj.RES[i][2] - 31;
					var comp = obj.RES[i][3];
					var loca = obj.RES[i][4];
					var lat  = obj.RES[i][5];
					var lng  = obj.RES[i][6];
					
					if (ypos < 0) { // Alaska ... not sure if this works for any location but Fairbanks, to be honest
						xpos += 100;
						ypos += 785;
					}
					
					if (xpos < 0) { // Hawaii's map is a Mercator projection, so we get to do this one the easy way
						ypos = (lat * -21.872) + 932;
						xpos = (lng * 22.857) + 3915;
					}
					
					var ndiv = new Element('div', { 
						'class':	'mapPoint',
						'id':		spid,
						'styles': {
							'position': 'absolute',
							'height': '20px',
							'width': '18px',
							'backgroundImage': 'url(/images/designapr09/mapdot3.png)',
							'zIndex': 3,
							'top': ypos,
							'left': xpos
						}
					}).inject(mh);
				}
				geoLocate();
			}
		// As it turns out, the math only works if there's padding exactly (10 / 370) * width pixels around the outside of the map.
		// By stunning coincidence, that's the size of the padding around the minimap, which is the first implementation of this
		// algorithm that I did. What are the chances.
		// ... anyway, that's why the map width is set differently from the actual map and we do some funky stuff with the return values.
		}).post({'mapwidth': 842, 'offsetx': 0, 'offsety': 0}).send();
	
		// Full-sized map functionality
		mh.addEvents({
			'click': function(ev) {
				clearBrightDots();
				$('zipformzip').value = '';
				$('alphadefault').selected = true;
				$('statedefault').selected = true;
				ipos = mh.getPosition();
				var mousex = ev.page.x - ipos.x - add;
				var mousey = ev.page.y - ipos.y - add;
				$('xline').setStyles({'position': 'absolute', 'top': mousey, 'height': 1, 'width': 810, 'backgroundColor': '#1769a6', 'display': 'block'});
				$('yline').setStyles({'position': 'absolute', 'left': mousex, 'height': 538, 'width': 1, 'backgroundColor': '#1769a6', 'display': 'block'});
				$('clickbox').setStyles({ 'position': 'absolute', 'top': mousey - 5, 'left': mousex - 5, 'height': 9, 'width': 9, 'backgroundImage': 'url(/images/designapr09/spinball.gif)','display': 'block', 'border': 'none' });
				$('mres').setStyles({'display': 'block', 'top': 100, 'left': 785, 'height': 428 });
				$('mres').set('html', '<p>One moment please ...</p>');
				//mapSlide.slideIn();
				var JSON = new Request.JSON({
					url: "/json/returnInstallers.cfm",
					onSuccess: function(obj,txt) {
						var arlen = obj.RES.length;
						var outtext = '';
						for (i = 0; i < arlen; i++) {
							var spid = obj.RES[i][0];
							var comp = obj.RES[i][1];
							var addr = obj.RES[i][2];
							var dist = obj.RES[i][3];
							//var punch = '<p><a href="/index.cfm/spKey/gallery?spid=' + spid + '"><b>' + comp + '</b></a><br />' + addr + '</p>';
							var punch = '<p><a href="#" onclick="getInstDetail(\'' + spid + '\'); return false;"><b>' + comp + '</b></a><br />' + addr + '</p>';
							outtext = outtext + punch;
							addBrightDot(spid);
							$('clickbox').setStyles({'backgroundImage': 'none', 'border': '1px solid white'});
						}
						if (outtext != '') {
							$('mres').set('html', outtext);
						} else {
							$('mres').set('html','<p>No FELLERS Certified providers were found within 250 miles of your selected location.</p>');
							$('clickbox').setStyles({'backgroundImage': 'none', 'border': '1px solid #1769a6'});
						}
					}
				}).post({'mousex': mousex + 14 + add, 'mousey': mousey + 14 + add, 'mapwidth': 842, 'offsetx': 0, 'offsety': 0 }).send();
			}
		});
		// End megamap
	}
});
// End domready event

function getInstDetail(spid) { // called when someone clicks an installer on the map popup
	setUpAlbum();
	var JSONinst = new Request.JSON({ // Holy cow do I ever love these Request.JSON things
		url: "/json/installerInfo.cfm",
		onSuccess: function(obj,txt) {
			curralbum = obj.ALBUM;
			$('windowbartext').set('text','FELLERS Certified Provider: ' + obj.COMPANY + ', ' + obj.CITY + ' ' + obj.STATE);
			$('titleholder').set('html','<h2>' + obj.COMPANY + '</h2>');
			$('addressholder').set('html',obj.ADDRESSHTML);
			$('detailholder').set('html',obj.DETAILHTML);
			setUpThumbs();
		}
	}).post({'spid': spid}).send();
}

function setUpAlbum() { // creates the "album" a.k.a. the installer info popup
	var vpos = window.getScroll();
	if (!($defined($('albumholder')))) {
		var albumdiv = new Element('div', { 'id': 'albumholder', 'styles': { 'top': vpos.y + 50 }}).inject('placeholder');
		var topdiv = new Element('div', { 'id': 'windowbar' }).inject(albumdiv);
		var toptext = new Element('p', { 'id': 'windowbartext', 'html': 'Loading ...' }).inject(topdiv);
		var closebutton = new Element('div', { 'id': 'closebutton',
			'events': {
				'mouseover': function(){
					$('closebutton').setStyle('backgroundColor', '#fc0');
				},
				'mouseout': function() {
					$('closebutton').setStyle('backgroundColor', 'orange');
				}
			}
		}).inject(topdiv);
		var closex = new Element('img', { 'src': '/images/designapr09/xbox.gif', 'height': 16, 'width': 16, 'border': 0 }).inject(closebutton);
		var leftdiv = new Element('div', { 'id': 'contentarea' }).inject(albumdiv);
		var txtdiv = new Element('div', { 'id': 'titleholder' }).inject(leftdiv);
		var adddiv = new Element('div', { 'id': 'addressholder' }).inject(leftdiv);
		var detdiv = new Element('div', { 'id': 'detailholder' }).inject(leftdiv);
		var pixdiv = new Element('div', { 'id': 'bigimage' }).inject(leftdiv);
		var blkdiv = new Element('div', { 'id': 'blackout' }).inject(leftdiv);
		var imgdiv = new Element('div', { 'id': 'imageholder' }).inject(pixdiv);
		var capdiv = new Element('div', { 'id': 'captionholder' }).inject(pixdiv);
		var rightdiv = new Element('div', { 'id': 'rightscroller' }).inject(albumdiv);
		closebutton.addEvent('click', function(){ $('albumholder').setStyle('display', 'none'); });
		pixdiv.addEvent('click', function(){
			$('bigimage').setStyle('display', 'none');
			$('blackout').setStyle('display', 'none');
			if ($('albumholder').getStyle('height').toInt() > 450) {
				$('albumholder').setStyle('height',450);
				$('contentarea').setStyle('height', 423);
				$('addressholder').setStyle('height', 423);
				$('rightscroller').setStyle('height', 423);	
			}
		});
		var newDrag = new Drag($('albumholder'), { handle: $('windowbar') });
		$('albumholder').pin();
	} else {
		$('windowbartext').set('html','Loading ...');
		$('titleholder').set('text','');
		$('addressholder').set('text','');
		$('detailholder').set('text','');
		$('rightscroller').set('text','');
		$('contentarea').setStyle('display','block');
		$('bigimage').setStyle('display','none');
		$('blackout').setStyle('display','none');
		$('albumholder').setStyle('display', 'block');
	}
}

function setUpThumbs() { // downloads and builds out the thumbnails on the right of the album popup
	if (curralbum != null && curralbum.length > 0) {
		for (x = 0; x < curralbum.length; x++) {
			o = curralbum[x];
			im = new Element('img', {
				'src': o.TSRC,
				'height': o.THEIGHT,
				'width': o.TWIDTH,
				'border': 0,
				'title': o.CAPTION,
				'alt': x,
				'events': {
					'click': function() {
						$('bigimage').empty();
						var blockheight = $('addressholder').getStyle('height').toInt();
						var x = this.alt.toInt();
						var hgt = curralbum[x].HEIGHT;
						var wdt = curralbum[x].WIDTH;
						im = new Element('img', {
							'src': curralbum[x].SRC,
							'height': hgt,
							'width': wdt,
							'border': 0,
							'title': curralbum[x].CAPTION
						}).inject($('bigimage'));
						cp = new Element('p', {
							'align': 'center',
							'html': curralbum[x].CAPTION + '<br /><span style="font-size: 10px">(click image to return)</span>',
							'styles': { 'color': 'white', 'fontWeight': 'bold', 'marginTop': '3px' }
						}).inject($('bigimage'));
						hgt += 33;
						var imgx = (700 - wdt) / 2;
						if (hgt <= 423) {
							var imgy = (423 - hgt) / 2;
							$('albumholder').setStyle('height', 450);
							$('contentarea').setStyle('height', 423);
							$('addressholder').setStyle('height', 423);
							$('rightscroller').setStyle('height', 423);
							blockheight= 423;
						} else {
							var imgy = 0;
							$('albumholder').setStyle('height', hgt + 27);
							$('contentarea').setStyle('height', hgt);
							$('addressholder').setStyle('height', hgt);
							$('rightscroller').setStyle('height', hgt);
							blockheight = hgt;
						}
						$('blackout').setStyles({
							'top': 32,
							'left': 5,
							'width': 700,
							'height': blockheight,
							'display': 'block'
						});
						$('bigimage').setStyles({
							'top': imgy + 32,
							'left': imgx + 5,
							'display': 'block'
						});
					}
				},
				'styles': {
					'padding': '0 0 5px',
					'marginLeft': 'auto',
					'marginRight': 'auto'
				}
			}).inject($('rightscroller'));
		}
	}
}

function addWrapTableRow() {
	if (wraptablerow < 10) {
		wraptablerow++;
		var tr = new Element('tr').inject($('additemrow'),'before');
		var td1 = new Element('td', {
			html: '<select id="itemtype' + wraptablerow.toString() + '" name="itemtype' + wraptablerow.toString() + '"><option value=""></option><option value="Automobile">Automobile</option><option value="Boat">Boat</option><option value="Building">Building</option><option value="Bus/RV">Bus or RV</option><option value="Cycle/ATV">Cycle or ATV</option><option value="Floor">Floor</option><option value="Trailer">Trailer</option><option value="Truck">Truck</option><option value="Van">Van</option><option value="Wall (Smooth)">Smooth Wall</option><option value="Wall (Rough)">Rough Wall</option><option value="Window">Window</option><option value="Other">Other (Please describe)</option></select>'
		}).inject(tr);
		var td2 = new Element('td', {
			html: '<input id="itemyear' + wraptablerow.toString() + '" name="itemyear' + wraptablerow.toString() + '" type="text" maxlength="4" size="5" />'
		}).inject(tr);
		var td3 = new Element('td', {
			html: '<input id="itemmake' + wraptablerow.toString() + '" name="itemmake' + wraptablerow.toString() + '" type="text" maxlength="50" size="10" />'
		}).inject(tr);
		var td4 = new Element('td', {
			html: '<input id="itemmodel' + wraptablerow.toString() + '" name="itemmodel' + wraptablerow.toString() + '" type="text" maxlength="50" size="10" />'
		}).inject(tr);
		var td5 = new Element('td', {
			html: '<input id="itemqty' + wraptablerow.toString() + '" name="itemqty' + wraptablerow.toString() + '" type="text" maxlength="4" size="5" />'
		}).inject(tr);
		var td6 = new Element('td', {
			html: '<input id="itemdesc' + wraptablerow.toString() + '" name="itemdesc' + wraptablerow.toString() + '" type="text" maxlength="150" size="20" />'
		}).inject(tr);
		if (wraptablerow == 10) {
			$('additemrow').destroy();
		}
	}
}

function zipFinder() {
	clearBrightDots();
	$('statedefault').selected = true;
	$('alphadefault').selected = true;
	var ntext = $('zipformzip').value.replace(/[\D]/g,'');
	if (ntext.length < 5) {
		$('zipformzip').setStyle('backgroundColor', '#fcc');
		$('zipformzip').addEvent('keydown', function() { this.setStyle('backgroundColor', 'white'); });
		return false;
	}
	$('mres').setStyle('display', 'block');
	$('mres').setStyles({ 'top': 100, 'left': 785, 'height': 428 });
	$('mres').set('html','<p>One moment please ...</p>');
	var JSON = new Request.JSON({
		url: "/json/returnInstallers.cfm",
		onSuccess: function(obj,txt) {
			if (obj.SCS == 'a') {
				ipos = $('mapholder').getPosition();
				var mousex = obj.XPO - 17;
				var mousey = obj.YPO - 13;
				$('xline').setStyles({'position': 'absolute', 'top': mousey, 'height': 1, 'width': 810, 'backgroundColor': '#1769a6', 'display': 'block'});
				$('yline').setStyles({'position': 'absolute', 'left': mousex, 'height': 538, 'width': 1, 'backgroundColor': '#1769a6', 'display': 'block'});
				$('clickbox').setStyles({ 'position': 'absolute', 'top': mousey - 5, 'left': mousex - 5, 'height': 9, 'width': 9, 'display': 'block', 'border': '1px solid white' });
				var arlen = obj.RES.length;
				var outtext = '';
				for (i = 0; i < arlen; i++) {
					var spid = obj.RES[i][0];
					var comp = obj.RES[i][1];
					var addr = obj.RES[i][2];
					var dist = obj.RES[i][3];
					//var punch = '<p><a href="/index.cfm/spKey/gallery?spid=' + spid + '"><b>' + comp + '</b></a><br />' + addr + '</p>';
					var punch = '<p><a href="#" onclick="getInstDetail(\'' + spid + '\'); return false;"><b>' + comp + '</b></a><br />' + addr + '</p>';
					outtext = outtext + punch;
					addBrightDot(spid);
				}
				if (outtext != '') {
					$('mres').set('html', '<p><strong>Search Results for ZIP code ' + ntext + '</strong></p>' + outtext);
				} else {
					$('mres').set('html','<p>No FELLERS Certified providers were found within 250 miles of your selected location.</p>');
					$('clickbox').setStyles({'backgroundImage': 'none', 'border': '1px solid #1769a6'});
				}
			} else {
				$('mres').set('html','<p>You have entered an unknown 5-digit U.S. ZIP postal code. Please try again.</p>');
			}
		}
	}).post({'localzip': ntext, 'mapwidth': 842, 'offsetx': 0, 'offsety': 0 }).send();
	return false;
}

function alphaFinder() {
	$('statedefault').selected = true;
	$('zipformzip').value = '';
	$('xline').setStyle('display','none');
	$('yline').setStyle('display','none');
	$('clickbox').setStyle('display','none');
	clearBrightDots();
	var ntext = $('alphaformalpha').value;
	$('mres').setStyle('display', 'block');
	$('mres').setStyles({ 'top': 100, 'left': 785, 'height': 428 });
	$('mres').set('html','<p>One moment please ...</p>');
	var JSON = new Request.JSON({
		url: "/json/returnInstallers.cfm",
		onSuccess: function(obj,txt) {
			if (obj.SCS == 'a') {
				ipos = $('mapholder').getPosition();
				var arlen = obj.RES.length;
				var outtext = '';
				for (i = 0; i < arlen; i++) {
					var spid = obj.RES[i][0];
					var comp = obj.RES[i][1];
					var addr = obj.RES[i][2];
					var dist = obj.RES[i][3];
					var punch = '<p><a href="#" onclick="getInstDetail(\'' + spid + '\'); return false;"><b>' + comp + '</b></a><br />' + addr + '</p>';
					outtext = outtext + punch;
					addBrightDot(spid);
				}
				if (outtext != '') {
					if (ntext == '0') { ntext = '0-9'; }
					$('mres').set('html', '<p><strong>Alpha Search Results: ' + ntext + '</strong></p>' + outtext);
				} else {
					$('mres').set('html','<p>No FELLERS Certified providers found that match your search criteria.</p>');
				}
			} else {
				$('mres').set('html','<p>You have entered an unknown code. Please try again.</p>');
			}
		}
	}).post({'alpha': ntext, 'mapwidth': 842, 'offsetx': 0, 'offsety': 0 }).send();
	return false;
}

function stateFinder() {
	$('alphadefault').selected = true;
	$('zipformzip').value = '';
	$('xline').setStyle('display','none');
	$('yline').setStyle('display','none');
	$('clickbox').setStyle('display','none');
	clearBrightDots();
	var ntext = $('stateformstate').value;
	if (ntext.length != 2) {
		$('alphaformalpha').setStyle('backgroundColor', '#fcc');
		$('alphaformalpha').addEvent('keydown', function() { this.setStyle('backgroundColor', 'white'); });
		return false;
	}
	$('mres').setStyles({ 'display': 'block', 'top': 100, 'left': 785, 'height': 428 });
	$('mres').set('html','<p>One moment please ...</p>');
	var JSON = new Request.JSON({
		url: "/json/returnInstallers.cfm",
		onSuccess: function(obj,txt) {
			if (obj.SCS == 'a') {
				var arlen = obj.RES.length;
				var outtext = '';
				for (i = 0; i < arlen; i++) {
					var spid = obj.RES[i][0];
					var comp = obj.RES[i][1];
					var addr = obj.RES[i][2];
					var dist = obj.RES[i][3];
					var punch = '<p><a href="#" onclick="getInstDetail(\'' + spid + '\'); return false;"><b>' + comp + '</b></a><br />' + addr + '</p>';
					outtext = outtext + punch;
					addBrightDot(spid);
				}
				if (outtext != '') {
					$('mres').set('html', '<p><strong>Search Results by State: ' + ntext + '</strong></p>' + outtext);
				} else {
					$('mres').set('html','<p>No FELLERS Certified providers matched your search criteria.</p>');
				}
			} else {
				$('mres').set('html','<p>You have entered an unknown code. Please try again.</p>');
			}
		}
	}).post({'state': ntext, 'mapwidth': 842, 'offsetx': 0, 'offsety': 0 }).send();
	return false;
}

function clearBrightDots() {
	$$('div').each( function(itm,idx) {
		if (itm.getStyle('backgroundImage') == 'url(/images/designapr09/brightdot2.png)') {
			itm.setStyle('backgroundImage','url(/images/designapr09/mapdot3.png)');
		}
	});
}

function addBrightDot(spid) {
	if ($defined($(spid))) {
		$(spid).setStyle('backgroundImage','url(/images/designapr09/brightdot2.png)');
	}
}

function geoLocate() {
	var JSON = new Request.JSON({
		url: "/json/geoLocate.cfm",
		onSuccess: function(obj,txt) {
			if (obj.OK == 'yes') {
				var ipos = $('mapholder').getPosition();
				var xpos = obj.XPOS + ipos.x + add + 5;
				var ypos = obj.YPOS + ipos.y + add;
				$('mapholder').fireEvent('click', {page: {x: xpos, y: ypos}});
			}
		}
	}).send();
}

// FELLERS.com legacy code below

function showdeal(thing,text,x,y) {
	thingid = thing.id.substr(3);
	$('installersearchtext').innerHTML = "<p id=\"instname\">" + text + "</p>";
	inst = $('installersearchtext');
	if (x <= 350) {
		inst.setStyles({
			position: 'absolute',
			top: (y - 9) + 'px',
			left: (x - 9) + 'px',
			padding: '5px',
			paddingTop: '14px',
			paddingLeft: '14px',
			width: '180px',
			fontSize: '12px',
			textAlign: 'left',
			color: 'white',
			overflow: 'hidden',
			backgroundImage: 'url(/images/starbg.png)',
			backgroundPosition: 'top left',
			display: 'block'
		});
	} else {
		inst.setStyles({
			position: 'absolute',
			top: (y - 9) + 'px',
			left: (x - 190) + 'px',
			padding: '5px',
			paddingTop: '14px',
			paddingLeft: '14px',
			width: '180px',
			fontSize: '12px',
			textAlign: 'left',
			color: 'white',
			overflow: 'hidden',
			backgroundImage: 'url(/images/starbg2.png)',
			backgroundPosition: 'top right',
			display: 'block'
		});	
	}
	inst.addEvent('mouseleave', function(){
		inst.setStyle('display', 'none');
		awaydeal(thing);
	});
	inst.addEvent('click', function(){
		inst.setStyle('display', 'none');
		awaydeal(thing);
	});
}

function awaydeal(thing) {

}

function clickdeal(thing,id) {
	locale = "/index.cfm/spKey/google.htm?location=" + id;
	window.location = locale;
}

function goBright(btn) {
	$(btn).src = '/images/designoct08/tabs/' + btn + 'o.gif';
}
function goDark(btn) {
	$(btn).src = '/images/designoct08/tabs/' + btn + 'd.gif';
}

function showdeal2(thing,text,x,y) {
	thingid = thing.id.substr(3);
	document.getElementById('installersearchtext').innerHTML = "<p id=\"instname\">" + text + "</p><p id=\"installerx\"></p>";
	var inst = $('instname');
	var inx = $('installerx');
	inst.style.position = "absolute";
	inx.style.position = "absolute";
	inx.style.display = "block";
	inst.style.top = (y * 0.7 - 4) + "px";
	inx.style.top = (y * 0.7) + "px";
	if (x > 350) {
		inst.style.left = (x * 0.7 - 170) + "px";
		inx.style.left = (x * 0.7 - 168) + "px";
		inst.style.backgroundImage = "url('http://www.fellerscertified.com/images/general/arrowur2.gif')";
		inst.style.backgroundPosition = "top right";
	} else {
		inst.style.left = (x * 0.7 - 4) + "px";
		inx.style.left = (x * 0.7 + 158) + "px";
		inst.style.backgroundImage = "url('http://www.fellerscertified.com/images/general/arrowul2.gif')";
		inst.style.backgroundPosition = "top left";
	}
	inst.style.backgroundRepeat = "no-repeat";
	inst.style.padding = "20px 8px 10px 8px";
	inst.style.width="160px";
	inx.style.width="10px";
	inx.style.height="10px";
	inx.style.cursor = "pointer";
	inst.style.overflow = "hidden";
	inst.style.fontSize = "10px";
	inst.style.lineHeight = "13px";
	inst.style.color = "white";
	inst.style.cursor = "pointer";
	inst.style.zIndex = 120;
	inx.style.zIndex = 130;
	inst.addEvent('mouseleave', function(){
		inst.setStyle('display', 'none');
		inx.setStyle('display', 'none');
		awaydeal2(thing);
	});
	inst.addEvent('click', function(){
		inst.setStyle('display', 'none');
		inx.setStyle('display', 'none');
		clickdeal2(thingid);
	});
	inx.addEvent('click', function(){
		inst.setStyle('display', 'none');
		inx.setStyle('display', 'none');
		awaydeal2(thing);
	});
}
function awaydeal2(thing) {
	if (document.getElementById(thing.id).style.backgroundImage == "url(/images/general/blue9.gif)")
		document.getElementById(thing.id).style.backgroundImage = "url('/images/general/bluepulse9.gif')"
	else
		document.getElementById(thing.id).style.backgroundImage = "url('/images/general/blue5.gif')";
}
function clickdeal2(id) {
	locale = "http://www.fellerscertified.com/index.cfm/spKey/gallery.htm?installerID=" + id;
	window.location = locale;
}
function getInstDetail(spid) {
	setUpAlbum();
	var JSONinst = new Request.JSON({ // Holy cow do I ever love these Request.JSON things
		url: "/json/installerInfo.cfm",
		onSuccess: function(obj,txt) {
			curralbum = obj.ALBUM;
			$('windowbartext').set('text','FELLERS Certified Provider: ' + obj.COMPANY + ', ' + obj.CITY + ' ' + obj.STATE);
			$('titleholder').set('html','<h2>' + obj.COMPANY + '</h2>');
			$('addressholder').set('html',obj.ADDRESSHTML);
			$('detailholder').set('html',obj.DETAILHTML);
			setUpThumbs();
		}
	}).post({'spid': spid}).send();
}

// setUpAlbum
// Creates the wrap provider info "window" popup

function setUpAlbum() {
	var vpos = window.getScroll();
	if (!($defined($('albumholder')))) {
		var albumdiv = new Element('div', { 'id': 'albumholder', 'styles': { 'top': vpos.y + 50 }}).inject('placeholder');
		var topdiv = new Element('div', { 'id': 'windowbar' }).inject(albumdiv);
		var toptext = new Element('p', { 'id': 'windowbartext', 'html': 'Loading ...' }).inject(topdiv);
		var closebutton = new Element('div', { 'id': 'closebutton',
			'events': {
				'mouseover': function(){
					$('closebutton').setStyle('backgroundColor', '#fc0');
				},
				'mouseout': function() {
					$('closebutton').setStyle('backgroundColor', 'orange');
				}
			}
		}).inject(topdiv);
		var closex = new Element('img', { 'src': '/images/designapr09/xbox.gif', 'height': 16, 'width': 16, 'border': 0 }).inject(closebutton);
		var leftdiv = new Element('div', { 'id': 'contentarea' }).inject(albumdiv);
		var txtdiv = new Element('div', { 'id': 'titleholder' }).inject(leftdiv);
		var adddiv = new Element('div', { 'id': 'addressholder' }).inject(leftdiv);
		var detdiv = new Element('div', { 'id': 'detailholder' }).inject(leftdiv);
		var pixdiv = new Element('div', { 'id': 'bigimage' }).inject(leftdiv);
		var blkdiv = new Element('div', { 'id': 'blackout' }).inject(leftdiv);
		var imgdiv = new Element('div', { 'id': 'imageholder' }).inject(pixdiv);
		var capdiv = new Element('div', { 'id': 'captionholder' }).inject(pixdiv);
		var rightdiv = new Element('div', { 'id': 'rightscroller' }).inject(albumdiv);
		closebutton.addEvent('click', function(){ $('albumholder').setStyle('display', 'none'); });
		pixdiv.addEvent('click', function(){
			$('bigimage').setStyle('display', 'none');
			$('blackout').setStyle('display', 'none');
			if ($('albumholder').getStyle('height').toInt() > 450) {
				$('albumholder').setStyle('height',450);
				$('contentarea').setStyle('height', 423);
				$('addressholder').setStyle('height', 423);
				$('rightscroller').setStyle('height', 423);	
			}
		});
		var newDrag = new Drag($('albumholder'), { handle: $('windowbar') });
		$('albumholder').pin();
	} else {
		$('windowbartext').set('html','Loading ...');
		$('titleholder').set('text','');
		$('addressholder').set('text','');
		$('detailholder').set('text','');
		$('rightscroller').set('text','');
		$('contentarea').setStyle('display','block');
		$('bigimage').setStyle('display','none');
		$('blackout').setStyle('display','none');
		$('albumholder').setStyle('display', 'block');
	}
}

// setUpThumbs
// Downloads and builds out the thumbnails on the right of the album popup

function setUpThumbs() {
	if (curralbum != null && curralbum.length > 0) {
		for (x = 0; x < curralbum.length; x++) {
			o = curralbum[x];
			im = new Element('img', {
				'src': o.TSRC,
				'height': o.THEIGHT,
				'width': o.TWIDTH,
				'border': 0,
				'title': o.CAPTION,
				'alt': x,
				'events': {
					'click': function() {
						$('bigimage').empty();
						var blockheight = $('addressholder').getStyle('height').toInt();
						var x = this.alt.toInt();
						var hgt = curralbum[x].HEIGHT;
						var wdt = curralbum[x].WIDTH;
						im = new Element('img', {
							'src': curralbum[x].SRC,
							'height': hgt,
							'width': wdt,
							'border': 0,
							'title': curralbum[x].CAPTION
						}).inject($('bigimage'));
						cp = new Element('p', {
							'align': 'center',
							'html': curralbum[x].CAPTION + '<br /><span style="font-size: 10px">(click image to return)</span>',
							'styles': { 'color': 'white', 'fontWeight': 'bold', 'marginTop': '3px' }
						}).inject($('bigimage'));
						hgt += 33;
						var imgx = (700 - wdt) / 2;
						if (hgt <= 423) {
							var imgy = (423 - hgt) / 2;
							$('albumholder').setStyle('height', 450);
							$('contentarea').setStyle('height', 423);
							$('addressholder').setStyle('height', 423);
							$('rightscroller').setStyle('height', 423);
							blockheight= 423;
						} else {
							var imgy = 0;
							$('albumholder').setStyle('height', hgt + 27);
							$('contentarea').setStyle('height', hgt);
							$('addressholder').setStyle('height', hgt);
							$('rightscroller').setStyle('height', hgt);
							blockheight = hgt;
						}
						$('blackout').setStyles({
							'top': 32,
							'left': 5,
							'width': 700,
							'height': blockheight,
							'display': 'block'
						});
						$('bigimage').setStyles({
							'top': imgy + 32,
							'left': imgx + 5,
							'display': 'block'
						});
					}
				},
				'styles': {
					'padding': '0 0 5px',
					'marginLeft': 'auto',
					'marginRight': 'auto'
				}
			}).inject($('rightscroller'));
		}
	}
}
