var pArray = new Array();

$(document).ready(function(){

    var tArray;
    
    
    $.ajax({
        url: './getprice.php',
        success: function(data){
        
            var xml = $(data).find("prices").get();
            
            $(xml).find("price").each(function(i){
            
                i = parseInt($(this).find("day").text());
                pArray[i] = $(this).find("value").text();
                
                
                
				var v = $( "#slider" ).slider( "option", "value" );
				
				$("#qqPrice").text("£" + calcPrice(v));
            });
            
            
        }
        
    });
    
    
    
    $(".nav").click(function(){
    
    
        document.location = $(this).children("a").attr("href");
        
    });
    
    $(".nav").hover(function(){
    
        $(this).addClass('navh');
        
    }, function(){
    
        $(this).removeClass('navh');
        
    });
    
    
    $("#btnBookNow").click(function(){
    
        $("#secureModal").show();
        $("#quickBookForm").submit();
        
    });
    // Retrive
    
    $.ajax({
        url: 'xml/testimonials.xml',
        success: function(data){
        
            tArray = $(data).find("testimonials").get();
            
            tArray = $(tArray).find("testimonial");
            testimonials();
        }
    });
    
    //Testimonials 
    
    setInterval(testimonials, 5000);
    
    function testimonials(){
    
        var rn = Math.floor(Math.random() * tArray.length);
        $("#testName").hide();
        $("#testDesc").hide();
        $("#testName").text($(tArray[rn]).find("name").text());
        $("#testDesc").text($(tArray[rn]).find("description").text());
        $("#testName").fadeIn(1000);
        $("#testDesc").fadeIn(1000);
        
        
    }
    
    // Set Buttons
    
    $('button').button();
    
    
    
    // Set Slider
    
    $("#slider").slider({
        range: "min",
        value: 7,
        min: 1,
        max: 21,
        step: 1,
        slide: function(event, ui){
            $("#qqDays").text(ui.value);
            $("#qqPrice").text("£" + calcPrice(ui.value));
            
        }
    });
    
    // Set Date Pickers
    
    
    
    
    $('#returnDate').datepicker({
        defaultDate: +2,
        minDate: +2,
        dateFormat: 'd/m/yy',
        onSelect: function(selectedDate){
            calcDays();
            
        }
    });
    
    
    $('#departDate').datepicker({
        defaultDate: +2,
        minDate: +2,
        dateFormat: 'd/m/yy',
        onSelect: function(selectedDate){
        
        
            setDatePickers();
            calcDays();
            
        }
    });
    
    
    
    
    
    
});


function setDatePickers(){


    var maxDate = new Date($("#departDate").datepicker("getDate").getTime());
    maxDate.setDate(maxDate.getDate() + 20);
    
    
    $("#returnDate").datepicker("option", "maxDate", maxDate);
    $("#returnDate").datepicker("refresh");
    $("#returnDate").datepicker("setDate", $("#departDate").datepicker("getDate"));
    $("#returnDate").datepicker("option", "defaultDate", $("#departDate").datepicker("getDate"));
    $("#returnDate").datepicker("option", "minDate", $("#departDate").datepicker("getDate"));
    $("#returnDate").datepicker("refresh");
}

function calcDays(){


    var departDate = $("#departDate").datepicker("getDate");
    var returnDate = $("#returnDate").datepicker("getDate");
    
    var one_day = 1000 * 60 * 60 * 24;
    
    var days = Math.ceil((returnDate - departDate) / (one_day));
    
    
    days++;
    
    
    $('#days').text(days);
    $('#price').text(calcPrice(days));
    $('[name = "days"]').val(days);
    $('[name = "price"]').val(calcPrice(days));
    
}



function calcPrice(iDays){

   return pArray[iDays];
    
}

