var timer;
var progressing = false;
var combinedWidth = 0;

( function($) {
$( function(){
    $("#slider").easySlider({
	auto: true, 
    continuous: true,
	prevNext: false,
	numeric: true,
	pause: 5000,
	speed: 500
    });

    /* Projects page - Progress Button code */
    $('#timelineOverlay').height( ($('.content').height() + 44) );
    
    $('#contentContainer')
        .mousemove( function(e){
            var textContainer;

            if ( $('.leftContent').length ) {
                textContainer = $('.leftContent');
            }
            else if ( $('.content').length ) {
                textContainer = $('.content');
            }

            var leftHeight = 90 + $(textContainer).height() + ( $(textContainer).hasClass('content') ? 256 : 0 );
        
            if ( e.pageY > 201 && e.pageY < leftHeight ) {
                progressSlide((e.pageY - 100));
            }
            else if ( e.pageY < 201 ) {
                progressSlide(120);
            }
            else if ( e.pageY > leftHeight ) {
                progressSlide( (leftHeight - ( $(textContainer).hasClass('content') ? 60 : 90 )) );
            }
        });
    $('#timelineButtonSlider')
        .hover( function(){
            $(this).css('cursor', 'pointer');
        })
        .click( function(){
            if ( progressing ) {
                return false;
            }

            progressing = true;

            if ( $('#timelineOverlay').css('display') == 'none' ){
                $(this).trigger('showTimeline');
            }
            else {
                $(this).trigger('hideTimeline');
            }

            progressing = false;
        })
        .bind('showTimeline', function(){
            $('#timelineOverlay').css('display','block');
            $('#timelineFiller').height( ($('#timelineOverlay').height() - $('#timelineContainer').height() - $('#timelinePurple').height() + 1) );

            $('.timelineYear:first, .timelineYear:last').width( ($('.timelineYear').width() / 2) + 'px' );

            $('.timelineYear').each( function(){
                combinedWidth = combinedWidth + $(this).width();
            })

            $('#timelineSliderSlide').width(combinedWidth + 'px');

            var diffWidth = Math.floor( (combinedWidth - $('#timelineSlider').width()) / 2 );

            if ( diffWidth > 0 ) {
                //$('#timelineSliderSlide').css('marginLeft', (diffWidth * -1) + 'px');
            }
            else {
                $('#timelineArrowLeft, #timelineArrowRight').hide();
            }
        })
        .bind('hideTimeline', function(){
            $('#timelineOverlay').css('display','none');
        });
    
    $('#timelineArrowLeft')
        .bind('mouseover', function(){
            $('#timelineSliderSlide').animate( { marginLeft: 0 }, 1500 );
        })
        .bind('mouseout', function(){
            $('#timelineSliderSlide').stop();
        });
        
    $('#timelineArrowRight')
        .bind('mouseover', function(){
            $('#timelineSliderSlide').animate( { marginLeft: (($('#timelineSliderSlide').width() - $('#timelineSlider').width()) * -1) + 'px' }, 1500 );
        })
        .bind('mouseout', function(){
            $('#timelineSliderSlide').stop();
        });

    $('.timelineDot div')
        .bind('mouseenter', function(){
            $(this).find('img:first').attr('src', $(this).find('img:first').attr('src').replace('.png', 'Hover.png') );
            if ( $(this).find('.timelineDetailBox').length == 0 ) {
                var newDetail = $('<div>').addClass('timelineDetailBox');
                $(newDetail).appendTo( $(this) );
                $('<img src="' + $(this).attr('rel') + '" />').appendTo( $(this).find('.timelineDetailBox') );
                $('<div>').addClass('timelineMonth').html( $(this).attr('id').split('-')[0] ).appendTo( $(this).find('.timelineDetailBox') );
            }

            $(this).find('.timelineDetailBox').show();
        })
        .bind('mouseleave', function(){
            $(this).find('img:first').attr('src', $(this).find('img:first').attr('src').replace('Hover.png','.png') );
            $(this).find('.timelineDetailBox').hide();
        })

    $('#progressButtonSlider')
        .hover( function(){
            $(this).css('cursor', 'pointer');
        })
        .bind('showProgress', function(){
            $('#progressOverlay').css('display', 'block')
                .find('.progressDetail').trigger('openDetail');
        })
        .bind('hideProgress', function(){
            $('#progressOverlay')
                .find('.progressDetail').trigger('closeDetail');
        })
        .click( function(){
            if ( progressing ) {
                return false;
            }

            progressing = true;

            if ( $('#progressOverlay').css('display') == 'none' ){
                $(this).trigger('showProgress');
            }
            else {
                $(this).trigger('hideProgress');
            }
        });

        $('#progressOverlay .progressDetail')
            .bind('openDetail', function(){
                $(this).fadeIn('fast');
                $(this).find('.progressBar').trigger('openBar');
                progressing = false;
            })
            .bind('closeDetail', function(){
                $(this).find('.progressBar').trigger('closeBar');
                $(this).fadeOut('fast');
                $('#progressOverlay').css('display', 'none');
                progressing = false;
            });

        $('#progressOverlay .progressDetail .progressBar')
            .bind('openBar', function(){
                var fullWidth = (parseFloat($(this).attr('rel')) / 100) * $(this).width();
                var animateWidth = fullWidth - $(this).find('.progressGreenLeft').width() - $(this).find('.progressGreenRight').width();

                if ( animateWidth < 1 ) {
                    animateWidth = 1;
                }

                $(this).find('.progressGreenMiddle').animate( {width: animateWidth + 'px'}, 'slow');
            })
            .bind('closeBar', function(){
                $(this).find('.progressGreenMiddle').animate( {width: '1px'}, 'slow');
            });
    /* END Projects page - Progress Button code */
});
})(jQuery);

// Function for sliding progress button with a very slight delay
function progressSlide(amount){
    var slider;

    if ( amount < 120 ) {
        amount = 120;
    }

    if ( jQuery('#progressButtonSlider').length ) {
        slider = jQuery('#progressButtonSlider');
    }
    else if ( jQuery('#timelineButtonSlider').length ) {
        slider = jQuery('#timelineButtonSlider');
    }
    else {
        return false;
    }

    jQuery(slider).css('top', amount + 'px');
}
