var t;
var slideIndex = 0;
function fadeSlide() {
	var slideshowDots = $$('div.slideshow-dots ul li');
	var s = $('slides');
	var s1 = s.down('div.slide');
	var s2 = s1.next(0);
	new Effect.Parallel([
//		new Effect.Move(s, { /*sync: true,*/ x: -1138, y: 0, mode: 'relative', duration: 0.5 }),
//		new Effect.Move(s, { /*sync: true,*/ x: -1384, y: 0, mode: 'relative', duration: 0.5 }),
//		new Effect.Opacity(s1, { /*sync: true,*/ from: 1, to: 0, duration: 0.3 }),
//		new Effect.Opacity(s2, { /*sync: true,*/ from: 0, to: 1, duration: 1.0 }) 
//		new Effect.Move(s, { sync: true, x: -1138, y: 0, mode: 'relative' })//,
		new Effect.Move(s, { sync: true, x: -1384, y: 0, mode: 'relative' }),
		new Effect.Opacity(s1, { sync: true, from: 1, to: 0 }),
		new Effect.Opacity(s2, { sync: true, from: 0, to: 1 }) 
	], {
		duration: 1.0, 
		afterSetup: function() {
			slideshowDots.invoke('removeClassName','selected');
			if (slideIndex == 3) 
				slideIndex = 0;
			else
				slideIndex++;
			slideshowDots[slideIndex].addClassName('selected');
		},
		afterFinish: function() {
//			s.setStyle({'left':'200px'});
//			s.setStyle({'left':'15px'});
			s.setStyle({'left':'0'});
			s.insert(Element.remove(s1));
		}
	});
}
function collapseDetail(elementId) {
	$(elementId).fade({ duration: 0.5 });
}
//function setupScroller() {
//	var lister = $('results_list').down('ul');
//	var listerHeight = lister.getHeight();
//	if (listerHeight > 315) {
//		$$('div.finder div#results_list a#page_up, div.finder div#results_list a#page_down').each(function(e){
//			e.show();
//			Event.observe(e, 'click', function(clickEvnt){
//				clickEvnt.stop();
//				var pos = lister.positionedOffset();
//				var y = 0;
//				if (e.id == 'page_up') {
//					if ((pos.top * -1) > 10) y = 365;
//				} else {
//					if (!((pos.top - 375)*-1 > listerHeight)) {
//						y = -365;
//					}
//				}
//				new Effect.Move(lister, { x: 0, y: y, mode: 'relative', duration: 0.5 });
//			});
//		});
//	}
//}
function getSelected() {//alert($$('div.choices').length);
	var nVisible = 1;
	var url = '/businesses/find/';
	$$('div#finder div.finder-top-nav li a.selected').each(function(e) {
		url += e.readAttribute('rel');
	});
	$$('div#finder div.choices').each(function(e,i) {
		if (e.visible() && !e.hasClassName('results')) {
			nVisible++;
			url += '/';
			$$('div#finder div.choices.grp-' + i + ' li a.selected').each(function(selectedElement,j) {
				if (j > 0) url += '_';
				url += selectedElement.readAttribute('rel');
			});
		}
	});
	if ($$('div#finder div.choices').length == nVisible) {
		new Ajax.Request(url, {
			onComplete: function(t) {
				var xml = t.responseXML;
				var results = xml.getElementsByTagName('result');
				$('results_list').update(results[0].childNodes[0].nodeValue);
				$('results').update(results[1].childNodes[0].nodeValue);
				if (!$('finder_results_container').visible()) {
//					Effect.SlideDown('finder_results_container', { duration: 0.2 });
//					$('finder_results_container').appear({ duration: 0.1 });
					$('finder_results_container').show();
				}
				
				var lister = $('results_list').down('ul');
				var listerHeight = lister.getHeight();
				if (listerHeight > 315) {
					$$('div.finder div#results_list a#page_up, div.finder div#results_list a#page_down').each(function(e){
						e.show();
						Event.observe(e, 'click', function(clickEvnt){
							clickEvnt.stop();
							var pos = lister.positionedOffset();
							var y = 0;
							if (e.id == 'page_up') {
								if ((pos.top * -1) > 10) y = 365;
							} else {
								if (!((pos.top - 375)*-1 > listerHeight)) {
									y = -365;
								}
							}
							new Effect.Move(lister, { x: 0, y: y, mode: 'relative', duration: 0.5 });
						});
					});
				}
				$$('div#results_list ul li a').each(function(businessLinkElement){
					Event.observe(businessLinkElement, 'click', function(businessLinkElementClickEvnt) {
						businessLinkElementClickEvnt.stop();
						//alert(businessLinkElement.href.replace('http://'+location.hostname,''));
						_gaq.push(['_trackEvent', 'Business Finder', businessLinkElement.href.replace('http://'+location.hostname,'')]);
						$$('div.finder div#results div.result').invoke('removeClassName', 'selected');
						$(businessLinkElement.readAttribute('rel')).addClassName('selected');
						Effect.ScrollTo(businessLinkElement.readAttribute('rel'), { duration: 0.5, offset: (businessLinkElement.cumulativeOffset().top * -1) + 50 });
					});
				});
				$$('div#results div.result').each(function(businessResultElement){
					Event.observe(businessResultElement, 'click', function(businessResultElementClickEvent) {
						businessResultElementClickEvent.stop();
						var businessResultElementHref = businessResultElement.down('a.view-detail').href;
						businessResultElementHref = businessResultElementHref.replace('http://'+location.hostname,'')
						_gaq.push(['_trackEvent', 'Business Finder', businessResultElementHref]);
//						new Ajax.Request('/businesses/detail/' + businessResultElement.down('a.view-detail').readAttribute('rel'), {
//							onComplete: function(t) {
//								var detailElementId = 'business_detail_' + businessResultElement.down('a.view-detail').readAttribute('rel');
//								var detailElement = $(detailElementId);
//								detailElement.update(t.responseText);
//								$$('div#' + detailElementId + ' div.collapse-bttn').invoke('show');
//								detailElement.appear({ duration: 0.5, afterFinish: function() {
//									setupBusinessDetailTabs();
//									Effect.ScrollTo(detailElementId, { duration: 0.5, offset: -20 });
//								}});
//							}
//						});
						//alert(businessResultElementHref);
						var businessId = businessResultElement.down('a.view-detail').readAttribute('rel');
						$('business_detail_' + businessId).src = '/businesses/detail/' + businessId;
						$('business_detail_' + businessId).appear({duration:0.5});
					});
				});
				
				if ($('results').getHeight() > 0) {
					$('finder-choices-inner').setStyle({ height: $('results').getHeight() + 380 + 'px' });
				}
			}
		});
	}
}
function setIframeHeight(elementId, height) {
	$$(elementId).each(function(e){ e.setStyle({'height':height}); });
}
/**
 * for multi-selectable options
 * @return
 */
