$(function() {
    
    // carousel
    var total_items = $("div.right-box").size();

    setTimeout(function() {
        carousel(1, total_items);
    }, 5000);    
    
    function carousel(active_id, total_items) {
        var next_id = active_id + 1;
    
        if (next_id > total_items)
            var next_id = 1;
        
        $("div.right-box.active").removeClass("active").fadeOut(1000);
        $("div.right-box.number-"+next_id).addClass("active").fadeIn(1000);
        
        setTimeout(function() {
            carousel(next_id, total_items);
        }, 8000);
    };
    
    
    $('.ratingstar').rating({ 
        callback: function(value, link){ 
            $("input#rating").val(value);
        } 
    });

    
    function roundNumber(num, dec) 
    {
    	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    	return result.toFixed(dec);
    }    
    
    var url = $("#resort_name").attr("title");
    $("#resort_name").autocomplete(url, {
        mustMatch:true
    });
    
    $("#resort_name").result(function(event, data, formatted) {
		if(data) {
			$("input#resort_id").val(data[1]);
		}
    });
    
    $("a.lightbox").lightBox();
    
    $("select#listings_for").change( function() {
        if ($(this).val() == 'rent')
        {
            $("input#for_rent").val('1');
            $("input#for_sale").val('0');
            $("div#rent_information").show();
            $("div#sale_information").hide();            
        }
        else if ($(this).val() == 'sale')
        {
            $("input#for_rent").val('0');
            $("input#for_sale").val('1');
            $("div#rent_information").hide();
            $("div#sale_information").show();            
        }
        else if ($(this).val() == 'both')
        {
            $("input#for_rent").val('1');
            $("input#for_sale").val('1');
            $("div#rent_information").show();
            $("div#sale_information").show();            
        }
    });
    
    $("select#for").change( function() {
        if ($(this).val() == 'rent')
        {
            $("input#for_rent").val('1');
            $("input#for_sale").val('0');
        }
        else if ($(this).val() == 'sale')
        {
            $("input#for_rent").val('0');
            $("input#for_sale").val('1');
        }
        else if ($(this).val() == 'both')
        {
            $("input#for_rent").val('1');
            $("input#for_sale").val('1');
        }
        else
        {
            $("input#for_rent").val('');
            $("input#for_sale").val('');
        }
    });

    $("input#rent_fixed_week").change(function () {
        if ($(this).val() == 1)
            $("#display_rent_week_number").show();
        else
            $("#display_rent_week_number").hide();
    });
    
    $("input#sale_fixed_week").change(function () {
        if ($(this).val() == 1)
            $("#display_sale_week_number").show();
        else
            $("#display_sale_week_number").hide();
    });
        
    $("input#sale_fixed_week").change(function () {
        if ($(this).val() == 1)
            $("#display_sale_week_number").show();
        else
            $("#display_sale_week_number").hide();
    });
    
    $("#sale_week_recurring").change(function () {
        if ($(this).val() == 'biannual')
            $("#sale_week_recurring_even_odd").show();
        else
            $("#sale_week_recurring_even_odd").hide();
    });
    
    function price_per_night()
    {
        if ($("input#rent_number_of_nights").val() && $("input#rent_price_per_night").val())
        {
            var rental_price = roundNumber($("input#rent_price_per_night").val() * $("input#rent_number_of_nights").val(),2);
            $("p#display_rent_price").html('$' + rental_price);
        }
    }
    
    $("input#rent_number_of_nights").change(function () {
        price_per_night();
    });
    $("input#rent_price_per_night").change(function () {
        price_per_night();
    });


    $("a#toggle_options").click(function() {
        $("div.options").slideToggle("fast");
    });
    


	$("a#book_this_now_button").click(function() {
		$("div.popup#book, div#popup_bg").show();
	});
	
	$("a#ask_a_question_button").click(function() {
		$("div.popup#contact, div#popup_bg").show();
	});

    $("a#book_this_instead").click(function() {
        $("div.popup, div#popup_bg").hide();
		$("div.popup#book, div#popup_bg").show();
	});
	
	
	$("a.close_popup").click(function() {
		$("div.popup, div#popup_bg").hide();
	});

    setTimeout(function() {
        $("p#flash").fadeOut("slow");
    }, 3000);
	
	$("a.delete-timeshare").click(function(e) {
		var href= $(this).attr("href");
		var timeshare_id = $(this).attr("id").substr(10);
		var token = $(this).attr("title");
        e.preventDefault();
        if (confirm('Confirm delete?'))
		{ 
			var f = document.createElement('form');
			f.style.display = 'none';
			this.parentNode.appendChild(f);
			f.method = 'POST';
			f.action = href;
			var m = document.createElement('input');
			m.setAttribute('type', 'hidden');
			m.setAttribute('name', 'id');
			m.setAttribute('value', timeshare_id);
			f.appendChild(m);
			var s = document.createElement('input');
			s.setAttribute('type', 'hidden');
			s.setAttribute('name', 'token');
			s.setAttribute('value', token);
			f.appendChild(s);
			f.submit(); 
		};
	});    
});