var which_id;

$(document).ready(function() {
    $("#middle .right_content div:not(.default)").hide();

    $("#middle .left_content li a").click(onClick);
    var loc = window.location.hash;
    
    if (loc.length > 0)
    {
        which_id = loc;
        doTransition();
    }
    
    initNavigation();
    initHovers();
    initSlideshows();
    
    $("p.product_item span").hide();
    $("p.product_item a").click(function(evt){
        $("p.product_item span").hide();
        var id = "#" + $(this).attr("name");
        $(id).show();
        evt.preventDefault();
    });
});

function initSlideshows(){
    if (jQuery.browser.msie){
      $("#middle .headline .image_row").html('<img src="/images/slides/msie.jpg">');
      $("#middle .headline .image_row").show();
    }else{
      $("#middle .headline .image_row").show();
      $(".img1").cycle({delay:0, timeout: 6000});
      $(".img2").cycle({delay:2000, timeout: 6000});
      $(".img3").cycle({delay:4000, timeout: 6000});
    }
}


function initNavigation(){
    var items = $("#middle .navigation li a");
    var total_width = $("#middle .navigation").outerWidth();
    var half_width = total_width / 2;
    items.each(function(i){
        var offset = total_width - ( 0.35 * (( i / items.length) * total_width) + 30);
        offset = offset - $(this).innerWidth();
        $(this).css("left", offset + "px");
        
/*         var img = "http://static.mailinglistsxpress.com/images/row" + (i + 1) + "-trans.png"; */
        var img = "http://static.mailinglistsxpress.com/images/row" + (i + 1) + "-trans.png";        
        $(this).before('<img src="' + img + '" alt="white overlay" class="trans_over_row" />');
        
    });
}

function initHovers(){
    $('#middle .navigation li:not(.hover)').hover(function(event){
        $(this).addClass('hover');
    },
    function(event){
        $(this).removeClass('hover');
    });

    $('#middle .middle_wrapper a:not(.hover)').hover(function(event){
        $(this).addClass('hover');
    },
    function(event){
        $(this).removeClass('hover');
    });
}

function doTransition(){
    var which_name = which_id.split("#")[1];
    $('#middle .left_content li a[name="'+which_name+'"]').addClass("current");
    $("#middle .right_content div:visible").slideUp("fast", function(){
        $(which_id).slideDown("fast");
        //window.location.hash = which_id;
    });
}

function onClick(event){
    $('#middle .left_content li a').removeClass("current");
    which_id = "#" + $(this).attr("name");
    doTransition();
    event.preventDefault();
}

