$(document).ready(function()
{
    var rootUrl = "http://www.rotterdamapartments.com/";
    
	$("a[rel='lightbox']").colorbox({
        width: '800px',
        height: '600px',
        close: 'Close' 
	});
    
    $('.innerfade').innerfade({
		speed: 750,
		timeout: 5000,
		type: 'random'
	});
    
    $(".validateForm").validate({
        errorPlacement: function(error, element) {
			error.prependTo( element.parent().next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
    });
    
    $(".homeText").jScrollPane();
    
    $('#carousel').jcarousel({
        scroll: 1
    });
    
    $('#order').change(function() {
        vars = getUrlVars();
        var url = '';
        var i = 0;
        for (var key in vars) {
            if (key != 'orderby') {
                if (i == 0) {
                    url += '?' + key + '=' + vars[key];
                } else {
                    url += '&' + key + '=' + vars[key];
                }
            }
            i++;
        }
        if (i == 0) {
            url += '?orderby=' + $(this).val();
        } else {
            url += '&orderby=' + $(this).val();
        }
        window.location = rootUrl + '3/4/Aanbod.html' + url;
    });
    
    jQuery.validator.messages.required = "";
    jQuery.validator.messages.email = "";  
    $("#verhuurRegistreerForm").validate({});
    $("#shortStayInformatieOpvragenForm").validate({});
    $("#contactForm").validate({});
    $("#intresseForm").validate({});
    
    $('#availableperdirect').change(function() {
         if ($(this).is(':checked')) {
            $('select[name=availableyear]').attr('disabled', true);
            $('select[name=availablemonth]').attr('disabled', true);
         } else {
            $('select[name=availableyear]').attr('disabled', false);
            $('select[name=availablemonth]').attr('disabled', false);
         }
    });
    
    $('#city').change(function() {
        $.getJSON(rootUrl + '3/4/Aanbod.html', {id: $(this).val(), ajax: true}, function(j){
            var options = '';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].value + '">' + j[i].display + '</option>';
            }
            $("#area").html(options);
        });
    });
    
    $('#urbanisation').change(function() {
        $.getJSON("controllers/woningen.php",{id: $(this).val(), ajaxVolledig: true}, function(j){
            var options = '';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].value + '">' + j[i].display + '</option>';
            }
            $("#urbanisationVolledig").html(options);
        });
    });
    
    $('#plaatsnaamSale').change(function() {
        $.getJSON("controllers/woningen.php",{id: $(this).val(), ajax: true}, function(j){
            var options = '';
            for (var i = 0; i < j.length; i++) {
                options += '<option value="' + j[i].value + '">' + j[i].display + '</option>';
            }
            $("#urbanisation").html(options);
        });
    });
    
    $('.addInterest').click(function() {
        $('#WoningItemForm').submit();    
        return false;
    });
});

function getUrlVars() {
    var map = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        map[key] = value;
    });
    return map;
}
