window.addEvent('domready', function() {
	/************************************************************
	 Villa Gallery
	******/
	if ($$('div#villa_gallery').length == 1) {
		if ($$('div#villa_gallery div#sectionWrap div').length > 0) {
			
			var items = $$('div#villa_gallery div#sectionWrap div').length / 2; // divided by two because of our nested 't' div for style purposes
			var pag = $$('ul.pag li');
			var cur = window.location.hash;

			// Check if a hash value has been passed thru the URL.. if so.. set this as the default one to start on...
			pattern = /\#g_img_([0-9])/;
			if (matches = cur.match(pattern)) {
				// We've got a hash passed in.. 
				pag.removeClass('active');
				pag[matches[1]-1].addClass('active');
			}

			scrl = new Fx.Scroll2('sectionOuter', {
				wait: false,
				duration: 500,
				transition: Fx.Transitions.Quad.easeInOut,
				wheelStops: false,
				offset: {
					'x':-1,
					'y':0
				}
			});

			pag.addEvent('click', function(e) {
				e.stop();
				$clear(gallTick); // clear the tick

				var el = this.getElement('a').get('href').replace('#','');

				pag.removeClass('active');

				this.addClass('active');
				scrl.toElement(el);
				if ($('gallery_pause').getProperty('class') == 'gallery_pause')
					startGalleryTick();
			});

			$('gallery_prev').addEvent('click', function(e) {
				$clear(gallTick); // clear the tick
				gallNav('prev');
				if ($('gallery_pause').getProperty('class') == 'gallery_pause')
					startGalleryTick();
			});

			$('gallery_next').addEvent('click', function(e) {
				$clear(gallTick); // clear the tick
				gallNav('next');
				if ($('gallery_pause').getProperty('class') == 'gallery_pause')
					startGalleryTick();
			});
			
			$('gallery_pause').addEvent('click', function(e) {
				if ($('gallery_pause').getProperty('class') == 'gallery_pause')
				{
					$clear(gallTick); // clear the tick
					$('gallery_pause').removeClass('gallery_pause');
					$('gallery_pause').addClass('gallery_play');
				}
				else
				{
					gallNav('next');
					startGalleryTick();
					$('gallery_pause').removeClass('gallery_play');
					$('gallery_pause').addClass('gallery_pause');
				}
			});

			function gallNav(d) {
				
				// get the current position
				var el = $$('ul.pag li.active')[0].getElement('a').get('href');
				
				var thisMatch = parseInt(el.replace('#g_img_',''));
								
				if (thisMatch) {
					pag.removeClass('active');
					
					if (d == 'prev') {
						if (thisMatch == 1) {
							var num = parseInt(items);
							pag[num-1].addClass('active');
							scrl.toElement($('g_img_' + num)); // Go to the last element
						} else {
							var num = parseInt(thisMatch-1);
							pag[num-1].addClass('active');
							scrl.toElement($('g_img_' + num)); // Go to minus 1
						}
					} else {
						if (thisMatch == items) {
							pag[0].addClass('active');
							scrl.toElement($('g_img_1')); // Go to the first element
						} else {
							var num = parseInt(thisMatch+1);
							pag[num-1].addClass('active');
							scrl.toElement($('g_img_' + num)); // Go to plus 1
						}
					}
				}


			}


			function startGalleryTick() {
				gallTick = (function() {
					gallNav('next');
				}).periodical('6000'); // change every 6 seconds..
				
			}
 
			startGalleryTick();

		}


	}
});
