var MENU_TIMER = null;
var TOP_COUNTER = null;

function house_search(url) {
    url  = '/'+url+'/'+$('select[name=beds]').val()+','+$('select[name=bedrooms]').val()+','+$('select[name=beach]').val()+','+$('select[name=area]').val();
    url += ','+($('#indoor_pool').is(':checked')?'1':'0');
    url += ','+($('#outdoor_pool').is(':checked')?'1':'0');
    url += ','+($('#fireplace').is(':checked')?'1':'0');
    url += ','+($('#internet').is(':checked')?'1':'0');
    url += ','+($('#cable_tv').is(':checked')?'1':'0');
    url += ','+($('#smoking').is(':checked')?'1':'0');
    url += ','+($('#pets').is(':checked')?'1':'0');
    url += ','+$('input[name=htype]:checked').val();
    url += '/1/';
    window.location.href = url;
    return false;
}

function switch_top() {
    TOP_COUNTER += 1;
    if(TOP_COUNTER == TOP_IMAGES.length) {
        TOP_COUNTER = 0;
    }
    $('#top_img_0 img').attr('src', $('#top_img_1 img').attr('src'));
    
    setTimeout(function() {
        $('#top_img_1 img').hide(0);
        setTimeout(function() {
            $('#top_img_1 img').attr('src', TOP_IMAGES[TOP_COUNTER]);
            $('#top_img_1 img').fadeIn(2000, function() {
                if(TOP_COUNTER < TOP_IMAGES.length - 1) {
                    $('#top_img_0 img').attr('src', TOP_IMAGES[TOP_COUNTER+1]);
                }
            });
        }, 500);
    }, 500);
    
    setTimeout(switch_top, 12000);
}

function switch_newsletter(subscribe) {
    $('#newsletter_box').fadeTo(500, 0.10, function() {
        if(subscribe) {
            url = '/default_ajax/subscribe_box/';
        } else {
            url = '/default_ajax/unsubscribe_box/';
        }
        $.post(url, {}, function(data) {
            $('#newsletter_box').html(data).fadeTo(500, 1.00);
        });
    });
}

function subscribe_newsletter() {
    var name = $('input[name=newsletter_name]').val();
    var email = $('input[name=newsletter_email]').val();
    $('#newsletter_box').fadeTo(500, 0.10, function() {
        $.post('/default_ajax/subscribe/', {newsletter_name:name, newsletter_email:email}, function(data) {
            $('#newsletter_box').html(data).fadeTo(500, 1.00);
        });
    });
}

function unsubscribe_newsletter() {
    var email = $('input[name=newsletter_email]').val();
    $('#newsletter_box').fadeTo(500, 0.10, function() {
        $.post('/default_ajax/unsubscribe/', {newsletter_email:email}, function(data) {
            $('#newsletter_box').html(data).fadeTo(500, 1.00);
        });
    });
}

$(document).ready(function() {
    $(function() {
    	$('a[rel=lightbox]').lightBox({fixedNavigation:true});
    });
    
    // CMS Dropdown Menu
    $('#show_menu ul li').mouseover(function() {
        if(MENU_TIMER) {
            window.clearTimeout(MENU_TIMER);
        	MENU_TIMER = null;
        }
        $('#show_menu ul li').find('li').css('display', 'none');
        $(this).find('li').css('display', 'block');
        $('#show_menu').css('z-index', '500');
    });
    $('#show_menu ul li').mouseout(function() {
        var this_ref = this;
        MENU_TIMER = window.setTimeout(function() {
            $(this_ref).find('li').css('display', 'none');
        }, 500);
    });
    
    // Top Slideshow
    if(TOP_IMAGES.length > 1) {
        TOP_COUNTER = 0;
        $('#top_img_0 img').attr('src', TOP_IMAGES[1]);
        setTimeout(switch_top, 12000);
    }
});

