
Array.prototype.remove = function(elem)
{
	var i;
	var found = false;
	
	for (i=0; i < this.length ;++i)
	{
		if (this[i] == elem)
		{
			this[i] = null;
			found = true;
			break;
		}
	}
	
	if (!found)
	{
		return false;
	}
	
	for (; i < this.length-1 ;++i)
	{
			this[i] = this[i+1];
	}

	this.length--;
	
	return this;
}

function getBrowser()
{
	var str = navigator.userAgent;
	
	if (str.indexOf('MSIE') != -1)
	{
		return 'IE';
	}
	else if (str.indexOf('Firefox') != -1)
	{
		return 'Firefox';
	}
	else if (str.indexOf('Mozilla') != -1)
	{
		return 'Mozilla';
	}
	else
	{
		return 'Other';
	}
	
}

//	Lightbox JS: Fullsize Image Overlays 
//	by Lokesh Dhakar - http://www.huddletogether.com

function inherits(derivedClass, baseClass)
{
	var sConstructor = baseClass.toString(); 
	var aMatch = sConstructor.match( /\s*function (.*)\(/ ); 
	
	if ( aMatch !== null )
	{ 
		derivedClass.prototype[aMatch[1]] = baseClass;
	}
	 
	for (var m in baseClass.prototype) {
			derivedClass.prototype[m] = baseClass.prototype[m];

	}
	
	derivedClass.prototype.__className = derivedClass.toString().match( /\s*function (.*)\(/ )[1];
};


function clearChildren(node)
{
   while (node.hasChildNodes()) {
       node.removeChild(node.firstChild);
   }
}

// Peter-Paul Koch's "quirksmode"

function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

// *******************

function addToWidgetList(element)
{
	if (typeof($(document).widgetList) == 'undefined')
	{
		$(document).widgetList = [element];
	}
	else
	{
		$(document).widgetList[$(document).widgetList.size()] = element;
	}
}

function SimplePanel(widgetID,width, height,root,style)
{

	function addSubmitButton(formId,text)
	{
    	var fieldDiv = document.createElement('div');
    
	    var fieldInput = document.createElement('input');
    	fieldInput.type = 'submit';
    	fieldInput.value = text;
    	fieldInput.className = "UIButton";
            
    	fieldDiv.appendChild(fieldInput);
    
    	formId.appendChild(fieldDiv);
    
    	return fieldInput;
	};
    
	function addButton(formId,text)
	{
    	var fieldDiv = document.createElement('div');
    
	    var fieldInput = document.createElement('input');
    	fieldInput.type = 'button';
    	fieldInput.value = text;
    	fieldInput.className = "UIButton";
        
    	fieldDiv.appendChild(fieldInput);
    
    	formId.appendChild(fieldDiv);
    
    	return fieldInput;
	};
	
	
	this.root = root || document.body;

	var fstyle;

	if (style === undefined)
		fstyle = 'exclusive';
	else
		fstyle = style;
		
	if (fstyle == 'exclusive')
	{	
		this.grayDiv = this.addGrayBackground();
	}

	this.mainPanel = this.createMainPanel(widgetID,width,height,fstyle);
	
	this.root.appendChild(this.mainPanel);
	
	this.placePanel();
};

SimplePanel.prototype.placePanel = function()
{
	if (this.fstyle == 'exclusive')
	{
		this.mainPanel.style.left = document.body.clientWidth / 2 - this.mainPanel.clientWidth / 2;
	   	this.mainPanel.style.top = document.body.clientHeight / 2 - this.mainPanel.clientHeight / 2;
	}
}

SimplePanel.prototype.addSimpleTextField = function(formId,fieldId,fieldName)
{
    var fieldDiv = document.createElement('div');
    fieldDiv.className = "UIFormRow";
    
    var labelSpan = document.createElement('span');
    labelSpan.className = "UILabelSpan";
    
    var fieldLabel = document.createElement('label');        
    fieldLabel.id = "label_"+fieldId;        
    fieldLabel.htmlFor = fieldId;
    fieldLabel.update(fieldName);
    fieldLabel.className = "UILabel";
    
    labelSpan.appendChild(fieldLabel);
    
    var fieldSpan = document.createElement('span');
    fieldSpan.className = "UIFieldSpan";
    
    var fieldInput = document.createElement('input');
    fieldInput.type = 'text';
    fieldInput.id = fieldId;
    fieldInput.name = fieldId;
    fieldInput.className = "UIFieldText";
    
    fieldInput.onkeypress = function()
    {
    	var e = (typeof(event) != 'undefined')?event:arguments[0];
		var key=e.keyCode || e.which;
		if (key==13)
		{
			$(formId).creator.makeRequest();
		}
	};
    
    fieldSpan.appendChild(fieldInput);
    
    fieldDiv.appendChild(labelSpan);
    fieldDiv.appendChild(fieldSpan);
    formId.appendChild(fieldDiv);
};

SimplePanel.prototype.addGrayBackground = function()
{
   	var grayDiv = document.createElement('div');
   	grayDiv.style.zIndex = 1;
   	
   	if (getBrowser() == 'IE')
	// Indico uses quirks mode, so, IE7 doesn't fix 'relative'
	{
		// dirty IE-specific hack...
		grayDiv.style.position = "absolute";
		grayDiv.style.right = "auto";		
		grayDiv.style.bottom = "auto";		
	  	grayDiv.style.left = '0px';
  	  	grayDiv.style.top =  '0px';
    	grayDiv.style.width = '100%';
  		grayDiv.style.height = getPageHeight();
	}
	else
	{
		grayDiv.style.position = 'fixed';
    	grayDiv.style.width = document.body.clientWidth;
   		grayDiv.style.height = document.body.clientHeight;
    	grayDiv.style.left = 0;
	    grayDiv.style.top = 0;
	}
   	
   	grayDiv.style.background = "#555555";
    grayDiv.style.display = 'none';
    
   	grayDiv.id = "UIGrayDiv";
   
    document.body.appendChild(grayDiv);
   
   	new Effect.Appear(grayDiv,{duration:1.0,to:0.7});
   
    return grayDiv;
};

SimplePanel.prototype.createMainPanel = function(widgetID,width,height,fstyle)
{
   	this.mainPanel = document.createElement('div');
   	this.mainPanel.id = widgetID;
   	this.mainPanel.className = "UISimpleDialogMainPanel";

   	if (width == -1)
   		this.mainPanel.style.width = 'auto';
   	else
   		this.mainPanel.style.width = width+'px';
   	
   	if (height == -1)
   		this.mainPanel.style.height = 'auto';
   	else
   		this.mainPanel.style.height = height+'px';
   
   	if (fstyle == 'inline')
   	{
   		this.mainPanel.style.position = 'relative';
   	}
   	else if (fstyle == 'exclusive')
   	{
   		if (getBrowser() == 'IE')
   		// Indico uses quirks mode, so, IE7 doesn't fix 'relative'
   		{
   			this.mainPanel.style.position = "absolute";		
   		}
   		else
   		{
   			this.mainPanel.style.position = 'fixed';    		
    	}
    	this.mainPanel.style.zIndex = 2;
   	}
   
   	this.widgetID = widgetID;
   	this.fstyle = fstyle;
   
   	return this.mainPanel;
};

SimplePanel.prototype.addCheckbox = function(formId,options)
{
   
    var count = 1;
   
   	for (var opt in options)
    {
   	    var fieldDiv = document.createElement('div');
        fieldDiv.className = "UIFormRow";
       
       	var fieldInput = document.createElement('input');
        fieldInput.type = 'checkbox';
        fieldInput.className = "UIFieldCheckbox";
        fieldInput.value = options[opt][1];
        fieldInput.name = opt;
        fieldInput.id = opt;
       
   	    var fieldLabel = document.createElement('label');
        fieldLabel.id = "label_"+opt;
   	    fieldLabel.className = "UILabel";
        fieldLabel.htmlFor = fieldInput.id;
   	    fieldLabel.update(options[opt][0]);
       
       	fieldDiv.appendChild(fieldInput);
        fieldDiv.appendChild(fieldLabel);
   
   	    formId.appendChild(fieldDiv);
       
       	count++;
    }
               
   	return fieldInput;
};

SimplePanel.prototype.close = function()
{
	if (this.fstyle == 'exclusive')
	{
		this.removeGrayBackground();
		Element.remove(this.mainPanel.id);
	}

};

SimplePanel.prototype.removeGrayBackground = function()
{
	Element.remove(this.grayDiv);
};

SimplePanel.prototype.putOnLoad = function()
{
	this.loadDiv = document.createElement('div');
	this.loadDiv.id = "UILoadDiv";
	document.body.appendChild(this.loadDiv);

   	this.loadDiv.update('<h1>Searching...</h1><br /><img src="images/UI_loader.gif" title="Searching" alt="Searching" />');
  	
  	var pos = findPos(this.mainPanel);
  	
  	if (getBrowser() == 'IE')	
	{
		this.loadDiv.style.position = "absolute";		
	}
  	else
	{
		this.loadDiv.style.position = "fixed";		
	}
		
	this.loadDiv.style.top = pos[1];
	this.loadDiv.style.left = pos[0];
	this.loadDiv.style.width = this.mainPanel.offsetWidth;
	this.loadDiv.style.height = this.mainPanel.offsetHeight;
};

SimplePanel.prototype.setStyle = function(name)
{
	this.mainPanel.className = name;
}

SimplePanel.prototype.hide = function(name)
{
	this.mainPanel.style.display = 'none';
}

SimplePanel.prototype.setInnerHTML = function(html)
{
	Element.update(this.mainPanel, html);
}

SimplePanel.prototype.placeAt = function(x,y)
{
	this.mainPanel.style.left = x + 'px';
	this.mainPanel.style.top = y + 'px';
}


SimplePanel.prototype.putOffLoad = function()
{
	this.destroy =  function()
	{
		Element.remove('UILoadDiv');
	}

	new Effect.Fade('UILoadDiv',{duration:0.5, afterFinish:this.destroy});
};

function SimpleDateField(element)
{
	$(this).element = element;
	
	var cal = Calendar.setup({
        inputField     :    element,
        eventName		:   "click",
        ifFormat       :    "%d/%m/%Y",
        showsTime      :    false
    });	
}

