

$(document).ready(function(){
    var showPricesRanges = function() {
        var value = $('#PropertyPropertyType').val();
        if(value == 'sale') {
            $('.buy_prices').show();
            $('.rent_prices').hide();
        } else {
            $('.buy_prices').hide();
            $('.rent_prices').show();            
        } 
    }
    $('#PropertyPropertyType').change(showPricesRanges);
    showPricesRanges();
    
	$('#PropertyPriceFromRent').change(function() {
		var price_from = $('#PropertyPriceFromRent').val();
		var price_to = $('#PropertyPriceToRent').val();
		var options = $('#PropertyPriceFromRent').get(0).options;
		var newList = '<select id="PropertyPriceToRent" name="data[Property][price_to_rent]">';
		var selected = false;
		for(var i = 0; i < options.length; i ++) {
			var option = options[i];
			if(parseFloat(option.value) > parseFloat(price_from) || option.value == '' || price_from == '') {
				var text;
				if(option.text == 'No minimum') {
					text = 'No maximum';
				} else {
					text = option.text;
				}
				var selected = '';
				if(price_to == option.value) {
					selected = ' selected="selected"';
				}
				newList += '<option' + selected + ' value="' + option.value + '">' + text + '</option>';
			}
		}
		newList += '</select>';		
		$('#PropertyPriceToRent').replaceWith(newList);
	});
    
	$('#PropertyPriceFromBuy').change(function() {
		var price_from = $('#PropertyPriceFromBuy').val();
		var price_to = $('#PropertyPriceToBuy').val();
		var options = $('#PropertyPriceFromBuy').get(0).options;
		var newList = '<select id="PropertyPriceToBuy" name="data[Property][price_to_buy]">';
		var selected = false;
		for(var i = 0; i < options.length; i ++) {
			var option = options[i];
			if(parseFloat(option.value) > parseFloat(price_from) || option.value == '' || price_from == '') {
				var text;
				if(option.text == 'No minimum') {
					text = 'No maximum';
				} else {
					text = option.text;
				}
				var selected = '';
				if(price_to == option.value) {
					selected = ' selected="selected"';
				}
				newList += '<option' + selected + ' value="' + option.value + '">' + text + '</option>';
			}
		}
		newList += '</select>';		
		$('#PropertyPriceToBuy').replaceWith(newList);
	})    
	
	$('#propertyscroller').jcarousel({
		vertical: true,
	    scroll: 2,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: carousel_initCallback,
		size: totalProperties,
		itemLoadCallback: jcarousel_itemLoadCallback,
		buttonNextCallback: jcarousel_buttonNextCallback,
		buttonPrevCallback: jcarousel_buttonPrevCallback
	});
		
	function jcarousel_buttonNextCallback(c, el, state) {
		if(state) {
			$('.downarrow').fadeTo('fast', 0.9);
		} else {
			$('.downarrow').fadeTo('fast', 0.2);
		}
	}
	
	function jcarousel_buttonPrevCallback(c, el, state) {
		if(state) {
			$('.uparrow').fadeTo('fast', 0.9);
		} else {
			$('.uparrow').fadeTo('fast', 0.2);
		}
	}
	
	function carousel_initCallback(carousel) {
	    $('.downarrow').bind('click', function(){
			carousel.next();
			return false;
		});
	
	    $('.uparrow').bind('click', function() {
	        carousel.prev();
	        return false;
	    });

	}
	
		
function jcarousel_itemLoadCallback(carousel, state)
{
    // Fetch the visible range first (should be already preloaded)
    jcarousel_itemFetchCallback(carousel, carousel.first, carousel.last);

    var visible = carousel.last - carousel.first + 1;

    // ---

    var first = carousel.last + 1;
    var last  = first + visible - 1;

    var first2 = last + 1;
    var last2  = first2 + visible - 1;

    jcarousel_itemFetchCallback(carousel, first, last, first2, last2);

    // ---

    var last  = carousel.first - 1;
    var first = last - visible + 1;

    var last2  = first - 1;
    var first2 = last2 - visible + 1;

    jcarousel_itemFetchCallback(carousel, first, last, first2, last2);
};

function jcarousel_itemFetchCallback(carousel, first, last, first2, last2)
{
	// Remove items to avoid big lists
    jcarousel_itemRemoveCallback(carousel, first2, last2);

	if (first < 1)
    	first = 1;

    // Check if "first" is out of range if the size was already set
    var size = carousel.size();
	if (carousel.options.wrap != 'circular' && size && first > size)
		return;

    jcarousel_itemQueryCallback(carousel, first, last);

};

function jcarousel_itemQueryCallback(carousel, first, last, realFirst)
{
    // Check if the requested items already exist
    if (carousel.has(first, last))
        return;

    jQuery.get(
        baseDir + 'properties/spinner/' + first + '/' + last,
        { },
        function(json) {
            jcarousel_itemAddCallback(carousel, first, last, json);
        },
        'json'
    );
};

function jcarousel_itemAddCallback(carousel, first, last, json)
{
    var items = json;

    // Set the size of the carousel
    if (items.length < (last - first + 1))
    	carousel.size(first + items.length - 1);

    $(json).each(function(i) {
        carousel.add(first + i, this.html);
    });
};

function jcarousel_itemRemoveCallback(carousel, first, last)
{
    if (!first || !last)
	    return;

    for (var i = first; i <= last; i++)
    	carousel.remove(i);
};


})


$(document).ready(function(){
	
	var numberOfImages = $('#imagearea li').length; 
	var currentImage = 0;
	var previousImage = numberOfImages;
	
	if(numberOfImages > 1){
		// Init
		$('#imagearea').css('visibility', 'visible');
		$('#imagearea li').fadeOut(1);
		$('#imagearea li:eq(0)').fadeIn(1).css('z-index', '1').css('opacity', 1);
		$('#imagearea li:eq(1)').css('z-index', '-1').show();
		
		setInterval(function(){					
			nextImage = currentImage + 1;
			if(nextImage >= numberOfImages){
				nextImage = 0;
			}
			
			$('#imagearea li:eq(' + currentImage + ')').show().css('z-index', '-1');
			$('#imagearea li:eq(' + nextImage + ')').css('z-index', '1').fadeIn();				
			
			setTimeout(function(){
				$('#imagearea li:eq(' + previousImage + ')').fadeOut();
			}, 500);
			
			previousImage = currentImage;
			currentImage ++;
			
			if(currentImage >= numberOfImages){
				currentImage = 0;
			}
			
		}, 5000);				
	}

});


