/* 
GlassBox is licensed under the terms of the MIT License.

Copyright (c) 2006-2008 Uli Preuss (http://www.glassbox-js.com)

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

/* Configuration */
var path_to_glassbox = "/javascripts/";
var path_to_background_images = "/images/backgrounds/";
/* End Configuration */

// You must efine path to root directory in any html-pages outside the root directory
// Relative path (for example "../")
var path_to_root_dir = ""; // Default

if ( typeof(GlassBox) == 'undefined' ) {
  var GlassBox = function() {
  
	THIS = this,
    this.version = "1.1.5",
    this.last_mod = "2008/02/15",
	
    this.bb_hor = "25px",
    this.bb_ver = "21px",
    this.gb_path = path_to_root_dir + path_to_glassbox,
    this.img_bg_path = path_to_root_dir + path_to_background_images,
    this.skin_path =  path_to_root_dir + path_to_glassbox + "skins/",
    this.skin_default = "default",
    this.multicontent_arr = new Array(),
    this.exists = false,
    this.is_false = true,
	    
    this.init = function( id, width, height, overflow, skinname, multicontent_num ) {
      
      if( this.exists == false ) {
        this.id = id;
        this.width = width;
        this.height = height;
        this.overflow = overflow;
      
        if( skinname != null && skinname != "" ) this.skinfolder = this.skin_path + skinname;
        else this.skinfolder = this.skin_path + this.skin_default;
      
        this.glassbox = document.getElementById( this.id );
		
        if( multicontent_num != null ) {
          if( this.is_false == true ) {
            var pageArray = new Array();
			var className = "glassbox_mc";
            var divs = document.getElementsByTagName("div");
            var pattern = new RegExp( "\\b" + className + "\\b" );
            for ( i = 0, j = 0; i < divs.length; i++ ) {
              if ( pattern.test( divs[i].className ) ) {
                pageArray[j] = divs[i];
                j++;
              }
			}
            this.header =  "<h1>" + pageArray[0].getElementsByTagName( "h1" )[0].innerHTML + "</h1>";
            for( i=0;i< pageArray.length;i++ ) {
              this.multicontent_arr[i] = pageArray[i].innerHTML;
            }
            this.glassboxContent = this.multicontent_arr[0];
            this.is_false = false;
          }
          else {
            if( multicontent_num == 1 ) {
              this.glassboxContent = this.multicontent_arr[ multicontent_num-1 ];
            }
            else {
              this.glassboxContent = this.header + this.multicontent_arr[ multicontent_num-1 ];
            }
          }
        }
        else {
          this.glassboxContent = this.glassbox.innerHTML;
        }
        this.glassbox.innerHTML = "";
    
        this.glassboxWidth = this.width;
        this.glassboxWidth1 = parseInt( this.glassboxWidth );
        this.boxborder_lr1 = parseInt( this.bb_hor );
        this.content_width = this.glassboxWidth1 - ( 2 * this.boxborder_lr1 ) + "px" ;
    
        this.glassboxHeight = this.height;
        this.glassboxHeight1 = parseInt( this.glassboxHeight );
        this.boxborder_tb1 = parseInt( this.bb_ver );
        this.content_height = this.glassboxHeight1 - ( 2 * this.boxborder_tb1 ) + "px" ;
    
        if (window.innerHeight) {
         this.screenWidth = window.innerWidth;
         this.screenHeight = window.innerHeight;
		} 
		else if ( document.documentElement.clientHeight ) {
          this.screenWidth = document.documentElement.clientWidth;
          this.screenHeight = document.documentElement.clientHeight;
        }
        else if ( document.body.clientHeight ) {
          this.screenWidth = document.body.clientWidth;
          this.screenHeight = document.body.clientHeight;
        }
      }
    },
    
    this.vscreen = function( left, margin ){
    
      this.glassboxHeight = this.screenHeight - ( parseInt( margin ) * 2 );
      this.glassboxHeight1 = parseInt( this.glassboxHeight );
      this.boxborder_tb1 = parseInt( this.bb_ver );
      this.content_height = this.glassboxHeight1 - ( 2 * this.boxborder_tb1 ) + "px" ;
    
      this.glassbox.style.position = "absolute";
      this.glassbox.style.left = left;
      this.glassbox.style.top = parseInt( margin ) + "px";
    
      this.createGlassbox();
    },
    
    this.ipos = function(){
      this.createGlassbox();
    },
    
    this.apos = function( left, top ){
    
      this.glassbox.style.position = "absolute";
      this.glassbox.style.left = left;
      this.glassbox.style.top = top;
    
      this.createGlassbox();
    },
    
    this.lbo = function( exitbut, opac ){
     if( this.exists != true ) {
        if( opac ) {
          var gb_overlay = document.createElement( "div" );
          gb_overlay.setAttribute( "id", this.id + "_overlay" );
          gb_overlay.style.position = "absolute";
          gb_overlay.style.backgroundColor = "#000000";
          gb_overlay.style.left = "0px";
          gb_overlay.style.top = "0px";
          gb_overlay.style.width = this.screenWidth + "px";
          gb_overlay.style.height = this.screenHeight + "px";
          gb_overlay.style.opacity = opac;
          gb_overlay.style.filter = "alpha( opacity=" + opac * 100 + " )";
          gb_overlay.style.zIndex = 999;
          gb_overlay.style.display = "block";
          var htmlbody = document.getElementsByTagName( "body" )[0];
          htmlbody.appendChild( gb_overlay );
        }
        if( exitbut != false ) {
          var exitButton = document.createElement( "div" );
          exitButton.setAttribute( "id","exitButton" );
          exitButton.style.position = "absolute";
          exitButton.style.left = this.glassboxWidth1 - 39 + "px";
          exitButton.style.top = 23 + "px";
          exitButton.style.zIndex = 1001;
          exitButton.title = "close";
          this.glassbox.appendChild( exitButton );
    
          var exitLink = document.createElement( "a" );
          exitLink.href = "javascript:THIS.fadeBox( '" + this.id + "' );";
          exitButton.appendChild( exitLink );
    
          var exitImage = document.createElement( "img" );
          exitImage.setAttribute( "id", "exitImage" );
          exitImage.style.border = 0;
          exitImage.src = this.skin_path + "exitButton.png";
          exitLink.appendChild( exitImage );
    
          try {
            new Effect.Appear( "exitButton" );
          }
          catch(e) {
            document.getElementById( "exitButton" ).style.display = "block";
          }
        }
        this.glassbox.ondblclick = function () {
          this.style.display = "none";
        };
        //this.glassbox.style.cursor = "pointer";
        //this.glassbox.style.cursor = "hand";
        this.glassbox.style.position = "absolute";
        this.glassbox.style.left = Math.round( ( this.screenWidth - this.glassboxWidth1 ) / 2 ) + "px";
        this.glassbox.style.top = Math.round( ( this.screenHeight - this.glassboxHeight1 ) / 2 ) + "px";
        this.glassbox.style.zIndex = 1000;
    
        this.createGlassbox();
        this.exists = true;
      }
	  else {
        document.getElementById( this.id + "_overlay" ).style.display = "block";
	  }
      this.appear();
    },
    
    this.createGlassbox = function(){
      
      var topleft = document.createElement( "div" );
      topleft.setAttribute( "id","topleft" );
      topleft.style.width = this.bb_hor;
      topleft.style.height = this.bb_ver;
      topleft.style.cssFloat = "left";
      topleft.style.styleFloat = "left";
      topleft.style.backgroundImage = "url( " + this.skinfolder + "/topleft.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        topleft.style.filter = this.iepngfix(this.skinfolder + "/topleft.png");
        topleft.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( topleft );
    
      var top = document.createElement( "div" );
      top.setAttribute( "id","top" );
      top.style.width = this.content_width;
      top.style.height = this.bb_ver;
      top.style.cssFloat = "left";
      top.style.styleFloat = "left";
      top.style.backgroundImage = "url( " + this.skinfolder + "/top.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        top.style.filter = this.iepngfix(this.skinfolder + "/top.png");
        top.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( top );
    
      var topright = document.createElement( "div" );
      topright.setAttribute( "id", "topright" );
      topright.style.width = this.bb_hor;
      topright.style.height = this.bb_ver;
      topright.style.cssFloat = "left";
      topright.style.styleFloat = "left";
      topright.style.backgroundImage = "url( " + this.skinfolder + "/topright.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        topright.style.filter = this.iepngfix(this.skinfolder + "/topright.png");
        topright.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( topright );
      
      var clear = document.createElement( "div" );
      clear.style.clear = "both";
      this.glassbox.appendChild( clear );
    
      var left = document.createElement( "div" );
      left.setAttribute( "id", "left" );
      left.style.width = this.bb_hor;
      left.style.height = this.content_height;
      left.style.cssFloat = "left";
      left.style.styleFloat = "left";
      left.style.backgroundImage = "url( " + this.skinfolder + "/left.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        left.style.filter = this.iepngfix(this.skinfolder + "/left.png");
        left.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( left );
    
      // Content
    
      var contentBox = document.createElement( "div" );
      contentBox.setAttribute( "id", "contentBox" );
      contentBox.style.width = this.content_width;
      contentBox.style.height = this.content_height;
      contentBox.style.cssFloat = "left";
      contentBox.style.styleFloat = "left";
      this.glassbox.appendChild( contentBox );
    
      var contentBoxBg = document.createElement( "div" );
      contentBoxBg.setAttribute( "id", "contentBoxBg" );
      contentBoxBg.style.width = "100%";
      contentBoxBg.style.height = "100%";
      contentBoxBg.style.overflow = this.overflow;
      contentBoxBg.style.backgroundColor = "#ffffff";
      contentBox.appendChild( contentBoxBg );
      this.contentBoxBg = contentBoxBg;    
    
      var content = document.createElement( "div" );
      content.setAttribute( "id", "contentgb" );
      content.innerHTML = this.glassboxContent;
      contentBoxBg.appendChild( content );
      this.content = content;    
      
      // End Content
    
      var right = document.createElement( "div" );
      right.style.width = this.bb_hor;
      right.style.height = this.content_height;
      right.style.cssFloat = "left";
      right.style.styleFloat = "left";
      right.setAttribute( "class", "boxmiddle" );
      right.setAttribute( "id", "right" );
      right.style.backgroundImage = "url( " + this.skinfolder + "/right.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        right.style.filter = this.iepngfix(this.skinfolder + "/right.png");
        right.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( right );
    
      var clear = document.createElement( "div" );
      clear.style.clear = "both";
      this.glassbox.appendChild( clear );
    
      var bottomleft = document.createElement( "div" );
      bottomleft.setAttribute( "id", "bottomleft" );
      bottomleft.style.width = this.bb_hor;
      bottomleft.style.height = this.bb_ver;
      bottomleft.style.cssFloat = "left";
      bottomleft.style.styleFloat = "left";
      bottomleft.style.backgroundImage = "url( " + this.skinfolder + "/bottomleft.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        bottomleft.style.filter = this.iepngfix(this.skinfolder + "/bottomleft.png");
        bottomleft.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( bottomleft );
      
      var bottom = document.createElement( "div" );
      bottom.setAttribute( "id", "bottom" );
      bottom.style.width = this.content_width;
      bottom.style.height = this.bb_ver;
      bottom.style.cssFloat = "left";
      bottom.style.styleFloat = "left";
      bottom.style.backgroundImage = "url( " + this.skinfolder + "/bottom.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        bottom.style.filter = this.iepngfix(this.skinfolder + "/bottom.png");
        bottom.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( bottom );
    
      var bottomright = document.createElement( "div" );
      bottomright.setAttribute( "id", "bottomright" );
      bottomright.style.width = this.bb_hor;
      bottomright.style.height = this.bb_ver;
      bottomright.style.cssFloat = "left";
      bottomright.style.styleFloat = "left";
      bottomright.style.backgroundImage = "url( " + this.skinfolder + "/bottomright.png )";
      if ( /MSIE (5\.5|6\.)/.test( navigator.userAgent )){ 
        bottomright.style.filter = this.iepngfix(this.skinfolder + "/bottomright.png");
        bottomright.style.backgroundImage = "none";
      }
      this.glassbox.appendChild( bottomright );
    
      var clear = document.createElement( "div" );
      clear.style.clear = "both";
      this.glassbox.appendChild( clear );      
    },
    this.appear = function( ms ){
      try {
        this.glassbox.style.display = "none";
        new Effect.Appear( this.id );
      }
      catch(e) {
        this.glassbox.style.display = "block";
      }
      this.glassbox.ondblclick = function () {
        THIS.fadeBox( this.id );
      };
      if( ms ) {
        setTimeout( "this.fadeBox( '" + this.id + "' )", ms );
      }
    },
    this.draggable = function(){
      try { // .. if Scriptaculous library is included
        new Draggable( this.id );
        this.glassbox.title = "You can drag me!";
      }
      catch(e) {}
    },
    this.backgroundImage = function( num ){
      try { // ..find and load background image
        var htmlbody = document.getElementsByTagName( "body" )[0];
        htmlbody.style.background ="url( " + this.img_bg_path + "bg" + num +".jpg )";
      }
      catch(e) {}
    },
    this.zindex = function( zindex ){
      this.glassbox.style.zIndex = zindex;
    },
    this.noro = function( rightmargin ){ //no right overflow
      if( rightmargin == null ) rightmargin = 0;
      var leftMarginScreen = this.screenWidth - ( parseInt( this.glassbox.style.left) + parseInt( this.glassboxWidth ) );
      if( leftMarginScreen < 0 ) {
        this.glassbox.style.left = ( parseInt( this.glassbox.style.left ) + leftMarginScreen + rightmargin + 14 ) + "px";
      }
    },
    this.fadeBox = function( id ){
      try { // .. if Scriptaculous library is included
        new Effect.Fade( id );
        if(document.getElementById( id + "_overlay" )) {
          new Effect.Fade( id + "_overlay" );
        }
      }
      catch(e) {
        document.getElementById( id ).style.display = "none";
        if(document.getElementById( id + "_overlay" )) {
          document.getElementById( id + "_overlay" ).style.display = "none";
        }
      }
    },
    this.iepngfix = function( bgimg ) {
	  return 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + bgimg + '", sizingMethod="scale")';
    }
  }
}


function getXML( method, url, targetId ) {
  try {
    req = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  } 
  catch (e) { /* No AJAX Support */ }
  req.onreadystatechange = function () {
    if ((req.readyState == 4) && (req.status == 200)) {
     document.getElementById( targetId ).innerHTML = req.responseText;
    }  
  }
  req.open( method, url );
  req.send( null ); 
}
