// Version 0.1
// Last Updated: October 7

$(document).ready(function() {

// Stingray
function stingray() {

	// Hide the colors
	setTimeout(function() {
		$("#navigation a span").fadeOut("slow");
		$("#fonts a span").fadeOut("slow");
		$("#search button span,#search_again button span").fadeOut("slow");
		}, 300);
	
	// Fade em in on hover
	$("#navigation a,#search button,#search_again button,#fonts a").hover(
		function() {
		$(this).find("span").fadeIn('250');
		},
		function() {
		$(this).find("span").fadeOut("normal");
		});
	}

// Inject the spans
function shrinkWrap() {
	$("a.symbol,ul.links a,h3.leaking a,.anchors a,.options a,#subnavigation a,#company_overview dt,.messages.attached li").each(
		function(i) { 
		$(this).html('<span>' +$(this).html() + '</span>');
	});
	}

// Attack of the Clones
function cloneSheep() {
	$("a.symbol span,.bar_right span,#subnavigation a span,.options a span,.anchors a span,#company_overview dt span,.messages.attached li span").each(function(i) { 
	$(this).clone().appendTo($(this).parent()).addClass("dolly");
	});
	}

// Patch it up!
function patchIt() {
	$(".splash").append('<span class="patch a"></span><span class="patch b"></span>');
	$(".advertisment").append('<span class="patch a"></span><span class="patch b"></span><span class="patch c"></span><span class="patch d"></span>');
	// Add A spacer
	$("ul.links a,h3.leaking a").prepend('<span class="spacer"></span>');
	}

function tagIt() {
	// Give IE a hand with those tricky forms
	$("input:text,input:password").addClass("text");
	$("input:checkbox").addClass("checkbox");

	// Last child support
	$(".sandwich li:last-child").addClass("last");
	// Striping the Posts
	$("ul li.post:odd").addClass("odd");
	$("ul.posts li:first").addClass("first");
	$("ul.posts li:last").addClass("last");
	
	// 2 column stripes
	/*
	$("dl dd:even,dl dt:even,li:even").addClass("even");
	$("dl dd.even:even,dl dt.even:even,li.even:even").addClass("doubleEvenZero");
	$("li.even:even + li + li + li").addClass("doubleEven");
	$("dl dd:odd,dl dt:odd,li:odd").addClass("odd");
	$("dl dd.odd:even,dl dt.odd:even,li.odd:even:next").addClass("doubleOdd");

	$("dl dt:first,dl dd:first,li:first").addClass("first");
	$("dl dt:last,dl dd:last,li:last").addClass("last");
	*/

	$(".stocks_grid").each(function(i) {

	var ul = $(this);
	ul.find("li:first").addClass("first").end().find("li:last").addClass("last");
	ul.find("li").each(function(x) {
		if (x % 2 == 0) { $(this).addClass("even"); }
		if (x % 2 == 1) { $(this).addClass("odd"); }
		if (x % 4 == 3) { $(this).addClass("xdoubleEven"); }	
        		})
	})

	}
	
// Sexy Sliding Action
function showSubnav() {
	// $("#subnavigation,#subnav_container").hide(); // done via CSS now, except in Safari cause it sucks
	$("#subnavigation").slideDown("500",
		function() {
		$("#subnavigation #subnav_container").fadeIn("slow");
	});
	}

// Bookmark Posts -- Cheers Jake
$(".post .star").click(function() {
    var bookmarkedPost = $(this).parents("li:first");
    bookmarkedPost.toggleClass("bookmarked");
    return false;
});

// Opening and closing the little board posts search

$(".options .search_posts").toggle(function() {
	$("#search_posts_form").fadeIn('fast');
	},function() {
	$("#search_posts_form").fadeOut('fast');
	});

// Our Board Posts JavaScript Extravaganza

// Hovering on posts
$(".post").hover(function() {
    
    if( $(this).is(".opened") )
        return;
    
    $(this).addClass('hovering');
},function() {
    
    $(this).removeClass('hovering');
});

// Opening Posts (It's pretty simple, just click on it!)
$(".private_messages .post").click(function() {

    if( $(this).is(".opened") )
        return; // Don't do anything

	var openingPost = $(this);
	
	openingPost.removeClass("hovering");
	openingPost.find(".excerpt, .time_short").fadeOut('150');
	
	openingPost.addClass("opened");
	openingPost.find(".closePost,.full,.signature,.controls,.user .info,.time").fadeIn('300');
	
	return false;
});

// Closing Posts (Clicking on the Red X ball)
$(".private_messages .post .closePost").click(function() {

    var closingPost = $(this).parents("li:first"); // This is the post

    closingPost.find(".full,.closePost,.signature,.controls,.user .info").fadeOut('150');

	closingPost.find(".time").fadeOut('150', function() {
	    closingPost.find(".excerpt").fadeIn('350').end();
	    closingPost.removeClass("opened").find(".time_short").fadeIn('150');
	});

	return false;
});

//Close a post by clicking on the h3 again
$(".private_messages .post h3").click(function() {

    var closingPost = $(this).parents("li:first"); // This is the post
    
    if( !closingPost.is(".opened") ) // we cant close the post if its already closed 
    	return;

	closingPost.find(".full,.closePost,.signature,.controls,.user .info").fadeOut('150');

	closingPost.find(".time").fadeOut('150', function() {
	closingPost.find(".excerpt").fadeIn('350').end();
	closingPost.removeClass("opened").find(".time_short").fadeIn('150');

	})
	return false;
});

// Opening Posts (It's pretty simple, just click on it!)
$(".post").click(function() {

    if( $(this).is(".opened") )
        return; // Don't do anything

	var openingPost = $(this);

	openingPost.removeClass("hovering");
	
if( !$.browser.msie ) { // for real browsers
	openingPost.find(".excerpt, .time_short").fadeOut('150');
    
	openingPost.find(".rating").fadeOut('150', function() {
        openingPost.addClass("opened");
        openingPost.find(".closePost,.full,.signature,.controls,.user .info,.time").fadeIn('300');
	        });
}
if( $.browser.msie ) { // for wannabe browsers
	openingPost.addClass("opened");
}

});

// Closing Posts (Clicking on the Red X ball)
$(".post .closePost").click(function() {

    var closingPost = $(this).parents("li:first"); // This is the post


if( !$.browser.msie ) { // for real browsers
	closingPost.find(".full,.closePost,.signature,.controls,.user .info").fadeOut('150');

	closingPost.find(".time").fadeOut('150', function() {
	closingPost.find(".excerpt").fadeIn('350').end().find(".rating").fadeIn('600');
	closingPost.removeClass("opened").find(".time_short").fadeIn('150');
	})
}
if( $.browser.msie ) { // for wannabe browsers
	closingPost.removeClass("opened");
}
    return false;
});

//Close a post by clicking on the h3 again
$(".post h3").click(function() {

    var closingPost = $(this).parents("li:first"); // This is the post
    
    if( !closingPost.is(".opened") ) // we cant close the post if its already closed 
    	return;

if( !$.browser.msie ) { // for real browsers
	closingPost.find(".full,.closePost,.signature,.controls,.user .info").fadeOut('150');

	closingPost.find(".time").fadeOut('150', function() {
	closingPost.find(".excerpt").fadeIn('350').end().find(".rating").fadeIn('600');
	closingPost.removeClass("opened").find(".time_short").fadeIn('150');
	})
}
if( $.browser.msie ) { // for wannabe browsers
	closingPost.removeClass("opened");
}

});

// From Detailed view to Minimal View (Close em all)
$(".views .minimal a").click(function() {

	if( $(this).parents("#board_posts").is(".minimal") )
		return; // don't do anything -- minimal was already clicked, clicking it again does nothing

	$(".views li").toggleClass("selected");
	$("#board_posts").removeClass("detailed").addClass("minimal");
	
	var closingPosts = $(".posts .post");

	closingPosts.find(".time").hide();
	closingPosts.find(".excerpt").fadeIn('75').end().find(".rating").fadeIn('75');
	closingPosts.removeClass("opened").find(".time_short").show();
    
    	});

// From Minimal to Detailed View (open em up again)
$(".views .detailed a").click(function() {

	if( $(this).parents("#board_posts").is(".detailed") )
		return; // don't do anything -- the detailed button has already been clicked

	$(".views li").toggleClass("selected");
	$("#board_posts").removeClass("minimal").addClass("detailed");

	var openingPosts = $(".posts .post");

	openingPosts.find(".excerpt, .time_short").hide();
    	openingPosts.find(".rating").hide();
	openingPosts.addClass("opened");
	openingPosts.find(".closePost,.full,.signature,.controls,.user .info,.time").show();

	});

// The Editing Action
/*
$("#edit_picks_sell,#edit_picks_buy,.edit_favorite_boards").click(function() {
	if( $(this).parent().is(".editing") ) 
		$(this).parent().toggleClass("editing").find(".findAndAdd").fadeOut('fast');

	else
		$(this).parent().toggleClass("editing").find(".findAndAdd").fadeIn('normal');
	});

$(".edit_watchlist").click(function() { 
	if( $(this).parent().parent().is(".editing") )
		$(this).parent().parent().toggleClass("editing").find(".findAndAdd").fadeOut('fast');

	else
		$(this).parent().parent().toggleClass("editing").find(".findAndAdd").fadeIn('normal');
	});
*/

// Nix me!
/*
$("a.delete").click(function() {
	$(this).parent().remove();
	});
*/

// Activate !

shrinkWrap();
cloneSheep();
patchIt();
tagIt();
stingray();

// Tabs

$('#company > ul').tabs();
$('#stock_graph > ul').tabs();
$('#user_profile > ul').tabs();
$('#your_blog > ul').tabs();
$('#contacts > ul').tabs();
$('#inbox > ul').tabs();

//$(".slider").slider();

});