$(function(){
    var _tooltipOptions = {
        position:'bottom center',
        layout:'<span class="tooltip"><span class="l"><span class="r"><em/></span></span></span>',
        effect:'fade',
        delay:0,
        predelay:10
    };

    $("a[rel=external]").each(function(){
        $(this).attr('target', '_blank');
    });

    function _initHomepage()
    {
        var homepage = $('#homepage');
        
        $('.buzz .social .subscribe input.text', homepage).labelify();

//        $('.community .people a img', homepage).hover(function(){
//            var fn = $(this);
//            fn.css({
//                zIndex:100
//            }).stop().animate({
//                opacity: 1.0,
//                left:'-17px',
//                top:'-17px',
//                height:'80px',
//                width:'80px'
//            }, 150);
//        }, function(){
//            var fn = $(this);
//            fn.css({
//                height:'45px',
//                width:'45px',
//                zIndex:1,
//                left:0,
//                top:0
//            }).stop().animate({
//                opacity: .6,
//                left:'0',
//                top:'0',
//                height:'45px',
//                width:'45px'
//            }, 150);
//        });
//
//        var opts = $.extend({offset:[20, 0]}, _tooltipOptions);
//
//        $('.community .people a', homepage).tooltip(opts);

        // slideshow manager
        var slideshowUl = $('#welcome .slideshow ul');
        var slideshowInterval;
        slideshowUl.find('li a').click(function(){
            clearInterval(slideshowInterval);
            var fn = $(this);
            var tabId = fn.attr('href');
            var tab = $(tabId);
            tab.siblings('div.tab').hide();
            tab.show();
            fn.parent().addClass('active').siblings().removeClass('active');
            return false;
        }).first().trigger('click');
        slideshowInterval = setInterval(function(){
            var link = slideshowUl.find('li.active').next();
            if (!link.length) {
                link = slideshowUl.find('li:first');
            }
            link.find('a').trigger('click');
        }, 5000);
        
        // twitter tabs
        $('.twitter .links li:not(.follow) a').click(function(){
            var fn = $(this);
            var tabId = fn.attr('href');
            var tab = $(tabId);
            tab.siblings('div.tab').hide();
            tab.show();
            fn.parent().addClass('active').siblings().removeClass('active');
            return false;
        }).first().trigger('click');

        // subcsribe form
        $('div.subscribe form', homepage).validate({
            rules:{email:{required:true,email:true}},
            ignoreTitle: true,
            onkeyup: false,
            onfocusout: false,
            showErrors:function(errorMap, errorList){
                $.each(errorList, function(key, elm){
                    $(elm.element)
                       .animate( {opacity: 0.2}, 175 )
                       .animate( {opacity: 1}, 150 )
                       .animate( {opacity: 0.2}, 150 )
                       .animate( {opacity: 1}, 150 )
                       .animate( {opacity: 0.2}, 150 )
                       .animate( {opacity: 1}, 150, function(){
                            $(this).blur().labelify();
                       });
                });
            }
        });

        // locations scroll
        var locations = $('div.locations div.wrap', homepage);
        var locationsUl = $('ul', locations);
        var locationsWidth = locations.width();
        var lastLi = locationsUl.find('li:last-child');
        locations.parent().mousemove(function(e){
            var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + 10;
            var left = (e.pageX - locations.offset().left) * (ulWidth-locationsWidth) / locationsWidth;
            locations.scrollLeft(left);
        });
    }
    _initHomepage();

    $('div.sidebar input[type=text]').labelify();

    $('div.post form').find('input[type=text], textarea').labelify();

    // contact page
    if ($('#contact').length) {
        $('#contact form select').SelectCustomizer();
        $('#subject_iconselect').text('choose a subject:');
        $('#contact').find('input[type=text], textarea').labelify({
            text:'label'
        });
    }

    $('div.comments img[alt]').tooltip(_tooltipOptions);
    
    var validateOptions = {
        rules:{
            author:{
                required:true,
                minlength:2
            },
            email:{
                required:true,
                email:true
            },
            comment:{
                required:true
            }
        },
        ignoreTitle: true,
        onkeyup: false,
        onfocusout: false,
        showErrors:function(errorMap, errorList){
            $.each(errorList, function(key, elm){
                $(elm.element)
                   .animate( {opacity: 0.2}, 175 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150, function(){
                        $(this).blur().labelify();
                   });
            });

            
        },
        submitHandler:function(_form){
            var form = $(_form);
            var post = form.parents('div.post');
            var comments = post.find('div.comments');
            var messages = post.find('div.messages');
            var post_id = form.find('input[name=comment_post_ID]').val();
            var link = form.attr('action');
            var loadLink = link.replace('wp-comments-post.php', 'wp-content/themes/hp/comments-ajax.php?id=' + post_id + '&type=comments')

            form.ajaxSubmit({
                beforeSubmit:function(){
                    form.find('input.button').hide();
                },
                success:function(){
                    messages.hide().html('Thank you for commmenting!').slideDown('fast');
                    setTimeout(function(){
                        messages.slideUp('fast')
                    }, 10000);
                    form.clearForm();
                    form.find('input.button').show();
                    comments.find('ul').load(loadLink);
                    comments.find('img[alt]').tooltip(_tooltipOptions);
                },
                error:function(request){
                    var error = request.responseText.match(/<p>([^<]+)<\/p>/)[1];
                    form.find('input.button').show();
                    messages.hide().html(error).slideDown('fast');
                }
            });
        }
    };
    $('form.comment-form').each(function(key, form){
        $(form).validate(validateOptions);
    });

    $('div.box-subscribe form').validate({
        rules:{email:{required:true,email:true}},
        ignoreTitle: true,
        onkeyup: false,
        onfocusout: false,
        showErrors:function(errorMap, errorList){
            $.each(errorList, function(key, elm){
                $(elm.element)
                   .animate( {opacity: 0.2}, 175 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150, function(){
                        $(this).blur().labelify();
                   });
            });
        }
    });




    function _initMedia()
    {
        var PER_PAGE = 9;

        var media = $('#media');

        if (!media.length) {
            return false;
        }
        var list = media.find('ul.list');
        var fcontent = $('#fancy-content');
        var flinks = fcontent.find('a.prev, a.next');

        var link, img, title, li;
        var entryClick = function (element) {
            link = $(element);
            img = fcontent.find('div.image');
            title = fcontent.find('div.title');
            li = link.parent();

            img.html(li.find('.full').html());
            title.text(li.find('.tooltip').text());
            fcontent.attr('rel', link.attr('rel'));
        }

        list.find('li a').fancybox({
            'padding':40,
            'onStart':entryClick,
            'overlayShow':false
        });

        fcontent.find('a.prev, a.next').click(function(e){
            e.preventDefault();
            var current = $('#flnk-' + fcontent.attr('rel')).parent();
            var li;
            if ($(this).hasClass('next')) {
                li = current.next();
                if (!li.length) {
                    li = current.siblings(':first');
                }
            } else {
                li = current.prev();
                if (!li.length) {
                    li = current.siblings(':last');
                }
            }
            entryClick(li.find('a'));
            flinks.height(fcontent.find('img').height())
            $.fancybox.center();
        });
//        media.find('a.load-more').click(function(e){
//            e.preventDefault();
//            list.find('li:hidden:lt(' + PER_PAGE + ')').show();
//
//            if (list.find('li:hidden').length == 0) {
//                $(this).remove();
//            }
//        }).trigger('click');
        list.find('li a').tooltip(_tooltipOptions);
        return true;
    }
    _initMedia();
});
