/*----------------------------------------------------------------------
Incremental.js -- Copyright KEHK Inc. 2008

The incremental library holds high level information about the browser 
environment, loads the skin for the site, and has utility classes used 
by the skin.

Created by Robert Beaupre Sept 4, 2008
Updated by Robert Beaupre Sept 28, 2008
----------------------------------------------------------------------*/

var incremental = new Incremental();
incremental.enhance();
incremental.addLoadEvent(incremental.enhance_when_loaded);

function Incremental(){
  this.skin_name       = "rye"; 
  this.skin_version    = "01";
  this.base_path       = "/incremental/";
  this.skin_modifier   = "_skins";

  this.browsers        = ["konqueror", "safari", "omniweb", "opera", "webtv", "icab", "msie", "firefox"];
  this.oss             = ["linux", "x11", "mac", "win"];
  this.browser         = "";
  this.browser_version = "";
  this.os              = "";

  this.protocol        = "";
  this.host            = "";
  this.path            = "";
  this.file_requested  = "";
 
  this.enhance = function(){
	if(document.write){ //make sure we have some basic functionality
	  this.add_stylesheet(this.base_path + "incremental.css", "screen, projection");
	  this.add_stylesheet(this.get_path() + this.skin_name + "_" + this.skin_version + ".css", "screen, projection");
	  this.add_stylesheet(this.get_path() + this.skin_name + "_print_" + this.skin_version + ".css", 'print');
	  this.add_javascript(this.get_path() + this.skin_name + "_" + this.skin_version + ".js");

	  this.detect_browser();
	  
	  this.extract_url_data();
	}
  }

  this.enhance_when_loaded = function(){
    
  }

  this.add_stylesheet = function(path, media){
    var stylesheet = document.createElement("link");
    stylesheet.setAttribute("rel", "stylesheet");
    stylesheet.setAttribute("type", "text/css");
    stylesheet.setAttribute("media", media);
    stylesheet.setAttribute("href", path);
	
    if (typeof stylesheet!="undefined"){
	  document.getElementsByTagName("head")[0].appendChild(stylesheet);
    }
  }
  
  this.add_javascript = function(path){
    var js = document.createElement("script");
	js.setAttribute("type", "text/javascript");
	js.setAttribute("src", path);
	
    if (typeof js!="undefined"){
	  document.getElementsByTagName("head")[0].appendChild(js);
    }
  }

  this.get_path = function(){
	return this.base_path + this.skin_name + this.skin_modifier + "/";
  }

  this.detect_browser = function(){
	var ua = window.navigator.userAgent.toLowerCase();
    
	for(i=0; i<this.browsers.length;i++){
		if(this.browser == ""){
			if(ua.indexOf(this.browsers[i]) + 1){
				this.browser = this.browsers[i];
			}
		} 
	}
	
	if(this.browser == ""){
		this.browser = "unknown";
	}
	
	if(this.browser == "konqueror") {
		this.os = "linux";
	}
	
	for(i=0; i<this.oss.length;i++){
		if(this.os == ""){
			if(ua.indexOf(this.oss[i]) + 1){
				this.os = this.oss[i];
			}
		} 
	}
	
	if(this.os == ""){
	  this.os = "unknown";
	}
	
	if(this.browser_version == ""){
	  if(ua.indexOf("version") + 1){
		this.browser_version = ua.charAt(ua.indexOf("version") + 1 + "version".length);
	  }else{
		this.browser_version = ua.charAt(ua.indexOf(this.browser) + 1 + this.browser.length);
	  }
	}
  }

  this.extract_url_data = function(){
	var slash = '/' 
	if (location.pathname.match(/\\/)) { 
	  slash = '\\'
    }
	this.protocol        = location.protocol;
	this.host            = location.host;
	this.path            = location.pathname;
	this.file_requested  = location.pathname.substring(location.pathname.lastIndexOf(slash) + 1, location.pathname.length);
  }

  this.highlight_links = function(highlight_class, parent_highlight_class){
    links = document.getElementsByTagName("a")
    
    for(i=0; i<links.length;i++){
	  if(links[i].pathname == this.path || "/" + links[i].pathname == this.path || (links[i].pathname == "index.html" && this.path == "/")){
	    links[i].className = highlight_class;
	    
	  }
	}
		
  }

 this.addLoadEvent = function(func)  {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    }else {
      window.onload = function() {
        if (oldonload) {
	      oldonload();
        }
        func();
      }
    }
  }
}


