images = ['/images/gallery/gallery_66[1].jpg', '/images/gallery/gallery_61[1].jpg', '/images/gallery/gallery_173[1].jpg', '/images/gallery/gallery_72[1].jpg', '/images/gallery/gallery_127[1].jpg', '/images/gallery/gallery_167[1].jpg', '/images/gallery/gallery_49[1].jpg', '/images/gallery/gallery_67[1].jpg', '/images/gallery/gallery_74[1].jpg', '/images/gallery/gallery_57[1].jpg', '/images/gallery/the_lounge.jpg'];
descriptions = ['Tower Guestroom', 'Presidential Suite, Bedroom', 'Hyatt Regency Pier Sixty-Six Resort & Spa', 'Easily accessible marina', 'Picturesque views of Fort Lauderdale', 'Newly renovated Lobby', 'Three-Pool Waterfall Oasis', '', 'Pier Top Sunday Brunch', 'Panorama Terrace, Cocktail Reception', 'The Lounge'];
imgThemes=[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1];
themes=[0, 2]

noImages = images.length;
currentImg=0;
allLoaded = false;
imgLoaded = [true];
for (j=1;j<noImages; j++){
	imgLoaded[j] = false;
}

$(window).load(function(){
	loadImage(currentImg);

// close button
	if(document.referrer.indexOf(window.location.host) > 0) {
		$('#info_bar .close').click(function(){
			$(this).attr('href', '#')
			history.back();
			return false;
		});
	}

// left and right nav buttons
	$('#l_arrow').click(function(){
		iSlideRight();
		return false;
	});
	$('#r_arrow').click(function(){
		iSlideLeft();
		return false;
	});

// theme navigation
	$('#gallery_nav a:eq(0)').click(function(){
		if (currentImg != themes[0]) {		
			if(imgLoaded[themes[0]]) {
				$('.images_wrap').stopTime('slideshow');
				$('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
				$('.images_wrap').stopTime('slideshow');
				$('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
				nextImg = themes[0];
				moveImage(nextImg, 'fw');
			}
		}	
		return false;
	});
	$('#gallery_nav a:eq(1)').click(function(){
		if (currentImg != themes[1]) {	
			if(imgLoaded[themes[1]]) {
				$('.images_wrap').stopTime('slideshow');
				$('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
				$('.images_wrap').stopTime('slideshow');
				$('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
				nextImg = themes[1];
				moveImage(nextImg, 'fw');
			}
		}
		return false;
	});
	
});

function loadImage(i) {
	if(i+1 < noImages) {
		$('.images img:eq('+i+')').after('<img class="image image'+(++i)+'" alt="" src="'+images[i]+'">');
		$('.image'+i).load(function(){
			imgLoaded[i] = true;
			loadImage(i);
			if(i == noImages-1)
				allLoaded = true;
				startSlideshow();
		});
	}
}
function startSlideshow() {
	if(allLoaded) {
		$('.images_wrap').everyTime(5000, 'slideshow', function(){
			nextImg = currentImg + 1;
			if(nextImg == noImages) nextImg = 0;
			moveImage(nextImg, 'fw');
		});
	}
}
function iSlideLeft() {
	$('.images_wrap').stopTime('slideshow');
	$('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
	if((currentImg < noImages-1) && imgLoaded[currentImg+1]) {
		nextImg = currentImg + 1;
	} else if(currentImg == noImages-1) {
		nextImg = 0;
	}
	moveImage(nextImg, 'fw')
}
function iSlideRight() {
	$('.images_wrap').stopTime('slideshow');
	$('.images_wrap').oneTime(5000, 'slideshow', startSlideshow);
	if(currentImg > 0){
		nextImg = currentImg - 1;
	} else if((currentImg == 0) && imgLoaded[noImages-1]) {
		nextImg = noImages-1;
	}
	moveImage(nextImg, 'bk')
}
function moveImage(nextImg, dir) {
	if (dir == 'fw') {
		$('.images img:eq('+nextImg+')').css({left: '1330px'}).animate({left: '0px'});
		$('.images img:eq('+currentImg+')').animate({left: '-1330px'});
	} else if (dir == 'bk') {
		$('.images img:eq('+nextImg+')').css({left: '-1330px'}).animate({left: '0px'});
		$('.images img:eq('+currentImg+')').animate({left: '1330px'});
	} else {
		return false;
	}
	currentImg = nextImg;

	$('#gallery_nav a').removeClass('on');
	$('#gallery_nav a:eq('+imgThemes[currentImg]+')').addClass('on');
	$('.info p').html(descriptions[currentImg]);
	if( $('.info p').height() > 16 ) {
		$('.info p').animate({marginTop: '-7px'});
	}  else {
		$('.info p').animate({marginTop: '0px'});
	}
	$('#quantity').html(currentImg+1 + ' of ' + noImages)
}

