$(document).ready(function(){
	
	$('#prix').bind('click',function()
	{
		target = $('#end');
		$.scrollTo( target, { speed:1000} ); 
		$('#more_info > ul').tabs('select', 1);
		$.scrollTo( target, { speed:1000} ); 
	});
	
	

	
	$(".date").date_input();
	
	$('.hotel_line').bind('mouseover',function()
	{
		$(this).addClass('hotel_line_hover');
	})
	.bind('mouseout',function()
	{
		//$(this).css('background','#ffffff');
		$(this).removeClass('hotel_line_hover');
	});
	
	$('p.notice').fadeOut(5000);

	$('input#finishButton').bind('click', function()
	{
		var id_hotel =  $('#hotel_hotel_id').val();
		var goto = site_uri + 'hotel/add_temporada/' + id_hotel + "/";
		$('#room_form').attr('action', goto);
		$('#room_form').submit();
	})

	$('A[rel="popup"]').click( function() {
        var opciones="toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=550,height=250,top=85, left=140";
        
        window.open( $(this).attr('href') ,'',opciones);
        return false;
    });	
     
    $('a.doSearch').bind('click',function()
    {
    	var el = $(this).attr('href');
    	$(el).selectOptionsByText($(this).text());
    	$('#adv_search').submit();
    	return false;
    });
    
    //boton busqueda x nombre del hotel
    $('#nameSearch').bind('click', function()
    {
	    var el = $(this).attr('href');
    	if ( $(el).is(':visible') )
    	{
	    	$('#normalSearch').show();
    		$('#advanced').hide();    	
    		$(el).hide();
    	}
    	else
    	{
	    	$('#normalSearch').hide();
    		$('#advanced').hide();    	
    		$(el).show();
    	}
    	return false;
    });

    //boton busqueda avanzada
    $('#advancedSearch').bind('click', function()
    {
    	var el = $(this).attr('href');
    	if ( $(el).is(':visible') )
    	{
    		//advanced
    		$('#advancedSearch').children('img').attr('src','images/advanced_' + iso + '.jpg');//$('#advancedSearch').children('img').src('images/simple_' + iso + '.jpg');
    		$('#normalSearch').show();
    		$('#nombreHotel').hide();    	
    		$(el).hide();
    	}
    	else
    	{
    		//busqueda simple
    		$('#advancedSearch').children('img').attr('src','images/simple_' + iso + '.jpg');//src(site_uri + 'static/frontend/default/);
    		$('#normalSearch').show();
    		$('#nombreHotel').hide();    	
    		$(el).show();
       	}
    	return false;
    });
    
    //ajax para comprobar la direccion del hotel
    $('#checkAddress').bind('click', function()
    {
    	var address = '?loc=' + $('#direccion').val() + '&cp=' + $('#cp').val() + '&city=Eivissa&country=ES&format=xml&google2=1';
		$.ajaxSetup({
		  dataType : 'script'
		});
    
    	
	    $.post(site_uri + "hotel/getXMLDireccion/",{
             loc: $('#direccion').val(),
             cp: $('#cp').val(),
             city: "Eivissa",
             country: "ES",
             format : 'xml'
           }, function(xml) {
				var resultado = new Array();
				resultado = xml.split("|");
				if (resultado[0] != '')
				{
					$('#latitud').val(resultado[0]);
					$('#longitud').val(resultado[1]);
				}
				else
				{
					alert("I can't find lat/long for this address '"+$('#direccion').val()+"'")				
				}
       		}); 
    });
    
    //boton busqueda x hotel
    $('#gotoHotel').bind('change', function()
    {
    	var loc = site_uri  + 'hotel/show_id/' + $(this).val();
    	$('#adv_search').attr({target:'_top',action:loc});
    	$('#adv_search').submit();
    	//document.location.href = site_uri  + 'hotel/show_id/' + $(this).val();
    });
    
    
    //cambiador del select de zonas
    $('#zonaSelect').bind('change', function()
    {
    	var zonaId = $(this).val();
    	

    		var colchoneta = $('#localidadSelect').get(0);
	    	colchoneta.options.length = 0; //reset to zero length
		
			colchoneta.options[0] = new Option(locations[0][1],locations[0][0]);
			j = 1;
		

			for(var i = 1; i < locations.length; ++i) {
		    	if(zonaId != '')
    			{
					if (locations[i][2] == zonaId)
					{
	    				colchoneta.options[j] = new Option(locations[i][1],locations[i][0]);
		    			j++;
			    	}
				}
				else
				{
					colchoneta.options[i] = new Option(locations[i][1],locations[i][0]);
				}
			}
    });
    
    //cambiador de imagenes grandes x thumbs
    /*
    $('#thumb_container a').bind('click', function()
    {  	
    	var src = $(this).attr('href');
		
    	$('#bigPict img').attr('src','');
    	$('#bigPict img').attr('src',src);

    	return false;
    });
    */

	$('#butSearch').bind('click',function()
	{ 
		$('#adv_search').submit();
	});
   
    //hover e las tarifas
    $('.tarifaTd').bind('mouseover',function(){$(this).css("background", "#f4f4f4");}).bind('mouseout',function(){$(this).css("background", "#eee");});
});


jQuery.fn.selectOptionsByText = function(value, mode) {
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;
			
			
			for(var i = 0; i<optionsLength; i++) {
				if (jQuery.trim(this.options[i].text) == jQuery.trim(value)) {
					this.options[i].selected = true;
				};
			}
		}
	)
	return this;
}