/*************************************************/
/* Copyright (c)   2004        Markus Niedermann */
/* All rights reserved.           www.crearex.ch */
/*************************************************/


document.adapter_js = true;


// Objekte
function Coord(x, y){
  this.x = (!x)?0:x;
  this.y = (!y)?0:y;
  
  this.toString = objToString;
  this.equals = equalsCoord;
}

function Canvas(x, y, width, height){
  this.width = (!width)?0:width;
  this.height = (!height)?0:height;
  
  this.Coord = Coord;
  this.Coord(x, y);
  
  this.equalsCoord = this.equals;
  this.equals = equalsCanvas;
}

function objToString(){
  var ret = "{";
  for(p in this ){
    if (typeof this[p] == "function" || typeof this[p] == "object") continue;
    if(ret.length > 1)
      ret += ",";
    ret += p + ":" + this[p];
  }
  return ret + "}";
}

function equalsCoord(/*Coord*/ c){
  return (this.x == c.x && this.y == c.y);
}
function equalsCanvas(/*Canvas*/ c){
  return ( this.equalsCoord == c.equalsCoord && this.width == c.width && this.height == c.height);
}




// Funktionen

function getLayerRef(id, document) {
  if (!document)
    document = window.document;
  if (document.layers) {
    for (var l = 0; l < document.layers.length; l++)
      if (document.layers[l].id == id)
        return document.layers[l];
    for (var l = 0; l < document.layers.length; l++) {
      var result = getLayerRef(id, document.layers[l].document);
      if (result)
        return result;
    }
    return null;
  }
  else if (document.all) {
    return document.all[id];
  }
  else if (document.getElementById) {
    return document.getElementById(id);
  } else {
    return null;
  }
}

function setPosition(objLayer, coords){

    if (document.layers) {
      objLayer.top = coords.y;
      objLayer.left = coords.x;
    } else if (window.opera) {
      objLayer.style.top = coords.y;
      objLayer.style.left = coords.x;
    } else if (document.all) {
      objLayer.style.top = coords.y;
      objLayer.style.pixelLeft = coords.x;
    } else if (document.getElementById) {
      objLayer.style.top = coords.y + 'px'; 
      objLayer.style.left = coords.x + 'px';
    }
}

function setPositionById(id, coords){
  var objLayer = getLayerRef(id);
  
  if (document.layers) {
    objLayer.top = coords.y;
    objLayer.left = coords.x;
  } else if (window.opera) {
    objLayer.style.top = coords.y;
    objLayer.style.left = coords.x;
  } else if (document.all) {
    objLayer.style.top = coords.y;
    objLayer.style.pixelLeft = coords.x;
  } else if (document.getElementById) {
    objLayer.style.top = coords.y + 'px'; 
    objLayer.style.left = coords.x + 'px';
  }
}


function setVisibility(objLayer, visible) {

  if(document.layers){
    objLayer.visibility  = 
        (visible == true) ? 'show' : 'hide';
  } else {
    objLayer.style.visibility = 
        (visible == true) ? 'visible' : 'hidden';
  }

}

function setVisibilityById(id, visible) {
	var objLayer = getLayerRef(id);
	setVisibility(objLayer, visible);
}



function setVisibility4DivByPrefix(prefix, visible, d){
  if (!d)
    d = window.document;

  if(document.layers){
    for (var l = 0; l < d.layers.length; l++){
      if(d.layers[l].id.substr(0, prefix.length ) == prefix)
        setVisibility(d.layers[l], visible);
      setVisibility4DivByPrefix(prefix, 
                                visible, 
                                d.layers[l].document);
    }

  } else if(document.all) {

    var layers = document.all.tags("div"); 
    for(i=0; i < layers.length; i++) { 
      if(layers[i].id.substr(0, prefix.length ) == prefix )
        setVisibility(document.all.tags("div")[i], visible);
    }

  } else if(document.getElementsByTagName) {

    var layers = document.getElementsByTagName("div");
    for(i=0; i < layers.length; i++){
      if(layers[i].id.substr(0, prefix.length ) == prefix)
        setVisibility(layers[i], visible);
    }

  }
}

function getMouseXY(evt) {
  e = evt || window.event;
  if(!e) return null;
  
  if(document.layers) {
    return new Coord(e.pageX, e.pageY);
  }else if(window.opera){
    return new Coord(e.clientX, e.clientY);
  }else if(document.all ) {
    return new Coord(e.x + document.body.scrollLeft, e.y + document.body.scrollTop);
  }else if(document.getElementById) {
    return new Coord(e.pageX , e.pageY );
  }
}

function /*out: Coord*/ getLayerCoords( /*in: HTML-Object*/ objLayer ){
  var coords = new Coord();
  
  if (document.layers) {
    coords.y = objLayer.top;
    coords.x = objLayer.left;
  } else if ( window.opera ) {
    coords.y = objLayer.style.top;
    coords.x = objLayer.style.left;
  } else if (document.all) {
    o = objLayer;
    while(o.offsetParent) {
      coords.y += parseInt(o.offsetTop);
      coords.x += parseInt(o.offsetLeft);
      o = o.offsetParent;
    }
  } else if (document.getElementById) {
    coords.y = parseInt( document.defaultView.getComputedStyle(objLayer, '').getPropertyValue('top') ); 
    coords.x = parseInt( document.defaultView.getComputedStyle(objLayer, '').getPropertyValue("left") );
  }
 
  return coords;
}

