
function resetField( field, resetText )
{
    if ( field.value == resetText ) field.value = '';
}

function addLoadEvent( func )
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
        window.onload = func;
    else
    {
        window.onload = function()
        {
            if (oldonload) oldonload();
            func();
        }
    }
}

function resetSelector( object )
{
    object.selectedIndex = 0;
}

function resetSelectorArray( objectArray )
{
    for ( var i in objectArray )
        objectArray[i].selectedIndex = 0;
}

function fetchPackageOfTheWeek( type, doFade, isClicked )
{
    if ( PackageOfTheWeekIsLocked ) return false;
    PackageOfTheWeekIsLocked = true;

    var url = '/gourmethotels/ajax/fetch_package_of_the_week?type=' + type;

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function( transport )
        {
            var content = $( 'packageOfTheWeekContent' + PackageOfTheWeekToggler );
            content.update( transport.responseText );


            if ( doFade === true )
                new Effect.Appear('packageOfTheWeekContent' + PackageOfTheWeekToggler, {duration:1, from:0.0, to:1.0});
            else
                new Element.show('packageOfTheWeekContent' + PackageOfTheWeekToggler);

            PackageOfTheWeekToggler = ( PackageOfTheWeekToggler == 1 ) ? 2 : 1;

            new Effect.Fade('packageOfTheWeekContent' + PackageOfTheWeekToggler, {duration:1, from:1.0, to:0.0, afterFinish: function() {PackageOfTheWeekIsLocked = false;}});

            if ( doFade === true )
                new Effect.Fade('packageOfTheWeekContent' + PackageOfTheWeekToggler, {duration:1, from:1.0, to:0.0, afterFinish: function() {PackageOfTheWeekIsLocked = false;}});
            else
                new Element.hide('packageOfTheWeekContent' + PackageOfTheWeekToggler, {afterFinish: function() {PackageOfTheWeekIsLocked = false;}});
        }
    });

    // hold the current periodical executor and start a new one
    if ( isClicked === true )
    {
        PackageOfTheWeekPeriodicalExecuter.stop();
        startPeriodicalExecuterFetchPackageOfTheWeek();
    }

//     PackageOfTheWeekIsLocked = false;
    return false;
}

function startPeriodicalExecuterFetchPackageOfTheWeek()
{
    PackageOfTheWeekPeriodicalExecuter = new PeriodicalExecuter(function(ps)
    {
        fetchPackageOfTheWeek( 'rand', true );
    }, 20);
}

function fetchHotelTip( doFade )
{
    var url = '/gourmethotels/ajax/fetch_hotel_tip';

    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function( transport )
        {
            var content = $( 'hotelTipContent' + HotelTipToggler );
            content.update( transport.responseText );

            if ( doFade === true )
                new Effect.Appear('hotelTipContent' + HotelTipToggler, {duration:1, from:0.0, to:1.0});
            else
                new Element.show('hotelTipContent' + HotelTipToggler);

            HotelTipToggler = ( HotelTipToggler == 1 ) ? 2 : 1;

            if ( doFade === true )
                new Effect.Fade('hotelTipContent' + HotelTipToggler, {duration:1, from:1.0, to:0.0});
            else
                new Element.hide('hotelTipContent' + HotelTipToggler);
        }
    });

    return false;
}

function startPeriodicalExecuterFetchHotelTip()
{
    HotelTipPeriodicalExecuter = new PeriodicalExecuter(function(ps)
    {
        fetchHotelTip( true );
    }, 20);
}
/*
function fetchPackageLineContent( nodeId, showDetails, dontShowHotelLink )
{
    showDetails = showDetails == 1 ? 1 : 0;

    // init the PackageLineContentBoxHeight array
    if ( typeof PackageLineContentBoxHeight[nodeId] == 'undefined' ) PackageLineContentBoxHeight[nodeId] = new Array();

    var toToggler = showDetails == 1 ? 2 : 1;
    var to = $( 'packageLineContent' + nodeId + '-' + toToggler );

    var fromToggler = showDetails == 1 ? 1 : 2;
    var from = $( 'packageLineContent' + nodeId + '-' + fromToggler );

    to.hide();

    if ( ( showDetails == 1 ) && ( to.innerHTML.blank() ) )
    {
        dontShowHotelLink = dontShowHotelLink == 1 ? 1 : 0;
        var url = '/gourmethotels/ajax/fetch_package_line_content?nodeId=' + nodeId + '&showDetails=' + showDetails + '&dontShowHotelLink=' + dontShowHotelLink;

        new Ajax.Request(url, {
            method: 'get',
            asynchronous: false,
            onSuccess: function( transport )
            {
                to.update( transport.responseText );
            }
        });
    }

//     alert(to.getHeight() );

    // save the height of the descriotion and the intro the first time
    if ( typeof PackageLineContentBoxHeight[nodeId][1] == 'undefined' ) PackageLineContentBoxHeight[nodeId][1] = to.getHeight();
    if ( typeof PackageLineContentBoxHeight[nodeId][2] == 'undefined' ) PackageLineContentBoxHeight[nodeId][2] = from.getHeight();

//     alert( PackageLineContentBoxHeight[nodeId][1] );
//     alert( PackageLineContentBoxHeight[nodeId][2] );

    // the 2 divs must have the same position
    from.absolutize();
    to.absolutize();
    to.clonePosition( from );

    // crossfade
    to.appear( { duration:1, from:0.0, to:1.0 } );
    from.fade( { duration:1, from:1.0, to:0.0 } );

    // get the parent container
    var parentObj = to.getOffsetParent();

    // set the old height
    parentObj.setStyle( { height: PackageLineContentBoxHeight[nodeId][toToggler] + 'px' } );
    // morph to the new height
    parentObj.morph( { height: PackageLineContentBoxHeight[nodeId][fromToggler] + 'px' } );
}
*/

