//Create  Forums.Controls Namespace
if (typeof Forums == 'undefined') var Forums = {};
if (!Forums.Controls) Forums.Controls = {};
if (!Forums.Utils) Forums.Utils = {};

[].indexOf || (Array.prototype.indexOf = function(v,n) {
  n = (n==null)?0:n; var m = this.length;
  for(var i = n; i < m; i++)
    if( _proxy_jslib_handle(this, (i), 0, 0) == v)
       return i;
  return -1;
});

Forums.Controls.Popup = function(elementId,showId,onShowScript,iFrameId,width,height) {	
	
    this._backgroundID = '_PopupBackground';
    this._backgroundClass = 'PopupBackground';
    this._elementID = elementId;
    this._showID = showId;	
    this._onShowScript=onShowScript;
	this._iFrameId=iFrameId;
	this._width = width;
	this._height = height;
    
    this._backgroundElement = null;
    this._element = null;
    this._showElement = null;
	this._initialized = false;	
	this._Select = new (Array)();
	if(this._showID != '')
	{
		this.hookHandler();			
	}
}

Forums.Controls.Popup.prototype = 
{
	initialize : function() {		
		this._backgroundElement = document.createElement('div');
		this._backgroundElement.className = this._backgroundClass;
		this._backgroundElement.id = this._backgroundID;			
		
		//Set Properties
		this._backgroundElement.style.display = 'none';
		this._backgroundElement.style.position = 'absolute';
		
		
		this._backgroundElement.style.left = '0px';
		 _proxy_jslib_assign('', this._backgroundElement.style, 'top', '=', ( '0px'));
		this._backgroundElement.style.zIndex = 1000;
    
		//Create the element
		this._element =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this._elementID);
		this._element.style.display = 'none';
        this._element.style.position = 'absolute';
        this._element.style.zIndex = 2000;
		
		this._resizeHandler=null;
		this._scrollHandler=null;		
		
		//Add the background element to DOM
		this._element.parentNode.appendChild(this._backgroundElement);		
		this._initialized=true;
		
	},
	
	hookHandler: function() {
		this._showElement =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this._showID);
		this._showElement.onclick = Forums.Utils.Delegate(this,this.show);	
	},
	
	show : function() {	
		if (!this._initialized)
		{
			this.initialize();
		}
		
		//Execute OnShowScript
		eval(_proxy_jslib_proxify_js((this._onShowScript), 0, 0) );		
		this.resize();
		this._backgroundElement.style.display = 'block';    						
		//Show the Object		      
        this._element.style.visibility = 'visible'; 
		this._element.style.display = 'block';  
		if(this._iFrameId)
		{
			 _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this._iFrameId).style.display = 'block';		
		}
        
        //Add Event Handlers to Window Resize and Scroll
		this._resizeHandler = Forums.Utils.Delegate(this,this.resize);
		this._scrollHandler = Forums.Utils.Delegate(this,this.resize);
		
        Forums.Utils.AddEvent(window, 'resize', this._resizeHandler);     
        Forums.Utils.AddEvent(window, 'scroll', this._scrollHandler);     
        
        this.hideSelect();    
       
        return false;        
	},
	
	hideSelect : function() {	  
		  var selectInParent =  _proxy_jslib_handle(document, 'getElementsByTagName', '', 1, 0)('SELECT');	
		  this._Select = new (Array)();		  	  		  
		  for (var i = 0 ; i < selectInParent.length; i++) 
		  {							  
			   _proxy_jslib_assign('', this._Select, (i), '=', ( {tag:  _proxy_jslib_handle(selectInParent, (i), 0, 0), oldv:  _proxy_jslib_handle(selectInParent, (i), 0, 0).style.visibility})) ;
			   _proxy_jslib_handle(selectInParent, (i), 0, 0).style.visibility = 'hidden'; 
		  }
	},

	showSelect : function() {
		  for (var i = 0 ; i < this._Select.length; i++) 
		  {
			 _proxy_jslib_handle(this._Select, (i), 0, 0).tag.style.visibility =  _proxy_jslib_handle(this._Select, (i), 0, 0).oldv;
		  }
	},

	close : function() {		
		this._backgroundElement.style.display = 'none';		
		this._element.style.visibility = 'hidden';	
		
		//Firefox has issues closing the outer element from an IFrame.
		if(this._iFrameId)
		{			
			 _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this._iFrameId).style.display = 'none';
			 _proxy_jslib_assign('',  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this._iFrameId), 'src', '=', (''));
		}		
		
		//Remove Event Handlers to Window Resize and Scroll
        Forums.Utils.RemoveEvent(window, 'resize', this._resizeHandler);     
        Forums.Utils.RemoveEvent(window, 'scroll', this._scrollHandler);   
        
        this.showSelect();  
        
	},
	
	resize : function() {	
		var size = GetWindowSize();
		var spos = GetWindowScrollXY();	
		var ex = 0;
		var ey = 0;
		
		this._backgroundElement.style.width = Math.max(Math.max(document.documentElement.scrollWidth,  _proxy_jslib_handle(document, 'body', '', 0, 0).scrollWidth), size[0])+'px';
        this._backgroundElement.style.height = Math.max(Math.max(document.documentElement.scrollHeight,  _proxy_jslib_handle(document, 'body', '', 0, 0).scrollHeight), size[1])+'px';                 
        
		//Not caluclating the height and width properly		
        //var eheight = this._element.offsetHeight? this._element.offsetHeight: this._element.scrollHeight;
        //var ewidth = this._element.offsetWidth? this._element.offsetWidth: this._element.scrollWidth;    		
		
		
        var ex = (size[1] - this._height) / 2 ;       
        var ey = (size[0] - this._width) / 2;
        
        if(this._element.style.position == 'absolute')
        {
			 _proxy_jslib_assign('', this._element.style, 'top', '=', (  ex + spos[1] + "px"));
			this._element.style.left = ey + spos[0] + "px";
        }
        else
        {
			 _proxy_jslib_assign('', this._element.style, 'top', '=', (  ex + "px"));
			this._element.style.left = ey + "px";
        }
	}
}	

		
Forums.Utils.Delegate = function(that , thatMethod) {
    return function() { return thatMethod.apply(that, arguments); }
}

