
/**
* SpecialOffer - reusable class that controls a single special offer 
* 
* requires ;
* 		- jquery.js
* 		- vh.util.js
*/



var HotelSpecialOffer = makeClass();

HotelSpecialOffer.prototype = {
	
	_data : {}, // JSON object that holds all Special Offer Data
	appended : false // flag if we've already appended the booking form

};

function bookNow() {

    // final checks
    return true;

};

HotelSpecialOffer.prototype.init = function(SpecialOfferJSON) {

    // global var - BOOKING_FORM_HTML
    if (typeof window.BOOKING_FORM_HTML === "undefined") {
        window.BOOKING_FORM_HTML = $('#BookingContainerDiv').html();
        $('#BookingContainerDiv').empty();
    }    
    
    // global var - CURRENT_HSO
    if (typeof window.CURRENT_HSO === "undefined") {
        window.CURRENT_HSO = {};
    }

	// ref
	var self = this;
	
	// guard
	if (VH.UTIL.isUndefinedOrNull(SpecialOfferJSON)) {
		return;
	}
	
	// set the data for this instance	
	self._data = SpecialOfferJSON;

	self.InitControls();

};

HotelSpecialOffer.prototype.InitControls = function() {

    var hso = this;

    // open 
    $(hso._data.openBtn).click(function(e) {
        e.preventDefault();

        // is there a form open?
        if (!VH.UTIL.isUndefinedOrNull(CURRENT_HSO.UnloadBookingForm)) {
            CURRENT_HSO.UnloadBookingForm();
        }

        CURRENT_HSO = hso;
        hso.LoadBookingForm();
        
    });

    // close
    $(hso._data.closeBtn).click(function(e) {
        e.preventDefault();
        CURRENT_HSO.UnloadBookingForm();
    })

};

HotelSpecialOffer.prototype.LoadBookingForm = function() {

    var hso = this;

    // set up buttons
    $(hso._data.openBtn).hide();
    $(hso._data.closeBtn).show();

    // load booking form
    $(hso._data.containerId).html(BOOKING_FORM_HTML)

    hso.RenderCalendar();
    hso.RenderInfo();
    hso.UpdateHiddenFields(); 

}

HotelSpecialOffer.prototype.GetDate = function(date) {

    var dateStr = date.split("/");
    return new Date(dateStr[2], dateStr[1] - 1, dateStr[0]);
    
}

HotelSpecialOffer.prototype.RenderCalendar = function() {

    var hso = this;

    var hotelBookableFrom = new Date(CALENDAR_DATA.EarliestBookableDate);
    var hotelBookableTo = new Date(CALENDAR_DATA.LatestBookableDate);

    $("#staticCal").data("bookableFrom", hso._data.bookableFrom);
    $("#staticCal").data("bookableTo", hso._data.bookableTo);

    $("#staticCal").datepicker({

        // basic options
        rangeSelect: false, // no date range selection
        yearRange: '-0:+2', // sets to current year + 2
        firstDay: 1, // sets to Monday
        hideIfNoPrevNext: true, // hide links

        // min, max and formatting
        minDate: hotelBookableFrom, // earliest selectable date
        maxDate: hotelBookableTo, // latest selectable date
        setDate: hso.GetDate(hso._data.bookableFrom), // default selected date
        dateFormat: 'D d M yy',
        dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],

        // callbacks
        onRender: hso.FixDisplay, // custom Zolv callback
        onSelect: hso.UpdateForm,
        beforeShowDay: hso.highlightOfferDates
    });

    $('#staticCal').datepicker('setDate', hso.GetDate(hso._data.bookableFrom));

    $("#sOfferDepartureDate").val(
		$.datepicker.formatDate('D d M yy', hso.GetDate(hso._data.bookableFrom))
	);

};

HotelSpecialOffer.prototype.highlightOfferDates = function(date) {

    var cal = $("#staticCal");

    var dateStr = cal.data("bookableFrom").split("/");
    var soBookableFrom = new Date(dateStr[2], dateStr[1] - 1, dateStr[0]);

    dateStr = cal.data("bookableTo").split("/");
    var soBookableTo = new Date(dateStr[2], dateStr[1] - 1, dateStr[0]);

    soBookableFrom.setDate(soBookableFrom.getDate() - 1);
    soBookableTo.setDate(soBookableTo.getDate() + 1); 

    if (VH.UTIL.CompareDate(date, soBookableFrom) && VH.UTIL.CompareDate(soBookableTo, date)) {
        return [true, "soDate"];
    }
    return [true, ""];
};


HotelSpecialOffer.prototype.FixDisplay = function() {

    var calendar = $("#staticCal");
    var selects = calendar.find("select");
    selects.eq(0).css({ "width": "100px" }); // month
    selects.eq(1).css({ "width": "60px" }); // year

    // prev/next labels
    calendar.find(".ui-datepicker-prev label:empty").hide();
    calendar.find(".ui-datepicker-next label:empty").hide();
};


HotelSpecialOffer.prototype.UnloadBookingForm = function() {

    var hso = this;

    // set up buttons
    $(hso._data.openBtn).show();
    $(hso._data.closeBtn).hide();

    // kill booking form
    $(hso._data.containerId).empty();

}

HotelSpecialOffer.prototype.RenderInfo = function() {

    var hso = this;

    // bulleted list
    $("#txtDestinationName").text(hso._data.hotelName);
    $("#txtDuration").text(hso._data.duration);

    var formattedFrom = $.datepicker.formatDate('d M yy', hso.GetDate(hso._data.bookableFrom))
    var formattedTo = $.datepicker.formatDate('d M yy', hso.GetDate(hso._data.bookableTo))
    $("#txtDepartureDates").text(formattedFrom + " - " + formattedTo);
    
    $("#txtFromPrice").text(hso._data.fromPrice);

    // airports
    var airportTxt = "";
    var totalOptions = $("#BookingFormPanel #where select option").length - 1;
    $("#BookingFormPanel #where select option").each(function(i) {
        if (i != totalOptions) {
            airportTxt += (i != totalOptions - 1) ? $(this).text() + ", " : $(this).text();
        } else {
            airportTxt += " or " + $(this).text();
        }
    });
    $("#txtDepartureAirports").text(airportTxt);

}

HotelSpecialOffer.prototype.UpdateForm = function(date) {
    $("#sOfferDepartureDate").val(date);
};


HotelSpecialOffer.prototype.UpdateHiddenFields = function() {

    var hso = this;

    $("#sOfferMainAccom").val(hso._data.accomCode);
    $("#sOfferDestination").val(hso._data.arrivalAirport);
    $("#sOfferProgram").val(hso._data.programId);
    $("#sOfferCompany").val(hso._data.companyCode);
    $("#sOfferDuration").val(hso._data.duration);
    $("#sOfferHotelName").val(hso._data.hotelName);
    
};


