
// ----------------------------------------------------- 
// SKIAREAS ACCORDION
// -----------------------------------------------------

$("#SkiAreas").ready(function() {
    $("#SkiAreas").tabs();
});

// -------------------------------------------------
// offers
// -------------------------------------------------
var _contactStateID;

$(document).ready(function () {
    //button hotel list
    $(".request .hotel-list .requestall-button").click(function () {
        $(".request .hotel-list .item input").attr('checked', true);
    });
    $(".request .hotel-list .request-button").click(function () {

        //set selected hotels
        var hotels = $(".request .hotel-list .item input:checked");
        var list = "";

        for (j = 0; j < hotels.length; j++) {
            list = list + "<li>" + hotels.eq(j).parent().parent().attr("hotel") + "</li>";
        }
        $(".request .summary .selected-hotels").html(list);
    });

    //set selechted hotels
    $(".request .summary .selected-hotels").ready(function () {

        //set selected hotels
        var hotels = $(".request .hotel-list .item input:checked");
        var list = "";

        for (j = 0; j < hotels.length; j++) {
            list = list + "<li>" + hotels.eq(j).parent().parent().attr("hotel") + "</li>";
        }
        $(".request .summary .selected-hotels").html(list);
    });

    // -------------------------------------------------
    // calendar
    // -------------------------------------------------
    var mindate = getDate(".calendar", "mindate");
    var maxdate = getDate(".calendar", "maxdate");
    var language = $(".calendar:first").parent().attr("language");
    if (language == 0) language = "de";
    $.getScript("/App_Common/java/jQuery/jQuery.ui.datepicker-" + language + ".js", function () {
        $(".calendar").datepicker($.extend({},
            $.datepicker.regional[language], {
                showOn: 'button',
                buttonImage: '/media/calendar.gif',
                buttonImageOnly: true,
                minDate: mindate,
                maxDate: maxdate
            }));

    });


    // ------------------------------------------
    // children -contact
    // ------------------------------------------
    var activechildren = parseInt($(":input[ctype='showchildren'] option:selected").text());
    var children;
    if (activechildren > 0) {
        children = $(":input[ctype='showchild']:gt(" + (activechildren - 1) + ")");
        children.hide();
    }
    else {
        children = $(":input[ctype='showchild']");
        $(".children-ages").hide();
    }

    $(":input[ctype='showchildren']").change(function () {
        var showchildren = parseInt($(this).find("option:selected").text());
        $(".children-ages").hide();
        children.hide();
        if (showchildren > 0) {
            $(".children-ages").show();
            $(":input[ctype='showchild']:lt(" + showchildren + ")").show();
        }

    });
    // ------------------------------------------
    // offers request
    // ------------------------------------------
    var viewState = document.getElementById(_contactStateID);

    if (viewState) {
        if (viewState.value == "contact") {
            setContactView();
        }
        $("#offers .ca-request").click(function () {
            var errorMessage = ca_getModuleArg($(this), "ca-request");
            setContactView(this, errorMessage);
        });
        $("#offers .ca-request-item").click(function () {
            setContactView(this, "");
        });
        $("#offers .ca-request-all").click(function () {
            requestAll()
        });

        $("#offers .ca-pricegroup").click(function () {
            setListView();
        });
    }

    // ------------------------------------------
    // companies
    // ------------------------------------------

    $("#Companies .item .description").click(function () {
        $(this).toggleClass('expanded');
        var v1 = $(this).parent().find('.v1');
        var v2 = $(this).parent().find('.v2');

        if ($(this).hasClass('expanded')) {
            v1.hide();
            v2.show(100);
        }
        else {
            v2.hide(100);
            v1.show();
        }
    });
});

function setContactView(item,errorMessage) {
    document.getElementById(_contactStateID).value = "contact";
    if (item != null) {
        var cb = $(item).parent().find("input");
        cb.attr("checked", true);
    }
    if ($("#offers .pricegroup-panel input:checked").length > 0) {
        $("#offers .contact-panel").show("fast");
        $("#offers .pricegroup-panel").hide("fast");
        $("#offers .ca-selected-groups").html(writeSelectedHotels());
    }
    else {
        alert(errorMessage);
    }
}

function writeSelectedHotels() {
    var result = "";
    $("#offers .pricegroup-panel input:checked").each(function() {
        result += "<div>" + $(this).parent().find("span").html() + "</div>";
        //result += $(this).parent().parent().find(".hotel-list").html();
    });
    return result;
}

function setListView() {
    
        $("#offers .contact-panel").hide("fast");
        $("#offers .pricegroup-panel").show("fast");
        document.getElementById(_contactStateID).value = "list";
    
}
function requestAll() {
    $("#offers .contact-panel").show("fast");
    $("#offers .pricegroup-panel").hide("fast");
    document.getElementById(_contactStateID).value = "contact";
    $("#offers .pricegroup-panel input").attr("checked", true);
    $("#offers .ca-selected-groups").html(writeSelectedHotels());
}

function getDate(rawdate) {
    if (rawdate != null && rawdate != "") {
        var parts = rawdate.split("-");
        if (parts.length == 3)
            return new Date(parseInt(parts[0], 10), (parseInt(parts[1], 10) - 1), parseInt(parts[2], 10));
    }
}


function getDate(calendar,attribute) {
    var rawdate = $(calendar).parent().attr(attribute);
    if (rawdate != null) {
        var parts = rawdate.split("|");
        if (parts.length == 3)
            return new Date(parseInt(parts[0], 10), (parseInt(parts[1], 10) - 1), parseInt(parts[2], 10));
        }
    }