Forums.Utils.AddEvent = function (obj, type, callMethod) { 
	
  if (obj.attachEvent) 
  {	 
	 _proxy_jslib_assign('', obj, ('e'+ type + callMethod), '=', ( callMethod)); 
	 _proxy_jslib_assign('', obj, (type + callMethod), '=', ( function() { _proxy_jslib_handle(obj, ('e' + type + callMethod), 1, 0)( window.event );})) 

 obj.attachEvent( 'on' + type,  _proxy_jslib_handle(obj, (type + callMethod), 0, 0) ); 
  } 
  else 
  {	
    obj.addEventListener(type, callMethod, false); 
  }  
} 

Forums.Utils.RemoveEvent = function (obj, type, callMethod) { 
  if ( obj.detachEvent ) 
  { 
    obj.detachEvent( 'on' + type,  _proxy_jslib_handle(obj, (type + callMethod), 0, 0) ); 
     _proxy_jslib_assign('', obj, (type + callMethod), '=', ( null)); 
  } 
  else 
  {  
    obj.removeEventListener( type, callMethod, false ); 
  }
}

Forums.Utils.TimedExecution = function (callback,interval) {
    this.callback=callback;
    this.interval=interval;
    
    this.start = function() {
        this.timer= _proxy_jslib_handle(null, 'setInterval', setInterval, 1, 0)(this.callback,interval);
    }

    this.stop = function() {
        if(!this.timer) return;
        clearInterval(this.timer);
    }
}

/* Settings Page Popup */
var objTimedExec;
var cHeight;
var maxHeight;
var settingsIFrameId='SettingsIFrame';		
var SettingsPageSrc = function(page) {	
	var obj= _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(settingsIFrameId);		
	cHeight=0;
	obj.style.height = "0px";
	 _proxy_jslib_assign('', obj, 'src', '=', (page));	
}	

function SetIFrameHeight() {			  
	//find the height of the internal page	
	maxHeight=  _proxy_jslib_handle( _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(settingsIFrameId).contentWindow.document, 'body', '', 0, 0).scrollHeight; 		
	objTimedExec=new (Forums.Utils.TimedExecution)(function() {changeHeight()},10);
	objTimedExec.start();		 
}
		
