jQuery.extend(
  jQuery.expr[ ":" ],
  { reallyvisible : function (a) { return !(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length); }}
);

$(document).ready(function()
{
    loadInit();
});

function expandFlashContainer()
{
    $('.flash-container').delay(310).animate({"height":"544px"}, {duration:1250, easing:'easeInOutExpo'});
}

function loadInit()
{

/*	**************************************
	TWITTER SHARING
	*********************************** */

	var twitter_links = $('a.twitter-share-button');

	twitter_links.each(function( index, el ){

		var el = $(el); // this
		var url = el.attr('href'); // original URL
		var string = ' '; // string to append to the URL
		var items = el.parents('.time-block').find('ul:first li a'); // a elements from the first leaderboard panel

		// get the first three items from the leaderboard
		for( var i = 0; i < 3; i++) {
			// 1. Item one etc.
			var text = (i + 1) + '. ' + $( items[ i ] ).attr('title') + ' ';
			var escaped_text = escape( text );

			string += escaped_text;

		}

		// Set the URL
		el.attr({
			'href' : url + string
		});

	});


/*	**************************************
	SUGGESTION FORM
	*********************************** */

    $('form#suggest-awesomeness').ajaxForm({
		beforeSubmit: function() { // Ensure validation works
            return $('form#suggest-awesomeness').validate().form();
        },
    	success: function ( response ) {
			$('.suggestion-box').html( response );
    	}
    }).validate({

		rules: {
			email: {
				required: true,
				email: true
			},
			thing: {
				required: true
			},
			link: {
				required: true,
				url: true
			}
		},
		messages: {
			email: '',
			thing: '',
			link: ''
		}
    });

	$("a.submit-it-again").live("click", function()
	{
	   $(".suggest-container").html(suggestContainerHtml);
	    return false;
	});

/*	**************************************
	ABOUT US MODAL
	*********************************** */
    $('a.about').click(function( event )
    {
        event.preventDefault();
        
        $('.about-popup').lightbox_me(
        {
            modalCSS : {
                top: '165px'
            }
        });

        return false;
    });

/*	**************************************
	LEADERBOARDS
	*********************************** */
    $("a.most-awesome").hide();

    $('.most-least-header a').click(function(event){

    	var header = $(".most-least-header");
    	var most_container = $('.leader-container');
    	var least_container = $('.least-container');
    	var len = 150;

    	event.preventDefault();

		$('a', header).toggle();

		header.toggleClass("least");
		header.toggleClass("most");
		
		if ( $( most_container ).is(':visible') ) {
			
			most_container.fadeOut( len, function() {
				least_container.fadeIn( len );
			});
			
		} else {

			least_container.fadeOut( len, function() {
				most_container.fadeIn( len );
			});

		}
    });

    $(".time-block ul li").hover(function()
    {
        $(this).addClass("hover");
    },function()
    {
        $(this).removeClass("hover");
    });

    $(".submit-it").hover(function()
    {
        $(this).addClass("hover");
    },function()
    {
        $(this).removeClass("hover");
    });

    $(".content .header-links a.twitter").hover(function()
    {
        $(this).addClass("hover");
    },function()
    {
        $(this).removeClass("hover");
    });

    $('.things-slider1').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 0,
        next:   $(".right#next1"),
        prev:   $(".left#prev1")
    });

    $('.things-slider2').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 0,
        next:   $(".right#next2"),
        prev:   $(".left#prev2")
    });

    $('.things-slider3').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 0,
        next:   $(".right#next3"),
        prev:   $(".left#prev3")
    });

    $('.things-slider4').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 0,
        next:   $(".right#next4"),
        prev:   $(".left#prev4")
    });

    $('.things-slider5').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 0,
        next:   $(".right#next5"),
        prev:   $(".left#prev5")
    });

    $('.things-slider6').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 0,
        next:   $(".right#next6"),
        prev:   $(".left#prev6")
    });

    $('.things-slider7').cycle({
        fx:     'fade',
        speed:   400,
        timeout: 0,
        next:   $(".right#next7"),
        prev:   $(".left#prev7")
    });
}

