/**
 * changes splash picture in function of the direction
 * @param direction - the direction(next, previous)
 */
function change_picture (direction) {
    //get all pictures for the page
    var pictures = $('page_pictures');
    //get current picture
    var current_picture = $('splash_header');
    if (!pictures || !current_picture) {
        return false;
    }
    //convert pictures string to array
    pictures = pictures.value.split(';');
    if (pictures.length <= 1) {
        return false;
    }
    current_picture = current_picture.src;
    //get pictures path
    var pictures_path = current_picture.replace(/(.*\/)[^\/]*/, '$1');
    //get picture name
    var current_picture = current_picture.replace(/.*\/([^\/]*)/, '$1');

    //iterate trough pictures and set next picture as current
    for (var i = 0; i < pictures.length; i++) {
        if (pictures[i] == current_picture) {
            if (direction == 'previous') {
                if (pictures[i-1]) {
                    //set previous picture(if exists) as current
                    current_picture = pictures[i-1];
                } else {
                    //set last picture as current
                    current_picture = pictures[pictures.length-1];
                }
                break;
            } else {
                if (pictures[i+1]) {
                    //set next picture as current
                    current_picture = pictures[i+1];
                } else {
                    //set first picture as current
                    current_picture = pictures[0];
                }
                break; 
            }
        }
    }
    //set picture in the container
    $('splash_header').src = pictures_path + current_picture;
    return false;
}

/**
 * A special function used with the Zapatec Calendar
 * It validates date so that it:
 * - can not be BEFORE Check-In date
 * - can not be before today
 */
function disallowDateBefore(dateCheckOut) {
    var startDate = new Date;
    startDate.setDate(startDate.getDate()-1);

    //dateCheckOut.setHours(0,0,0,0)
    if ((startDate != null) && startDate > dateCheckOut)
        // startDate is defined, make sure cal date is NOT before start date
        return true;

    var now = new Date()
    now.setHours(0,0,0,0)
    if (dateCheckOut < now)
        // check out date can not be befor today if startDate NOT defined
        return true;

    return false;
}

 /**
  * A special function used with the Zapatec Calendar
  * It validates date so that it:
  * - can not be AFTER Check-Out date
  * - can not be after today
  */
function disallowDateAfter(dateCheckIn) {
    var endDate = new Date;
    endDate.setDate(endDate.getDate()-1);

    dateCheckIn.setHours(0,0,0,0)

    if ((endDate != null) && dateCheckIn > endDate)
       // endDate defined, calendar date can NOT be after endDate
       return true;

    return false;
}

/**
 * Special function that translate the date from the Zapatec Calendar
 * in ISO format and write it in the hidden INPUT field,
 * which we use to pass the date to the data base
 *
 * @param element - object which represents the visible INPUT field
 */
function formatDate(element) {

    if (element.params) {
        //get the field from calendar object params
        element = element.params.inputField;
    }
    
    var element = $('calendar_date');
    var delement = $('ci_day');
    var ymelement = $('ci_ym');
    if (!element || !delement || !ymelement) return false;
    
    ym = element.value.replace(/(\d{4}-\d{2})-\d{2}/, '$1');
    day = element.value.replace(/\d{4}-\d{2}-(\d{2})/, '$1');
    delement.value = day;
    ymelement.value = ym;
}

/**
 * pecial function that Initialize the Zapatec Calendar *
 *
 * @param elementID - the ID of the visible INPUT field
 * @param triggerButton - the ID of the button which will activate the calendar
 * @param visibleFormat - the date format user will see
 * @param innerFormat - the date format which will be passed to the data base(ISO)
 * @param statusFunction - function that can be used to change the appearance of a specific date
 */
function calendarInit(elementId, triggerButton, showTime, visibleFormat, innerFormat, statusFunction, theme) {
    Zapatec.Calendar.setup({
                            firstDay          : 1,
                            weekNumbers       : true,
                            showOthers        : true,
                            showsTime         : showTime,
                            singleClick       : false,
                            electric          : false,
                            canType           : true,
                            inputField        : elementId,
                            button            : triggerButton,
                            ifFormat          : visibleFormat,
                            daFormat          : innerFormat,
                            dateStatusFunc    : statusFunction,
                            onUpdate          : formatDate,
                            showOthers        : false,
                            theme             : theme
                          });
}

/**
 * Function to show reservation form
 */
function showForm(container) {
    var element = $(container);
    if (!container) return false;
    if (element.style.display == 'none') {
        Effect.BlindDown(element);
    } else {
        Effect.BlindUp(element);
    }
}
