
$(document).ready(function() {

  $("#topnavbar ul li").each(function() {
     $(this).children("ul").hide();
     var pos = $(this).position();
     var top = $(this).height() + pos.top;
     var left = pos.left;
     $(this).children("ul").css("top", top + "px");
   });
   $("#topnavbar ul li").hover(function(){
       $(this).children("ul").css("left", $(this).position().left + "px");

      $(this).children("ul").slideDown("fast");
   }, function () {
      $(this).children("ul").slideUp("fast");
   });
   
    //hide empty ccart
    if($("li.widget_wpsc_shopping_cart div.shopping-cart-wrapper").html().length == 4) {
        $(".widget_wpsc_shopping_cart").hide();
    }
    
    //show cart if something gets added
    $(".wpsc_buy_button").mouseup(function() {
        $("li.widget_wpsc_shopping_cart").show();
    });
    //hide cart if it gets cleared.
    $(".emptycart").click(function () {
        $("li.widget_wpsc_shopping_cart").hide();
    });
    //add trisaetum link after twitter widget
    $(".widget_twitter ul").after("<a class=\"twitter_link\" href=\"http://twitter.com/trisaetum\">@trisaetum</a>");

    //remove price & quantity from sold-out items
    $(".product_form").each(function() {
        var countButton = $(this).children(".wpsc_buy_button_container").children(".wpsc_buy_button").length;

        if(countButton == 0) {
            $(this).children().not(".entry-utility").hide();
        }

    });
    //text wrapping hack 
    $(".single_product_display .imagecol img").load(function() {
        var wholepagewidth = $(this).parent().parent().parent().width()
        if ($(this).width() < 350) {
            $(this).parent().parent().parent().children(".productcol").width(wholepagewidth - $(this).width() - 20);
        }
    });
    //allow padding in a grid
    $(".default_product_display").each(function(index) {
        var ltr = index % 3;
        if(ltr == 0) {
          $(this).addClass("left");
        }
        else if (ltr == 1) {
          $(this).addClass("middle");
        }
        else {
            $(this).addClass("right");
        }
    });

    //Compress right nav menu when the user is not in it.
    $(".widget-container .current-menu-item").parent().css("display", "block");

    //Use custom widht for .productcol in single page product:
   
});

$(".widget-container ul.sub-menu").parent().hover(function(){

        alert("false");
    if($(this).children(".current-menu-item").size() == 0) {
        $(this).children(".sub-menu").not("ul .current-menu-item").slideDown("slow");
    }

}, function () {
    if($(this).children(".current-menu-item").size() == 0) {
        var obj = $(this);
        setTimeout(function() {
            obj.children(".sub-menu").not("ul .current-menu-item").slideUp("slow");
        }, 750);
    }
});

$(window).resize(function() {
    $("#topnavbar ul li a").not("#topnavbar ul li ul li a").css({"padding-left" : "0", "padding-right" : "0"});
    fixnav();
});
$(document).ready(function() {fixnav();});

function fixnav() {
    var w_ul = (($(window).width() - $("#emblem").width())/2);
    $("#menu-top-left").width(Math.round(w_ul));
    $("#menu-top-right").width($(window).width() - $("#emblem").width() - $("#menu-top-left").width());

    $("#topnavbar ul").not("#topnavbar ul li ul").each(function() {
       //alert($(this).children("li").length)
       var w_items = 0;
       var count = -1;
       $(this).children("li").not("li ul li").each(function() {
           w_items += $(this).width();
           count++;
       });
       var w_total_padding = $(this).width() - w_items;
       var w_item_padding = Math.round(w_total_padding/($(this).children("li").not("li ul li").length*2));
       for(var i = 1;i<=count;i++)
       {
           $(this).children("li:nth-child(" + i + ")").not("li ul li").children("a").css({"padding-left" : w_item_padding + "px", "padding-right" : w_item_padding + "px"});
       }

       w_items = 0;
       $(this).children("li").not("li ul li").each(function() {
           w_items += $(this).width();
       });
       var w_padding_l = w_item_padding;
       var w_padding_r = $(this).width() - w_items - w_item_padding;
       count++;
       $(this).children("li:nth-child(" + count + ")").not("li ul li").children("a").css({"padding-left" : w_padding_l + "px", "padding-right" : w_padding_r + "px"});

    });
}