function EdgeRounder(){

  this.edge_lengths = [5, 3, 2, 1];

  this.round = function(element, top, bottom, background_color, foreground_color){
	if(this.can_round()){
	  if(top){
	    this.round_top(element, background_color, foreground_color);	
	  }
	  if(bottom){
	    this.round_bottom(element, background_color, foreground_color);	
	  }	
	}
  }

  this.round_top = function(el, background_color, foreground_color){
	var top_container = document.createElement("span");
	top_container.style.display = "block"; 
	top_container.style.backgroundColor = background_color;

	for(var i=0;i<4;i++){
	  var x = document.createElement("span");
	  x.style.backgroundColor = foreground_color;
	  x.style.display = "block"; 
	  x.style.height = "1px"; 
	  x.style.overflow = "hidden";
	  x.style.margin = "0 " + this.edge_lengths[i] + "px";
	
	  if(i == 3){
	    x.style.height = "2px";  
	  }
	
	  top_container.appendChild(x);
    }

	el.insertBefore(top_container,el.firstChild);
  }

  this.round_bottom = function(el, background_color, foreground_color){
    var bottom_container = document.createElement("span");
    bottom_container.style.display = "block"; 
	bottom_container.style.backgroundColor = background_color;
	
	for(i=3;i>=0;i--){
	  var x = document.createElement("span");
	  x.style.backgroundColor = foreground_color;
	  x.style.display = "block"; 
	  x.style.height = "1px"; 
	  x.style.overflow = "hidden";
	  x.style.margin = "0 " + this.edge_lengths[i] + "px";
	  if(i == 3){
	    x.style.height = "2px"; 
	  }
	  bottom_container.appendChild(x);
    }

	el.appendChild(bottom_container,el.firstChild);
  }

  this.can_round = function(){
	var c_r = false;
	
	if(document.getElementById && document.createElement){
	  c_r = true;
	}
  
    return c_r;
  }
}

function IETransparentPNGFixer() {
  this.applyPositioning = true;

  this.fix = function(blank_transparent_gif_location){

    for (var i = document.all.length - 1, obj = null; (obj = document.all[i]); i--) {
	  // background pngs
	  if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) {
	    this.background_image_fix(obj, blank_transparent_gif_location);
	  }
	  // image elements
	  if (obj.tagName=='IMG' && obj.src.match(/\.png$/i) !== null){
	    this.foreground_image_fix(obj, blank_transparent_gif_location);
	  }
	  // apply position to 'active' elements
	  if (this.applyPositioning && (obj.tagName=='A' || obj.tagName=='INPUT') && obj.style.position === ''){
		obj.style.position = 'relative';
	  }
    }
  }
	
  this.background_image_fix = function(obj, blank_transparent_gif_location) {
    var mode = 'scale';
	var bg	= obj.currentStyle.backgroundImage;
	var src = bg.substring(5,bg.length-2);
	if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
	  mode = 'crop';
	}
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
	obj.style.backgroundImage = 'url('+blank_transparent_gif_location+')';
  }

  this.foreground_image_fix = function(img, blank_transparent_gif_location) {
    var src = img.src;
	img.style.width = img.width + "px";
	img.style.height = img.height + "px";
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
	img.src = blank_transparent_gif_location;
  }	
}

function ensure_the_hight_is_right(factor){
  if(!factor){
    factor = 0;	
  }
  var content_container = document.getElementById("content_container");
  var content_area = document.getElementById("content_area");
  var height = 0;
  if(window.innerHeight){
	height = window.innerHeight;
  }else if(document.documentElement.clientHeight){
	height = document.documentElement.clientHeight;
  }

  if(content_container.offsetHeight < height){
	content_area.style.height = content_area.offsetHeight - 25 + factor + (height - content_container.offsetHeight) + "px";
  }
}
