
var iev;
var plusonetimer;

function onReady()
{
	iev = getInternetExplorerVersion()
	onResize();
	$.fx.prototype.cur = function(){ // we are overwriting this function to remove limiting of the size of the

    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
		  return this.elem[ this.prop ];
		}
		
		var r = parseFloat( jQuery.css( this.elem, this.prop ) );
		return typeof r == 'undefined' ? 0 : r;
	}
	
	initContent();
	initAccordion();
	initScrollPane();
	initPanels();
	initHeaderLinks();
	initBGGlow();
	initShareForm();
	
	
	if($('#GoogleMap').length > 0)
		initGoogleMap();
	else loadSlideshow();
	
	//$('#GoogleMap').length > 0 ? initGoogleMap() : initSlideshow();
	$('#CreditsLink').hover(creditsHover);
	
	if(document.URL.indexOf("waikiki") >= 0) {
		$("#LanguageDD").iwsDropdown("#languageList", {initialValue:currentLanguage});
	}
	
	plusonetimer = setInterval(hidePlusOne,50);
}
function hidePlusOne() {
	if($("#___plusone_0").length > 0) {
		$("#___plusone_0").css("opacity","0");
		clearInterval(plusonetimer);
	}
}

function initShareForm() {
	$("#SocialCloseButton").click(function() {
		
		$("#ShareBox").animate({"height":"0px"});
		var h = 580;
		$("#ShareBoxWrapper").animate({ "height":"0px","top":h});
	});
	
	$("#LanguageCloseButton").click(function() {
		
		$("#ShareBox2").animate({"height":"0px"});
		var h = 560;
		$("#ShareBoxWrapper2").animate({ "height":"0px","top":h});
	});
	
	$("#OpenShareButton").click(function() {
		$("#LanguageCloseButton").trigger('click');
		$("#ShareBox").animate({"height":"40px"});
		$("#ShareBoxWrapper").animate({ "height":"80px"});
	});
	
	$("#OpenLanguageButton").click(function() {
		$("#SocialCloseButton").trigger('click');
		$("#ShareBox2").animate({"height":"60px"});
		$("#ShareBoxWrapper2").animate({ "height":"100px"});
	});
	
	$("#SendEmailButton").click(function() {
		var error = "";
		if($("input[name='mailfrom']").val() == "" || !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test($("input[name='mailfrom']").val())){
		error += "Please enter your email address\n"
	}
	if($("input[name='mailto']").val() == "" || !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test($("input[name='mailto']").val())){
		error += "Please enter your friend's email address\n"
	}
		
		if(error == "") {
			var data = $("#EmailShareForm").serialize();
			data.url = document.URL;
			$.post("/library/ajax/SendToFriend.cfm", data);
			$("#SendEmailButton").html("YOUR MESSAGE HAS BEEN SENT").css("cursor","default");
			$("#EmailShareForm input").val("");
			$("#EmailShareForm textarea").val("");
			setTimeout(resetSendButton,1800);
		} else alert(error);
	});
	
	function resetSendButton() {
		$("#SendEmailButton").html("SEND").css("cursor","pointer");
	}
	
	$("#EmailFormOpener").click(function() {
		$("#ShareBox").animate({"height":"164px"});
		var h = 580 - 124;
		$("#ShareBoxWrapper").animate({"top":h, "height":"189px"});
	});
}

