var noobSlide=new Class({initialize:function(a){this.items=a.items;this.mode=a.mode||'horizontal';this.modes={horizontal:['left','width'],vertical:['top','height']};this.size=a.size||240;this.box=a.box.setStyle(this.modes[this.mode][1],(this.size*this.items.length)+'px');this.button_event=a.button_event||'click';this.handle_event=a.handle_event||'click';this.onWalk=a.onWalk||null;this.currentIndex=null;this.previousIndex=null;this.nextIndex=null;this.interval=a.interval||5000;this.autoPlay=a.autoPlay||false;this._play=null;this.handles=a.handles||null;if(this.handles){this.addHandleButtons(this.handles)}this.buttons={previous:[],next:[],play:[],playback:[],stop:[]};if(a.addButtons){for(var b in a.addButtons){this.addActionButtons(b,$type(a.addButtons[b])=='array'?a.addButtons[b]:[a.addButtons[b]])}}this.fx=new Fx.Tween(this.box,$extend((a.fxOptions||{duration:500,wait:false}),{property:this.modes[this.mode][0]}));this.walk((a.startItem||0),true,true)},addHandleButtons:function(a){for(var i=0;i<a.length;i++){a[i].addEvent(this.handle_event,this.walk.pass([i,true],this))}},addActionButtons:function(a,b){for(var i=0;i<b.length;i++){switch(a){case'previous':b[i].addEvent(this.button_event,this.previous.pass([true],this));break;case'next':b[i].addEvent(this.button_event,this.next.pass([true],this));break;case'play':b[i].addEvent(this.button_event,this.play.pass([this.interval,'next',false],this));break;case'playback':b[i].addEvent(this.button_event,this.play.pass([this.interval,'previous',false],this));break;case'stop':b[i].addEvent(this.button_event,this.stop.create({bind:this}));break}this.buttons[a].push(b[i])}},previous:function(a){this.walk((this.currentIndex>0?this.currentIndex-1:this.items.length-1),a)},next:function(a){this.walk((this.currentIndex<this.items.length-1?this.currentIndex+1:0),a)},play:function(a,b,c){this.stop();if(!c){this[b](false)}this._play=this[b].periodical(a,this,[false])},stop:function(){$clear(this._play)},walk:function(a,b,c){if(a!=this.currentIndex){this.currentIndex=a;this.previousIndex=this.currentIndex+(this.currentIndex>0?-1:this.items.length-1);this.nextIndex=this.currentIndex+(this.currentIndex<this.items.length-1?1:1-this.items.length);if(b){this.stop()}if(c){this.fx.cancel().set((this.size*-this.currentIndex)+'px')}else{this.fx.start(this.size*-this.currentIndex)}if(b&&this.autoPlay){this.play(this.interval,'next',true)}if(this.onWalk){this.onWalk((this.items[this.currentIndex]||null),(this.handles&&this.handles[this.currentIndex]?this.handles[this.currentIndex]:null))}}}});



	window.addEvent('domready', function() {

		if ($('map-container')) {
			//have the map zoomed in on the appropriate house on page load
			if ($('lucky-101-map')) $('map-container').scrollTo(575, 300);
			if ($('regent-101-map')) $('map-container').scrollTo(0, 556);
			if ($('highlander-house-101-map')) $('map-container').scrollTo(975, 50);
		}


		if($('map')) {
			var map = $('map');
			var min = 938; //allows the image to fit in the div that does not have the same aspect ratio of the image
			var max = $('map').getStyle('width').toInt();
			
			//map.setStyle('width', min);

			var myDragScroller = new Drag('map-container', {
				style: false,
				invert: true,
				modifiers: {x: 'scrollLeft', y: 'scrollTop'}
			});

			var mySlider = new Slider('slider', 'handle', {
				range: [min, max],
				steps: 3,
				onChange: function(width) {
					map.setStyle('width', width);
				}
			});
			
			$('container').addEvents({
				dblclick: function() { zoom_in(mySlider); },
				mousewheel: function(e) {
					e.stop();
					if(e.wheel > 0) { zoom_in(mySlider); }
					else if(e.wheel < 0) { zoom_out(mySlider); }
				}
			});
			$('zoom-out').addEvent('click', function() { zoom_out(mySlider); });
			$('zoom-in').addEvent('click', function() { zoom_in(mySlider); });
			
			//zoom_in(mySlider); // hack :(
			mySlider.set(1829); // set slider to fully zoomed-in

		}


	if($('slide-box') != undefined) {
		$$('a.handle').addEvent('click',function(e) {
			e.stop();
		});
		var slideShow = new noobSlide({
			box: $('slide-box'),
			items: [0,1,2],
			size: 584,
			autoPlay: true,
			handles:$$('.handle'),
			onWalk:function(currentItem,currentHandle) {
				//make sure all captions are faded out
				var oldHandleId = $$('.active').get('id');
				var oldCaption = $('cap'+oldHandleId);
				oldCaption.fade('out');
				//fade the current caption in
				var newHandleId = currentHandle.get('id');
				var newCaption = $('cap'+newHandleId);
				newCaption.fade('in');
				  
				//change the photo
				this.handles.removeClass('active');
				currentHandle.addClass('active');
			}
		});
		

	}

	//image gallery
	if($('photos-box') != undefined) {
		var photoShow = new noobSlide({
			box: $('photos-box'),
			items: $$('#photos-box span'),
			handles:$$('.thumbnail'),
			size: 584,
			autoPlay: false,
			onWalk:function(currentItem,currentHandle) {
				//make sure all captions are faded out
				var oldHandleId = $$('.active_thumb').get('id');
				var oldCaption = $('cap'+oldHandleId);
				oldCaption.fade('out');
				//fade the current caption in
				var newHandleId = currentHandle.get('id');
				var newCaption = $('cap'+newHandleId);
				newCaption.fade('in');
				  
				// set the active thumbnail
				this.handles.removeClass('active_thumb');
				currentHandle.addClass('active_thumb');
			}
		});
	}

	if($('home-slide-box') != undefined) {
		$$('a.handle').addEvent('click',function(e) {
			e.stop();
		});
		var slideShow = new noobSlide({
			box: $('home-slide-box'),
			items: [0,1,2],
			size: 589,
			autoPlay: true,
			handles:$$('.handle'),
			onWalk:function(currentItem,currentHandle) {
				//make sure all captions are faded out
				var oldHandleId = $$('a.active').get('id');
				var oldCaption = $('cap'+oldHandleId);
				oldCaption.fade('out');
				//fade the current caption in
				var newHandleId = currentHandle.get('id');
				var newCaption = $('cap'+newHandleId);
				newCaption.fade('in');

				this.handles.removeClass('active');
				currentHandle.addClass('active');
			}
		});
		

	}

	if((load_video = $$('a.load_video')).length) {
		load_video.addEvents({
			click: function(e) {
			var videoId = this.get('id');
			//make an ajax call to get a description of this part note
			e.stop();//prevent the page from changing
			//make the ajax call, replace text
			dispVideo(videoId, true);
			}
		});
	}

	if((thumbs = $$('.thumbnail')).length) {
		thumbs.addEvents({
			click: function(e) {
				/// hide previous captions
//				$$('.gallery-callout-text').setStyle('visibility', 'hidden');
//				$$('.gallery-callout-text').setStyle('opacity', '0');
			}
		});
	}

});

	document.ondragstart = function () { return false; };//fix dragging of map in IE
	
	function zoom_in(slider) {
		//zoom out
		slider.set(slider.step + slider.stepSize);
		// calculate where to scroll to
		var scroll = $('map-container').getScroll();
		var newX = ((slider.step / (slider.step - slider.stepSize)) * (scroll.x + ($('map-container').getStyle('width').toInt() / 2))) - (($('map-container').getStyle('width').toInt() / 2));
		var newY = ((slider.step / (slider.step - slider.stepSize)) * (scroll.y + ($('map-container').getStyle('height').toInt() / 2))) - (($('map-container').getStyle('height').toInt() / 2));
		//scroll to the calculated area
		$('map-container').scrollTo(newX, newY);		
	}
	function zoom_out(slider) {
		//zoom in
		slider.set(slider.step - slider.stepSize);
		// calculate where to scroll to
		var scroll = $('map-container').getScroll();
		//below can be read as new image dimension / old image dimension * current scroll value + one half of the map container size - one half of the map conatiner size (basically, find the difference between the old and new image dimension and readjust the center point based on that ratio)
		var newX = (((slider.step - slider.stepSize) / slider.step) * (scroll.x + ($('map-container').getStyle('width').toInt() / 2))) - (($('map-container').getStyle('width').toInt() / 2));
		var newY = (((slider.step - slider.stepSize) / slider.step) * (scroll.y + ($('map-container').getStyle('height').toInt() / 2))) - (($('map-container').getStyle('height').toInt() / 2));
		//scroll to the calculated area
		$('map-container').scrollTo(newX, newY);
	}

	function dispVideo(videoId, autoplay) {
			//make the ajax call, replace text
			var req = new Request.HTML({
				method: 'get',
				url: '/includes/ajax/ajaxResponse.php',
				data: { 'call' : 'loadVideo',
					'id' : videoId,
					'autoplay' : autoplay},
				update: $('video-content')
			}).send();
	};