function fetchPackageLineContent( nodeId, showDetails, dontShowHotelLink )
{
    showDetails = showDetails == 1 ? 1 : 0;
    var to = $( 'packageLineContent' + nodeId + '-1' );

    dontShowHotelLink = dontShowHotelLink == 1 ? 1 : 0;
    var url = '/gourmethotels/ajax/fetch_package_line_content?nodeId=' + nodeId + '&showDetails=' + showDetails + '&dontShowHotelLink=' + dontShowHotelLink;

    new Ajax.Request(url, {
        method: 'get',
        asynchronous: false,
        onSuccess: function( transport )
        {
            to.update( transport.responseText );
        }
    });
}

function openHotelGoogleMapsBox( contentObjectId )
{
    var win = new Window({className: "mac_os_x", width:800, height: 500, destroyOnClose: true, recenterAuto: false, title: "Location", draggable: false, resizable: false, maximizable: false, minimizable: false});
    win.setAjaxContent( "/gourmethotels/ajax/hotel_google_maps_box_html?objectId=" + contentObjectId );
    win.showCenter(true);
}

function openPackageFlightSearchBox( contentObjectId )
{
    var win = new Window({className: "mac_os_x", width:300, height: 250, destroyOnClose: true, recenterAuto: false, title: "Flug suchen", draggable: false, resizable: false, maximizable: false, minimizable: false});
    win.setAjaxContent( "/gourmethotels/ajax/package_flight_search_box_html?objectId=" + contentObjectId );
    win.showCenter(true);
}

/*
function fetchPackageSearchSelectors( caller )
{
    var url = '/gourmethotels/ajax/fetch_package_search_selectors';

    new Ajax.Request(url, {
        method: 'get',
        asynchronous: false,
        onSuccess: function( transport )
        {
            $( 'packageSearchSelectors' ).update( transport.responseText );
        }
    });
}
*/

function setMoneyVoucherTemplate( objectId )
{
    jQuery( '#voucherPreviewObjectId' ).val( objectId );
    updateMoneyVoucherPreviewImage();
}

function updateMoneyVoucherPreviewImage()
{
    var img = jQuery( '#voucherPreviewImg' );
    var link = jQuery( '#voucherPreviewLink' );
    var addressingElem = jQuery( '#ContentObjectAttribute_ezstring_data_text_47517' );
    var amountElem = jQuery( '#ContentObjectAttribute_data_integer_47518' );
    var messageElem = jQuery( '#ContentObjectAttribute_data_text_47519' );

    var imgSrc = '/gourmethotels/money_voucher/preview_image?pattern_id=' + jQuery( '#voucherPreviewObjectId' ).val()
        + '&addressing=' + escape( addressingElem.val() )
        + '&amount=' + escape( amountElem.val() )
        + '&message=' + escape( messageElem.val() );
    img.attr( "src", imgSrc );
    link.attr( "href", imgSrc + '&width=600' );
}

function setPackageVoucherTemplate( objectId )
{
    jQuery( '#voucherPreviewObjectId' ).val( objectId );
    updatePackageVoucherPreviewImage();
}

function updatePackageVoucherPreviewImage()
{
    var img = jQuery( '#voucherPreviewImg' );
    var link = jQuery( '#voucherPreviewLink' );
    var addressingElem = jQuery( '#ContentObjectAttribute_ezstring_data_text_47517' );
    var personsElem = jQuery( '#ContentObjectAttribute_data_integer_50109' );
    var messageElem = jQuery( '#ContentObjectAttribute_data_text_47519' );

    var imgSrc = '/gourmethotels/package_voucher/preview_image/' + VoucherPreviewPackageObjectId + '?pattern_id=' + jQuery( '#voucherPreviewObjectId' ).val()
        + '&addressing=' + escape( addressingElem.val() )
        + '&persons=' + escape( personsElem.val() )
        + '&message=' + escape( messageElem.val() );
    img.attr( "src", imgSrc );
    link.attr( "href", imgSrc + '&width=600' );
}


var PackageOfTheWeekToggler = 1;
var PackageOfTheWeekPeriodicalExecuter;
var HotelTipToggler = 1;
var HotelTipPeriodicalExecuter;
var PackageOfTheWeekIsLocked = false;
var PackageLineContentBoxHeight = new Array();

var VoucherPreviewAddressing = "";
var VoucherPreviewAmount = 0;
var VoucherPreviewMessage = "";
var VoucherPreviewPackageObjectId = 0;

