$(document).ready(function(){
    BindForm();
});

window.onload = function(){
	ColsHeight($('.inner_tiles .tile'));
}

$(window).resize(function(){
    ColsHeight($('.inner_tiles .tile'));
});

function BindForm(){
    var $overlay = $('.overlay');
    var $form = $('.hidden_form');
    $('.order_button').click(function(){
        var product = $(this).attr('rel');
        var pad = ($(window).height() - 496)/2;
        if (pad < 0){
            pad = 0;
        }
        var newtop = $(window).scrollTop() + pad;
        $('#form-product').val(product);
        $form.find('.title .temp_title').html(product)
        $overlay.stop().css('display', 'block').animate({opacity: 0.5}, 500);
        $form.stop().css({display: 'block', top: newtop}).animate({opacity: 1}, 500);
        return false;
    });
    $form.find('.close').click(function(){
        $overlay.stop().animate({opacity: 0}, 500, function(){
            $(this).css('display', 'none');
        });
        $form.stop().animate({opacity: 0}, 500, function(){
            $(this).css('display', 'none');
            $form.find('.title .temp_title').html('&nbsp;')
        });
        return false;
    });
}

function ColsHeight($container){
    var maxHeight = 0;
    $container.css('height', 'auto');
    $container.each(function(){
        if ($(this).height() > maxHeight){
            maxHeight = $(this).height();
        }
    });
    $container.css('height', maxHeight);
}
