$(document).ready(function () {
    initLightBox();
    initPNGFix();
    initInlineFormLabels();
    initAddAmounts();

    //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;
    });

    $("ul.gallery li").hover(function () { //On hover...

        var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'

        //Set a background image(thumbOver) on the <a> tag - Set position to bottom
        $(this).find("a.thumb").css({ 'background': 'url(' + thumbOver + ') no-repeat center bottom' });

        //Animate the image to 0 opacity (fade it out)
        $(this).find("span").stop().fadeTo('normal', 0, function () {
            $(this).hide() //Hide the image after fade
        });
    }, function () { //on hover out...
        //Fade the image to full opacity 
        $(this).find("span").stop().fadeTo('normal', 1).show();
    });
    $("ul.gallery li .thumb").fancybox();
});

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();
    });
}

function initAddAmounts() {
    $(".individualamount").keyup(function () {
        var add = 0;
        $(".individualamount").each(function () {
            add += Number($(this).val());
        });

        $('.totalamount').val(add.toFixed(2));
    });
};
