
//fix bad navigation style

fixNavigationStyle = function() {
    usernav = jQuery('#nav #user');
    globnav = jQuery('#nav');
    mainnav = jQuery('#main-nav', globnav);
    twidth = 0;
    jQuery('li', mainnav).each(
        function (){
            twidth = twidth + jQuery(this).width();
        }
    )
    if (globnav.width() - twidth - usernav.width() < 212) mainnav.css('clear', 'right');
    else mainnav.removeAttr('style');
}

jQuery(window).load(fixNavigationStyle);
jQuery(window).wresize(fixNavigationStyle);

// fix actions alone style (with no submenu)
// must be improved

fixActionsAloneStyle = function() {
    jQuery('#contentActionMenus li').each(function(){
        if (! jQuery('dl', this).length) jQuery('a:first', this).css({'float': 'right', 'padding': '8px 6px'});
    })
}

//jQuery(document).ready(fixActionsAloneStyle);


// fix sphinx with exit class

fixSphinxLinks = function() {
    jQuery('ul:has(li.toctree-l1)').addClass('toctree');
    jQuery('p:has(span.versionmodified)').addClass('p-versionmodified');
}

jQuery(document).ready(fixSphinxLinks);

// helpers
isInString = function(str1, str2) {
    try {
      var reg=new RegExp(".*"+str1+".*$","i");
      if(str2.match(reg))
         return true;
      else
         return false;
    }
    catch(e) {return false}
}

isFromPloneOrg = function(link) {
    if (isInString('http://.*plone.org', link)) {
        return true;
    }
    if (isInString('http://.*plone.net', link)) {
        return true;
    }
    return false;
}


// add ' (en)' in accordion links when link is on plone.org

tagEnglishLinksOn = function(domId, outside, tag) {
    jQuery(domId).each(function() {
        hreflink = jQuery(this).attr('href');
        if (isFromPloneOrg(hreflink)) {
            if (outside) jQuery(this).after(tag);
            else jQuery(this).append(tag);
        }
    })
}

markPloneOrgLinks = function() {
    tagEnglishLinksOn('#accordion dt a', 1, '<sup> En</sup>');
    tagEnglishLinksOn('#content .plain a', 1, '<sup style="font-size: 7px"> En</sup>');
    tagEnglishLinksOn('#footer dl a', 0, '<sup style="font-size: 7px"> En</sup>');
    tagEnglishLinksOn('.section-home-page #current .spotlight a', 0, '<sup style="color:#555; font-size: 9px"> En</sup>');
}


jQuery(document).ready(markPloneOrgLinks);


// vertical animation of providers logos in footer on load

// Height in pixels
var LOGOSHEIGHT = 128 ;
// Time to go from top to bottom in 1/100e of seconds
var SPEED = 1900;

vanimate = function (wrapper, height, time, n ) {
    n--;
    if (n>0) {
        wrapper.animate({top: - height}, time);
        wrapper.animate({top: 0}, time, "swing", function(){vanimate(wrapper, height, time, n)});
    }
    else { 
        wrapper.stop();
        wrapper.hide();
        }
}

animateProviders = function() {
    container = jQuery('#footer #providers');
    logos = jQuery('img', container);
    var nb = 0;
    if (logos.length){
        nb = jQuery(logos).length;
        height = LOGOSHEIGHT*(nb-1);
        time = SPEED*(nb-1);
        if (nb>1) {
            logosWrapper = jQuery('#providersTable', container);
            vanimate(logosWrapper, height, time, 100);
            logosWrapper.mouseover(function(){
                logosWrapper.stop();
            });
            logosWrapper.mouseout(function(){
                logosWrapper.stop();
                vanimate(logosWrapper, height, time, 100);
            });            
        }
    }
    // when no logos destroy the clients container
    else container.remove();
}

jQuery(window).load(animateProviders);

