/**
 * @author dax
 */

/*Begin Image Manipulate*/

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
	for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;
	if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document;
  if(d.images){
	  if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
	  for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}
	}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

/*End Image Manipulate*/

/*Begin Browser Detect*/

// Browser Detect  v2.1.6
// documentation: http://www.dithered.com/javascript/browser_detect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)

function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

   // browser name
   this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1); 
   this.isIcab        = (ua.indexOf('icab') != -1); 
   this.isAol         = (ua.indexOf('aol') != -1); 
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirefox     = (ua.indexOf('firefox/') != -1 || ua.indexOf('firebird/') != -1);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
   
   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
   
   // rendering engine versions
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
   this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
   this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
   
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   
   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOmniweb) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   else if (this.isIcab) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
   }
   
   this.versionMajor = parseInt(this.versionMinor); 
   
   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);
   
   // css compatibility mode
   this.mode = document.compatMode ? document.compatMode : 'BackCompat';

   // platform
   this.isWin    = (ua.indexOf('win') != -1);
   this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
   this.isMac    = (ua.indexOf('mac') != -1);
   this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
   this.isLinux  = (ua.indexOf('linux') != -1);
   
   // specific browser shortcuts
   this.isNS4x = (this.isNS && this.versionMajor == 4);
   this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
   this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
   this.isNS4up = (this.isNS && this.versionMinor >= 4);
   this.isNS6x = (this.isNS && this.versionMajor == 6);
   this.isNS6up = (this.isNS && this.versionMajor >= 6);
   this.isNS7x = (this.isNS && this.versionMajor == 7);
   this.isNS7up = (this.isNS && this.versionMajor >= 7);
   
   this.isIE4x = (this.isIE && this.versionMajor == 4);
   this.isIE4up = (this.isIE && this.versionMajor >= 4);
   this.isIE5x = (this.isIE && this.versionMajor == 5);
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
   this.isIE6x = (this.isIE && this.versionMajor == 6);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);
   
   this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser = new BrowserDetect();

/*End Browser Detect*/

/*Begin Popup Functions*/
/*  
    --------------------------------------------------------------------------
  Code for link-hover text boxes
  Original: Mike McGrath (Web Site: http://website.lineone.net/~mike_mcgrath) 
  modified by Nicolas Hoening (Web Site: http://nicolashoening.de)
    Works only if the BrowserDetect script has run before (http://dithered.chadlindstrom.ca/javascript/browser_detect)
    --------------------------------------------------------------------------
*/

var iex = browser.isIE || browser.isOpera; // opera has a similar engine to IE
var nav = (document.layers);
var old = browser.isNS && (!document.layers && !document.getElementById);
var n_6 = browser.isNS6up;
var op = browser.isOpera; // this is only opera
if (browser.isSafari || browser.isFirefox || browser.isMozilla || browser.isKonqueror || browser.isGecko) n_6 = true; //they work the same for this

// create the popup box - inline so everyone, including Opera, will tell the width
document.write('<div id="pup" style="visibility:hidden;display:inline;"></div>');

var skin = null   // this is the style of our popup we'll modify

var minMarginToBorder = 15; // set how much minimal space there should be to
              // the next border (horizontally)
var popwidth = 0;   // this is how wide your popup is, we'll read it
              // from the stylesheet later, so keep this as-is

// initialize the capture pointer
if(nav) document.captureEvents(Event.MOUSEMOVE);
if(n_6) document.addEventListener("mousemove", get_mouse, true);
if(nav || iex) document.onmousemove = get_mouse;


// assign style object when not already known
function assignSkin() {
  if(nav) skin = document.pup;
  if(iex) skin = document.getElementById('pup').style;
  if(n_6) skin = document.getElementById("pup").style;
}
  
//getting the popwidth - we'll get this only once, too! 
//Then it will always have the stylesheet value
function assignPopWidth(){
    if (iex && !op) popwidth = parseInt(document.getElementById("pup").currentStyle.width);
  if (op)  popwidth = parseInt(document.defaultView.getComputedStyle(document.getElementById('pup'),null).width);
  if (n_6)  popwidth = parseInt(document.defaultView.getComputedStyle(document.getElementById("pup"),null).getPropertyValue('width'));
  //skin.display = "none";  //turn "inline" off now, it widens the page horizontally when the parked popup is positioned
}

