$(document).ready(function() {
    UWEMP.init();

	jQuery('#more-themes').click(function() {
		jQuery.get('/wp-content/themes/uwemp/theme-info.php', function(data) { jQuery.facebox(data) });
		return false;
	});
});

var UWEMP = {
    init: function() {
        UWEMP.home.init();
        UWEMP.follow.init();
        UWEMP.tabs.init();
        
        $('#search label').label_as_input();
    },
    
    tabs: {
        init: function() {
            $('.tabs li').click(function() {
                var id = $(this).attr('id');
                
                $('.tabs li').removeClass('current');
                $(this).addClass('current');
                
                $('.tab-content').hide();
                $('#' + id + '-content').show();
            });
        }
    },
    
    home: {
        timer: null,
        
        init: function() {
            
            if (!$('ul.carousel').get(0)) {
                return;
            }
    
            $('ul.carousel').show();
            jQuery('.carousel').jcarousel({
                initCallback: function() {
                    var heighest = 0;
                    $('.articles, .video').each(function() {
                        var height = $(this).height();
                        if (height > heighest) {
                            heighest = height;
                        }
                    }).each(function() {
                        var height = $(this).height();
                        if (height < heighest) {
                            var obj = $('.jcarousel-container, .desc', this);
                            var new_height = heighest - height + obj.height();
                            obj.height(new_height);
                            
                        }
                    });
                }
            });
    
            $('ul.carousel li a').click(function() {
                var id = $(this).attr('rel');
                
                $('.articles .posts li').hide();
                $('#' + id).show();
                
                $('ul.carousel li a').removeClass('current');
                $(this).addClass('current');
                
                UWEMP.home.rotate.stop();
                
                return false;
            });
            
            $('.articles .post h3 a').hover(function() {
                UWEMP.home.rotate.stop();
            }, function() {
                UWEMP.home.rotate.start();
            });
            
            UWEMP.home.rotate.start();
            
            var heighest = 0;
            $('.box1').each(function() {
                var height = $('.content', this).height();
                if (height > heighest) {
                    heighest = height;
                }
            });
            $('.box1 .content').height(heighest);
    
            var heighest = 0;
            $('.box2').each(function() {
                var height = $('.content', this).height();
                if (height > heighest) {
                    heighest = height;
                }
            });
            $('.box2 .content').height(heighest);
        },
        
        rotate : {
            start : function() {
                var rotate = function() {
                    var next = $('.carousel .current').parents('li').next();
                    
                    if (!next.get(0)) {
                        next = $('.carousel li').eq(0);
                    }
                    
                    next.find('a').trigger('click');
                    
                    UWEMP.home.timer = window.setTimeout(rotate, 8000);  
                };
                UWEMP.home.timer = window.setTimeout(rotate, 8000);
            },
            
            stop : function() {
                window.clearTimeout(UWEMP.home.timer);
            }
        }
    },
    
    follow: {
        init: function() {
            $('a.follow, a.unfollow').click(UWEMP.follow.click);
        },
        
        click: function() {
            var anch = $(this);
            $.get($(this).attr('href') + '?ajax=1', function(data) {
                if (data && data.indexOf('ERROR: ') > -1) {
                    var msg = data.replace('ERROR: ', '');
                    jQuery.facebox(msg);
                    return;
                }
                var newanch = $(data);
                anch.replaceWith(newanch)
                newanch.click(UWEMP.follow.click);
                if (anch.hasClass('follow')) {
                    jQuery.facebox('You are now following this author and will receive\
                        email updates when they publish new articles.');
                }
            });
            return false;
        }
        
    }
};

$.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;
        });
    }
});
