/* $.noConflict(); */


var l3_rotator_1_current_position = 3;
var l3_rotator_1_stories = 0;
var l3_rotator_1_number_of_stories = 0;

var l3_rotator_2_current_position = 0;
var l3_rotator_2_stories = 0;
var l3_rotator_2_number_of_stories = 0;

var l3_rotator_3_current_position = 0;
var l3_rotator_3_stories = 0;
var l3_rotator_3_number_of_stories = 0;

jQuery(document).ready( function () {
	initialize_all();
});

function initialize_all() {
	manage_content();
	handle_comment_link();
	initialize_search_box();
	initialize_page_scripts();
	initialize_meta_box_navigation();
	initialize_small_featured_story_navigation();
	initialize_top_rotator_navigation();
	initialize_large_featured_story_navigation();
	initialize_switcher_navigation();
	initialize_attachment_lightbox();
}

function handle_comment_link() {
	hash = parent.location.hash;
	
	if (hash.length >= 2) {
		if (hash.indexOf("IDC") >= 0) {
			jQuery("div.switcher_nav > div.story").removeClass("selected");
			jQuery("div.switcher_nav > div.comments").addClass("selected");
			jQuery("#switcher > div").animate({left: "-589px"}, 400);
		}
	}
}

function manage_content() {
	jQuery("#featured_stories_large_rotator_box").children().clone().appendTo("#featured_stories_large_nav_box");
	
	if (jQuery("#idc-commentcount").html())
		jQuery("div.switcher_nav .comments").html("Comments (" + jQuery("#idc-commentcount").html() + ")");
}

function initialize_search_box() {
	jQuery("#search_box").click( function() {
		if (jQuery(this).val() == "Enter search keywords here") {
			jQuery(this).val("");
			jQuery(this).css("color", "#000");
		}
	})
		.val("Enter search keywords here");
}

function initialize_meta_box_navigation()
{
	jQuery("#meta_feed_box .header").click( function() {
		jQuery("#meta_feed_box > .selected").removeClass("selected");
		jQuery(this).parent().addClass("selected");
	});
	
	return true;
}

function initialize_large_featured_story_navigation()
{
	l3_rotator_1_stories = jQuery("#featured_stories_large_rotator_box").children();
	l3_rotator_1_number_of_stories = l3_rotator_1_stories.size();

	if (l3_rotator_1_number_of_stories <= 1)
		return;

	var l3_rotator_1_interval = setInterval("large_featured_story_rotate()", 15000);
	jQuery([l3_rotator_1_stories[1], l3_rotator_1_stories[2], l3_rotator_1_stories[3]]).css("display", "none").css("z-index", "-1");

	large_featured_story_rotate();
}

function large_featured_story_rotate()
{
	old_position = l3_rotator_1_current_position;
	new_position = (l3_rotator_1_current_position + 1) % l3_rotator_1_number_of_stories;

	l3_rotator_1_current_position = new_position;

	jQuery(l3_rotator_1_stories[old_position]).css("z-index", "100");

	jQuery(jQuery("#featured_stories_large_nav_box li")[new_position]).addClass("active");
	jQuery(jQuery("#featured_stories_large_nav_box li")[old_position]).removeClass("active");

	jQuery(l3_rotator_1_stories[new_position]).css("z-index", "101").fadeIn(600, function() {
							jQuery(l3_rotator_1_stories[old_position]).css("z-index", "-1").css("display", "none");
						});
}

function initialize_small_featured_story_navigation()
{
	jQuery("#featured_stories_small_box_nav div.newer").hide();

	l3_rotator_2_stories = jQuery("#featured_stories_small_rotator_box").children();
	l3_rotator_2_number_of_stories = l3_rotator_2_stories.size();

	if (l3_rotator_2_number_of_stories <= 1) 
		return;

	jQuery("#featured_stories_small_box_nav div.newer").click( function() {
		l3_rotator_2_current_position -= 1;
		small_featured_story_update();
	});

	jQuery("#featured_stories_small_box_nav div.older").click( function() {
		l3_rotator_2_current_position += 1;
		small_featured_story_update();
	});
	
	var l3_rotator_2_interval = setInterval("small_featured_story_rotate()", 12000);
}

function small_featured_story_rotate()
{
	old_position = l3_rotator_2_current_position;
	new_position = (l3_rotator_2_current_position + 1) % (l3_rotator_2_number_of_stories-1);

	l3_rotator_2_current_position = new_position;

	small_featured_story_update();
}

function small_featured_story_update()
{
	if (l3_rotator_2_current_position + 2 >= l3_rotator_2_number_of_stories)
		jQuery("#featured_stories_small_box_nav div.older").hide();
	else
		jQuery("#featured_stories_small_box_nav div.older").show();
	if (l3_rotator_2_current_position == 0)
		jQuery("#featured_stories_small_box_nav div.newer").hide();
	else
		jQuery("#featured_stories_small_box_nav div.newer").show();
	
	jQuery("#featured_stories_small_rotator_box .small_featured_story").queue("fx", []);
	jQuery("#featured_stories_small_rotator_box .small_featured_story").stop().animate({left: (-294*l3_rotator_2_current_position) + "px"}, {queue: false, duration: 600});
}

function initialize_top_rotator_navigation()
{
	l3_rotator_3_stories = jQuery("#top_page_rotator ul.stories").children();
	l3_rotator_3_number_of_stories = l3_rotator_3_stories.size();

	if (l3_rotator_3_number_of_stories == 1) 
		return;

	jQuery("#top_page_rotator > .right_nav > .left_arrow").click( function() {
		if (l3_rotator_3_current_position > 0) {
			l3_rotator_3_current_position -= 1;
			top_rotator_update();
		}
	});

	jQuery("#top_page_rotator > .right_nav > .right_arrow").click( function() {
		if (l3_rotator_3_current_position + 3 < l3_rotator_3_number_of_stories) {
			l3_rotator_3_current_position += 1;
			top_rotator_update();
		}
	});
}

function top_rotator_update()
{
	jQuery("#top_page_rotator").queue("fx", []);
	jQuery("#top_page_rotator ul.stories li").stop().animate({left: (-260*l3_rotator_3_current_position) + 135 + "px"}, {queue: false, duration: 600});
}

function initialize_page_scripts()
{
/*	jQuery("#twitter_box .content").append("<script type=\"text/javascript\" src=\"http://twitter.com/javascripts/blogger.js\"></script>\n<script type=\"text/javascript\" src=\"http://twitter.com/statuses/user_timeline/arbiteronline.json?callback=twitterCallback2&amp;count=7\" defer=\"defer\"></script>"); */
}

function initialize_attachment_lightbox()
{
	if (jQuery("dt.gallery-icon a").size() > 0) {
		jQuery("dt.gallery-icon a").each(function() {
			var description = jQuery(this).attr('title');
			var image_location = jQuery(this).find("img").attr("src");
			jQuery(this).attr("href", image_location.replace("-150x150", ""));
		});
	}
}


function initialize_switcher_navigation()
{
	jQuery("div.switcher_nav > div.comments").click(function() {
		jQuery("div.switcher_nav > div.story").removeClass("selected");
		jQuery("div.switcher_nav > div.comments").addClass("selected");
		jQuery("#switcher > div").animate({left: "-589px"}, 400);
	});
	jQuery("div.switcher_nav > div.story").click(function() {
		jQuery("div.switcher_nav > div.story").addClass("selected");
		jQuery("div.switcher_nav > div.comments").removeClass("selected");
		jQuery("#switcher > div").animate({left: "0px"}, 400);
	});
}