var ua = navigator.userAgent;
var isIE6 = (ua.match(/MSIE 6\.0/)); 

IE_CorrectAlpha_PNG = function(){
    if (isIE6) {
        docimages = jQuery('body img');
        docimages.each(function(){        
          	imgExt  = this.src.substring(this.src.length-3, this.src.length);
          	imgExt  = imgExt.toUpperCase();
          	if (imgExt == "PNG"){
            		imgID    = (this.id) ? "id='" + this.id + "' " : "";
            		imgClass= (this.className) ? "class='" + this.className + "' " : "";
            		imgTitle= (this.title) ? "title='" + this.title + "' " : "title='" + this.alt + "' ";
            		imgStyle= "display:inline-block;" + this.style.cssText;
            		if (this.align == "left") { imgStyle = "float:left;"  + imgStyle; } else if (this.align == "right"){ imgStyle = "float:right;" + imgStyle; }
            		if (this.parentElement.href)   { imgStyle = "cursor:hand;" + imgStyle; }       
            		strNewHTML    = '<span '+imgID+imgClass+imgTitle+' style="width:'+this.width+'px; height:'+this.height+'px;'+imgStyle+';'+'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+this.src+'\', sizingMethod=\'scale\');"></span>';
            		this.outerHTML = strNewHTML;
          	}        
        })        
    }
}


jQuery(document).ready(IE_CorrectAlpha_PNG);



fixCorners = function() {
/* when no column two background image must be white on right corners */
    var col2 = jQuery('#portal-column-two'); 
    var col1 = jQuery('#portal-column-one'); 
    if (! col2.length) {
        jQuery('#corner-bottom-right').css('background', "transparent url('./corner-right-bottom-white.gif') top right no-repeat");
        jQuery('#corner-bottom-right').css('border-color', "white");
        jQuery('#corner-top-right').css('background', "transparent url('./corner-right-top-white.gif') top right no-repeat");
        jQuery('#corner-top-right').css('border-color', "white");
    } 
/* when no columns 1 or 2,  we must fix a div color which don't have a class */    
    if (!col1.length && ! col2.length) {
        jQuery('.managePortletsFallback').each(function(){
            jQuery(this).parent().css('background-color', 'white');
        });
    }
}

jQuery(document).ready(fixCorners);

/* fix an error in some plone templates conception (no content area) */
fixContentPadding = function() {
    var content = jQuery('#content');
    if (! content.length) jQuery('.documentContent').css('padding', '1em');
}

jQuery(document).ready(fixContentPadding);

fixScreenPadding = function() {
    /* for small screen we must fix padding */
    if(jQuery('body').width()<1041) {
        jQuery('body').css('padding', '0 0 34px 0');
    }
    else jQuery('body').removeAttr('style');
}

jQuery(document).ready(fixScreenPadding);
jQuery(window).bind('resize', fixScreenPadding);


fixNavtreeStyle = function() {
    var navitems = jQuery('.portletItem .navTreeItem a');
    if (navitems.length) jQuery(navitems[navitems.length-1]).addClass('lastNavtreeItem');
}

jQuery(document).ready(fixNavtreeStyle);


fixPortletsStyleFor = function(portlets, dontforcefirstfooter) {
    dontforcefirstfooter = (dontforcefirstfooter) ? dontforcefirstfooter : false;
    if (portlets.length) { 
        var firstPortlet = jQuery(portlets[0]);
        firstPortlet.addClass('firstPortlet');
        var firstFooter = jQuery('.portletFooter', firstPortlet);
        if (!(firstFooter.length || dontforcefirstfooter)) {
           firstPortlet.append('<dd class="portletFooter emptyFooter">&nbsp;</dd>');
        }        
        var lastPortlet = jQuery(portlets[portlets.length-1]);
        lastPortlet.addClass('lastPortlet');
        var lastFooter = jQuery('.portletFooter', lastPortlet);
        if (!lastFooter.length) {
           lastPortlet.append('<dd class="portletFooter emptyFooter">&nbsp;</dd>');
        }
    }      
}

fixPortletsStyle = function() {
    fixPortletsStyleFor(jQuery('#portal-column-one .portlet'), dontforcefirstfooter=true);
    fixPortletsStyleFor(jQuery('#portal-column-two .portlet'));
}

jQuery(document).ready(fixPortletsStyle);


fixSafariChrome = function() {
    if(window.devicePixelRatio) {
        jQuery('#portal-columns').addClass('relativeCols');
        jQuery('#portal-footer').addClass('relativeFooter');
        jQuery('#portal-colophon').addClass('relativeColophon');
        jQuery('#corners-bottom-wrapper').addClass('relativeCornersBottom');
    }
}

jQuery(document).ready(fixSafariChrome);

fixGlobalNav = function() {
     
     if (jQuery('#portal-column-one').length) coloneWidth= jQuery('#portal-column-one').width();
     else coloneWidth = 0;
     jQuery('#portal-globalnav').width(coloneWidth+jQuery('#portal-column-content').width());

    jQuery('#portal-globalnav .portal-tab-link').each(function(){
        theight = jQuery(this).height();
        if (theight>20) {
            jQuery(this).css('top', '20px');
        }
    })
}

jQuery(window).load(fixGlobalNav);
jQuery(window).resize(fixGlobalNav);



