$(document).ready(function(){

	if ($.browser.msie) $('.telephones').css('zoom','1');
		
	var current_rooms = 1;
		
	/*dispara modal para todos los destinos*/
	$(function(){
		$('.nyroModal').nyroModal({
		callbacks:{
			afterShowBg: function(){
				$('.main-form select').css('visibility','hidden');
				$('.drop-down-link').removeClass('active');
				$('.drop-down').addClass('hidden');
				$('.telephones, .languages').css('z-index',-1);
			},
			afterClose: function(){
				$('.main-form select').css('visibility','visible');
				$('.telephones, .languages').css('z-index',10);
				//alert($('.block-field').first().html());
			}
		}});
	});
	
	/*caja pestañas*/
	$('.tabs a').click(
		function(){
			$('.tabs li').removeClass('active');
			$(this).parent('li').toggleClass('active');
			var ancla = $(this).attr('href');
			$('.content-tabbed .tab').addClass('hidden')
			$(ancla).removeClass('hidden');
			return(false);
		}
	)
	/*caja telefonos*/
	 $('.drop-down-link').click
		(function(){
			if($(this).is('.active')){
				$(this).removeClass('active');
				$(this).siblings('.drop-down').addClass('hidden');
			}
			else {
				$('.drop-down-link').removeClass('active');
				$('.drop-down').addClass('hidden');
				$(this).addClass('active');
				$(this).siblings('.drop-down').removeClass('hidden');
			}
			return(false);
	 });
	$('body').click(function(){
		 $('.drop-down').addClass('hidden');
		 $('.drop-down-link').removeClass('active');
	 })
	 

	$('input.search-hotel').click(
		function(){
			$(this).siblings('label').removeClass('active');
			$(this).next('label').addClass('active');
			$('#country-label-1').show();
			$('#country-label-2').hide();
			// cambiar el tipo de busqueda
			document.getElementById("tipoBusqueda").value = 'TIPO_BUSQUEDA_PAIS_DESTINO';
		}
	);
	$('label.search-hotel').click(
		function(){
			$(this).siblings('label').removeClass('active');
			$(this).addClass('active');
			$('#country-label-1').show();
			$('#country-label-2').hide();
			// cambiar el tipo de busqueda
			document.getElementById("tipoBusqueda").value = 'TIPO_BUSQUEDA_PAIS_DESTINO';
		}
	);
	$('input.search-flyhotel').click(
		function(){
			// redirigir a paquetes.riu
			window.location = "http://www.paquetes.riu.com/";
			//$(this).siblings('label').removeClass('active');
			//$(this).next('label').addClass('active');
			//$('#country-label-2').show();
			//$('#country-label-1').hide();
			// cambiar el tipo de busqueda
			//document.getElementById("tipoBusqueda").value = 'TIPO_BUSQUEDA_HOTEL';
		}
	);
	$('label.search-flyhotel').click(
		function(){
			// redirigir a paquetes.riu
			window.location = "http://www.paquetes.riu.com/";
			//$(this).siblings('label').removeClass('active');
			//$(this).addClass('active');
			//$('#country-label-2').show();
			//$('#country-label-1').hide();
			// cambiar el tipo de busqueda
			//document.getElementById("tipoBusqueda").value = 'TIPO_BUSQUEDA_HOTEL';
		}
	);
	
	$('label.infield').setInfield();
	
	$('#grupo_habitaciones').setRoomChooser();
})

/***********/
/* plugins */
/***********/

$.fn.setInfield = function() {
	return this.each(function() {

		$input = $(this).next('input');
		$(this).next('input').remove();
		$(this).wrap('<div style="position:relative; float:left">');
		$(this).after($input);
		$(this).css({'position':'absolute','top':3,'left':5 });
		
		$(this).click(function() { $(this).hide();$(this).next('input').focus()});
		

		$(this).parent().find('input').focus(function() {$(this).parent().find('label').hide();});
		$(this).parent().find('input').blur(function() {
			if ($(this).val() == '' || $(this).val() == undefined)
					$(this).parent().find('label').show();
		});
		
		if ($(this).parent().find('input').val()!='' && $(this).parent().find('input').val()!= undefined ) $(this).hide();

	
	});
}

$.fn.exists = function(){return $(this).length>0;};

$.combo = function(elements, values, sel, nombre, edadNinos) {

	output = '<select';
	if (nombre != 'undefined'){
		output += ' name="'+nombre+'" ';
	}
	output +='>';
	for (ix = 0 ; ix < elements.length ;ix++ ) {
		// esto es para los hoteles de ciudad
		if (edadNinos != 'undefined' && edadNinos == ix) break;
		element = elements[ix];		
		value=values[ix];
		if (sel != 'undefined' && sel == value) selected = 'selected = "true" ';
		else selected = '';
		output += '<option value="'+value+'" '+selected+' >'+element+'</option>';

	}
	
	return output += '</select>'; 
}


/* infernal explorer forces to do weird stuff */
function correct_sizes() {		
	w = $('.main-form-middle').width();
	w2 = w - 10 + 40;
	if (current_rooms == 2) p2 = 130;
	else p2 = 130;
	p = w - p2;		
	$('.main-form-top .left-border').css('width','10px');
	$('.main-form-bottom  .left-border').css('width','10px');
	$('.main-form-top .right-border').css('width',w2 + 'px');
	$('.main-form-bottom  .right-border').css('width',w2 + 'px');
	$('#submit-search').css('left',parseInt(p + 1 )+ 'px');
	$('#submit-search').css('left',p + 'px');
	//$('#submit-search').css('top',h2 + 'px');
}

/* Utils */
function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'),with_this);
}	

