﻿/// <reference path="../../script/jquery-1.3.2-vsdoc.js" />

/*
Hotel Link Bar interaction setup
*/
COMO.HotelLinkBar = (function($)
{
	var slideshows = [];

	function hidePanel(element)
	{
		// requires an <a> element from the link bar
		$(element).removeClass('active').nextAll('.content').hide();

		// turn off image slideshow
		var previewImages = $(element).parent().find('.preview-image').get();
		var index = $('#hotellinkbar .preview-image').index(previewImages);
		if (index >= 0 && slideshows[index])
		{
			slideshows[index].pauseEngine();
		}
	}

	function showPanel(element)
	{
		// requires an <a> element from the link bar
		$(element).addClass('active').nextAll('.content').show();

		// turn on image slideshow
		var previewImages = $(element).parent().find('.preview-image').get();
		var imageContainer = $(element).parent().find('.preview-image');
		for (var i = 0; i < $(imageContainer).size(); i++)
		{
			var targetFile = $(imageContainer).eq(i).attr("title") + ".html";
			$(imageContainer).eq(i).load("/hotels/cocoa/central/controls/hotellinkbar_images/" + targetFile);
		}
		var index = $('#hotellinkbar .preview-image').index(previewImages);
		if (index >= 0 && slideshows[index])
		{
			slideshows[index].unpauseEngine();
		}
	}

	function hideFreezePanel(skip)
	{
		$('#freezepanel').hide();

		if (!skip)
		{
			// restart main image slideshow if exists
			if (COMO.PageImages && COMO.PageImages.unpause)
			{
				COMO.PageImages.unpause();
			}

			// restart intro page highlight slideshow if exists
			if (COMO.Intro && COMO.Intro.Highlights && COMO.Intro.Highlights.unpause)
			{
				COMO.Intro.Highlights.unpause();
			}
		}
	}

	function showFreezePanel(skip)
	{
		$('#freezepanel').show();

		if (!skip)
		{
			// stop main image slideshow if exists
			if (COMO.PageImages && COMO.PageImages.pause)
			{
				COMO.PageImages.pause();
			}

			// stop intro page highlight slideshow if exists
			if (COMO.Intro && COMO.Intro.Highlights && COMO.Intro.Highlights.pause)
			{
				COMO.Intro.Highlights.pause();
			}
		}
	}

	function setup()
	{
		// set up the image slideshows for each panel
		$('#hotellinkbar .preview-image').each(function(i)
		{
			// this == the matched element
			slideshows[i] = new imageslideshowEngine();
			slideshows[i].images = $('img', this).get();
			slideshows[i].init();
			slideshows[i].pauseEngine();
		});

		//Show/hide tab for hotels/estates
		$('#hotellinkbar .linkblock a').click(
      function(e)
      {
      	var thisClass, otherClass;
      	if ($(this).parents('.linkblock').hasClass('hotels'))
      	{
      		thisClass = 'hotels';
      		otherClass = 'estates';
      	}
      	else
      	{
      		thisClass = 'estates';
      		otherClass = 'hotels';
      	}

      	hideFreezePanel(true);
      	//$('#' + otherClass).hide();
      	$('#hotels').fadeIn("slow");
      	
      	e.preventDefault();
      	
      	if (thisClass == 'estates')
      	{
      	    
      	    if ($('#hotellinkbar .' + thisClass).hasClass('linkblockactive'))
      	    {
      	        $('#shambala_panel').hide();
      	        $('#hotellinkbar .' + thisClass).removeClass('linkblockactive');
      	        $('#hotellinkbar .' + otherClass).addClass('linkblockactive');
      	    }
      	    else
      	    {
      	        $('#shambala_panel').show();
      	        $('#hotellinkbar .' + otherClass).removeClass('linkblockactive');
      	        $('#hotellinkbar .' + thisClass).addClass('linkblockactive');
      	        hidePanel($('.propertylist a.property.active'));
      	    }
      	}
        else
        {
      	    $('#shambala_panel').hide();
      	    $('#hotellinkbar .' + otherClass).removeClass('linkblockactive');
      	    $('#hotellinkbar .' + thisClass).addClass('linkblockactive');
        }	
      }
    );

		// setup the link actions for the bar
		$('.propertylist a.property').click(
			function(e)
			{
				if (!$(this).hasClass('activelink'))
				{
					if ($(this).hasClass('active'))
					{
						hidePanel(this);
						hideFreezePanel();
					}
					else
					{
						hidePanel($('.propertylist a.property.active'));
						showPanel(this);
						showFreezePanel();
					}
					e.preventDefault();
				}
				
				$('#shambala_panel').hide();
			    $('#hotellinkbar .estates').removeClass('linkblockactive');
      	        $('#hotellinkbar .hotels').addClass('linkblockactive');
			}
		);


		$("#shortbreakcheckallcheckbox").click(function()
		{
			var checkStatus = this.checked;
			$("input[@type=checkbox]").each(function()
			{
				this.checked = checkStatus;
			})
		});


		// setup the close button actions in the content
		$('#hotellinkbar .close').click(function(e)
		{
			hidePanel($(this).parents('div.content').prevAll('a')[0]);
			$('#shambala_panel').hide();
			$('#hotellinkbar .estates').removeClass('linkblockactive');
      	    $('#hotellinkbar .hotels').addClass('linkblockactive');
			e.preventDefault();
		});
		
		
		//shambala panel works slightly differently because anchor is in top nav.
		$('#shambala_panel_close').click(function(e)
		{
			$('#shambala_panel').hide();
			$('#hotellinkbar .estates').removeClass('linkblockactive');
      	    $('#hotellinkbar .hotels').addClass('linkblockactive');
			hideFreezePanel();
			e.preventDefault();
		});
		
		
		

		//fix layout problem
		if (jQuery("#linkboxes").children("li").size() > 1)
		{
			jQuery("#email-print").removeClass("movedbyscript");
			jQuery("#email-print").css("position", "relative");
			jQuery.each(jQuery.browser, function(i, val)
			{
				var coefficient;
				if (val)
				{
					coefficient = -160;
					if (i == "msie" && jQuery.browser.version.substr(0, 3) == "7.0")
					{
						coefficient = -190;
					}
					jQuery("#email-print").css("top", (coefficient * jQuery("#linkboxes").children("li").size()).toString() + "px");
				}
			});
		}
	}

	return {
		setup: setup
	};

})(jQuery);

