///////////////////////////////////////////////////////////////////////////////
//
//  tristudioz.js
//
var border_thickness = 10;
var scrap_edge_thickness = 9;
var hw_ratio = 1.54;
var min_outer_margin_percent = .05;

var BorderTop;
var ContentWidth;
var ContentHeight;
var BorderHeight;
var InsideHeight;

function ReStitch()
{
	processNotebookStitch("logo", "sd_footer", "main_body");
	var rule;
	
	rule = getCSSRule('.inside',        'save');
	rule.style.height = InsideHeight  + 'px';

	rule = getCSSRule('#content_border', 'save');
	rule.style.height = BorderHeight + 'px';
	rule.style.width  = ContentWidth  + 'px';

	rule = getCSSRule('#content',        'save');
	rule.style.height = ContentHeight  + 'px';
	rule.style.width  = ContentWidth  + 'px';

}

function processNotebookStitch(div_above, div_below, div_parent)
{
	var originalVerticalGap;
	var realWidth;
	var realHeight;
	var ratio;
	var element;
	
	//	Get the width we have to work with...
	element = document.getElementById(div_parent);
	realWidth = element.clientWidth - (2 * border_thickness);
	
	//	Get the height we have to work with...
	element = document.getElementById(div_parent);
	originalVerticalGap = element.clientHeight;
	element = document.getElementById(div_below);
	originalVerticalGap -= element.clientHeight;
	element = document.getElementById(div_above);
	originalVerticalGap -= element.clientHeight;
	
	//	Reduce it by the required margin...
	realHeight = originalVerticalGap - (2 * border_thickness) - scrap_edge_thickness;
	realWidth *= (1.0 - (2 * min_outer_margin_percent));
//	realHeight *= (1.0 - (2 * min_outer_margin_percent));
	
	ratio = realWidth / realHeight;
	if (ratio > hw_ratio)
	{
		//  Height is ok, width is too big so fix it...
		realWidth = realHeight * hw_ratio;
	}
	else
	{
		//  Width is ok, height is too big so fix it...
		realHeight = realWidth / hw_ratio;
	}

	element = document.getElementById(div_above);
	BorderTop = element.clientHeight + ((originalVerticalGap - realHeight) / 2);
	ContentWidth = realWidth;
	ContentHeight = realHeight;	
	BorderHeight = ContentHeight + scrap_edge_thickness;
	InsideHeight = ContentHeight - scrap_edge_thickness;
}

function getContentHeight()
{
	document.write(ContentHeight + 'px');
}

function getBorderTop()
{
	document.write(BorderTop + 'px');
}

function getBorderHeight()
{
	document.write(BorderHeight + 'px');
}

function getWidth()
{
	document.write(ContentWidth + 'px');
}

function getInsideHeight()
{
	document.write(InsideHeight + 'px');
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

function getCSSRule(ruleName, deleteFlag) {               // Return requested style obejct
   ruleName=ruleName.toLowerCase();                       // Convert test string to lower case.
   if (document.styleSheets) {                            // If browser can play with stylesheets
      for (var i=0; i<document.styleSheets.length; i++) { // For each stylesheet
         var styleSheet=document.styleSheets[i];          // Get the current Stylesheet
         var ii=0;                                        // Initialize subCounter.
         var cssRule=false;                               // Initialize cssRule. 
         do {                                             // For each rule in stylesheet
            if (styleSheet.cssRules) {                    // Browser uses cssRules?
               cssRule = styleSheet.cssRules[ii];         // Yes --Mozilla Style
            } else {                                      // Browser usses rules?
               cssRule = styleSheet.rules[ii];            // Yes IE style. 
            }                                             // End IE check.
            if (cssRule)  {                               // If we found a rule...
               if (cssRule.selectorText.toLowerCase()==ruleName) { //  match ruleName?
                  if (deleteFlag=='delete') {             // Yes.  Are we deleteing?
                     if (styleSheet.cssRules) {           // Yes, deleting...
                        styleSheet.deleteRule(ii);        // Delete rule, Moz Style
                     } else {                             // Still deleting.
                        styleSheet.removeRule(ii);        // Delete rule IE style.
                     }                                    // End IE check.
                     return true;                         // return true, class deleted.
                  } else {                                // found and not deleting.
                     return cssRule;                      // return the style object.
                  }                                       // End delete Check
               }                                          // End found rule name
            }                                             // end found cssRule
            ii++;                                         // Increment sub-counter
         } while (cssRule)                                // end While loop
      }                                                   // end For loop
   }                                                      // end styleSheet ability check
   return false;                                          // we found NOTHING!
}                                                         // end getCSSRule 

function killCSSRule(ruleName) {                          // Delete a CSS rule   
   return getCSSRule(ruleName,'delete');                  // just call getCSSRule w/delete flag.
}                                                         // end killCSSRule

function addCSSRule(ruleName) {                           // Create a new css rule
   if (document.styleSheets) {                            // Can browser do styleSheets?
      if (!getCSSRule(ruleName)) {                        // if rule doesn't exist...
         if (document.styleSheets[0].addRule) {           // Browser is IE?
            document.styleSheets[0].addRule(ruleName, null,0);      // Yes, add IE style
         } else {                                         // Browser is IE?
            document.styleSheets[0].insertRule(ruleName+' { }', 0); // Yes, add Moz style.
         }                                                // End browser check
      }                                                   // End already exist check.
   }                                                      // End browser ability check.
   return getCSSRule(ruleName);                           // return rule we just created.
} 