// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

    NiftyLoad=function()    {
		Nifty("ul#tab a","small transparent top");
        Nifty("div.rounded-corners,div.rounded-border,div.just-rounded-border,div.highlight-corners,div.highlight-border,div#pane-container", "big");
		Nifty("div.faux-button a,div#owner-content-area", "small");
		Nifty("div.rounded-corners-t,div.rounded-border-t", "big transparent");
    }

function vIE(){
    return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;
}


function openPopup(url, name, arguments) {
	newwindow=window.open(url, name, arguments);
	if (window.focus) {newwindow.focus()}
	return false;
}

function earlierThanThisMonth( given_date ) {
    today = new Date();
    return ( given_date.getFullYear() <= today.getFullYear() && given_date.getMonth() < today.getMonth() );
}

function dateWithYear( date_string ) {
    today = new Date();

    if( date_string.slice( -1 ) == "!" )
        // a ! at the end means respect the date, no matter what.
        return date_string.slice( 0, -1 );
    else if ( date_string.indexOf("," ) <= 0 )
        // No year given, provide a default year
        date_with_year = date_string + ", " + today.getFullYear();
    else
        date_with_year = date_string

    // If the given date is earlier than the current month, switch it to the same day next year.    
    if( earlierThanThisMonth( new Date( date_with_year ) ) )
        return date_with_year.slice( 0, date_with_year.indexOf(",") ) + ", " + (today.getFullYear() + 1);
    else
        return date_with_year;
}

function arrival_date_changed(arrival_input, departure_input) {
    if (arrival_input.value == null || arrival_input.value.strip().length == 0) {
        return;
    }
    
    arrival_input.value = dateWithYear( arrival_input.value );
    arrival_date = new Date( arrival_input.value );
    departure_date = new Date( departure_input.value );

    if (departure_input.value == null || departure_input.value == "" || departure_date <= arrival_date ) {
        departure_date = new Date( arrival_date.getFullYear(), arrival_date.getMonth(), arrival_date.getDate() + 2 );
        departure_input.value = departure_date.toFormattedString();
    }
}

function departure_date_changed(arrival_input, departure_input) {
    if (departure_input.value == null || departure_input.value.strip().length == 0) {
        return;
    }

    departure_input.value = dateWithYear( departure_input.value );
    arrival_date = new Date( arrival_input.value );
    departure_date = new Date( departure_input.value );

    if (arrival_input.value == null || arrival_input.value == "" || arrival_date >= departure_date ) {
        arrival_date = new Date( departure_date.getFullYear(), departure_date.getMonth(), departure_date.getDate() - 2 );
        arrival_input.value = arrival_date.toFormattedString();
    }
}    

function indicateBusy(containerId) {
    return getBusyOverlay(document.getElementById(containerId),{color:'black', opacity:0.4, text:'', style:'color:white'},{color:'#fff', size:64, type:'o'});
}

var async_calls = new Array();
var async_hash = new Array();
var call_to_process = -1;
var interrupt_async_calls = false;


function push_async_call( call_script, hash_key ) {
    // Returns the index of the script in the asynchronous queue.
    index = async_calls.push( call_script ) - 1;
    if( hash_key != null ) {
        async_hash[hash_key] = index;
    }
    
    return index;
}

function process_next_async_call() {
    if( call_to_process >= 0 && call_to_process < async_calls.length ) {
        script = async_calls[call_to_process];

        if( script != null ) {
            eval( async_calls[call_to_process] );
        }

        call_to_process++;
    }
}

function process_async_hash(hash_key) {
    index = async_hash[hash_key];
    if( index != null && index >= 0 && index < async_calls.length ) {
        script = async_calls[index];
        if( script != null ) {
            async_calls[index] = null;
        }
        
        eval( script );
    }
}

function start_async_calls() {
    if( call_to_process == -1 ) {
        call_to_process = 0;
        process_next_async_call();
    }
}    

Ajax.Responders.register({
  onCreate: function() {
    if($('spinner') && Ajax.activeRequestCount>0)
    {
       Element.show('spinner');
    }
  },
  onComplete: function() {
    if($('spinner') && Ajax.activeRequestCount==0)
    {
      Element.hide( 'spinner' )
    }
  }
});