(function($) { $(function() {
   // hover class for dropdowns
   $("#portal-globalnav li").hover(
     function() { $(this).addClass("hover"); },
     function() { $(this).removeClass("hover"); }
   );
   
   // line up the dropdown links with the top button
   $(document).ready(function(){
       numTabs = $("#portal-globalnav li").length;
       for (i = 0; i < numTabs; i++) {
           totalWidth = $("#portal-globalnav li:eq("+i+")").width();
           itemWidth = $("#portal-globalnav li:eq("+i+") a span").width();
           space = (totalWidth - itemWidth)/2 - 1;
           $("#portal-globalnav li:eq("+i+") ul.submenu li a").css("padding-left",space);
       }
   });
   
   // change newsletter signup button text
   $(".portletMailChimp .context").attr("value","Submit").show();

   // pre-fills the newsletter textfield
   $(".portletMailChimp .textType").val("email@myemail.com").show();
   $(".portletMailChimp .textType").focusin(function(){
       if($(this).val() == "email@myemail.com"){$(this).val("").show();}
    });
    $(".portletMailChimp .textType").focusout(function(){
        if($(this).val() == ""){$(this).val("email@myemail.com").show();}
    });
   
   // determine if user is logged in
   if ( typeof anonymous !== 'undefined' && anonymous ){
       createCookie('MoinAuth', '', -1);
       $("#edit-li").hide();
   } else {
       $("#portal-personaltools.anon").hide();
       $(".externalSiteAuth").show();
   }

   // forces hover event on director's pick
   $(".portlet-static-directors-picks a").hover(
   function(){
       $(this).addClass("dpickhover");
   },
   function(){
       $(this).removeClass("dpickhover");
   });

   // update "current" tab
   if ( $(".info-paging-info").length > 0 ) {
       $("#view-anchor").removeClass("current");
       $("#edit-anchor").removeClass("current");
       $("#history-anchor").addClass("current");
   } else if ( $("#footer ul.editbar").length > 0 ) {
       $("#view-anchor").addClass("current");
       $("#edit-anchor").removeClass("current");
       $("#history-anchor").removeClass("current");
   } else if ( $("form#editor").length > 0 ) {
       $("#view-anchor").removeClass("current");
       $("#edit-anchor").addClass("current");
       $("#history-anchor").removeClass("current");
   }
   
});

$(window).load(function(){
    var capnum = $("dl.captioned").length;
    
    if (capnum > 0) {
        // set width on image <dl>s to be set at the width of the image
        // to keep caption from pushing out the width
        for(i = 0; i < capnum; i++) {
             var imgWidth = $("dl.captioned img").eq(i).width();
             $("dl.captioned").css("width",imgWidth);
        }
    
        // hide image caption area if there is no caption
        for(i = 0; i < capnum; i++) {
             if ($(".image-caption").eq(i).text() == "") {
                 $(".image-caption").eq(i).hide();
                 $("dl.captioned img").css("float","left");
             }
        }
        
    }
    
    // fix up "fake captions" to display like normal captions
    theImages = $(".inline-image").not('img');
    var fakeCaptions = $(theImages).length;

    if (fakeCaptions > 0) {
        for(i = 0; i < fakeCaptions; i++) {
            // apply class for styling
            $(theImages).eq(i).addClass("captionedImage");
            
            // get alignment from image
            var alignment = $(theImages).eq(i).find("img").attr("align");
            $(theImages).eq(i).css("float",alignment).addClass(alignment);
            
            // set width on container
            var imgWidth = $(theImages).eq(i).find("img").width();
            $(theImages).eq(i).css("width",imgWidth);
        }

    }
    
    // add Plone classes to floating images
    $("img[align='right']").addClass("image-right");
    $("img[align='left']").addClass("image-left");
    $("#content-core table[align='right']").addClass("captionedImage right");
    $("#content-core table[align='left']").addClass("captionedImage left");
    $('table.captionedImage td:not(:has(img))').addClass("captionCell");
    
});

$('form#editor').live('submit', function(){
    if ( typeof oFCKeditor !== 'undefined' ){
        var editor_iframe = $('#' + oFCKeditor.InstanceName + '___Frame');
        var editor_instance = $('#' + oFCKeditor.InstanceName);
        if ( editor_iframe.length > 0 &&  editor_instance.length > 0 ) {
            editor_instance[0].value = editor_iframe.contents().find('iframe').contents().find('body').html();
        }
    }
});

})(jQuery);