function onResize()
{
	var newWrapperHeight = getBrowserHeight() - $('#Footer').height();
	if(newWrapperHeight < 650) newWrapperHeight = 650;

	$('#OuterWrapper').css('height', newWrapperHeight + 'px');
	$('#InnerWrapper').css('top', (newWrapperHeight - $('#InnerWrapper').height()) / 2 +9 + 'px');
	$('#Footer').css('width', $('#FooterMeasure').width() > 980 ? 'auto' : 980 + 'px');
	$("#OuterWrapper").css("visibility","visible");
	$('#Footer').css("visibility","visible");
}
function initBGGlow() {
	if(iev > 8.0 || iev == -1 || 1) {
		$(".glow_hover").mouseenter(function() {
			$(this).stop().animate({backgroundColor:'#fff'},300);	
		}).mouseleave(function() {$(this).stop().animate({backgroundColor:'#F2F2F2'},300);});
		
		$(".GlowBGContent").mouseenter(function() {
			$(".GlowBG",this).stop().animate({backgroundColor:'#fff',opacity:1},300);	
		}).mouseleave(function() {$(".GlowBG",this).stop().animate({backgroundColor:'#F2F2F2',opacity:0},300);});
		
		$(".GlowBG").each(function() {
			var ct = $($($(this).siblings()[0]).children()[0]);
			$(this).height(ct.height() +parseInt(ct.css("padding-top"))+parseInt(ct.css("padding-bottom")) );
			$(this).width(ct.width() + parseInt(ct.css("padding-left"))+parseInt(ct.css("padding-right"))+10);
			$($(this).siblings()[0]).css("z-index",4);
			$($(this).siblings()[0]).css("position","absolute");
		});
	} else {
		
	}
}

function initHeaderLinks() {
	$(".queueLink").click(function() {
		if($('#PanelCentered').length > 0){
			queuedLink = $(this).attr("data-link");
			$('#PanelCentered').animate({ left:'0px', width:'980px' }, 500,onLinkoutReady);
		} else {
			queuedLink = $(this).attr("data-link");	
			toggleBothPanels(false,onHotelNavReady);
		}
	});
}

function initContent()
{
	if($('#CopyContainerCentered').length > 0){
		$('#CopyContainerCentered').fadeTo(1000, 1);
	}
}

/* ACCORDION */
var sideNavAnimationSpeed = 0;
var tier1;
var tier1Height;
var tier2;
var tier2Height = [];
var queuedLink = "";
var sidenavReady = false;
var setup = true;

