$(document).ready(function() {
    $('header label').label_as_input();
    $('ul li:last-child').addClass('last');
    $('.cols .col:last-child').addClass('last');
    $('footer li').not('.last').append('<span>|</span>');
    
    $('#about .team .col').gridify();
    
    $('.actions .facebook, .actions .twitter').click(function() {
        window.open($(this).attr('href'),'sharer','toolbar=0,status=0,width=626,height=436');
        return false;
    });
    
    LAL.Portfolio.init();
    LAL.Comments.init();
});

var LAL = {

    Comments : {
        
        init : function() {
            $('#commentform').ajaxError(function() {
                if (!$('.error-msg', this).get(0)) {
                    $('.comment-notes', this).after('<p class="error-msg copy">Please fill in all the required fields below.</p>');
                }
                $('.error-msg', this).hide().fadeIn();
            });
            
            $('#commentform').submit(function() {
                $.post($(this).attr('action'), $(this).serialize(), function(data) {
                    $('#comment').val('');
                    window.location.reload();
                });
                return false;
            });
        }
        
    },

    Portfolio : {
        current : 0,

        init : function() {
            $('#portfolio article').gridify();
            
            $('article.portfolio .view').click(function() {
                LAL.Portfolio.popup(this);
                return false;
            });
        },
        
        popup : function(obj) {
            LAL.Portfolio.current = 0;
            
            $.blockUI.defaults.css = {};
            $.blockUI({ message : '\
                <div class="nav prev">&lt;</div>\
                <div class="nav next">&gt;</div>\
                <div class="viewer"></div>\
            ' });
            $('.blockMsg .viewer').html($(obj).parents('article').find('.gallery').clone().show());

            $('.blockMsg .viewer h4 .left').append(' <span>|</span> <b class="progress"></b>');
            $('.blockMsg .viewer h4').append('<a href="#" class="close">close</a>');

            LAL.Portfolio.show_item(0);
            
            $('.blockMsg .prev').click(function() {
                LAL.Portfolio.show_item(-1);
            });

            $('.blockMsg .next').click(function() {
                LAL.Portfolio.show_item(1);
            });
            
            $('.blockMsg .viewer .close').click(function() {
                $.unblockUI();
                return false;
            });
            
            $('.blockOverlay').click(function() {
                $.unblockUI();
            });
        },
        
        show_item : function(x) {
            var item_num = LAL.Portfolio.current + x;
            var num_lis = $('.blockMsg .viewer li').size();
            if (item_num >= num_lis)
                item_num = 0;
            if (item_num < 0)
                item_num = num_lis - 1;
            
            var li = $('.blockMsg .viewer li').hide().eq(item_num);
            
            var img = new Image();
            $(img).load(function() {
                li.width(img.width).fadeIn();
                $('.blockMsg').center();
            });
            img.src = $('img', li).attr('src');
            
            LAL.Portfolio.current = item_num;
            $('.blockMsg .progress').html((item_num+1) + '/' + num_lis);
        }

    }

};


(function($){
    
 	$.fn.extend({ 
 		label_as_input: function() {
            
    		return this.each(function() {
                var input = $(this).siblings('input.text,textarea').eq(0);
                var txt = $(this).text();
                
                $(this).hide();
                
                var _focus = function() {
                    if (input.val() == txt) {
                        input.removeClass('dim');
                        input.val('');
                    }
                };
                
                var _blur = function() {
                    if (input.val() == '') {
                        input.addClass('dim');
                        input.val(txt);
                    }
                };
            
                input.focus(_focus);
                input.blur(_blur);
                
                _focus();
                _blur();
                
                input.parents('form').submit(_focus);
                
                return this;
    		});
    	}
	});

    $.fn.center = function() {
        // Always return each...
        return this.each(function() {
            var t = $(this);

            var leftMargin = t.width() / 2;
            var topMargin = t.height() / 2;

            if( typeof( window.pageYOffset ) == 'number' ) {
                // Netscape
                var scrollOffset = window.pageYOffset;
            } else if( document.body && document.body.scrollTop ) {
                // DOM
                var scrollOffset = document.body.scrollTop;
            } else if( document.documentElement && document.documentElement.scrollTop ) {
                // IE6 standards compliant mode
                var scrollOffset = document.documentElement.scrollTop;
            }
            else {
                var scrollOffset = 0;
            }

            var topOffset = ($(window).height() / 2) + scrollOffset - topMargin;
            var leftOffset = ($('body').width() / 2) - leftMargin;
            
            if (topOffset < 0)
                topOffset = 0;

            t.css({
                position: 'absolute',
                left: leftOffset + 'px',
                top: topOffset + 'px'
            });
        });
    };

    $.fn.gridify = function(per_row) {
        if (!per_row && this.size()) {
            var container = this.parent();
            var item = this.eq(0);
            per_row = Math.floor(container.width() / item.width());
        }
        
        return this.each(function(i) {
            if ((i+1) % per_row == 0) {
                $(this).addClass('last');
                
                var max_height = 0;
                var el = $(this);
                for (var x = 1; x <= per_row; x++) {
                    var h = el.height();
                    if (h > max_height)
                        max_height = h;
                    el = el.prev();
                }
                
                var el = $(this);
                for (var x = 1; x <= per_row; x++) {
                    el.height(max_height);
                    el = el.prev();
                }
            }
        });
    };
    
})(jQuery);

