// Always send the authenticity_token with ajax
$(document).ajaxSend(function(event, request, settings) {
    if ( settings.type == 'post' ) {
        settings.data = (settings.data ? settings.data + "&" : "")
            + "authenticity_token=" + encodeURIComponent( AUTH_TOKEN );
    }
});

//Links ajax
$(function(){
	//Hacemos la solicitud del link vía ajax
	$("a.ajax").click(function(e){
		$.ajax({
		  url: this.href,
		  // data: data,
		  // success: success,
		  dataType: 'script'
		});
		e.preventDefault();
	});
	
	//Link para eliminar campos basados en nested forms
	$("a.remove_field").click(function(){
		$(this).prev("input[type=hidden]")[0].value = '1'
		$(this).parent(".fields").fadeOut();
	});
});

//Funcion para borrar imagen
function delete_image(link){
	if (confirm("¿Está seguro?")){
		$.post(link.href, {_method:'delete'}, null, "script");
		$(link).parent("li").fadeOut(300,function(){
			$(this).remove();
		});
	}
}


//Para agregar campos nuevos en un nested form
function add_fields(link, association, content) {  
    var new_id = new Date().getTime();  
    var regexp = new RegExp("new_" + association, "g");  
    $(link).parent().before(content.replace(regexp, new_id));  
}

//Agregamos este atributo para las validaciones
$(function(){
	$.metadata.setType("attr", "validate");
});

//Validaciones
// jQuery.validator.setDefaults({ 
//     debug: true 
// });
$(function(){
	$("form.edit_property").validate();
	$("#new_account").validate(); // Formulario de registro de inmobiliarias
	$("#change_password").validate();
	$("form.validate").validate();
});

//MODALBOXS
$(function() {
	$('.modal').boxy();
});

function openNewModal(mylink){
	new Boxy.load(mylink.href);
	Boxy.get(mylink).hide();
}

//CORNERS
$(function(){
	$(".fieldsgroup").corner();
	$("#thumbnails li").corner("3px");
	$(".square_banner").corner("3px");
	$(".box").corner("top 5px");
	$("#recomendados li").corner("5px");
	$("#my_account h2").corner("top 5px");
});

//FUNCION PARA RESETEAR VALOR POR DEFECTO
$(function(){ 
	// find all the input elements with title attributes
	$('input[title!=""]').hint();
});

// Galería de fotos
$(document).ready(function() {
    $('#galeria-fotos').cycle({
			fx: 'fade',
			timeout: 0,
			next: '.galeria-sig',
			prev: '.galeria-ant',
			after: galleryOnAfter
	});
});

function galleryOnAfter(curr,next,opts) {
	var caption = (opts.currSlide + 1) + ' / ' + opts.slideCount;
	$('#galeria-title').html(curr.title);
	$('#galeria-counter').html(caption);
}
// end Galería de fotos

//Selects encadenados para ubicación
$(function () {
  var country = $('#select_country_id');
  var estate = $('#select_estate_id');
	var city = $('#select_city_id');
	var zone = $('#select_zone_id');

  country.selectChain({
      target: estate,
      url: '/properties/locations_to_select.js',
			data: country 
      // data: { ajax: true, anotherval: "anotherAction" }            
  });  

  estate.selectChain({
      target: city,
      url: '/properties/locations_to_select.js',
			data: estate
      // data: { ajax: true, anotherval: "anotherAction" }            
  });

	city.selectChain({
      target: zone,
      url: '/properties/locations_to_select.js',
			data: city
      // data: { ajax: true, anotherval: "anotherAction" }            
  });

  // note that we're assigning in reverse order
  // to allow the chaining change trigger to work
  // cat.selectChain({
  //     target: el,
  //     url: 'select-chain.php',
  //     data: { ajax: true }
  // }).trigger('change');
});

// Manager / Admin Internal Tabs
$(document).ready(function()  
{  
    $(".tab_content").hide();  
    $("ul.tabs li:first").addClass("active").show();  
    $(".tab_content:first").show();  
  
    $("ul.tabs li").click(function()  
       {  
        $("ul.tabs li").removeClass("active");  
        $(this).addClass("active");  
        $(".tab_content").hide();  
  
        var activeTab = $(this).find("a").attr("href");  
        $(activeTab).fadeIn();  
        return false;  
    });  
});

$(function(){
 	$('input.check_box_task').click(function(){  
		var task = $(this).parent("li")
		//$(this).replaceWith("<img src='/images/admin/ajax_loader.gif' />");
  	if($(this).is(':checked')){
			$.ajax({
				type: "POST",
			  url: "/manager/tasks/" + $(this).val() + "/complete",
			  data: { _method:'PUT'},
			  success: function(){
					task.addClass("completed")
				},
			  dataType: 'script'
			});				
		}
  });
});