$(function(){
    $('.form label').perfectForm();
    
    $(".fancybox").fancybox({
        'overlayOpacity': 0.7,
        'overlayColor': '#000'
    });
    
});


jQuery.fn.autoHeight = function(options) 
{
    var settings = {
        minHeight   : false,
        limitHeight  : false,
        ignore  : '',
        padding : 10
    }

    if(options) {
        jQuery.extend(settings, options);
    };

    var maxHeight = 0;

    this.not(settings.ignore).each(function(){
        if ($(this).height() > maxHeight){
            if(settings.limitHeight && maxHeight >= settings.limitHeight) {
                maxHeight = settings.limitWidth;
            } 
            else if(settings.minHeight && maxHeight <= settings.limitHeight)
            {
                maxHeight = settings.minHeight;
            } 
            else 
            {
                maxHeight = $(this).height();
            }
        }
    });  

    this.not(settings.ignore).height(maxHeight + settings.padding);

}


jQuery.fn.perfectForm = function(options)
{
    var settings = {
        overlap: true,
        ignore: ''             
    }

    if(options) {
        jQuery.extend(settings, options);
    };

    this.not(settings.ignore).each(function(){
        var input = $(this).next('input[type=text], input[type=password], textarea');
        var label = $(this);
        
        // first check all inputs for values, then hide it's label if it has a value
        if( settings.overlap ) {
            label.addClass('overlap');
        }
        
        if( settings.overlap && input.val() != '' ) {
            label.hide();
        }
        
        label.click(function(){
            label.hide();
            input.focus();
        }, function(){
            if( input.val() == '' ) {
                label.show();
            }
        });
        
        // then add a focus event to all inputs to hide their labels when focused on, 
        // and remain hidden on blur if a value is entered
        input.focus(function(){
            label.hide();
        }).blur(function(){
            if( $(this).val() == '' ) {
                label.show();
            }
        });
    });
}

