window.addEvent('domready', function(){

	$$('.slider').each( function(el){
		new Slider(el);
	});

	if ($('mapcontainer') && GBrowserIsCompatible()) {
		doMap('mapcontainer');
	}

	// Google Analytics
	if(typeof _gat == "function"){
		var pageTracker = _gat._getTracker("UA-1609580-6");
		pageTracker._initData();
		pageTracker._trackPageview();
	}

});

function doMap(el){
	var map = new GMap2(document.getElementById(el));
	window.addEvent('unload', GUnload);
	var tinyIcon = new GIcon();
	tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	tinyIcon.iconSize = new GSize(12, 20);
	tinyIcon.shadowSize = new GSize(22, 20);
	tinyIcon.iconAnchor = new GPoint(6, 20);
	tinyIcon.infoWindowAnchor = new GPoint(5, 1);
	markerOptions = { icon:tinyIcon };
	// lat/long found just by typing it in google and getting it from the url
	var point = new GLatLng(30.917027,-99.786501);
	map.setCenter(point, 7);
	var marker = new GMarker(point, markerOptions);
	map.addOverlay(marker);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
}


var Slider = new Class({

	options: {
		// duration of the transition
		speed: 1000,
		// ms to wait before next slide
		waitTime: 7000,
		// CSS selector for each slide
		slideSel: '.slide',
		// CSS selector for next and previous
		prevSel: '#prev',
		nextSel: '#next',
		zIndex: '10'
	},

	initialize: function(el, options) {
		this.setOptions(options);
		this.element = $(el);

		// get an array of all slides
		this.slides = this.element.getElements(this.options.slideSel);
		this.total = this.slides.length;
		this.counter = 1;

		// don't show the arrows if there's only one image
		if (this.total > 1) {
			// assume there is only one previous and next, if at all
			this.element.getElements(this.options.prevSel).addEvent('click', this.handleEvent.bindWithEvent(this, [false]));
			this.element.getElements(this.options.nextSel).addEvent('click', this.handleEvent.bindWithEvent(this, [true]));
		} else {
			this.element.getElements(this.options.prevSel).remove();
			this.element.getElements(this.options.nextSel).remove();
		}
		// show the first one
		this.current = this.slides[0];
		this.current.setStyle('display', 'block');
		this.shifter = window.setTimeout( this.figureSlide.bind(this, [true]), this.options.waitTime );
	},

	handleEvent: function(e, next) {
		e.stop();
		this.figureSlide(next);
	},

	figureSlide: function(next) {

		// find the direction based on whether true or false was passed in
		if (next) {
			this.counter++;
		} else {
			this.counter--;
		}

		// allow for wrap-around
		if (this.counter > this.total) {
			this.counter = 1;
		} else if (this.counter < 1) {
			this.counter = this.total;
		}

		// call the actual effect for the new slide
		this.doSlide(this.counter);

		window.clearTimeout(this.shifter);
		this.shifter = window.setTimeout( this.figureSlide.bind(this, [true]), this.options.waitTime );
	},

	doSlide: function(n){

		// it won't be the current slide until the replaceSlide function goes
		var oldOne = this.current;
		var newOne = this.slides[n-1];
		var oldfx = oldOne.effect('opacity', { duration: this.options.speed, transition: Fx.Transitions.linear, fps: 100 });
		var newfx = newOne.effect('opacity', { duration: this.options.speed, transition: Fx.Transitions.linear, fps: 100,
			onComplete: this.replaceSlide.bind(this, {oldslide: oldOne, newslide: newOne})
		});
		newfx.set(0);
		oldOne.setStyle('z-index', '1');
		newOne.setStyles({
			'display': 'block',
			'z-index': this.options.zIndex
		});
		oldfx.start(0);
		newfx.start(1);
	},
	
	replaceSlide: function(obj){
		obj.oldslide.setStyle('display', 'none');
		this.current = obj.newslide;
	}	

});
Slider.implement(new Options);