function /*out: Coord*/ getLayerCoordsById( /*in: String*/ id ){
  var objLayer = getLayerRef(id);
  var coords = new Coord();
  
  if (document.layers) {
    coords.y = objLayer.top;
    coords.x = objLayer.left;
  } else if ( window.opera ) {
    coords.y = objLayer.style.top;
    coords.x = objLayer.style.left;
  } else if (document.all) {
    o = objLayer;
    while(o.offsetParent) {
      coords.y += parseInt(o.offsetTop);
      coords.x += parseInt(o.offsetLeft);
      o = o.offsetParent;
    }
  } else if (document.getElementById) {
    coords.y = parseInt( document.defaultView.getComputedStyle(objLayer, '').getPropertyValue('top') ); 
    coords.x = parseInt( document.defaultView.getComputedStyle(objLayer, '').getPropertyValue("left") );
  }
 
  return coords;
}


function getImageRef(id, document) {
	if (!document) document = window.document;

	if (document.layers) {
		for (var i = 0; i < document.images.length; i++) {
				if (document.images[i].name == id)
			return document.images[i];
		}
	
		for (var l = 0; l < document.layers.length; l++) {
			var result = getImageRef(id, document.layers[l].document);
			if (result) return result;
		}
		alert("Image not found: " + id);
		return null;
	} else if (document.images) {
		return document.images[id];
	} else if (document.getElementById) {
		return document.getElementById(id);
	} else {
		alert("Image not found: " + id);
		return null;
	}
}



function getImageCanvas(img) {

	if(document.layers) {
		return new Canvas(img.x, img.y, img.width, img.height);
	} else {
		
		var canvas = new Canvas(0, 0, img.width, img.height);
		while(img) {
			canvas.x += img.offsetLeft;
			canvas.y += img.offsetTop;
			img = img.offsetParent;
		}
		return canvas;
	}
}


function openURL(url, targetFrame) {
	if((targetFrame==null) || (targetFrame=='')) targetFrame = '_self';
	if((url!=null) && (url!='')) {
				
		if(window.parent.frames[targetFrame]!=null) {
			window.parent.frames[targetFrame].document.location = url;
		} else {
			window.location = url;
		}

	}	
}



function showAllProperties(obj) {
	var str = "";
	for(property in obj) {
		str += property + " = " + obj[property] + "<br>\n";
	}
	
	document.write(str);
}





// --------------------------------------------------------------------
// Web Browser Adapter
// --------------------------------------------------------------------

navigator.family = 'gecko';

// Detect Browser
if (document.layers) {
	navigator.family = 'nn4';
}

if (document.all) {
	navigator.family = 'ie4';
}

if (window.navigator.userAgent.toLowerCase().match('gecko')) {
	navigator.family = 'gecko';
}



BrowserAdapter.prototype.getImage = function(id, document) {
	if(!document) document = window.document;

	if(document.layers) {
		for (var i = 0; i < document.images.length; i++){
			if (document.images[i].name == id)
			return document.images[i];
		}
		for (var l = 0; l < document.layers.length; l++) {
			var result = this.getImage(id, document.layers[l].document);
			if (result)	return result;
		}
		return null;
	}
	else if(document.images) {	
		return document.images[id];
	}
	else if(document.getElementById) {
		return document.getElementById(id);
	} else {
		alert("WARNING: Could not load image " + id);
		return null;		
	}
}


