var objResizableLightbox;

function CLightbox( intWidth, intHeight, strRequestUrl, strContentDivId, strTitle, strFormId, boolIsHideClose ) {

     this.intHeight 		= intHeight;
     this.intWidth			= intWidth;

     this.strTitle         	= strTitle;
     this.strRequestUrl    	= strRequestUrl;
     this.strContentDivId  	= strContentDivId;
     this.strLightboxDivId 	= strContentDivId + '_lightbox';
     this.strFormId			= strFormId;
     this.boolIsHideClose   = 0;
	 this.boolIsFromResidentWorks = 0; 	
	
     this.intScrollY		= 0;

     this.intViewportWidth	= 0;
     this.intViewportHeight = 0;

     this.strOnCloseRedirectUrl	= 'javascript:void(0);';
     this.strOnCloseCallbackJs	= null;

     this.initialize = function() {
     	this.addLightboxMarkup();
     	this.resize();

     	objResizableLightbox = this;
     }

	 // To check, request coming from resident works.
     intMatchPos = this.strRequestUrl.search(/residentworks/i);			
	 if( -1 != intMatchPos ) {
		this.boolIsFromResidentWorks = 1;
	 }
		
     if( 'undefined' != typeof boolIsHideClose ) {
     	this.boolIsHideClose   = boolIsHideClose;
     }

     // Set functions
     this.setWidth = function( intWidth ) {
     	this.intWidth = intWidth;
     }

     this.setHeight = function( intHeight ) {
     	this.intHeight = intHeight;
     }

     this.setRequestUrl = function( strRequestUrl ) {
        this.strRequestUrl = strRequestUrl;
     }

     this.setTitle = function( strTitle ) {
     	this.strTitle = strTitle;
     }

     this.setFormId = function( strFormId ) {
     	this.strFormId = strFormId;
     }

     this.setContent = function( strContent ) {
     	this.strContent = strContent;
     }

     this.setOnCloseRedirectUrl = function( strOnCloseRedirectUrl ) {
     	this.strOnCloseRedirectUrl = strOnCloseRedirectUrl;
     }

     this.setOnCloseCallbackJs = function( strOnCloseCallbackJs ) {
     	this.strOnCloseCallbackJs = strOnCloseCallbackJs;
     }

     // other functions
     this.addLightboxMarkup = function() {
        objBody = document.getElementsByTagName('body')[0];

        // create overlay div
        if( !$('overlay') ) {
           objOverlayDiv = document.createElement('div');
           objOverlayDiv.id = 'overlay';
        }

        // create light box div		
		objLightboxDiv 				= document.createElement('div');
        objLightboxDiv.id 			= 'lightbox';
		objLightboxDiv.style.width	= this.intWidth + 'px';       
		 
		if( 1 == this.boolIsFromResidentWorks ) {
			objLightboxDiv.style.width	= (this.intWidth+8)+ 'px';
		}
		
		if( 1 == this.boolIsHideClose) {
        	objLightboxDiv.innerHTML 	= '<b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b><div style="padding:5px 5px 5px 5px; *padding:10px 5px 0px 10px;" class="lightboxTitle"><div id="lightbox_title" style="font-weight:bold; float:left; line-height:16px; padding-left: 2px;">'+this.strTitle+'</div><div style="text-align:right; float:right; *float:none; padding-right:2px; margin-bottom:8px;"><a href="#" onclick="javascript:objResizableLightbox.remove();" style="color:#FFFFFF;text-decoration:none;">[X]</a></div></div><div style="clear:both;height:100%;"><div id="'+this.strContentDivId+'" style="margin-bottom:10px; height:'+this.intHeight+'px; *height:450px; width:'+this.intWidth+'px;overflow:auto;"></div><b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b>';
        }else if( 2 == this.boolIsHideClose ){
        	objLightboxDiv.innerHTML 	= '<b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b><div style="padding:10px 10px 0px 10px; *padding:10px 10px 10px 10px;"><div id="lightbox_title" class="lightboxTitle" style="float:left; margin-bottom:8px; padding-left: 12px;">'+this.strTitle+'</div><div style="text-align:right; float:right; *float:none; padding-right:16px;"><a href="'+this.strOnCloseRedirectUrl+'" onclick="javascript:objResizableLightbox.remove();'+this.strOnCloseCallbackJs+'"><img src="/Common/images/close.png" width="12" height="12" border="0"></a></div></div><div style="clear:both;height:100%;"><div id="'+this.strContentDivId+'" style="margin-bottom:10px; height:'+this.intHeight+'px; *height:450px; width:'+this.intWidth+'px;overflow:auto;"></div><b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b>';
        }else{
			if( 1 == this.boolIsFromResidentWorks ) {
	        	objLightboxDiv.innerHTML 	= '<div style="background:#5082c1; border:1px solid #81a0ce; overflow:none; width:'+(this.intWidth+8)+'px;"><div style="padding:10px 10px 0px 10px; margin:1px; height:28px; background:url(/Common/images/rw_light_box_title_background.gif) repeat-x;"><div id="lightbox_title" class="lightboxTitle" style="float:left; margin-bottom:5px; font:bold 13px arial; color:#fff;">'+this.strTitle+'</div><div style="text-align:right; float:right; *float:none;"><a href="#" onclick="javascript:objResizableLightbox.remove();"><img src="/Common/images/rw_light_box_close.gif" width="19" height="17" border="0"></a></div></div><div style="background:#fff; margin:0px 4px 4px 4px;"><div id="'+this.strContentDivId+'" style="height:'+this.intHeight+'px; *height:450px; width:'+this.intWidth+'px; overflow:auto;"></div></div></div>';
			}else{
				objLightboxDiv.innerHTML 	= '<b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b><div style="padding:10px 10px 0px 10px; *padding:10px;"><div id="lightbox_title" class="lightboxTitle" style="float:left; margin-bottom:8px; padding-left: 12px;">'+this.strTitle+'</div><div style="text-align:right; float:right; *float:none; padding-right:12px;"><a href="#" onclick="javascript:objResizableLightbox.remove();"><img src="/Common/images/close.jpg" width="55" height="15" border="0"></a></div></div><div style="clear:both;height:100%;"><div id="'+this.strContentDivId+'" style="margin-bottom:10px; height:'+this.intHeight+'px; *height:450px; width:'+this.intWidth+'px;overflow:auto;"></div><b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b>';
			}
        }

        objBody.appendChild(objOverlayDiv);
        objBody.appendChild(objLightboxDiv);
     }

     this.setPosition = function() {
     	this.intLeft 	= (Math.round((this.intViewportWidth - this.intCurrentWidth)/2) + 250);
     	this.intTop 	= (Math.round((this.intViewportHeight - this.intCurrentHeight)/2) + 192);

     	$('lightbox').style.top 			 = this.intTop + 'px';
     	$('lightbox').style.left 			 = this.intLeft + 'px';
     	$('lightbox').style.width			 = this.intCurrentWidth + 'px';
     	$(this.strContentDivId).style.width  = this.intCurrentWidth + 'px';
     	$(this.strContentDivId).style.height = this.intCurrentHeight + 'px';
     }

     this.resize = function() {

     	if( 0 == document.viewport.getDimensions().width ) {
     		this.intViewportWidth = document.body.clientWidth;
     	} else {
     		this.intViewportWidth = document.viewport.getDimensions().width;
     	}

     	if( 0 == document.viewport.getDimensions().height ) {
     		this.intViewportHeight = document.body.clientHeight;
     	} else {
     		this.intViewportHeight = document.viewport.getDimensions().height;
     	}

     	if( this.intWidth >= this.intViewportWidth ) {
        	this.intCurrentWidth = this.intViewportWidth - 100;
     	} else {
     		this.intCurrentWidth = this.intWidth;
     	}

     	if( this.intHeight > this.intViewportHeight ) {
        	this.intCurrentHeight = this.intViewportHeight - 100;
     	} else {
     		this.intCurrentHeight = this.intHeight;
     	}

     	this.setPosition();
     }

     this.show = function () {
     	this.initialize();

     	this.intScrollY = this.getScrollY();

     	this.displayWindowScrolling('100%', 'hidden');
     	this.setWindowScroll(0,0);
     	this.displaySelects('hidden');
     	this.displayApplets('none');

     	this.resize();
		
     	$('lightbox_title').innerHTML = this.strTitle;

        $('overlay').style.display 	= 'block';
        $('lightbox').style.display = 'block';
		
		if( 1 == this.boolIsFromResidentWorks ) {
			$('lightbox').style.width = (this.intWidth+8)+ 'px';
		}

        if( 'undefined' != typeof this.strContent ) {
			this.displayContent();
        } else {
	        this.loadInfo();
        }
     }

     this.hide = function() {
        $('overlay').style.display = 'none';
        $('lightbox').style.display = 'none';
     }

     this.remove = function() {
        Element.remove($('lightbox'));
        Element.remove($('overlay'));

        this.setWindowScroll(0,this.intScrollY);
		this.displayWindowScrolling( 'auto', 'auto' );
		this.displaySelects('visible');
		this.displayApplets('block');

        objResizableLightbox = null;
     }

     this.loadInfo = function() {
     	if( 'undefined' != typeof this.strFormId && null != this.strFormId ) {
			new Ajax.Updater(this.strContentDivId,
				this.strRequestUrl,
				{ parameters: $(this.strFormId).serialize(true), onLoading: this.showWaitingImage(this.strContentDivId), evalScripts:true }
			);
     	} else {
			new Ajax.Updater(this.strContentDivId,
				this.strRequestUrl,
				{ onLoading: this.showWaitingImage(this.strContentDivId), evalScripts:true }
			);
     	}
     }

     this.displayContent = function() {
     	$(this.strContentDivId).innerHTML = this.strContent;
     }

     this.displaySelects = function( strVisibility ) {
		if ( navigator.appName == "Microsoft Internet Explorer" ) {
	     	arrobjSelects = document.getElementsByTagName('select');
			for(i = 0; i < arrobjSelects.length; i++) {
				arrobjSelects[i].style.visibility = strVisibility;
			}
		}
     }

     this.displayApplets = function( strDisplay ) {
     	arrobjApplets = document.getElementsByTagName('applet');
		for(i = 0; i < arrobjApplets.length; i++) {
			arrobjApplets[i].style.display = strDisplay;
		}
     }

     this.displayWindowScrolling = function( strHeight, strVisibility ) {
 		objBody = document.getElementsByTagName('body')[0];
		objBody.style.height 	= strHeight;
		objBody.style.overflow 	= strVisibility;

		objHtml = document.getElementsByTagName('html')[0];
		objHtml.style.height 	= strHeight;
		objHtml.style.overflow 	= strVisibility;
     }

     this.getScrollY = function() {
		if (self.pageYOffset) {
			return self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			return document.documentElement.scrollTop;
		} else if (document.body) {
			return document.body.scrollTop;
		}

		return 0;
     }

     this.setWindowScroll = function( intX, intY ) {
     	window.scrollTo(intX, intY);
     }

     this.showWaitingImage = function( strDivId ) {
		var strBumperImage = '<div class="section_container" style="text-align:center; vertical-align:bottom;"';

	  	if( 0 < document.getElementById(strDivId).clientWidth )
	  	 	strBumperImage += ' width:' + document.getElementById(strDivId).clientWidth + 'px;';

	  	if( 0 < document.getElementById(strDivId).clientHeight )
	  	 	strBumperImage += ' height:' + document.getElementById(strDivId).clientHeight + 'px;';

	  	 	if( 2 == this.boolIsHideClose ){
	  	 		strBumperImage += '"><img src="/Common/images/ajax_loader.gif" width="32" height="32" border="0" >';
	  	 	} else {
				strBumperImage += '"><img src="/Common/images/loading.gif" width="36" height="36" border="0" >';
	  	 	}
			strBumperImage += '</div>';

	  		document.getElementById(strDivId).innerHTML = strBumperImage;
     }

     //this.initialize();
}

resizeLightbox = function() {
	if( null != objResizableLightbox && 'object' == typeof objResizableLightbox ) {
		objResizableLightbox.resize();
	}
}

this.resizeLightbox = resizeLightbox;

Event.observe(window, 'resize', resizeLightbox, false);