$(document).ready(function() {
    positionObjects();

	$("#mainMenu ul li").each(function(i) {
		$(this).find("ul").css("display", "none");
	});
	
    $("#mainMenu ul li ul").parent().each(function(i) { // we only want the top level list items ... not all LIs under mainMenu
        $(this).children("ul:eq(0)").css({ top: $(this).find('a:eq(0)').outerHeight() }); // position submenu
        $(this).hover(function() {
            selectTab($(this)); // make UI/CSS changes for top-level menu
            $(this).children("ul:eq(0)").show(); // show submenu
        }, function() {
            //selectTab(null); // hide submenus, and clear tabs (don't select a new one)
        });
    });

    /*$("#mainMenu ul li ul li").hover(function() { // show submenu indicators
        $(this).addClass("hoverRightBottom");
        $(this).children("a").addClass("hoverLeftBottom");
    }, function() { // remove submenu selected indicators
        $(this).removeClass("hoverRightBottom");
        $(this).children("a").removeClass("hoverLeftBottom");
    });*/

    //$("#mi" + selectedTab).mouseover(); // show default tab
	
	// Feature Rotator
	$("#curFeatureText").show(); //Show Banner
	$("#curFeatureText").animate({ opacity: 0.85 }, 1); //Set Opacity            
	$(".main_image").click(function() {
        $(this).find('a').click();
    });
    $(".main_image").hover(function() {
        featureSlideshow_pauseAdvanceFeature();
    }, function() {
        featureSlideshow_unpauseAdvanceFeature();
    });

    //Click and Hover events for thumbnail list
    $(".image_thumb ul li").click(function() {
        featureSlideshow_pauseAdvanceFeature();
        //Set Variables
        var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
        var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
        var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
        var imgStoryLink = $(this).find('.storyLink a').attr('href'); //Get URL of story
            var imgDescHeight = $(".main_image").find('#curFeatureText').find('.block').height(); //Calculate height of block
        if ($(this).is(".active")) {  //If it's already active, then...
            return false; // Don't click through
        } else {
            //Animate the Teaser
                $("#curFeatureText .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function() {
                    $("#curFeatureText .block").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 250);
                    $("#curFeatureImage").attr({ src: imgTitle, alt: imgAlt });
                    $(".main_image a").attr("href", imgStoryLink);
                $("#mainImageHREF_TEXT").attr("href", imgStoryLink);
            });
        }

        featureSlideshow_curFeature = parseInt($(this).attr("id").substring(7)); // set curFeature ID
        $(".image_thumb ul li").removeClass('active'); // set as active item
        $(this).addClass('active');  //add class of 'active' on this list only
        //featureSlideshow_timer = setTimeout('featureSlideshow_advanceFeature()', featureSlideshow_timeoutMiliseconds);
        featureSlideshow_unpauseAdvanceFeature();
        return false;

    }).hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    featureSlideshow_advanceFeature();	
	$(window).resize(positionObjects);
});

function selectTab(tab) {
    $("#mainMenu ul li ul").hide(); // hide all submenus that might be open

    // remove all css indicators
    $("#mainMenu ul li").removeClass("hoverRightTop");
    $("#mainMenu ul li a").removeClass("hoverLeftTop");
    $("#mainMenu ul li ul li").removeClass("hoverRightBottom");
    $("#mainMenu ul li ul li a").removeClass("hoverLeftBottom");

    // now show the passed in tab's            
    if (tab != null) {
        tab.addClass("hoverRightTop");
        tab.children("a").addClass("hoverLeftTop");
    }
}

function placeOverArea(placeHolderID, objectToMoveID) {
    var placeHolder = $("#" + placeHolderID);
    var movingObject = $("#" + objectToMoveID);
    movingObject.css({ left: placeHolder.offset().left, top: placeHolder.offset().top });
}

function positionObjects() {
    placeOverArea("header", "headerArea");
    placeOverArea("menuPlaceHolder", "mainMenu>ul");
}

// FEATURE slide show
var featureSlideshow_timeoutMiliseconds = 6000;
var featureSlideshow_curFeature = -1;
var featureSlideshow_numFeatures = 0;
var featureSlideshow_timer;
function featureSlideshow_advanceFeature() {
    featureSlideshow_curFeature = (featureSlideshow_curFeature + 1) % featureSlideshow_numFeatures;
    $("#feature" + featureSlideshow_curFeature).click();
    featureSlideshow_unpauseAdvanceFeature();
}
function featureSlideshow_pauseAdvanceFeature() {
    clearTimeout(featureSlideshow_timer);
}
function featureSlideshow_unpauseAdvanceFeature() {
    clearTimeout(featureSlideshow_timer);
    featureSlideshow_timer = setTimeout('featureSlideshow_advanceFeature()', featureSlideshow_timeoutMiliseconds);
}
var featureSlideshow_thumbnails = [];
function featureSlideshow_addThumbnail(imgID, marginTop, marginLeft, scaleWidth, scaleHeight)
{
	featureSlideshow_thumbnails.push(new Array(imgID, marginTop, marginLeft, scaleWidth, scaleHeight));
}
function featureSlideshow_resizeThumbnails()
{
	for(var i=0;i<featureSlideshow_thumbnails.length;i++)
	{
		var curThumbnail = featureSlideshow_thumbnails[i];
		$("#"+curThumbnail[0]).css("margin",curThumbnail[1]+"px 0px 0px "+curThumbnail[2]+"px");
		$("#"+curThumbnail[0]).css("width",curThumbnail[3]+"px");
		$("#"+curThumbnail[0]).css("height",curThumbnail[4]+"px");
	}
}

// DIGG BUTTON
(function() {
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://widgets.digg.com/buttons.js';
s1.parentNode.insertBefore(s, s1);
})();

// Twitter
tweetmeme_style = 'compact'; // compact twitter button

