
$(document).ready(function() { //perform actions when DOM is ready
	var $_GET = {};
	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
		function decode(s) { return decodeURIComponent(s.split("+").join(" ")); }
		$_GET[decode(arguments[1])] = decode(arguments[2]);
	});

  
var z = 0; //for setting the initial z-index's
var inAnimation = false; //flag for testing if we are in a animation

$('#pictures img').each(function() { //set the initial z-index's
z++; //at the end we have the highest z-index value stored in the z variable
$(this).css('z-index', z); //apply increased z-index to <img>
});

function swapFirstLast(isFirst) {
    if(inAnimation) return false; //if already swapping pictures just return
    else inAnimation = true; //set the flag that we process a image
    
    var processZindex, direction, newZindex, inDeCrease; //change for previous or next image
    
    if(isFirst) { processZindex = z; direction = '-'; newZindex = 1; inDeCrease = 1; } //set variables for "next" action
    else { processZindex = 1; direction = ''; newZindex = z; inDeCrease = -1; } //set variables for "previous" action
    
    $('#pictures img').each(function() { //process each image
      if($(this).css('z-index') == processZindex) { //if its the image we need to process
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() { //animate the img above/under the gallery (assuming all pictures are equal height)
          $(this).css('z-index', newZindex) //set new z-index
            .animate({ 'top' : '0' }, 'slow', function() { //animate the image back to its original position
              inAnimation = false; //reset the flag
            });
        });
      } else { //not the image we need to process, only in/de-crease z-index
        $(this).animate({ 'top' : '0' }, 'slow', function() { //make sure to wait swapping the z-index when image is above/under the gallery
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease); //in/de-crease the z-index by one
        });
      }
    });
    
    return false; //don't follow the clicked link
  }
  
  $('#next a').click(function() {
    return swapFirstLast(true); //swap first image to last position
  });
  
  $('#prev a').click(function() {
    return swapFirstLast(false); //swap last image to first position
  });
  //FIN PARTI PHOTO SWITCHER


//Default Action
$(".tab_content").hide(); //Hide all content
$("ul.tabs li.lisel2").addClass("active").show(); //Activate first tab
$(".tab_content.sel2").show(); //Show first tab content


//On Click Event
$("ul.tabs li").click(function() {
	$("ul.tabs li").removeClass("active"); //Remove any "active" class
	$(this).addClass("active"); //Add "active" class to selected tab
	$(".tab_content").hide(); //Hide all tab content
	var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
	$(activeTab).show(); //Fade in the active content
	
	return false;
});

	$('#meteo').weatherfeed(['USMA0314'],{unit: 'f',wind: false,link: false});
	
	$(".r_closesearch").click(function () {
		if($("#r_search_content").is(":visible") == true) {
			$("#r_search_content").hide("slide", { direction: "up" }, 500);
			$("#switch_search").attr("src","/images/bt_opensearch.png");
		}
		else {		
			$("#r_search_content").show("slide", { direction: "up" }, 500);
			$("#switch_search").attr("src","/images/bt_closesearch.png");	
		}
	});
	
	$( ".r_datepicker" ).datepicker(
	{
			minDate:"06/01/2011",
			maxDate:"09/30/2011" ,
			hideIfNoPrevNext: true ,
			numberOfMonths: 4,
			beforeShowDay: highlightOdds,
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		}
	);
	
	
	$( ".rd_datepicker1" ).datepicker(
	{
			minDate:"06/01/2011",
			maxDate:"07/31/2011" ,
			hideIfNoPrevNext: true ,
			numberOfMonths: 2,
			beforeShowDay: highlightOdds,
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		}
	);
	
	$( ".rd_datepicker2" ).datepicker(
	{
			minDate:"08/01/2011",
			maxDate:"09/30/2011" ,
			hideIfNoPrevNext: true ,
			numberOfMonths: 2,
			beforeShowDay: highlightOdds,
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		}
	);
	
	
	
	function highlightOdds(date) {
		if (date.getDate() < 10)
			dd = '0'+date.getDate();
		else
			dd = date.getDate();
		
		if ((date.getMonth()+1) < 10)
			mm = '0'+(date.getMonth()+1);
		else
			mm = (date.getMonth()+1);
		yy = date.getFullYear();
			
		ret = 'cal'+dd+mm+yy;
		ajax_check_dispo('check_house_disponibility','"'+$_GET["id"]+'","'+dd+'","'+mm+'","'+yy+'"', ret);
		return [true, ret];
	}
	
	

	$('<td></td>').insertAfter('.ui-datepicker-calendar tbody tr td:last-child').addClass('lastCol');
	$('tbody td.lastCol').each(function(i) {
		var $item = $(this);
		var textValue = $item.prev().text() + '-' + $item.prev().prev().text();
		var testValueF = $item.prev().prev().prev().prev().prev().prev().prev().text();
		var testValueL = $item.prev().text();
		var ClassLastCase =  $item.prev().attr("class")
		if ( Number(testValueF) || Number(testValueL) ) {
			var $a = $("<a class='affrate' id='rate"+i+"'></a>").attr('href', '#');
			ajax_check_rate('check_house_rate', '"'+$_GET["id"]+'","'+ClassLastCase+'"', 'rate'+i);
			$a.appendTo($item);
		}
	});
	$("<th>rate($)</th>").insertAfter(".ui-datepicker-calendar thead tr th:last-child");
	$(this).get(0).disabled = true;
	
});
