var $j = jQuery.noConflict();
$j(document).ready(function()
{
	
/* Funcion para la caja de Reservaciones que define cual de las opciones se va a usar: Hotel ó HOtel + Avion */	
	var RadioVal = $j('input[name=ReservRadios]:checked').val();	
	if (RadioVal == "HF")
	{
		$j('#DivLeavingFrom').show();
	} 
	else
	{
		$j('#DivLeavingFrom').hide();
	}
	
	$j('#RadioHotel').click(function()
	{
		$j('#DivLeavingFrom').hide();		
	});
	
	$j('#RadioHotelAvion').click(function()
	{
		$j('#DivLeavingFrom').show();		
	});
		
	$j('#Adults_1').val(2);
	$j('#Rooms').val(1);
	
	for (i=2; i<=5; i++) { $j('#RGroup'+ i).hide();	}	
	for (i=1; i<=5; i++) { $j('#ComboEdadNino'+ i).hide(); }
		
	$j('#Rooms').live('change', function()
	{
		var count = $j('#Rooms').val();
		
		for (i=2; i<=5; i++) { $j('#RGroup'+ i).hide(); }		
		for (i=2; i<=count; i++) { $j('#RGroup'+ i).show();	}
		
		var ContCombos = 0;
		$j('.Edades div').each(function()		
		{									
			if ($j(this).is(':visible')) { ContCombos = ContCombos + 1;	}
		});
						
		if (count < ContCombos)
		{
			for (i=1;i<=5; i++) { $j('#ComboEdadNino'+ i).hide(); }			
			for (i=1; i<= count; i++) { $j('#ComboEdadNino'+ i).show(); }
		}			
		
		var tc = parseInt(count) + 1;		
		for (i=tc; i<=5; i++) {	$j('#Children_' + i).val(0); $j('#Adults_' + i).val(1);}					
	});
		
	MuestraChildren = function(Identificador)
	{
		var array = $j('#'+Identificador).attr('id').split('_');
		var Tot = $j('#'+Identificador).val();
						
		if (Tot != 0)
		{
			$j('.Edades').show();						
			$j('#ComboEdadNino' + array[1]).show();
			
			for (i=1; i<=3; i++) { $j('#EdadNino' + array[1] + '_' + i).hide(); }			
			for (i=1; i<=Tot; i++) { $j('#EdadNino' + array[1] + '_' + i).show(); }
		}
		else
		{
			$j('#ComboEdadNino' + array[1]).hide();
			var Cont = 	$j('#Rooms').val();			
			for(i=1; i<=Cont; i++)
			{				 
				var valor = $j('#Children_'+i).val();
				if (valor != 0) { break; }
				else { $j('.Edades').hide(); }
			}				
		}
	}			
	
	if ($j('#HotelID').val() == "1209")
		{
			$j('.Children').hide();	
		}	
		
	$j('#HotelID').live('change', function()
	{
		
		if ($j(this).val() == "1209" || $j(this).val() == "487")
		{
			$j('.Children').hide();	
		}
		else
		{
			$j('.Children').show();	
		}
	});
							
/* ********************************************************************************************************** */	
	
	
/* INICIO FUNCIONES MOSTRAR/OCULTAR HABITACIONES Y TARIFAS ************************************************** */

    $j('.toggleDet').click(function(){
		$j(this).parents('.descHab').children('.detallesHab').toggle(500);
        return false;
    });

	/**  PROMO HABITACION **/	
	$j('.promoCompleta').each(function(){
		if(parseInt($j(this).height()) == 20){
			$j(this).children('.vPromoCompleta').remove();
		}
	});

    $j('.promoCompleta').each(function(i){
		$j(this).data('myHgt', $j(this).height()).css('height', '20px');
	});
		
    $j('.vPromoCompleta').click(function () {

        if (parseInt($j(this).parent('.promoCompleta').height()) < $j(this).parent('.promoCompleta').data('myHgt')) {
            $j(this).parent('.promoCompleta').animate({
                height: $j(this).parent('.promoCompleta').data('myHgt')
            }, 300);
        } else {
            $j(this).parent('.promoCompleta').animate({
                height: 20
            }, 300);
        }

        return false;
    });
	
    var tooltip_modal = '<div class="promoModal"></div>';

    $j('.promoTip').click(function (e) {

        _idSpl = this.id.split('_');
        _tip = $j('#promo_' + _idSpl[1]);

        _tipOff = $j(this).offset();

        if ($j('.promoModal').length > 0) {
            $j('.promoModal').remove();
        }
        $j('body').append(tooltip_modal);
        $j('.promoModal').text(_tip.text()).prepend('<div class="cerrar"><img src="/_lib/images/cerrar_promo.gif"/></div>');
        $j('.promoModal').css({
            top: _tipOff.top - 100,
            left: _tipOff.left - 100
        });

        if ($j('.promoModal').length > 0) {
            if (e.stopPropagation) {
                e.stopPropagation();
            } else if (window.event) {
                window.event.cancelBubble = true;
            }
            $j(':not(.promoTip)').live('click', function () {
                $j('.promoModal').remove();
                $j(this).die('click');
            });
        }

    });
	
	/* POLITICAS DE CANCELACION HABITACIONES NUEVAS */

	var getRoomInfo = function(tip, idioma) {

		$j('body').append(tooltip_modal);
		//$j('div',$j('.tooltip')).html('<img src="/_lib/java/jquery/ligthbox-images/lightbox-ico-loading-ESP.gif" />');
		tipOff = tip.offset();
		$j('.promoModal').css({
			left: tipOff.left - 180,
			top: tipOff.top - 300,
			height: 270,
			overflow: 'auto'
		});
		$j('.promoModal').show();
	
		var toGet = tip.attr('rel'); //Habitaci�n
	
		if ($j('input[name=IAN_' + toGet + '_cancellationPolicy]').length < 1) {
			return false;
		}
	
		var targetContent = $j('input[name=IAN_' + toGet + '_cancellationPolicy]').val();
	
		if (targetContent != '') {
			$j('.promoModal').html(targetContent).prepend('<div class="cerrar"><img src="/_lib/images/cerrar_promo.gif"/></div>'); ;
		}
	
		$j('.promoModal').live('click', function() {
			$j(this).remove();
		});
	
		if ($j('.promoModal').length > 0) {
			$j(':not(.promoModal)').live('click', function() {
				$j('.promoModal').remove();
				$j(this).die('click');
			});
		}
	}

	if (!!$j('.Info').length) {
		$j('.Info').click(function() {
			getRoomInfo($j(this));
			return false;
		});
	}
/*

/* Imagenes con zoom */
	var imgDisplay = '<div class="img_display"></div>';
	var galeria_display = function() {
		$j('a[ref=zoom]').mouseover(function() {
			if ($j('.img_display').length < 1) {
				var me = $j(this).offset();
				$j('body').append(imgDisplay);

				if (me.left <= $j('body').width()) {
					$j('.img_display').html('<img src="' + $j(this).attr('rel') + '" />').css({
						left: me.left + parseInt($j('img', $j(this)).width()) + 5,
						top: me.top - (parseInt($j('img', $j(this)).height()))
					});
				} else {
					$j('.img_display').html('<img src="' + $j(this).attr('rel') + '" />').css({
						left: me.left - parseInt($j('.img_display').width()) - 5,
						top: me.top - (parseInt($j('img', $j(this)).height()))
					});
				}
			}
		});
		$j('a[ref=zoom]').mouseout(function() {
			$j('.img_display').remove();
		});
		$j('a[ref=zoom]').click(function() {
			if (/\#/.test($j(this).attr('href'))) {
				return false;
			}
		});
	} ();
	
/* *************************************************************************************************************************** */	
	
/* Funcion para ls gallerias de las habitaciones ***************************************************************************** */	
$j('.RoomGallery .thumbs a').click(function(){ // <-- 
		
		//Guarda la ruta de la imagen que vamos a cargar en la variable ruta
		var ruta = $j(this).attr('ref');
		
		//Si la ruta de la imagen es igual a la imagen que ya está cargada
		if(ruta == $j('.BigPic img').attr('src')){
			//se termina la función
			return false;
		}
		//Cambia el src por "ruta"
		$j('.BigPic img').attr('src',ruta);
		
		//Esconde la imagen un ratito
		$j('.BigPic img').hide();
		
		
		$j('.BigPic img').load(function(){
			//aparece la imagen con fade in
			$j('.BigPic img').fadeIn();			
		});
		
		return false;
	});			
	var rutaInicial = $j('.RoomGallery .thumbs a:first').attr('ref');	
	$j('.BigPic').html('<img src="'+rutaInicial+'" />');
/* *************************************************************************************************************************** */
	
/* Funcion que activa la pestaña del menu actual ***************************************************************************** */
	$j('#nav').find('li').each(function(i)
	{		
		if ($j(this).is('.selected'))	
		{
			if ($j(this).is('#list'))
			{
				$j(this).find('span').removeClass('dir');
				$j(this).find('span').addClass('dirOn');	
			}
			else
			{
				$j(this).find('a').removeAttr('href');
				var css={'text-decoration':'none',
				         'text-shadow':'0 1px 0 #CEC8AF',
						 'color':'#444445'};
				$j(this).find('a').css(css);
			}	
		}
	});
/* *************************************************************************************************************************** */

/* Funciones de ayuda o informacion de tipo OnmouseOver y OnmouseOut de las habitaciones */

	var Housekeeping = '<h2>Royal Service&reg; Featuring Royal Housekeeping&reg;</h2><ul class="OneCol"><li>NEW! Innovative (Mexico´s first) online check-in available within 48 hours of arrival</li><li>NEW! Bose stereo with DVD/CD/Radio plus loaded iPod with music for all tastes &amp; complimentary DVD library (except for Royal Junior Suites)</li><li>NEW! Rotating 32" LCD satellite TV for multiple-position viewing from bed, Jacuzzi or terrace</li><li>Upgraded mini-bar, liquor cabinet system and snack selection</li><li>NEW! Decide your own time for daily suite make-up and special, comprehensive evening turndown service including choice of 3 aromatherapy scents (romantic, relaxing or invigorating)</li><li>Decide your own time for daily suite make-up and special, comprehensive evening turndown service including choice of 3 aromatherapy scents (romantic, relaxing or invigorating)</li><li>NEW! Pillow Menu: Extra Soft Plus Hypo-allergenic; Orthopedic; Memory; Aromatic; and Sweet Dreams</li><li>His &amp; Hers plush bathrobes and slippers</li><li>Luxurious bath amenities</li><li>NEW! Special welcome amenity</li><li>Complimentary wireless Internet access in public areas and suites (available from Royal Jr. Ste. Oceanfront and upper categories) and complimentary internet access in the business center</li><li>Royal Concierge &amp; Romance Concierge ready to anticipate, assist, advise and take care of your every need</li></ul>';
        		
	var Bed = "<h2>Royal Bed</h2> <p>The new white-on-white plush Royal Bed adds an incomparable level of style and comfort to Reals new luxury ROYAL hotel brand. Designed to pamper guests with a luxurious combination of excellent support and extreme comfort, the bed features an oversized hand-carved mahogany headboard, bed skirt, plush custom designed ten-inch pillow-top mattress set, and a soft pillow-top pad covered by a quilted mattress cover. The bed is further complemented with two pillows from each of the three comfort levels (Support, Soothe, Soft) for a total of six pillows. The bedding features 300-thread-count Egyptian cotton sheets, a hypo-allergenic comforter nestled inside a cotton duvet cover, decorative pillows and a colorful bed scarf. Plus, an extensive pillow menu is available for guests staying in the Royal Service suites, ensuring pillow selections that match each guests personal preferences... Sweet Dreams!</p>";				

	var tooltiphtml = "<div id='tt2' style='display:none; width:300px;'><div id='tt2top'></div><div id='tt2cont'></div><div id='tt2bot'></div></div>"
	
	$j('body').append(tooltiphtml);
	$j('.tooltip').hover(
	function()
	{			
		$j('#tt2cont').html(eval($j(this).attr('id')));
		$j('#tt2').css({top: $j(this).offset().top - 100, left: $j(this).offset().left + 300});
		$j('#tt2').show();
		
	},
	function()
	{
		$j('#tt2').hide();
	});
	
	
	$j('.Assistence').click(function(){
		
		offset = $j(this).offset();
		
		console.log(parseInt($j('body').width()) - parseInt($j(this).width()));
		console.log(offset.left);
	
		if(offset.left - parseInt($j('body').width()) <= parseInt($j('body').width()) / 2){
			$j('#help').css({left: offset.left - 150, top: offset.top - 20});    
		}else{
			$j('#help').css({left: offset.left + 220, top: offset.top - 20});    
		}
		
		
		
		$j('#help').show();	
	});
	
	$j('.AssistenceHM').click(function()
	{
		$j('#help').css({left: $j(this).offset().left + 220, top: $j(this).offset().top - 20});    
		$j('#help').show();	
	});
	
	$j('#Close').click(function()
	{
		$j('#help').hide();
	});
	
	$j('#Closehm').click(function()
	{
		$j('#helphm').hide();
	});
	
	//var Off = $j('#DivLeavingFrom').offset();
	
	$j('#mhelp').live('click',function()
	{				
		var Off = $j(this).offset();		
		$j('#helphm').css({left: Off.left + 30 , top: Off.top}); 
		$j('#helphm').show();
	});
	
	
	
/* *************************************************************************************************************************** */
//AQUI menu desplegable //////////////////////////////////
		 $j("ul.dropdown li").hover(function(){
		   $j(this).addClass("hover");
		   $j('> .dir',this).addClass("open");
		   $j('ul:first',this).css('visibility', 'visible');
		 },function(){
		   $j(this).removeClass("hover");
		   $j('.open',this).removeClass("open");
		   $j('ul:first',this).css('visibility', 'hidden');
		 });
//ACA//////////////////////////////////////////////////
/*---------------------------------------------- Tarjetas de Credito -------------------------------------------------------*/

	$j('#CreditCard').click(function()
	{		
		$j('.modal_mensualidades').show();	
		$j('.cerrar_modal').show();
	});
	
	$j('.cerrar_modal').click(function()
	{
		$j('.modal_mensualidades').hide();
		$j(this).hide();
	});

/*---------------------------------------------- Tarjetas de Credito -------------------------------------------------------*/

	$j('#CheckRate').click(function()
	{		
		var RadioValue = $j('input[name=ReservRadios]:checked').val();
		if (RadioValue == "HF")
		{						
			if ($j('#Leavingfrom').val() == "City Name or Airport Code")
			{								
				alert("Invalid City Name or Airport Code");				
				$j('#Leavingfrom').focus();
				return(false);
			}
		}
	});

/*------------------------------------------------------------------------------------------------------------------------------*/

	verificaTipoOnSubmit = function ()
	{ 
		var hotel;
		hotel = $j('#HotelID').val(); ///document.getElementById("HotelID").value;		
		url = '/Reservations/'; 
		
		if (hotel == "230")
		{
			url = url +  'Gran-Porto-Real';
		}
		if (hotel == "5")
		{
			url = url +  'Gran-Caribe-Real';
		}
		if (hotel == "231")
		{
			url = url +  'Real-Playa-Carmen';
		}
		if (hotel == "487")
		{
			url = url +  'The-Royal-Playa-Carmen';
		}
		if (hotel == "1209")
		{
			url = url +  'The-Royal-Cancun';
		}
	
		var RadioVal = $j('input[name=ReservRadios]:checked').val();	
		if (RadioVal == "H")
		{			
			url = url + '/default.asp'; 
			$j('#forma').attr('action',url);			
		} 
		else
		{
			url = url + '/defaultPackages.asp'; 
			$j('#forma').attr('action',url);
		}
	}
});