BrowserAdapter.prototype.detectBrowser = function() {
	
	// convert all characters to lowercase to simplify testing
    agent=navigator.userAgent.toLowerCase();
    

    // *** BROWSER VERSION ***
    // Note: On IE5, these return 4, so use is_ie5up to detect IE5.
    major = parseInt(navigator.appVersion);
    minor = parseFloat(navigator.appVersion);

    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    this.is_nav  = ((agent.indexOf('mozilla')!=-1) && (agent.indexOf('spoofer')==-1)
                && (agent.indexOf('compatible') == -1) && (agent.indexOf('opera')==-1)
                && (agent.indexOf('webtv')==-1) && (agent.indexOf('hotjava')==-1));
    this.is_nav2 = (this.is_nav && (major == 2));
    this.is_nav3 = (this.is_nav && (major == 3));
    this.is_nav4 = (this.is_nav && (major == 4));
    this.is_nav4up = (this.is_nav && (major >= 4));
    this.is_navonly      = (this.is_nav && ((agent.indexOf(";nav") != -1) ||
                          (agent.indexOf("; nav") != -1)) );
    this.is_nav6 = (this.is_nav && (major == 5));
    this.is_nav6up = (this.is_nav && (major >= 5));
    this.is_gecko = (agent.indexOf('gecko') != -1);


    this.is_ie     = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));
    this.is_ie3    = (this.is_ie && (major < 4));
    this.is_ie4    = (this.is_ie && (major == 4) && (agent.indexOf("msie 4")!=-1) );
    this.is_ie4up  = (this.is_ie && (major >= 4));
    this.is_ie5    = (this.is_ie && (major == 4) && (agent.indexOf("msie 5.0")!=-1) );
    this.is_ie5_5  = (this.is_ie && (major == 4) && (agent.indexOf("msie 5.5") !=-1));
    this.is_ie5up  = (this.is_ie && !this.is_ie3 && !this.is_ie4);
    this.is_ie5_5up =(this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5);
    this.is_ie6    = (this.is_ie && (major == 4) && (agent.indexOf("msie 6.")!=-1) );
    this.is_ie6up  = (this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5 && !this.is_ie5_5);

    // KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
    // or if this is the first browser window opened.  Thus the
    // variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.
    this.is_aol   = (agent.indexOf("aol") != -1);
    this.is_aol3  = (this.is_aol && this.is_ie3);
    this.is_aol4  = (this.is_aol && this.is_ie4);
    this.is_aol5  = (agent.indexOf("aol 5") != -1);
    this.is_aol6  = (agent.indexOf("aol 6") != -1);

    this.is_opera = (agent.indexOf("opera") != -1);
    this.is_opera2 = (agent.indexOf("opera 2") != -1 || agent.indexOf("opera/2") != -1);
    this.is_opera3 = (agent.indexOf("opera 3") != -1 || agent.indexOf("opera/3") != -1);
    this.is_opera4 = (agent.indexOf("opera 4") != -1 || agent.indexOf("opera/4") != -1);
    this.is_opera5 = (agent.indexOf("opera 5") != -1 || agent.indexOf("opera/5") != -1);
    this.is_opera5up = (this.is_opera && !this.is_opera2 && !this.is_opera3 && !this.is_opera4);

    this.is_webtv = (agent.indexOf("webtv") != -1); 

    this.is_TVNavigator = ((agent.indexOf("navio") != -1) || (agent.indexOf("navio_aoltv") != -1)); 
    this.is_AOLTV = this.is_TVNavigator;

    this.is_hotjava = (agent.indexOf("hotjava") != -1);
    this.is_hotjava3 = (this.is_hotjava && (major == 3));
    this.is_hotjava3up = (this.is_hotjava && (major >= 3));

    // *** JAVASCRIPT VERSION CHECK ***
    this.is_js;
    if (this.is_nav2 || this.is_ie3) this.is_js = 1.0;
    else if (this.is_nav3) this.is_js = 1.1;
    else if (this.is_opera5up) this.is_js = 1.3;
    else if (this.is_opera) this.is_js = 1.1;
    else if ((this.is_nav4 && (minor <= 4.05)) || this.is_ie4) this.is_js = 1.2;
    else if ((this.is_nav4 && (minor > 4.05)) || this.is_ie5) this.is_js = 1.3;
    else if (this.is_hotjava3up) this.is_js = 1.4;
    else if (this.is_nav6 || this.is_gecko) this.is_js = 1.5;
    // NOTE: In the future, update this code when newer versions of JS
    // are released. For now, we try to provide some upward compatibility
    // so that future versions of Nav and IE will show they are at
    // *least* JS 1.x capable. Always check for JS version compatibility
    // with > or >=.
    else if (this.is_nav6up) this.is_js = 1.5;
    // NOTE: ie5up on mac is 1.4
    else if (this.is_ie5up) this.is_js = 1.3
    
    // HACK: no idea for other browsers; always check for JS version with > or >=
    else this.is_js = 0.0;
		
	
	
}

function BrowserAdapter() {
	this.detectBrowser();
	
}

var browser = new BrowserAdapter();




// --------------------------------------------------------------------
// Adapter for the Main Frame
// --------------------------------------------------------------------



var mainFrame = new MainFrame();

function MainFrame() {
	this.width = 0;
	this.height = 0;
	this.observer = new Array();
	this.observerCount = 0;
}

MainFrame.prototype.setSize = function(width, height) {
	this.width = width;
	this.height = height;
	this.notify();
}

MainFrame.prototype.notify = function() {
	for(i=0 ; i<this.observerCount ; i++) this.observer[i].frameNotify(this);		
}

MainFrame.prototype.addObserver = function(observer) {
	this.observer[this.observerCount] = observer;
	this.observerCount++;
}


function getWindowSize() {
	var width = 0;
	var heigth = 0;
	if (window.innerWidth) width = window.innerWidth;
		else if (document.body && document.body.offsetWidth) width = document.body.offsetWidth;
		else width = 0;				
	if (window.innerHeight) height = window.innerHeight;
 		else if (document.body && document.body.offsetHeight) height = document.body.offsetHeight;
 		else height = 0;
 		
 	mainFrame.setSize(width, height);	
}		

getWindowSize();
window.onresize = getWindowSize;
