$(document).ready(function() {
    initLightBox();
    initPNGFix();
    initInlineFormLabels();

    //Main Menu Stuff
    var config = {
        sensitivity: 2,
        interval: 100,
        over: MainMenuHoverOver,
        timeout: 500,
        out: MainMenuHoverOut
    };
    $("#header nav li .sub").css({ 'opacity': '0' });
    $("#header nav li").hoverIntent(config);

    $('#portfolio_slider').coinslider({
        width: 700,
        height: 400,
        delay: 5000,
        opacity: 0.7,
        hoverPause: true
    });

    $(".tweet").tweet({
        username: "techflare",
        join_text: "auto",
        avatar_size: 0,
        count: 5,
        auto_join_text_default: "",
        auto_join_text_ed: "we",
        auto_join_text_ing: "we were",
        auto_join_text_reply: "we replied to",
        auto_join_text_url: "we were checking out",
        loading_text: "loading tweets..."
    });

    $('.newsBox #tabMenu > li').click(function() {
        if (!$(this).hasClass('selected')) {
            $('.newsBox #tabMenu > li').removeClass('selected');
            $(this).addClass('selected');
            $('.newsBox .item').slideUp('1500');
            $('.newsBox .item:eq(' + $('.newsBox #tabMenu > li').index(this) + ')').slideDown('1500');
        }
        return false;
    });
});

function initLightBox() {
    $('a[rel="lightbox"]').lightBox();
}
function initPNGFix() {
    $(document).pngFix();
}
function initInlineFormLabels() {
    $('input[title]').each(function() {
        $(this).blur(function() {
            if ($(this).val() === '') {
                $(this).removeClass('good');
                $(this).addClass('bad');
            } else {
                $(this).removeClass('bad');
                $(this).addClass('good');
            }
        });
    });
    
    $('input[rel="email"]').each(function(){
        $(this).blur(function(){
            var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
            if(filter.test($(this).val())){
                $(this).removeClass('bad');
                $(this).addClass('good');
            }else{
                $(this).removeClass('good');
                $(this).addClass('bad');
            }
        });
    });

    $('textarea[title]').each(function() {
       $(this).blur(function() {
            if ($(this).val() === '') {
                $(this).removeClass('good');
                $(this).addClass('bad');
            } else {
                $(this).removeClass('bad');
                $(this).addClass('good');
            }
        });
    });
}

function MainMenuHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show();
    (function($){
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function(){
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function(){
                rowWidth += $(this).width();
            });
        };
    })(jQuery);
    
    if($(this).find(".row").length > 0){
        var biggestRow = 0;
        $(this).find(".row").each(function(){
            $(this).calcSubWidth();
            if(rowWidth > biggestRow){
                biggestRow = rowWidth;
            }
        });
        
        $(this).find(".sub").css({'width':biggestRow});
        $(this).find(".row:last").css({'margin':'0'});
        
    }else{
        $(this).calcSubWidth();
        $(this).find(".sub").css({'width':rowWidth});
    }
}

function MainMenuHoverOut(){
    $(this).find(".sub").stop().fadeTo('fast', 0, function(){
        $(this).hide();
    });
}