var comments =
{
    speed: 1000,
    reply_speed: 500,
    
    init: function() {
        if ($('#comments').length > 0) {
            $('#comments a.closer').click(comments.close);
            $('#comments span.reply a').click(comments.reply_to);
            
            $('span.reply').show();
            if (!$('#page.blog').hasClass('single')) {
                $('#comments').hide();
            }
            
            $('#page.video .action .comment a').click(comments.toggle);
            $('#page.blog .action .comment a').click(comments.toggle);
        }
    },
    
    toggle: function(e) {
        if ($('#comments:visible').length > 0) {
            return comments.close(e);
        }
        else {
            return comments.open(e);
        }
    },
    
    close: function(e) {
        $('#comments').slideUp(comments.speed);
        
        e.preventDefault();
        return false;
    },
    
    open: function(e) {
        $('#comments').slideDown(comments.speed);
        
        e.preventDefault();
        return false;
    },
    
    reply_to: function(e) {
        var comment = $(this).parents('.comment').first();
        var reply_form = comment.find('div.reply');
        
        if (comment.find('div.reply:visible').length > 0) {
            reply_form.slideUp(comments.reply_speed);
        }
        else {
            reply_form.slideDown(comments.reply_speed);
        }
        
        e.preventDefault();
        return false;
    }
};

var login =
{
    init: function() {
        if ($('#page').hasClass('login')) {
            $('#login_form').submit(login.submission);
            $('.forgotform form').submit(login.submission);
            $('.loginform .forgot a').click(login.forgot);
        }
        
        var header = $('#header .nav .user li.login');
        if (header.length > 0) {
            header.find('a.login').click(login.popup);
            header.find('div.login').first().click(login.popup);
            header.find('form').submit(login.submission);
            $('.loginform .forgot a').click(login.forgot);
        }
    },
    
    submission: function(e) {
        if (!validate.fields($(this))) {
            
            
            e.preventDefault();
            return false;
        }
        else {
            return true;
        }
    },
    
    forgot: function(e) {
        $('.loginform').fadeOut(250, function(){
            $('.forgotform').fadeIn(250);
        });
        
        e.preventDefault();
        return false;
    },
    
    popup: function(e) {
        var header = $('#header .nav .user li.login');
        if (header.find('div.login:visible').length > 0) {
            header.find('div.login').hide();
        }
        else {
            header.find('.forgotform').hide();
            header.find('.loginform').show();
            header.find('div.login').show();
        }
        
        e.preventDefault();
        return false;
    }
};

var register =
{
    init: function() {
        if ($('#page').hasClass('login')) {
            $('#member_form').submit(register.submission);
        }
    },

    submission: function(e) {
        if (!validate.fields($(this))) {
            
            
            e.preventDefault();
            return false;
        }
        else {
            return true;
        }
    }
};

var profile =
{
    init: function() {
        if ($('#page').hasClass('account')) {
            $('#page.account form').submit(profile.submission);
        }
    },

    submission: function(e) {
        if (!validate.fields($(this))) {
            
            
            e.preventDefault();
            return false;
        }
        else {
            return true;
        }
    }
};

var contact =
{
    init: function() {
        if ($('#page').hasClass('contact')) {
            $('#page.contact form').submit(contact.submission);
        }
    },

    submission: function(e) {
        if (!validate.fields($(this))) {


            e.preventDefault();
            return false;
        }
        else {
            return true;
        }
    }
};

var validate =
{
    error_class: 'error',
    
    fields: function(jQ) {
        var error_fields = new Array();
        
        var req = jQ.find('.req');
        var match = jQ.find('.match');
        
        if (match.length > 0) {
            var val = match.first().val();
            match.each(function(){
                if ($(this).val() != val) {
                    error_fields.push($(this));
                }
            });
        }
        
        if (req.length > 0) {
            req.each(function(){
                if (!$(this).val() || !($(this).val().length > 0)) {
                    error_fields.push($(this));
                }
            });
        }
        
        validate.errorify(jQ, error_fields);
        
        return !(error_fields.length > 0);
    },
    
    errorify: function(jQ, fields) {
        jQ.find('.'+validate.error_class).removeClass(validate.error_class);
        
        $(fields).each(function(){
            if (!$(this).hasClass(validate.error_class)) {
                $(this).addClass(validate.error_class);
            }
        });
    }
}

