

$(document).ready(function(){
    /*
     * Calculate the width of the horizontal articles to make scrolling possible
     */
    if ($(".news-category dd")) {
        var articles = $(".news-category dd");
        var articlesWidth = articles.length * (articles.width() + parseInt(articles.css('margin-right').replace("px", ""))) + 20;
        $('#articles').width(articlesWidth);
    }
	
	$("#articles").find("dd").each(function(i){
		$(this).mouseenter(function(){
            if ($(this).hasClass('disabled')) {
				$(this).fadeTo("normal", 1);
			}
		});
		
		$(this).mouseleave(function(){
            if ($(this).hasClass('disabled')) {
				$(this).fadeTo("normal", 0.33);
			}
		});
	});
	
	if($("#tagMenu")) {
		$("#tagMenu").change(function () {
	        $("#tagMenu option:selected").each(function () {
	        	if($(this).val() != "") {
					var keyword = $(this).val();
					$("#articles").find("dd").each(function(i){
						if($(this).attr("tags").indexOf(keyword) >= 0) {
							$(this).fadeTo("normal", 1);
							$(this).toggleClass( "disabled", false );
						} else {
							$(this).fadeTo("normal", 0.33);
							$(this).toggleClass( "disabled", true );
						}
					});
				} else {
					$("#articles").find("dd").each(function(i){
						$(this).fadeTo("normal", 1);
						$(this).toggleClass( "disabled", false );
					});
				}
	        });
	    })
	}
    $("#tagMenu").change();

    if ($("#categoryMenu:has(dd)")) {
        if (!location.hash) {
            $("#categoryMenu").find("dd").each(function(i){
                $(this).removeClass('selected');
            });
            $("#categoryMenu dd:first").addClass('selected');
        }
        else {
            $("#categoryMenu dd a[href$='" + location.hash + "']").parent().addClass('selected');
        }
        $("#categoryMenu").find("dd").each(function(i){
            $(this).find('a').click(function(){
                if (!$(this).parent().hasClass('selected')) {
                    $("#categoryMenu").find("dd").each(function(i){
                        $(this).removeClass('selected');
                    });
                    $(this).parent().addClass('selected');
                }
                else {
                    return false;
                }
            });
        });
    }
    
    var mainContentOffset = $("#mainContent").position().left * -1;
    
    $.localScroll.defaults.axis = 'x';
    $.localScroll.defaults.hash = true;
    $.localScroll.defaults.offset = mainContentOffset;
    $.localScroll();
    
   /*$.History.bind(function(state){
        // Our Generic Handler - Always runs last.
		//alert('history changed to state:' + state);
        $.scrollTo($(state), {
            speed: 1000,
            offset: mainContentOffset
        });
    });*/
    /*
	var target = location.hash;
    if (target) {
        $.scrollTo(target, {
            speed: 1000,
            offset: mainContentOffset
        });
    }*/
});