function changeHeight() {	
	if(cHeight <= maxHeight)
	{
		//change the height of the iframe				
		 _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(settingsIFrameId).style.height=cHeight + "px";				
		cHeight+=20;
	}
	else
	{
		 _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(settingsIFrameId).style.height=maxHeight + "px";				
				objTimedExec.stop();
	}
} 

/* Navigate away Popup */
Forums.Controls.PostPopup = function (elementId, okId, closeID, showId, showPreviewId, postId, postPreviewId, subjectId, bodyId, width, height,msg) {
	this.subjectId = subjectId;
	this.bodyId = bodyId;
	this.closeID = closeID;
	this.showID = showId;
	this.showPreviewId = showPreviewId;
	this.postId = postId;	
	this.postPreviewId = postPreviewId;
	this.message = msg;	
	this.okId = okId;
	this._showElement = null;
	this._okElement = null;
	this._prevShowhandler = null;
	this._bodyElement = null;
	this.popup = new (Forums.Controls.Popup)(elementId,'','','',width,height); 	
	
	if(this._showID!='')
	{
		Forums.Utils.AddEvent(window, 'load', Forums.Utils.Delegate(this,this.initialize)); 		
	}	
}

Forums.Controls.PostPopup.prototype = 
{
	getTextArea : function() {
		var body =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.bodyId);
		var objects =  _proxy_jslib_handle( _proxy_jslib_handle(null, 'body', body, 0, 0), 'getElementsByTagName', '', 1, 0)("textarea");
		var tarea;
		if ( objects.length >= 1)
		{
			tarea = objects[0];
		}		
		return tarea;
	},
	
	CheckAndShow : function() {	
		window.onbeforeunload = null; 	
		var sub =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.subjectId);
		
		if( ( sub &&  _proxy_jslib_handle(sub, 'value', '', 0, 0) != '') || (this._bodyElement &&  _proxy_jslib_handle(this._bodyElement, 'value', '', 0, 0) != ''))
		{
			this.popup.show();
			
			if (!this._closeElement)
			{
				this._closeElement =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.closeID);
				this._closeElement.onclick = Forums.Utils.Delegate(this, _proxy_jslib_handle(this, 'close', '', 0, 0));
			}	

			if(!this._okElement)
			{
				this._okElement =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.okId);				
				this._okElement.onclick = Forums.Utils.Delegate(this,this.ok);				
			}	
			return false;
		}

		return true;
				
	},
	
	initialize: function() {
		this._showElement =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.showID);
		this._showElement.onclick = Forums.Utils.Delegate(this,this.CheckAndShow);	
		
		var showPreviewElement =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.showPreviewId);
		showPreviewElement.onclick = Forums.Utils.Delegate(this,this.CheckAndShow);		
		
		var postbtn =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.postId);
		postbtn.onclick = function() { window.onbeforeunload = null };
		
		var postpreviewbtn =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.postPreviewId);
		postpreviewbtn.onclick = function() { window.onbeforeunload = null };
		
		var body =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(this.bodyId);
		this._bodyElement = this.getTextArea();
		
		if(this._bodyElement)
		{
			Forums.Utils.AddEvent( _proxy_jslib_handle(null, 'body', body, 0, 0), 'keyup', Forums.Utils.Delegate(this,this.onBodyKeyPress));		
		}
	},	
	
	onBodyKeyPress : function(evt) {		
		if( _proxy_jslib_handle(this._bodyElement, 'value', '', 0, 0) != '')
		{				
			window.onbeforeunload = Forums.Utils.Delegate(this,this.callBeforeUnload);
		}
		else
		{
			window.onbeforeunload = null;
		}
	},
	
	close : function() {
		window.onbeforeunload = Forums.Utils.Delegate(this,this.callBeforeUnload);
		 _proxy_jslib_handle(this.popup, 'close', '', 1, 0)();
		return false;
	},
	
	ok : function() {		
		 _proxy_jslib_handle(this.popup, 'close', '', 1, 0)();		
		 _proxy_jslib_handle( _proxy_jslib_handle(window, 'location', '', 0, 0), 'replace', '', 1, 0)( _proxy_jslib_handle(this._showElement, 'href', '', 0, 0));		
	},
	
	callBeforeUnload : function (evt) {		
		if( !evt ) evt = window.event;
		evt.returnValue = this.message;
	}
}

 ;
_proxy_jslib_flush_write_buffers() ;