var player =
{
    fade: 200,
    pre_embed: '<iframe src="',
    post_embed: '" width="100%" height="100%" frameborder="0"></iframe>',
    mobile_safari: false,
    
    init: function() {
        var user_agent = navigator.userAgent.toLowerCase();
        if ((user_agent.indexOf('iphone') != -1) ||
            (user_agent.indexOf('ipod') != -1) ||
            (user_agent.indexOf('ipad') != -1)) {
                player.mobile_safari = true;
        }
        
        var players = $('#page.video .player, #page.home .player, #page.blog .ipad .player');
        var imagers = $('.extras .image a');
        var lb = $('#lightbox');
        var switchers = $('a.switcher');
        var div = switchers.find('div');
        var tvs = $('.post.tv .blogoverlay, #page.blog .droid .player, #page.blog .droid .head');
        var lightboxers = $('.player').not(players).not(tvs);
        
        if (players.length > 0) {
            players.click(player.play);
        }
        if (lightboxers.length > 0) {
            lightboxers.click(player.lightbox);
        }
        if (imagers.length > 0) {
            imagers.click(player.imagebox);
        }
        if (lb.length > 0) {
            lb.click(player.lb_toggle);
        }
        if (switchers.length > 0) {
            switchers.find('video').hide();
            div.click(player.switcher_toggle);
            div.css('background-color', 'transparent').css('position', 'absolute').css('left', '2px').css('top', '2px').css('z-index', '1000').css('width', '620px').css('height', '349px');
            switchers.click(player.switcher);
        }
        if (tvs.length > 0) {
            tvs.click(player.tv);
        }
    },
    
    play: function(e) {
        player.do_play($(this))
        
        e.preventDefault();
        return false;
    },
    
    do_play: function(jQ) {
        var href = jQ.attr('href');
        var div = jQ.find('div');
        
        div.html(player.pre_embed+href+player.post_embed);
        div.show();
        jQ.find('span').hide();
        jQ.find('img').hide();
    },
    
    un_play: function(jQ) {
        var href = jQ.attr('href');
        var div = jQ.find('div');
        
        div.html('');
        div.hide();
        jQ.find('span').show();
    },
    
    lightbox: function(e) {
        var href = $(this).attr('href');
        var lb = $('#lightbox');
        
        if (player.mobile_safari) {
            lb.css('left', window.pageXOffset).css('top', window.pageYOffset);
        }
        
        var vid = $('<div></div>');
        vid.addClass('videoplayer');
        lb.append(vid);
        
        vid.html(player.pre_embed+href+player.post_embed);
        
        var x = $('<a></a>');
        x.html('Close video').addClass('x').attr('href', '#').click(player.lb_toggle);
        vid.append(x);
        
        player.lb_toggle(e);
        vid.css('left', player.find_left(vid)+'px');
        vid.css('top', player.find_top(vid)+'px');
        vid.show();
        
        e.preventDefault();
        return false;
    },
    
    imagebox: function(e) {
        var href = $(this).attr('href');
        var lb = $('#lightbox');
        
        var img = $('<div></div>');
        img.addClass('imageplayer');
        lb.append(img);
        
        img.html($(this).find('.lb').html());
        
        var x = $('<a></a>');
        x.html('Close image').addClass('x').attr('href', '#').click(player.lb_toggle);
        img.append(x);
        
        player.lb_toggle(e);
        img.css('left', player.find_left(img)+'px');
        img.css('top', player.find_top(img)+'px');
        img.show();
        
        e.preventDefault();
        return false;
    },
    
    tv: function(e) {
        var post = $(this).parents('.post');
        
        if (post.find('.head:visible').length > 0) {
            player.do_play(post.find('.player').first());
            post.find('.head').hide();
        }
        else {
            player.un_play(post.find('.player').first());
            post.find('.head').show();
        }
        
        e.preventDefault();
        return false;
    },
    
    lb_toggle: function(e) {
        var lb = $('#lightbox');
        if (lb.is(':visible')) {
            lb.fadeOut(player.fade, function(){
                lb.html('');
            });
        }
        else {
            lb.fadeIn(player.fade);
        }
        
        e.preventDefault();
        return false;
    },
    
    find_left: function(jQ) {
        var width = jQ.width();
        var win = $(window).width();
        
        return parseInt((win / 2) - (width / 2));
    },
    
    find_top: function(jQ) {
        var hgt = jQ.height();
        var win = $(window).height();
        
        return parseInt((win / 2) - (hgt / 2));
    },
    
    switcher: function(e) {
        var vid = $(this).find('video').first();
        
        $(this).find('img').hide();
        $(this).find('span').hide();
        $(this).find('div').show();
        
        vid.show();
        vid[0].play();
        
        e.preventDefault();
        return false;
    },
    
    switcher_toggle: function(e) {
        var playing = $(this).parent().find('video:visible').first();
        var other = $(this).parent().find('video:hidden').first();
        
        playing[0].pause();
        other[0].currentTime = playing[0].currentTime;
        other[0].play();
        other.fadeIn(100);
        playing.fadeOut(100);
        
        e.preventDefault();
        return false;
    }
};

var sharer =
{
    init: function() {
        if ($('#page').hasClass('video')) {
            $('.action .share .email').hide();
            $('.action .share a').click(sharer.email);
            $('.action .share .email').first().click(sharer.email);
            
            $('.action .share form').submit(function(e){
                if (!validate.fields($(this))) {
                    e.preventDefault();
                    return false;
                }
                else {
                    return true;
                }
            });
        }
    },
    
    email: function(e) {
        var share = $('.action .share');
        if (share.hasClass('active')) {
            share.removeClass('active');
            share.find('div.email').hide();
        }
        else {
            share.addClass('active');
            share.find('div.email').show();
        }
        
        e.preventDefault();
        return false;
    }
};

var autolinker =
{
    href: '/',
    
    init: function() {
        var links = $('.autolink a');
        
        if (links.length > 0) {
            autolinker.href = links.first().attr('href');
            setTimeout("autolinker.go()", 2500);
        }
    },
    
    go: function(href) {
        document.location = autolinker.href;
    }
};

var placeholder =
{
    init: function() {
        var texts = $('input[type="text"][title]');
        
        if (texts.length > 0) {
            texts.focusin(placeholder.focus);
            texts.focusout(placeholder.unfocus);
            placeholder.set_placeholder(texts);
        }
    },
    
    focus: function(e) {
        placeholder.unset_placeholder($(this));
    },
    
    unfocus: function(e) {
        placeholder.set_placeholder($(this));
    },
    
    set_placeholder: function(jQ) {
        jQ.each(function(index, Element) {
            var title = $(this).attr('title');
            if ($(this).val().length < 1 && title.length > 0) {
                $(this).val(title);
                $(this).css('color', '#999');
            }
        });
    },
    
    unset_placeholder: function(jQ) {
        jQ.each(function(index, Element) {
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
                $(this).css('color', '#000');
            }
        });
    }
};

$(document).ready(function(){
    comments.init();
    login.init();
    register.init();
    profile.init();
    contact.init();
    player.init();
    sharer.init();
    autolinker.init();
    placeholder.init();
});
