/*

 WordPress JavaScript functions

2010-02-28T16:26:08-08:00 - Start creating files 

*/

// create the main namespace for everything
var com = com || {}; // "var" does nothing if com already exists
com.YourFriendPaul = com.YourFriendPaul || {}; // make the object if needed
if( com.YourFriendPaul.toString() != 'com.YourFriendPaul' ){ 
   com.YourFriendPaul.toString = function(){ return 'com.YourFriendPaul'; };
}

(function( base ){
   
   base.wp = base.wp || {};
   var $ = base.wp;
   if( $.toString() != base.toString() + '.wp' ){ 
      $.toString = function(){ return base.toString() + '.wp'; }; 
   }
   
   // switch for running on test page
   $.onWP = true;

   $.elemIDs = {
      mainWrapperId: $.onWP ? 'mainContainer' : 'allContentWrapper',
      sideWrapperId: $.onWP ? 'sidebar' : 'rhContainer',
      contentWrapperId: $.onWP ? 'content' : 'mainContainer',
      appendTextOutId: $.onWP ? 'headerDebugInfo' : 'msgWinWidth'
   };

   function getStyle( elem, styleProp ){
      
      var y = "";
      
   	if ( window.getComputedStyle ){
   		// DOM version - the cascaded style, usually in px but could be any fixed unit (in or mm)
         // Uses actual style name with hypen if it has one
         y = document.defaultView.getComputedStyle( elem, "" ).getPropertyValue( styleProp );
   	}
      
      else if ( elem.currentStyle ){
   		// IE only, requires camelCase instead of hyphen
         y = elem.currentStyle[ styleProp ];
   	}
      
      return y;
   }
/*
function getElementStyle(elem, IEStyleProp, CSSStyleProp) {
    //var elem = document.getElementById(elemID);
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}
*/
   $.pageWidthChangeMods = function(){
   
      var allWidth = 0;
      var sideWidth = 0;
      var contentWidth = 0;
      
      // page is the wp equivalent of my allContentWrapper
      var allContent = document.getElementById( $.elemIDs.mainWrapperId );
      var sideContent = document.getElementById( $.elemIDs.sideWrapperId );
      var contentContent = document.getElementById( $.elemIDs.contentWrapperId );
      
      // all 3 pieces need to be available to continue
      if( !allContent || !sideContent || !contentContent ){ 
         
         base.util.doc.appendText( $.appendTextOutId, 'warning: resize component not available in ' + 
               $.toString() + ".pageWidthChangeMods()", 'p' );
         return; 
      }
      
      allWidth = allContent.offsetWidth;
      sideWidth = sideContent.offsetWidth;
      contentWidth = contentContent.offsetWidth;

      /*
      base.util.doc.appendText( $.elemIDs.appendTextOutId, 
            'before size change: allWidth: ' + allWidth + ', sideWidth: ' + sideWidth +
            ', contentWidth(offsetWidth): ' + contentWidth, 'p' );
      */


      var padEtc = 0;
      // to do: deal with 'auto' in return values
      var padLeft = parseInt( getStyle( contentContent, 'padding-left') || getStyle( contentContent, 'paddingLeft') , 10);
      var padRight = parseInt( getStyle( contentContent, 'padding-right') || getStyle( contentContent, 'paddingRight'), 10);
      //var marginLeft = parseInt( getStyle( contentContent, 'margin-left') || getStyle( contentContent, 'marginLeft'), 10);
      //var marginRight = parseInt( getStyle( contentContent, 'margin-right') || getStyle( contentContent, 'marginRight'), 10);
      /*
      base.util.doc.appendText( $.elemIDs.appendTextOutId, 
               'pl: ' + padLeft + ', pr: ' + padRight +
               ', ml: ' + marginLeft + ', mr: ' + marginRight, 'p' );
      */

      padEtc = padLeft + padRight;
      contentContent.style.width = allWidth - sideWidth - padEtc + 'px';
      allWidth = allContent.offsetWidth;
      sideWidth = sideContent.offsetWidth;
      contentWidth = contentContent.offsetWidth;
      
      /*
      base.util.doc.appendText( $.elemIDs.appendTextOutId, 
               'allWidth: ' + allWidth + ', sideWidth: ' + sideWidth +
               ', contentWidth(offsetWidth): ' + contentWidth, 'p' );
      */
   };

   
   function trim12 (str) {
   	var	str = str.replace(/^\s\s*/, ''),
   		ws = /\s/,
   		i = str.length;
   	while (ws.test(str.charAt(--i)));
   	return str.slice(0, i + 1);
   }

   /* 
   return an empty string if no changes were made 
   return the new url if it needs to be modified
   */
   function fixBloggerFragments( url ){
   
       url = "" + url;
       var url_split = url.split( '#' );
       newURL = '';
       
       /* no fragment to bother with, quit */
       if( url_split.length < 2 ) return '';
   
       url_split[ 1 ] = trim12( url_split[ 1 ] ); 
      
       var pattern = /^(\d*)$/i;
      
       /* look for digits in fragment */
       var matches = pattern.exec( url_split[ 1 ] );
       if( null === matches ){ return ''; }
   
       /* modify it the fragment is all digits */
       if( url_split[ 1 ] == matches[ 0 ] ){
      
          newURL = url_split[ 0 ] + '#Blogger_' + url_split[ 1 ];
          return newURL;
       }
   }

   /* the only way to get the url fragment is through JavaScript,
   so fix the URL if it is an old Blogger style digits only fragment
   */
   $.fixBloggerURL = function (){
   
         var curURL = window.location;
         var newURL = fixBloggerFragments( curURL );
         if( newURL != "" ){ window.location.replace( newURL ); }
   
   }
   

})( com.YourFriendPaul );


