$(document).ready(function(){
    $('input, textarea').example(function() {
        return $(this).attr('title');
    });

    var w= 0;
    $('.menu li.parent').each(function(){
        w+=$(this).outerWidth(false) + 1;
    });

   $('ul.menu').css('width',w);
        

    initForm();
    if($('#propiedades_estado').length){
        $('#propiedades_estado').wrapChildren({
            childElem : 'li' ,
            groupSize: 11,
            wrapper : '<ul class="left">'
        });
        $('#propiedades_estado').show();
    }
    $('ul.tabs, div.tabs_wrap').tabs('div.panes > div',{history: true});

});


function initForm(){
    $("#contact_form").validate({
        //set the rules for the field names
        rules: {
            "ksa_contacto[nombre]": {
                required: true,
                minlength: 3
            },
            "ksa_contacto[email]": {
                required: true,
                email: true
            },
            "ksa_contacto[telefono]": {
                required: true,
                minlength: 5
            },
            "ksa_contacto[comentarios]": {
                required: true,
                minlength: 5
            }
        },
        //set messages to appear inline
        messages: {
            name: {
                required:"Este campo es obligatorio.",
                minlength:"Introduce al menos 2 caracteres."
            },
            email: {
                required:"Este campo es obligatorio.",
                email:"Introduce una direccion de e-mail válida"
            },
            comment: {
                required:"Este campo es obligatorio.",
                minlength:"Introduce al menos 5 caracteres."
            }
        },

        //hide errors forever. We just need element highlighting
        errorPlacement: function(error, element) {
            error.hide();
        },

        //Submit the Form
        submitHandler: function() {
            //Post the form values via ajax post
            $.post($("#contact_form").attr('action'), $("#contact_form").serialize()+'&ajax=1', function(result){
                clearFields();
                $('#contact_form').remove();		//hide comment form
                $('#email_success').fadeIn(500);	//Show mail success div
            });
        }
    });
}

function clearFields(){
    $('input, textarea').each(function(){
        $(this).val('');
    });
}

// Copyright (c) 2011 Russ Cam
// -------------------------------------------------------
// Dual licensed under the MIT and GPL licenses.
//   - http://www.opensource.org/licenses/mit-license
//   - http://www.opensource.org/licenses/gpl-3.0
(function($){
    $.fn.wrapChildren = function(options) {

        options = $.extend({
            childElem : undefined,
            groupSize : 1,
            wrapper : '<div>'
        }, options || {});

        if (options.childElem === undefined) return this;

        return this.each(function() {
            var elems = $(this).children(),
            len = pos = 0;

            elems.each(function(i,value) {
                len += $(value).is(options.childElem)? 1 : 0;
                if (len > 0 && (len % options.groupSize === 0) || (i === elems.length - 1)) {
                    var wrapper = $('<ul class="left"></ul>');
                     if(i === elems.length - 1){
                       wrapper.addClass('last_prop');
                    }
                    elems.slice(pos,i + 1).wrapAll(wrapper);
                    len = 0;
                    pos = i + 1;
                }
                   
            });
        });

    }
})(jQuery);