function initAccordion()
{
	
	tier1Height = $('#SideNavigation').children('li').length * $('#SideNavigation').children('li').height();
	$("#SideNavigation>li>ul>li>a").click(function() {
		$(this).closest('ul').children('li').children('a').removeClass('active');
		$(this).addClass('active');
	});
	
	$('#SideNavigation').css('top', ($('#LeftPanel').height() - tier1Height) / 2 + 'px');
	$('#SideNavigation').children('li').children('a').click(tier1Click);
	
	$('#SideNavigation').find('ul').each(function(index, value){
		var items = $(this).children('li');
		tier2Height[index] =  items.length * items.height();
		$(this).attr("data-expanded",items.length*items.height());
		$(this).css({ height:0 });
		
		$("a",this).each(function() {
			if($(this).attr("data-link") == unescape(window.location.pathname)) {
				$(this).addClass("active");
				$(this).click();
			}
			
			$(this).click(function() {
				if($(this).attr("data-link").indexOf("tour") > -1)
				{
					queuedLink = $(this).attr("data-link");
					toggleBothPanels(false,onHotelNavReady);
				}
				else {
					queuedLink = $(this).attr("data-link");
					toggleRightPanel(false,onHotelNavReady);
				}
			});
		});
	});

	var activeItemFound = false;	
	$('#SideNavigation').children('li').each(function() {
		var that = $(this);

		$("a",this).each(function() {
			if($(this).attr("data-link")+"/" == unescape(window.location.pathname)) {
				$(this).addClass("active");
				that.children('a').click();
				
				activeItemFound = true;
			}
		});
	});
	if(!activeItemFound) {
		var tempPathArr = unescape(window.location.pathname).split("/")
		tempPathArr = tempPathArr.splice(0,tempPathArr.length-2);
		var tempPath = tempPathArr.join("/");
		
		$('#SideNavigation').children('li').each(function() {
			var that = $(this);
			$("a",this).each(function() {
				if($(this).attr("data-link") == tempPath) {
					$(this).addClass("active");
					
					that.children('a').click();
					activeItemFound = true;
				}
			});
		});
	}
	
	if(!activeItemFound) {
		var tempPathArr = unescape(window.location.pathname).split("/")
		tempPathArr = tempPathArr.splice(0,tempPathArr.length-3);
		var tempPath = tempPathArr.join("/");
		
		$('#SideNavigation').children('li').each(function() {
			var that = $(this);
			$("a",this).each(function() {
				if($(this).attr("data-link") == tempPath) {
					$(this).addClass("active");
					that.children('a').click();
					activeItemFound = true;
				}
			});
		});
	}
	
	setup = false;
	sideNavAnimationSpeed = 500;
	sidenavReady = true;
	
	
	if( iev < 8.0 && iev > 0)
		positionForIE7();
	
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function positionForIE7() {
	var openItems = $(".SideNavigationOn","#SideNavigation").parent().children('ul').children('li');
	
	expansionHeight = openItems.length * 20;
	if(openItems.length > 0)
		expansionHeight -= 36;

	var newTop = (525 - tier1Height - expansionHeight) / 2;
	
	$('#SideNavigation').stop().animate({ top: newTop + 'px' },0);
}

function onHotelNavReady() {
	fadeSlideshow(false,onLinkoutReady);
}

function onLinkoutReady() {
	window.open(queuedLink,"_self");
}

function tier1Click()
{
	var clickedIndex = -1;
	var clickedItem = $(this)[0];
	var tier2Count = $(this).parent().children('ul').children('li').length;
	
	var found = false;
	/*if($(this).attr("data-override-link")) {
		queuedLink = $(this).attr("data-override-link");
		if($(this).attr("data-override-link").indexOf("") > -1)
			toggleBothPanels(false,onHotelNavReady);
		else
			toggleRightPanel(false,onHotelNavReady);
	}*/
	
	/*$("a",$(this).parent()).each(function() {
		$.log($(this).attr("data-link"));
		if(!found && $(this).attr("data-link")) {		
			//$(this).click();
			found = true;
		}
	});*/
	
	var expansionHeight;
	var isSpecials = false;
	var isContact = false;
	$('#SideNavigation').children('li').children('a').each(function(index, value){
		if(clickedItem == $(this)[0]){
			clickedIndex = index;

			if($(this).text() != "SPECIAL OFFERS") {
				
				if(tier2Count > 0){
					$(this).parent().animate({ marginTop:25 + 'px' },sideNavAnimationSpeed);
					$(this).parent().children('ul').animate({ height:$(this).siblings('ul').attr("data-expanded") + 'px', marginBottom:25 + 'px' },sideNavAnimationSpeed);
				} else {
					$(this).parent().animate({ marginTop:0 + 'px' },sideNavAnimationSpeed);
					$(this).parent().children('ul').animate({ height:0 + 'px', marginBottom:0 + 'px' },sideNavAnimationSpeed);
				}
			} else isSpecials = true;
			
			if($(this).text() != "CONTACT US") {
			} else isContact = true;
			
			$(this).addClass('SideNavigationOn');
		} else {
			$(this).parent().animate({ marginTop:0 + 'px' },sideNavAnimationSpeed);
			$(this).parent().children('ul').animate({ height:0 + 'px', marginBottom:0 + 'px' },sideNavAnimationSpeed);
			$(this).removeClass('SideNavigationOn');
		}
	});
	var openItems = $(".SideNavigationOn","#SideNavigation").parent().children('ul').children('li');
	var ignoreCount = 0;
	for(var i = 0;i < openItems.length;i++) {
		if($(openItems[i]).css("display") == "none") {
			ignoreCount++;
		}
	}
	expansionHeight = (openItems.length-ignoreCount) * 15 + 64;
	if((openItems.length-ignoreCount) > 0)
		expansionHeight -= 36;
		
	if(isSpecials || isContact)
		expansionHeight = 0;
		
	var newTop = (525 - tier1Height - expansionHeight) / 2 ;
	queuedLink = $(this).attr("data-link");
	var ch = $(this).siblings('ul').children('li').children('a');
	var activeFound = false;
	ch.each(
		function() { if($(this).hasClass('active')) {activeFound = true;}
	});
	
	if(ch[0]) {
		queuedLink = $(ch[0]).attr("data-link");
		if(!activeFound)
			$(ch[0]).addClass("active")
	}
	
	if($(clickedItem).html() == "SPECIAL OFFERS") {
		queuedLink = $(clickedItem).attr('data-link');
	}
	
	if(queuedLink.indexOf("tour") > -1) {
		toggleBothPanels(false,onHotelNavReady);	
	}
	else if(sidenavReady) {
		toggleRightPanel(false,onHotelNavReady);
		$('#SideNavigation').stop().animate({ top: newTop + 'px' },sideNavAnimationSpeed,handleSideAnimComplete);
	}
	else {
		toggleRightPanel(false,onHotelNavReady);
		$('#SideNavigation').stop().animate({ top: newTop + 'px' },sideNavAnimationSpeed);
	}
}

function handleSideAnimComplete() {
	window.open(queuedLink,"_self");
}

function initScrollPane()
{
	if($('.ScrollPane').length > 0){
 		$('.ScrollPane').css('height', 500 - $('.ScrollPane').position().top + 'px');
		$("#SpecialsScrollpane").css("height",440 - $('.ScrollPane').position().top + 'px');
		$(".SpecialOfferDetailScrollpane").css("height",430 - $('.ScrollPane').position().top + 'px');
		
		$('.ScrollPane').jScrollPane({ showArrows:false, verticalDragMaxHeight:17, verticalGutter:-9 });
		$(".jspDrag").css("height","17px");
	}
}

var panelsShowing = false;
var panelsDisabled = false;

function initPanels()
{
	if(panelsDisabled)
	{
		$("#SidePanelHider").css("visibility","hidden");
		return;
	}
	if($('#PanelCentered').length > 0){
		var spd = 500;
		if(unescape(window.location.pathname).indexOf("/special-offers/") > -1 && 
			unescape(window.location.pathname).split('/').length > 3)
			spd = 0;
		
		$('#PanelCentered').animate({ left:'225px', width:'530px' }, spd);
	} else {
		
		togglePanels();
	}
	$('#SidePanelHider').find('p').click(function() {
		togglePanels(500);
	});
}

function toggleRightPanel(on,callback) {
	if(on) {
		$('#RightPanel').stop().animate({ right:0 + 'px' },callback);
	} else $('#RightPanel').stop().animate({ right:-225 + 'px' },callback);
}

function toggleBothPanels(on,callback) {
	if($("#TourContainer").length > 0)
	{
		callback();
	} else {
		if(on) {
			$('#RightPanel').stop().animate({ right:0 + 'px' },750,callback);
			$('#LeftPanel').stop().animate({ left:0 + 'px' },750);
		} else {
			$('#RightPanel').stop().animate({ right:-225 + 'px' },750);
			$('#LeftPanel').stop().animate({ left:-225 + 'px' },750,callback);
			$('#RightPanelCentered').stop().animate({ right:-225 + 'px' },750);
			$('#LeftPanelCentered').stop().animate({ left:-225 + 'px' },750,callback);
			
		}
	}
}

function disablePanels() {
	panelsDisabled = true;
}

function togglePanels(left,right)
{
	var leftSpeed = 500;
	var rightSpeed = 500;
	if(unescape(window.location.pathname).split('/').length > 4 
		&& unescape(window.location.pathname).indexOf("/hotels/" > -1))
			leftSpeed = 0;
	
	if(unescape(window.location.pathname).indexOf("/special-offers/") > -1)
	{
		leftSpeed = 0;
		rightSpeed = 0;
	}
	
	if(left != undefined)
		leftSpeed = left;
	if(right != undefined)
		rightSpeed = right;
		
	panelsShowing = !panelsShowing;
	if(panelsShowing){
		$('#LeftPanel').stop().animate({ left:0 + 'px' },leftSpeed);
		$('#RightPanel').stop().animate({ right:0 + 'px' },rightSpeed);
		$('#SidePanelHider').find('p').text('HIDE PANELS')
	} else {
		$('#LeftPanel').stop().animate({ left:-225 + 'px' });
		$('#RightPanel').stop().animate({ right:-225 + 'px' });
		$('#SidePanelHider').find('p').text('SHOW PANELS');
	}
	leftSpeed = 500;
	rightSpeed = 500;
}

function creditsHover()
{
	if($(this).text() == 'CREDITS'){
		$(this).text('IDEAWORK STUDIOS');
	} else {
		$(this).text('CREDITS');
	}
}

function toggleOverlay(visible)
{
	if(visible){
		$('#Overlay').stop().fadeTo('', 0.5);
	} else {
		$('#Overlay').stop().fadeTo('', 0, function(){$(this).css('display', 'none');});
	}
}

/* SLIDESHOW */
function loadSlideshow() {
	initSlideshow();
}

var slides;
var slidesIndex = 0;
var swapSlides = true;
var defaultSlides = [{image:'/content/slides/slide1.jpg', duration:'5'}, {image:'/content/slides/slide2.jpg', duration:'5'}];
function initSlideshow(data)
{
	slides = defaultSlides;
	if(data)
		slides = data.slides;
		
	data = {"slides":
	    [{image:'/content/slides/blank.jpg', duration:'5'}]
    }
	slides = data.slides;
	
	if(slideshowData) {
		var dt = eval("("+slideshowData+")");
		if(dt.length > 0)
			slides = dt;
	}
	
	for(var i = 0;i < slides.length;i++) {
		if(slides[i].image.indexOf("content") == -1) {
			slides[i].image = "/content/slides/"+slides[i].image;
		}
	}
	
	if(slides.length > 0){
		$('#Slide1').bind('load', loadInitialSlide);
		$('#Slide1').attr('src', slides[slidesIndex].image);
	}
}


function fadeSlideshow(on,callback) {
	if($("#TourContainer").length > 0)
	{
		callback();
		return;
	}
	
	if(on) {
		if($('#GoogleMap').length > 0)
			$("#GoogleMap").fadeIn(500,callback);
		else $("#SlideContainer").fadeIn(1000,callback);
	} else {
		if($('#GoogleMap').length > 0)
			$("#GoogleMap").fadeOut(500,callback);
		else $("#SlideContainer").fadeOut(1000,callback);
	}
}

function loadInitialSlide()
{
	$('#Slide1').unbind('load', loadInitialSlide);
	$('#Slide1').fadeTo(1000, 1);
	
	if(slides.length > 1){
		$('#Slide2').attr('src', slides[++slidesIndex].image);
		setTimeout(nextSlide, 4000);
	}
}

function nextSlide()
{
	slidesIndex++;
	if(slidesIndex >= slides.length) slidesIndex = 0;
	changeSlide();
}

function prevSlide() 
{
	slidesIndex--;
	if(slidesIndex < 0) slidesIndex = slides.length-1;
		changeSlide();
}

function changeSlide() {
	if(swapSlides){
		$('#Slide2').fadeTo(1000, 1);
		$('#Slide1').fadeTo(1000, 0);
	} else {
		$('#Slide1').fadeTo(1000, 1);
		$('#Slide2').fadeTo(1000, 0);
	}
	swapSlides = !swapSlides

	setTimeout(loadSlide, 1000);
	setTimeout(nextSlide, 4000);
}

function loadSlide()
{
	if(swapSlides){
		$('#Slide2').attr('src', slides[slidesIndex].image);
	} else {
		$('#Slide1').attr('src', slides[slidesIndex].image);
	}
	//setTimeout(nextSlide, 500);
}


function initGoogleMapIE7() {
	var lng = $("#GoogleMap").attr("data-longitude");
	var lat = $("#GoogleMap").attr("data-latitude");
	
	 var map = new GMap2(document.getElementById("GoogleMap"));
     map.setCenter(new GLatLng(lat, lng), 13);
	 map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());

	  var point = new GLatLng(lat,lng);
          map.addOverlay(new GMarker(point,{title:GoogleMapMarketTitle}));
		  
		  
	setTimeout(function(){ $('#GoogleMap').fadeTo(500, 1);
	$("img[src='http://maps.gstatic.com/intl/en_us/mapfiles/smc.png']").closest('div').css("left","230px").css("bottom","10px");
	$("div[title='Show street map']").parent().css("right","230px")
	$("a[title='Click to see this area on Google Maps']").parent().css("left","234px");
	$(".terms-of-use-link").parent().css("right","230px");
	 }, 300);
	
}

