var path_to_glassbox = "includes/";
var path_to_background_images = "includes/skins/";
var path_to_root_dir = ""; 

if ( typeof(GlassBox) == 'undefined' ) {
  GlassBox = function() {
  
    THIS = this,
    this.version = "1.3.0",
    this.version_comment = "Changed: double click event | Extended: ajax content loading",
    this.last_mod = "2008/03/10",
    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.dblclick = true;
    this.init = function( id, width, height, overflow, skinname, resize, dblclick, multicontent_num ) {
      
	  if( this.exists == false ) {
        this.id = id;
        this.width = width;
        this.height = height;
        this.overflow = overflow;
      
        if( resize != null && resize != "" ) {
          if( resize == false ) this.resize = false;
          else if( resize == true ) this.resize = true;
		}
        if( dblclick == false ) this.dblclick = false;
        else if( dblclick == true ) this.dblclick = true;
        
		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 {
          if(typeof this.glassboxContent == "undefined") {
            this.glassboxContent = this.glassbox.innerHTML;
		  }
        }
        if(this.glassbox != null) {
          this.glassbox.innerHTML = "";
	    }

        this.glassboxWidth = parseInt( this.width );
        this.boxborder_lr1 = parseInt( this.bb_hor );
        this.content_width = this.glassboxWidth - ( 2 * this.boxborder_lr1 ) + "px" ;

        this.glassboxHeight = parseInt( this.height );
        this.boxborder_tb1 = parseInt( this.bb_ver );
        this.content_height = this.glassboxHeight - ( 2 * this.boxborder_tb1 ) + "px" ;

        var sd = getScreenDimensions ();  
        this.screenWidth = sd.screenWidth;
        this.screenHeight = sd.screenHeight;
      }
    },
      this.vscreen = function( left, margin ){
      this.glassboxHeight = this.screenHeight - ( parseInt( margin ) * 2 );
      this.boxborder_tb1 = parseInt( this.bb_ver );
      this.content_height = this.glassboxHeight - ( 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 ) {
          this.overlay = document.createElement( "div" );
          this.overlay.setAttribute( "id", this.id + "_overlay" );
          this.overlay.style.position = "absolute";
          this.overlay.style.backgroundColor = "#000000";
          this.overlay.style.left = "0px";
          this.overlay.style.top = "0px";
          this.overlay.style.width = this.screenWidth + "px";
          this.overlay.style.height = this.screenHeight + "px";
          this.overlay.style.opacity = opac;
          this.overlay.style.filter = "alpha( opacity=" + opac * 100 + " )";
          this.overlay.style.zIndex = 999;
          this.overlay.style.display = "block";
          var htmlbody = document.getElementsByTagName( "body" )[0];
          htmlbody.appendChild( this.overlay );
        }
        if( exitbut != false ) {
          var exitButton = document.createElement( "div" );
          exitButton.setAttribute( "id","exitButton" );
          exitButton.style.position = "absolute";
          exitButton.style.left = this.glassboxWidth - 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.style.position = "absolute";
        this.glassbox.style.left = Math.round( ( this.screenWidth - this.glassboxWidth ) / 2 ) + "px";
        this.glassbox.style.top = Math.round( ( this.screenHeight - this.glassboxHeight ) / 2 ) + "px";
        this.glassbox.style.zIndex = 1000;
        this.glassbox.style.minWidth = this.glassboxWidth + "px";
    
        this.createGlassbox();
        this.exists = true;
      }
      else {
        document.getElementById( this.id + "_overlay" ).style.display = "block";
      }
      this.appear();
    },
    this.createGlassbox = function(){
  
      var toprow = document.createElement( "div" );
      toprow.setAttribute( "id","toprow" );
      toprow.style.width = this.glassboxWidth;
      toprow.style.minWidth = this.glassboxWidth + "px";
      this.glassbox.appendChild( toprow );

      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";
      }
      toprow.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";
      }
      toprow.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";
      }
      toprow.appendChild( topright );
      
      var clear = document.createElement( "div" );
      clear.style.clear = "both";
      this.glassbox.appendChild( clear );
    
      var middlerow = document.createElement( "div" );
      middlerow.setAttribute( "id","middlerow" );
      middlerow.style.width = this.glassboxWidth;
      this.glassbox.appendChild( middlerow );

      var left = document.createElement( "div" );
      left.setAttribute( "id", "glassleft" );
      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";
      }
      middlerow.appendChild( left );

      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";
      middlerow.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", "content" );
      content.innerHTML = this.glassboxContent;
      contentBoxBg.appendChild( content );
      this.content = 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", "glassright" );
      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";
      }
      middlerow.appendChild( right );
    
      var clear = document.createElement( "div" );
      clear.style.clear = "both";
      this.glassbox.appendChild( clear );
    
      var bottomrow = document.createElement( "div" );
      bottomrow.setAttribute( "id","bottomrow" );
      bottomrow.style.width = this.glassboxWidth;
      this.glassbox.appendChild( bottomrow );

      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";
      }
      bottomrow.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";
      }
      bottomrow.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";
      }
      bottomrow.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";
      }
      /** 
      * @private 
      */
      this.glassbox.ondblclick = function () {
		  //alert('dblclick');
        if(THIS.dblclick == true) 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 {
        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")';
    },
    window.onresize = function () {
      if(THIS.resize == true) {
        var sd = getScreenDimensions ();  
        THIS.glassbox.style.left = Math.round( ( sd.screenWidth - THIS.glassboxWidth ) / 2 ) + "px";
        THIS.glassbox.style.top = Math.round( ( sd.screenHeight - THIS.glassboxHeight ) / 2 ) + "px";  
        if(THIS.overlay != undefined){
          THIS.overlay.style.width = sd.screenWidth + "px";
          THIS.overlay.style.height = sd.screenHeight + "px";
        }
	  }
    }
  }
}
function GETrequest( url, target_id, callback_id ) {
  try {
    req = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  } 
  catch (e) {  }
  req.onreadystatechange = function () {
	if ((req.readyState == 4) && (req.status == 200)) {
	 if(target_id && document.getElementById( target_id )) document.getElementById( target_id ).innerHTML = req.responseText;
	 if(callback_id) GETrequest_callbackHandler(callback_id);
    }  
  }
  req.open( 'GET', url );
  req.send( null ); 
}
function getScreenDimensions (){
  if (window.innerHeight) {
    screenWidth = window.innerWidth;
    screenHeight = window.innerHeight;
  } 
  else if ( document.documentElement.clientHeight ) {
    screenWidth = document.documentElement.clientWidth;
    screenHeight = document.documentElement.clientHeight;
  }
  else if ( document.body.clientHeight ) {
    screenWidth = document.body.clientWidth;
    screenHeight = document.body.clientHeight;
  }
  return {
    "screenWidth": screenWidth, 
    "screenHeight": screenHeight 
  }
}