function x_attachChoices() {
	$('finder-choices').down('div.choices').nextSiblings().invoke('hide');
	$$('#finder-choices div.choices li a').each(function(e) {
		if (!e.hasClassName('feature')) {
			Event.observe(e, 'click', function(clickEvnt) {
				clickEvnt.stop();
				_gaq.push(['_trackEvent', 'Business Finder', e.href.replace('http://'+location.hostname,'')]);
				if (e.readAttribute('rel') == 0) {
					e.up('li').nextSiblings().each(function(ee) {
						ee.down('a').removeClassName('selected');
					});
				} else {
					e.up('ul').down('li.all a').removeClassName('selected');
				}
				if (e.hasClassName('selected')) {
					e.removeClassName('selected');
				} else {
					e.addClassName('selected')
				}
	
				// check to make sure something is selected
				var nSelected = 0;
				e.up('div.choices ul').childElements().each(function(eee) {
					if (eee.down('a').hasClassName('selected')) {
						nSelected++;
					}
				});
				if (nSelected == 0) {
					e.up('ul').down('li.all a').addClassName('selected');
				}
				
				e.up('div.choices').next().appear({ duration: 0.3 });
				getSelected();
			});
		}
	});
}
/**
 * for single selectable options
 * @return
 */
function attachChoices() {
	$('finder-choices').down('div.choices').nextSiblings().invoke('hide');
	$$('#finder-choices div.choices li a').each(function(e) {
		if (!e.hasClassName('feature')) {
			Event.observe(e, 'click', function(clickEvnt) {
				clickEvnt.stop();
				//alert(e.href.replace('http://'+location.hostname,''));
				_gaq.push(['_trackEvent', 'Business Finder', e.href.replace('http://'+location.hostname,'')]);
				if (e.readAttribute('rel') == 0) {
					e.up('li').nextSiblings().each(function(ee) {
						ee.down('a').removeClassName('selected');
					});
				} else {
					e.up('ul').down('li.all a').removeClassName('selected');
				}
				if (e.hasClassName('selected')) {
					e.removeClassName('selected');
					e.up('ul').down('li.all a').addClassName('selected');
				} else {
					//e.up('ul').down('li a').invoke('removeClassName','selected');
					e.addClassName('selected');
					e.up('li').siblings().each(function(eeee){
						//alert(eeee.down('a'));
						eeee.down('a').removeClassName('selected');
					});
					//alert(e.up('li').nextSiblings());
//					return;
				}
	
				// check to make sure something is selected
				var nSelected = 0;
				e.up('div.choices ul').childElements().each(function(eee) {
					if (eee.down('a').hasClassName('selected')) {
						nSelected++;
					}
				});
				if (nSelected == 0) {
					e.up('ul').down('li.all a').addClassName('selected');
				}
				
				e.up('div.choices').next().appear({ duration: 0.3 });
				getSelected();
			});
		}
	});
}
function setupBusinessDetailTabs() {
	$$('body.business-detail div.tabs li a, body.business-detail ul.business-links a.map-link.google-map, div#finder_results_container div#results div.business-detail div.tabs li a, div#finder_results_container div#results div.business-detail ul.business-links a.map-link.google-map').each(function(e){
		Event.observe(e, 'click', function(clkEvnt){
			clkEvnt.stop();
			$$('body.business-detail div.tab-content, div#finder_results_container div#results div.business-detail div.tab-content').invoke('hide');
			$$('body.business-detail div.tabs li a, div#finder_results_container div#results div.business-detail div.tabs li a').invoke('removeClassName', 'selected');
			if (e.hasClassName('google-map')) {
				$$('body.business-detail div.tabs a.directions, div#finder_results_container div#results div.business-detail a.directions').invoke('addClassName','selected');
			} else {
				e.addClassName('selected');
			}
//			$(e.readAttribute('rel')).appear({ duration: 0.3 });
			if (e.hasClassName('google-map')) {
				//loadGoogleMapApi();
				var businessId = e.readAttribute('rel').replace(/directions_/, '');
				loadMap(businessId);
			}
			$(e.readAttribute('rel')).show();
		});
	});
	$$('body.business-detail div.tabs li a.google-map.selected, div#finder_results_container div#results div.business-detail div.tabs li a.google-map.selected').each(function(e){
		var businessId = e.readAttribute('rel').replace(/directions_/, '');
		loadMap(businessId);
	});
}
function closeFinder() {
	$('finder_results_container').hide();
	$('results').update();
	Effect.SlideUp('finder-choices', { duration: 0.5, afterFinish: function() { 
			$$('div.finder-top-nav li a').invoke('removeClassName','selected'); 
			//$('finder-choices').update();
			if (document.body.hasClassName('home')) {
				var interval = 6000; t = setInterval('fadeSlide()', interval);
			}
		} 
	});
}
function setupFinder() {
	$$('div.finder-top-nav li a').each(function(e){
//		Event.observe(e, 'click', function(msClkEvnt) { msClkEvnt.stop(); });
//		Event.observe(e, 'mouseover', function(msOvrEvnt) {
		Event.observe(e, 'click', function(msClkEvnt) {
			Event.stop(msClkEvnt);
			//alert(e.href.replace('http://'+location.hostname,''));
			_gaq.push(['_trackEvent', 'Business Finder', e.href.replace('http://'+location.hostname,'')]);
			clearTimeout(t);
			$('finder-choices').className = e.className;
			$$('div.finder-top-nav li a').invoke('removeClassName','selected');
			e.addClassName('selected');
			new Ajax.Updater('finder-choices', '/businesses/choices/' + e.readAttribute('rel'), {
				onComplete: function(t) {
					attachChoices();
//					setupBusinessDetailTabs();
					if (!$('finder-choices').visible()) {
						Effect.SlideDown('finder-choices', { duration: 0.5 });
					}
				}
			});
		});
	});
	setupBusinessDetailTabs();
}