var bounds;
function initGoogleMap()
{

	if(iev < 8 && iev > -1) {
		initGoogleMapIE7();
		return;
	}
	
	var lng = $("#GoogleMap").attr("data-longitude");
	var lat = $("#GoogleMap").attr("data-latitude");
	var latLng = new google.maps.LatLng(lat, lng);
	var zoomOptions = { position:google.maps.ControlPosition.BOTTOM_CENTER, style:google.maps.ZoomControlStyle.SMALL };
	var mapTypes = [google.maps.MapTypeId.HYBRID,google.maps.MapTypeId.ROADMAP,google.maps.MapTypeId.Satellite];
	
	var mapTypeOptions = { position:google.maps.ControlPosition.TOP_CENTER, 
							style:google.maps.MapTypeControlStyle.HYBRID,
							mapTypeIds:[google.maps.MapTypeId.ROADMAP,google.maps.MapTypeId.SATELLITE]};

    var mapOptions = {
      	zoom:13,
     	center:latLng,
	 	panControl:false,
	 	streetViewControl:false,
	  	zoomControlOptions:zoomOptions,
		mapTypeControlOptions:mapTypeOptions,
     	mapTypeId:google.maps.MapTypeId.ROADMAP
    };
	
    var map = new google.maps.Map(document.getElementById('GoogleMap'), mapOptions);
	
	var marker = new google.maps.Marker({
		map:map,
      	position:latLng,
      	title:GoogleMapMarketTitle
  	});
	
	google.maps.event.addListener(marker, 'click', function() {
		map.setZoom(13);
		map.setCenter(latLng);
	});

	setTimeout(function(){ $('#GoogleMap').fadeTo(500, 1); }, 300);
}

/* HELPER FUNCTIONS */
function shortenLink()
{
	bitly.shorten($(this).text(), $(this), function(url, el){
		$(el).attr('target', '_blank');
		$(el).attr('href', url);
		$(el).text(url);
		$('.ScrollPane').jScrollPane().data('jsp').reinitialise();
	});
}

function getToday()
{
	var date = new Date();
	var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
	var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
	var dateString = month + '/' + day + '/' + date.getFullYear();
	
	return dateString;
}

function getBrowserWidth()
{
	var browserWidth = 0;
  	if( typeof( window.innerWidth ) == 'number' ) {
		browserWidth = window.innerWidth;
  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		browserWidth = document.documentElement.clientWidth;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		browserWidth = document.body.clientWidth;
  	}
	
  	return browserWidth;
}

function getBrowserHeight()
{
	var browserHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		browserHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		browserHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		browserHeight = document.body.clientWidth;
	}
	
	return browserHeight;
}

function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
	  if (x==c_name)
		{
		return unescape(y);
		}
	  }
}

