function valida_form() {
	$('form#validar').validationEngine();
}

function cycle() {
	//Slider
	$('#slider').cycle({
		slideExpr:	'img',
		pager:		'#paginador',
		timeout:	4000,
		pagerAnchorBuilder: function(idx, slide) {
			return '<a href="#">&bull;</a>';
		}
	})
}

function carousel() {
	//Carousel
	jQuery('#carousel').jcarousel({
		auto: 0,
		visible: 3,
		scroll: 1
	})

	//Inmueble ampliado
	$('#carousel li img').click(function(){
		var src = $(this).attr('src'),
			big = $('#img_grande img');
		src = src.replace('thumb', 'big');
		big.animate({'opacity':'0'}, 200, function(){
			var objImagePreloader = new Image();
			objImagePreloader.onload = function() {
				big
					.removeAttr('src')
					.attr('src', src)
					.animate({'opacity':'1'}, 300);
			}
			objImagePreloader.src = src;
		})
		return false;
	})
}

function form_busqueda() {
	$('.select p a').click(function(){
		var num = $(this).data('select'),
			select = $('ul.select-'+num);
		select.slideDown(100);
		select.children('li').children('a').click(function(){
			var cat = $(this),
				value = cat.siblings('span').html();
			$('#span-sel-'+num).html(cat.html());
			$('#filtro_f'+num).val(value);
			create_form_action();
			select.slideUp(100);
			return false;
		})
		$('html').click(function() {
			select.hide(0);
		})
		return false;
	})
}

function create_form_action() {
	var form = $('#busqueda'),
		action = $('#public_path').html();
	form.children('input:hidden').each(function(){
		action += $(this).val()+'/';
	})
	form.attr('action', action);
}

function go(controller) {
	form_busqueda();
	switch (controller) {
		case 'home':
			cycle();
			break;
		case 'tasaciones':
		case 'contacto':
			valida_form();
			break;
		case 'inmuebles':
			carousel();
			break;
	}
}

$(document).ready(function(){
	var controller = $('#controller').html();
	go(controller);
})

