﻿
var timeouts = new Object();
function initRoomResult(meal_id)
{    
    $(".input_radio").each(function() 
    {
        var radio = $(this);
        radio.click(function(){
            var select = radio.parent('td').parent('tr').parent('tbody').find('select'); 
            var tryToSelectValue = select.val();
            
            var optionString ="";
            var optionList = radio.val().split(";");
            for(var i=0; i < optionList.length; i++) 
            {
                var option = optionList[i].split('|');
                var optionValue = option[2];
                
                if(parseInt(option[1]) > 0) optionValue += " +" + option[1] +" kr";
                else optionValue += " ingår";
                
                if(option[0] == tryToSelectValue) optionString += '<option selected="selected" value="'+option[0]+'" price="'+option[1]+'">'+ optionValue +'</option>';
                else optionString += '<option value="'+option[0]+'" price="'+option[1]+'">'+optionValue+'</option>';
            }
            
            select.html(optionString);
            if(optionString.indexOf("selected") <0) $("#" + select.attr("id") + " option:first").attr('selected','selected'); 
            
            
            updateRoomPricesAndTransFer();
            
        });
    });
    updateRoomPricesAndTransFer();
}

function updateRoomPricesAndTransFer() 
{
    timeouts.updateRoomPricesAndTransFer = setTimeout(_updateRoomPricesAndTransFer, 100);
}

function _updateRoomPricesAndTransFer() 
{
    var totalPrice = 0;
    $(".roomBottom").each(function(i) 
    {
        
        var radio = $("input[name='rooma_" + parseInt(i+1) + "']:checked");
        var selectedMeal = radio.parent('td').parent('tr').parent('tbody').find('select option:selected'); 
        var rowPrice = parseInt(radio.parent('td').next('td').next('td').next('td').text());
        var mealPrice = parseInt(selectedMeal.attr("price"));
        
        
        //alert(mealPrice + selectedMeal.attr("price")+selectedMeal.val());
        
        var roomPriceTag = radio.parent('td').parent('tr').parent('tbody').find('.roomPrice').children('b');
        roomPriceTag.text(rowPrice+mealPrice);
        totalPrice += parseInt(rowPrice+mealPrice);
    });
    
    var transferPrice = $(".transferDropDownList option:selected").attr("price");
    
    
    if(transferPrice != null) totalPrice += parseInt(transferPrice);
    $("#totalPriceForAllRoomsAndTransfer").text(totalPrice);
    clearTimeout(timeouts.updateRoomPricesAndTransFer);
}

function bookHotel(hotel_id, itin_id) 
{
    //alert(hotel_id +" - "+ itin_id);
    
    var booking = new Object();
    booking.hotel_id = parseInt(hotel_id);
    booking.itin_id = parseInt(itin_id);
    booking.roomAlternatives = new Array();
    booking.transferId = 0;
    booking.totalPrice = parseInt($("#totalPriceForAllRoomsAndTransfer").text());
    
    $(".roomBottom").each(function(i) 
    {   
    
        var radio = $("input[name='rooma_" + parseInt(i+1) + "']:checked");
        var selectedMeal = radio.parent('td').parent('tr').parent('tbody').find('select option:selected');
        
        var rooms = new Object();
        rooms.rooms = new Array();
        
        var room = new Object();
        room.roomtypeID = parseInt(radio.val().split("|")[3]);
        room.meals = new Array();
        
        var meal = new Object();
        meal.name = selectedMeal.val();
        room.meals.push(meal);
        
        rooms.rooms.push(room);
        booking.roomAlternatives.push(rooms);
        
    });
    
    var transferId = $(".transferDropDownList option:selected").val();
    if(typeof(transferId) != "undefined") 
    {
        var transfer =  new Object();
        transfer.transfertype = parseInt(transferId);
        booking.transfer = transfer;
    }
    
    doBooking(booking);
}


function showElement(id) 
{
    $("#"+id).show();
}

function scrollTo(id) {
    
    $.scrollTo(('#'+id));

}


function showflightDetails(obj, hotel_id, header, carrier) 
{
    var href = $(obj);
    var flight = $("#selectedFlight_div");
    
    flight.insertAfter(href);
    var div_height = flight.height() + 75;
    
    hs.graphicsDir = '/js/highslide/graphics/';
    hs.transitions = ['expand', 'crossfade'];
    hs.outlineType = 'rounded-white';
    hs.wrapperClassName = 'controls-in-heading';
    
    if(header == "") $("#fl_header_"+ hotel_id).text();
    if(carrier == "") $("#fl_logo_"+ hotel_id).attr("title");


    hs.fadeInOut = true;
    hs.dimmingOpacity = 0.02;
    hs.htmlExpand(obj, { wrapperClassName: 'draggable-header', headingText: '<b>Valt flyg</b>: '+ header +" med "+  carrier , width: 720, height: div_height });
    
}