// set dynamic coords when the mouse moves
function get_mouse(e)
{
    
  var x,y;
  var scroll_x_y = getScrollXY();
  
  
  //get X
  if (iex) x = scroll_x_y[0] + event.clientX;
  if (nav || n_6) x = e.pageX;

  //get Y
  if (iex) y = scroll_x_y[1] + event.clientY;
  if (nav || n_6) y = e.pageY;
  
  if (popwidth == 0) assignPopWidth();
  if (null === skin) assignSkin();
  
  
  x += 10; // important: if the popup is where the mouse is, the hoverOver/hoverOut events flicker
  
  
  var x_y = nudge(x,y); // avoids edge overflow
  if (isNaN(x_y[0])) x_y[0] = 0;
  if (isNaN(x_y[1])) x_y[1] = 0;
  
  //now set coordinates for our popup - n_6 wants "px", the others not
  //remember: the popup is still hidden
  if(nav || iex) {
    skin.left = x_y[0];
    skin.top = x_y[1];
  }else if(n_6){
    skin.left = x_y[0] + "px";
    skin.top = x_y[1] + "px";
  }
}

// avoid edge overflow
function nudge(x,y)
{
  var dims = getInnerWindowDimensions();
  scroll_x_y = getScrollXY();
  
  // right
  var xtreme = dims[0] - popwidth - minMarginToBorder;
  if (n_6 || nav) xtreme -= 25;
  if(x > xtreme) {
  x -= (parseInt(popwidth) + minMarginToBorder + 20 );
  }

  // left - should almost never be a problem - we're drawing the window 
  // to the right of the mouse per default (maybe corrected by the code above
  // but then this has the last horizontal word)
  if(x < 1) x -=  x - 1;

  // down - when the mouse is too close to the bottom, move it up.
  // I estimate the lines that fit in the width, assuming (a little pessimisticly) 
  // a char width of 15 pixels and a line height of 20 (That should work for most cases)
  // Unfortunately, I cannot read margin and padding to get even better values, 
  // since JS can only read what is set before itself, apparently. This works quite well 
  // with a padding of 5px.
  est_lines = parseInt(document.getElementById("pup").innerHTML.length / (parseInt(skin.width)/15) );
  est_lines_to_decide = max(est_lines,2);
  if((y + parseInt(est_lines_to_decide * 20)) > (dims[1] + scroll_x_y[1])) {
    y -= parseInt(est_lines * 20) + 20;
  }
  
  return [ x, y ];
}

// write content and display
function popup(msg)
{
  if (popwidth === 0) assignPopWidth();
  if (null === skin) assignSkin();
  
  if (null !== skin) { // maybe the browser isn't ready
    if(old) { //display plain message box for old browsers
    alert(msg);
    return;
    }
    
    if (!isNaN(popwidth)) { // fallback behaviour (for sthg that has been observed in IE7)
    if(iex || nav)  skin.width = popwidth;
    if(n_6)  skin.width = popwidth + "px";
    } else {
    if(iex || nav)  skin.width = 300;
    if(n_6)  skin.width = 300 + "px";
    }
          
    //write the message in
    if(nav) { 
    skin.document.open();
    skin.document.write(msg);
    skin.document.close();
    }
    document.getElementById("pup").innerHTML = msg;
    
    //make the popup visible
    skin.visibility ="visible";
    skin.display = "inline";
  }
}

// make content box invisible
function kill()
{
  if(!old) {
    skin.visibility = "hidden"; //invisible
  skin.display = "none";  //invisible
  }
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getInnerWindowDimensions(){
  var A;
  if (window.innerHeight !== undefined) A = [window.innerWidth,window.innerHeight]; // most browsers
  else{ // IE varieties
    var D = (document.documentElement.clientWidth == 0)? document.body: document.documentElement;
    A = [D.clientWidth,D.clientHeight];
  }
  return A;
}

function max(a,b){
    if (a>b) return a;
    else return b;
}
/*End Popup Functions*/
