/*----------------------------------------------------------------------
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       = "mn"; 
  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();
      }
    }
  }
  
  this.alternately_set_classes_on_table_rows = function(table, odd_class, even_class){
    var t_body = table.tBodies[0];
    if(t_body){
	  for (var i=0; i< t_body.rows.length; i++) {
	    if(is_even(i)){
	   	  t_body.rows[i].className = even_class;
	    }else{
		  t_body.rows[i].className = odd_class;
		}
	  }
	}
  }
}

function is_even(number) {
  return !(number % 2);
}

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 add_event_to_element(element, event_type, func, use_capture) {
  if (element.addEventListener) {
	element.addEventListener(event_type, func, use_capture);
	return true;
  }else if (element.attachEvent) {
	return element.attachEvent('on' + event_type, func);
  }else {
	element['on' + event_type] = func;
  }
}

function change_class_of_element(element, class_name){
  element.className = class_name;	
}

function give_id_to_element(element, id){
  if(!element.id){
    element.id = id;
  }	
}

Array.prototype.swap=function(a, b){
  var tmp=this[a];
  this[a]=this[b];
  this[b]=tmp;
}

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

function Request(){
  
  this.activex_ids = [ 
    'MSXML2.XMLHTTP.3.0', 
    'MSXML2.XMLHTTP', 
    'Microsoft.XMLHTTP' 
  ];	
}

Request.prototype.initialize = function(){
  var http_request;
  if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+... 
    http_request = new XMLHttpRequest(); 
	if (http_request.overrideMimeType) { 
	  http_request.overrideMimeType('text/xml'); 
	} 
  } else if (window.ActiveXObject) { // IE6 and older 
    for (var i = 0; i < this.activex_ids.length; i++) { 
	  try { 
	    http_request = new ActiveXObject(this.activex_ids[i]); 
	  } catch (e) {} 
	} 
  }  
  if (!http_request) { 
    alert('Unfortunately your browser doesn’t support asynchronous communication.'); 
    return false; 
  }
  return http_request;	
}


Request.prototype._get = function(){
  var response;	
  var return_xml = arguments[0];
  var url = arguments[1];
  var callback_function = arguments[2];
  var callback_arguments = new Array();

  var it = 0;
  for(var j=3; j < arguments.length; j++){
    callback_arguments[it] = arguments[j];
	it ++;	
  }

  var http_request = this.initialize();
	
  http_request.onreadystatechange = function() { 
    if (http_request.readyState !== 4) { 
	  // not ready yet 
	  return; 
	} 
	if (http_request.status !== 200) { 
	  // ready, but not OK 
	  alert('There was a problem with the request.(Code: ' + http_request.status + ')'); 
	  return; 
	} 
	if (return_xml) { 
	  response = http_request.responseXML; 
	} else { 
	  response = http_request.responseText; 
	} 

	// invoke the callback
	  callback_function(response, callback_arguments);
  }; 

  http_request.open('GET', url, true);
  if(http_request.setRequestHeader && return_xml){
    http_request.setRequestHeader('Accept', 'text/xml');
  } 
  http_request.send(null);	
}

Request.prototype._post = function(){
  var response;	
  var return_xml = arguments[0];
  var url = arguments[1];
  var params = arguments[2];
  var callback_function = arguments[3];
  var callback_arguments = new Array();

  var it = 0;
  for(var j=4; j < arguments.length; j++){
    callback_arguments[it] = arguments[j];
	it ++;	
  }

  var http_request = this.initialize();
	
  http_request.onreadystatechange = function() { 
    if (http_request.readyState !== 4) { 
      // not ready yet 
	  return; 
	} 
    if (http_request.status !== 200 && http_request.status !== 201) { 
      // ready, but not OK 
	  alert('There was a problem with the request.(Code: ' + http_request.status + ')'); 
	  return; 
	} 
	if (return_xml) { 
      response = http_request.responseXML; 
	} else { 
	  response = http_request.responseText; 
	} 

	// invoke the callback
	callback_function(response, callback_arguments);
  }; 

  http_request.open('POST', url, true);
  if(http_request.setRequestHeader && return_xml){
    http_request.setRequestHeader('Accept', 'text/xml');
  } 
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", params.length);
  http_request.setRequestHeader("Connection", "close");

  http_request.send(params);	
}

Request.prototype._put = function(){
	
}

Request.prototype._delete = function(){
  var response;	
  var return_xml = arguments[0];
  var url = arguments[1];
  var callback_function = arguments[2];
  var callback_arguments = new Array();

  var it = 0;
  for(var j=3; j < arguments.length; j++){
    callback_arguments[it] = arguments[j];
	it ++;	
  }

  var http_request = this.initialize();

  http_request.onreadystatechange = function() { 
    if (http_request.readyState !== 4) { 
      // not ready yet 
	  return; 
	} 
	if (http_request.status !== 200) { 
      // ready, but not OK 
	  alert('Unable to delete the item selected.'); 
	  return; 
	} 

	callback_function(callback_arguments);
  }; 

  http_request.open('DELETE', url, true);
  if(http_request.setRequestHeader && return_xml){
    http_request.setRequestHeader('Accept', 'text/xml');
  } 
  http_request.send(null);	
}


function string_to_date_from_xml(date){
  var dt = new Date();

  date_time = date.split('T');
  date_portion = date_time[0].split('-');
  if(date_time[1]){
    time_portion = date_time[1].split(' ')[0].split(':');
  }

  dt.setFullYear(date_portion[0], date_portion[1]-1, date_portion[2]);
  if(date_time[1]){
    dt.setHours(time_portion[0]);
    dt.setMinutes(time_portion[1]);
    dt.setSeconds(0);
  }else{
	dt.setHours(0);
    dt.setMinutes(0);
    dt.setSeconds(0);
  }
  return dt;
}

function checkUncheckAll(theElement) {
  var theForm = theElement.form, e = 0;
  for(e=0; e<theForm.length;e++){
    if(theForm[e].type == 'checkbox' && theForm[e].name != 'checkall'){
	  theForm[e].checked = theElement.checked;
	}
  }
}

function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
	curtop = obj.offsetTop
	while (obj = obj.offsetParent) {
	  curleft += obj.offsetLeft
	  curtop += obj.offsetTop
	}
  }
  return [curleft,curtop];
}

function findPosX(obj){
  var curleft = 0;
  if(obj.offsetParent){
    while(1){
      curleft += obj.offsetLeft;
      if(!obj.offsetParent){
          break;
      }
      obj = obj.offsetParent;
    }
  }else if(obj.x){
    curleft += obj.x;
  }
  return curleft;
}

function findPosY(obj){
  var curtop = 0;
  if(obj.offsetParent){
    while(1){
      curtop += obj.offsetTop;
      if(!obj.offsetParent){
        break;
      }
      obj = obj.offsetParent;
    }
  }else if(obj.y){
    curtop += obj.y;
  }
  return curtop;
}

function hide_div(element_name) { 
  if (document.getElementById) { // DOM3 = IE5, NS6 
    document.getElementById(element_name).style.visibility = 'hidden'; 
  } else { 
    if (document.layers) { // Netscape 4 
      document.layers[element_name].style.visibility = 'hidden'; 
    } else { // IE 4 
      document.all[element_name].style.visibility = 'hidden'; 
    } 
  } 
}

function show_div(element_name) { 
  if (document.getElementById) { // DOM3 = IE5, NS6 
    document.getElementById(element_name).style.visibility = 'visible'; 
  } else { 
    if (document.layers) { // Netscape 4 
      document.layers[element_name].style.visibility = 'visible'; 
    } else { // IE 4 
      document.all[element_name].style.visibility = 'visible'; 
    } 
  } 
}
