var quickPhotosArray = new Array();
var thumbIdx = 0;

$(document).ready(function() {
	$('.rightScrollableNav a.next').click(function() {
		updateScrollable($(this), 'next')
	});
	$('.rightScrollableNav a.prev').click(function() {
		updateScrollable($(this), 'prev')
	});
	setScrollableHeight();

	//remove the target="_blank" from google mini search results
	$(".txtContent .boxSearchResults table td a[target='_blank']").attr('target', '');

	// create custom tooltip effect for jQuery Tooltip 
	$.tools.tooltip.addEffect("slidedownup",
	// opening animation 
	    function(done) {
	    	this.getTip().slideDown().show();
	    },
	// closing animation 
	    function(done) {
	    	this.getTip().slideUp(function() {
	    		$(this).hide();
	    		done.call();
	    	});
	    }
	);

	$('.hlOneWidePhoto').fancybox({ type: 'image' });

	$(document).ready(function() {

	// BEGIN MAIN DROP DOWN NAVIGATION
	// documentation - flowplayer.org/tools/tooltip.html

	$(".nav_1").tooltip({
	    relative: true,
	    position: 'bottom right',
	    offset: [-7, -193],
	    effect: 'toggle',
	    onBeforeShow: function() {
	        $('.nav_1').addClass('navActive');
	    },
	    onHide: function() {
	        $('.nav_1').removeClass('navActive');
	    }
	});

	$(".nav_2").tooltip({
	    relative: true,
	    position: 'bottom right',
	    offset: [-7, -193],
	    effect: 'toggle',
	    onBeforeShow: function() {
	        $('.nav_2').addClass('navActive');
	    },
	    onHide: function() {
	        $('.nav_2').removeClass('navActive');
	    }
	});

	$(".nav_3").tooltip({
	    relative: true,
	    position: 'bottom right',
	    offset: [-7, -193],
	    effect: 'toggle',
	    onBeforeShow: function() {
	        $('.nav_3').addClass('navActive');
	    },
	    onHide: function() {
	        $('.nav_3').removeClass('navActive');
	    }
	});


	// END MAIN DROP DOWN NAVIGATION




	//more link if in use
	$("#thirdLevelMoreLink").tooltip({
	    relative: true,
	    position: 'bottom right',
	    offset: [0, -52],
	    effect: 'slidedownup'
	});

		//jQuery UI accordion - easy to toggle states
		/*$("#accordion").accordion({
		collapsible: true,
		autoHeight: false,
		active: false
		});*/

		$(".scrollableSpecials").scrollable({
			mousewheel: true,
			circular: false,
			speed: 300,
			onSeek: function(evt, idx) {
				var parentContainer = this.getItemWrap().parent().parent();
				parentContainer.find('.pageIndexSpecials').html(idx + 1);
			}
		});

		//BEGIN HOME GALLERY
		//GALLERY: TABS
		// initialize GALLERY-PHOTOS scrollable
		//        $(".scrollableContainer").scrollable({
		//            mousewheel: true,
		//            circular: false,
		//            speed: 300,
		//            onSeek: function(evt, idx) {
		//                var parentContainer = this.getItemWrap().parent().parent();
		//                parentContainer.find('.pageIndex').html(idx + 1);
		//            }
		//        });


		//END HOME GALLERY





		//BEGIN HOME SEO BOX
		$('#boxSEO').delay(2500).slideUp('slow', function() {
			// Animation complete - now fade in open button
			$('#boxSEOBtnOpen').show();
		});

		$('#boxSEOBtnClose').click(function() {
			$('#boxSEO').slideUp('slow', function() {
				// Animation complete - now fade in open button
				$('#boxSEOBtnOpen').show();
			});

		});

		$('#boxSEOBtnOpen').click(function() {
			$('#boxSEO').slideDown('slow');
			$('#boxSEOBtnOpen').hide();
		});
		//END HOME SEO BOX

		// quick photo gallery
		$('.quickPhotoGalleryThumbnail').click(function() {
			var newSrc = $(this).attr('large');
			var thumb = $(this).attr('src');
			thumbIdx = $('.quickPhotoGalleryThumbs:visible').find('.quickPhotoGalleryThumbnail').index($(this));

			var large = $(this).parent().parent().parent().find('.quickPhotoGalleryLargeImage');
			var fullSize = $(this).parent().parent().parent().find('.viewFullsize')
			$(fullSize).fadeOut('fast', function() {
				$(large).fadeOut('fast', function() {
					$(this).load(function() {
						$(this).fadeIn('fast');
						$(fullSize).fadeIn('fast');
					});
					$(this).attr('src', newSrc);
					$(this).attr('thumb', thumb);
				});
			});
		});

		// lightbox for quick photo gallery
		buildQuickPhotoGalleryArray();

		function setMinDepartureDate(dateText, datepicker) {
			var dt = new Date(dateText);
			dt.setDate(dt.getDate() + 1);
			$('#departureDate').datepicker('option', 'minDate', dt);
			if ($('#departureDate').val() == '') {
				$('#departureDate').datepicker('setDate', dt);
			}
		}
		function setMaxArrivalDate(dateText, datepicker) {
			var dt = new Date(dateText);
			dt.setDate(dt.getDate() - 1);
			$('#arrivalDate').datepicker('option', 'maxDate', dt);
		}
		$('#bookCalendar').click(function() {
			$('#arrivalDate').trigger('focus');
		});
		$('#btnSubmitBooking').click(function() {
			submitBookingWidget();
		});
		function submitBookingWidget() {
			var urlToUse = "https://booking.ihotelier.com/istay/istay.jsp";
			var queryString = "?HotelId=2009&LanguageID=1&Rooms=1&DateIn=" + $('#arrivalDate').val();
			queryString += "&DateOut=" + $('#departureDate').val();
			var a = $('#Adults').val();
			var c = $('#Children').val();
			queryString += "&Adults=" + a + "&Children=" + c;
			window.open(urlToUse + queryString, "iHotelier");
		}
		/* end booking widget */


		/* landing page promo gallery */
		$(".scrollablePromoGallery").scrollable({
			onBeforeSeek: function(evt, idx) {
				$('.scrollablePromoGallery').css('width', '648px');
			}
		});

		function repositionImage(container, image) {
			var containerHeight = $(container).height();
			var imgHeight = $(image).height();

			var diff = parseInt((imgHeight - containerHeight) / 2);
			$(image).css('margin-top', '-' + diff + 'px');
		}

		repositionImage($('.promoGalleryPhoto'), $('.promoGalleryPhoto img.promoGalleryLarge'));

		// fix each thumb - comment out if we want to vertically center each thumbnail
		/*
		$('.scrollablePromoGallery .thumbnail img').each(function() {
		repositionImage($(this).parent(), $(this));
		});
		*/

		$(".scrollablePromoGallery .thumbnail img").click(function() {
			var dataContainer = $(this).parent().parent();
			var title = $(dataContainer).find('.promoTitle').text();
			var description = $(dataContainer).find('.promoDescription').html();
			var linkText = $(dataContainer).find('.linkText').text();
			var linkUrl = $(dataContainer).find('.linkUrl').text();
			var linkTarget = $(dataContainer).find('.linkTarget').text();
			var fullImage = $(this).attr('full');

			$('.promoGalleryMidWrapper').fadeOut('fast', function() {
				$('.promoGalleryTitle').html(title);
				$('.promoGalleryDescription').html(description);

				if (linkUrl != '') {
					$('.promoGallerySelectedButton').text(linkText);
					$('.promoGallerySelectedButton').attr('href', linkUrl);
					$('.promoGallerySelectedButton').attr('target', linkTarget);
					$('.promoGallerySelectedButton').show();
				}
				else {
					$('.promoGallerySelectedButton').hide();
				}

				$('.promoGalleryPhoto img.promoGalleryLarge').load(function() {
					repositionImage($('.promoGalleryPhoto'), $(this));
					$('.promoGalleryMidWrapper').fadeIn('fast');
				});

				$('.promoGalleryPhoto img.promoGalleryLarge').attr('src', fullImage);

			});
		});
		/* end landing page promo gallery */

		$(".quickPhotoGalleryThumbs img[title]").tooltip({
			tipClass: 'quickPgTooltip',
			effect: 'slide',
			offset: [10, 0]
		});
	});
	function buildQuickPhotoGalleryArray() {
		quickPhotosArray = new Array();
		$('.quickPhotoGalleryThumbs:visible').find('.quickPhotoGalleryThumbnail').each(function() {
			quickPhotosArray.push($(this).attr('full'));
		});
	}
	function addFormField(form, fieldName, fieldValue) {
		var inputField = document.createElement("input");
		inputField.setAttribute("name", fieldName);
		inputField.setAttribute("id", fieldName);
		inputField.setAttribute("type", "hidden");
		inputField.setAttribute("value", fieldValue);
		form.appendChild(inputField);
	}
});