// initialise click events
jQuery(COMO.HotelLinkBar.setup);

function highlightPropertyType(type)
{
	setTimeout(function() { jQuery('#hotellinkbar .linkblock.hotels a').click(); }, 2000);
}

function hideBookingBar()
{
	jQuery("#bookingbartrigger").hide();
}


function hilightSelectedMenuItem(parentCode, code)
{
	var mainNavigationContainer = "#mainnavigation";
	var subNavigationContainer = "#subpagelinks";
	var pageNavigationContainer = ".pagenav";
	var depth;
	if (code.substring(0, 1) == 0)
	{
		depth = 0;
	}
	else if (code.substring(0, 1) == 1)
	{
		depth = 1;
	}
	else if (code.substring(0, 1) == 2)
	{
		depth = 2;
	}

	code = "default.aspx?code=" + code;
	//Select only <a> tags with the link that contains seltecteditem    
	jQuery(mainNavigationContainer + " a[href$='" + code + "']").css("background-color", "#E3E3E3");

	if (depth == 0)
	{
		jQuery(".pagenav a[href$='" + code + "']").css("background-color", "#E3E3E3");
	}
	else if (depth == 1)
	{
		jQuery(mainNavigationContainer + " a[href$='" + code + "']").parent().parent().parent().css("background-color", "#E3E3E3");
		jQuery(subNavigationContainer + " a[href$='" + code + "']").parent().css("background-color", "#E3E3E3");
	}
	else if (depth == 2)
	{
		jQuery(subNavigationContainer + " a[href$='" + code + "']").parent().css("background-color", "#E3E3E3");
		jQuery(mainNavigationContainer + " a[href$='" + parentCode + "']").parent().parent().parent().css("background-color", "#E3E3E3");
		jQuery(mainNavigationContainer + " a[href$='" + parentCode + "']").css("background-color", "#E3E3E3");
	}
}

function stretchText()
{
	jQuery("#content.page #bodyholder #linkboxes li .body > *").css("margin-right", "0px");
	jQuery("#content.section .bodytext #linkboxes li .body > *").css("margin-right", "0px");
	jQuery("#content.page #bodyholder #linkboxes li .body img").css("display", "none");
	jQuery("#content.section .bodytext #linkboxes li .body img").css("display", "none");
}

function moveEmailPrintPosition(promotion)
{
	jQuery("#email-print").addClass("movedbyscript");
	jQuery("#email-print").parent().children("ul").eq(0).before("<div id=\"space\"></div>");
	if (promotion == 'no_promotion')
	{
		jQuery("#email-print").parent().children("#space").css("height", "160px");
	}
}