function setScrollableHeight() {
	var intervalIdx = 0;
    
    var scrollableInterval = setInterval(function() {
        if (intervalIdx < 5) {
            $('.scrollableItem.active').each(function() {
                var myHeight = $(this).height() + 20;
                var container = $(this).parents('.scrollableContainer');
                $(container).animate({
                    height: myHeight
                }, 'fast');
            });
            intervalIdx++;
        }
        else {
            clearInterval(scrollableInterval);
        }
    }, 500);
}
function updateScrollable(navItem, direction) {
    var container = $(navItem).parents('.rightScrollableNav').siblings('.scrollableContainer');
    var allItems = $(container).find('.scrollableItem');
    var itemCount = $(allItems).length;
    var visibleItem = $(container).find('.scrollableItem.active');
    var visibleItemIndex = $(visibleItem).index();
    var navContainer = $(navItem).parents('.scrollableNav');
    var navPrev = $(navContainer).find('.prev');
    var navNext = $(navContainer).find('.next');
    var nextItemIndex = 0;

    if (direction == 'next') {
        nextItemIndex = (itemCount - 1 == visibleItemIndex) ? 0 : (visibleItemIndex + 1);
    }
    else {
        nextItemIndex = (visibleItemIndex == 0) ? (itemCount - 1) : (visibleItemIndex - 1);
    }
    var nextItem = allItems[nextItemIndex];

    $(visibleItem).fadeOut('fast', function() {
    	$(this).removeClass('active');
    	var thisHeight = $(nextItem).height();
    	var newContainerHeight = thisHeight;
		$(container).animate({
			height: newContainerHeight
		}, 'fast', function() {
			$(nextItem).fadeIn('fast', function() {
				$(this).addClass('active');
				$(navContainer).find('.index').html(nextItemIndex + 1);
				if (nextItemIndex == 0) {
					//$(navPrev).hide();
					$(navPrev).addClass('disabled');
				}
				else {
					//$(navPrev).show();
					$(navPrev).removeClass('disabled');
				}
				if (nextItemIndex == itemCount - 1) {
					//$(navNext).hide();
					$(navNext).addClass('disabled');
				}
				else {
					//$(navNext).show();
					$(navNext).removeClass('disabled');
				}
			});
		});
    });
}

var iHeroIndex = 0;
var rotateSpeed = 6000;
function updateHero() {
	iHeroIndex++;
	if (iHeroIndex > heroImgs.length - 1) {
		iHeroIndex = 0;
	}

	//$('#hero-loading').show();
	var oldBgImage = $('.bgMain');
	var newBgImage = $(oldBgImage).clone();
	$(newBgImage).css('z-index', 5);
	var img = $(newBgImage).find('img.hero');
	$(img).remove();
	var newImg = $('<img></img>');
	$(newImg).addClass('hero');
	$(newImg).load(function() {
		$(oldBgImage).fadeOut(700, function() {
			$(this).remove();
			$(newBgImage).css('z-index', 10);
			//$('#hero-loading').hide();
			setTimeout(function() {
				updateHero();
			}, rotateSpeed);
		});
	});
	$(newBgImage).append($(newImg));
	$('.bgWrap').append($(newBgImage));
	$(newImg).attr('src', heroImgs[iHeroIndex]);
}
$(function() {
	if (heroImgs != null && heroImgs != undefined && heroImgs.length > 1) {
		setTimeout(function() {
			updateHero();
		}, rotateSpeed);
	}
});
