var maxRows = 20;

//var homedirectory = location.protocol + '//' + location.hostname + '/';
//var homedirectory = 'http://localhost/eWossRedesign/';
//var homedirectory = 'http://www.ewoss.com/';
//var homedirectory = 'http://dev.ewoss.com/';
homedirectory = 'http://www.ewoss.com/';

function sortie(){

	this.resetData = function() {
		this.sackObj = null;
		this.type = 0;
  		this.columnIndex = 0;
		this.contentType = "";
		this.numRows = 0;
		this.numRowsDisplayed = 0;
		this.sortieFile = "";
		this.vars = "";
		this.sortieIndex = 0;
		this.refreshImage = "";
		this.closeImage = "";
		this.containerObj = null;
		this.headObj = null;
		this.editObj = null;
		this.contentObj = null;
		this.parentObj = null;
		this.saved = true;
		this.loaded = false;
  	};

	this.resetFunctions = function() {
  		this.onCompletion = function() { };
  		this.onMouseOverBox = function() { };
  		this.onMouseOutBox = function() { };
  		this.onMouseOverBoxHeader = function() { };
  		this.onMouseOutBoxHeader = function() { };
  		this.initDragDropBox = function() { };
	};
	
	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};
	
	this.docelm = function(str){
		return document.getElementById(str);
	};
	
	this.makeHeader = function(headText, editText, image, isWeather)
	{
	    var imagetext = "";
	    
	    if (image != null)
            imagetext = image[0].firstChild.nodeValue;
            
        if (isWeather)
            imagetext = "http://toolbar.metrotoolbars.com/images/weather/50x50/1.gif";
	
		var theHead = "";
		if (image != null) {
		    if (this.type == 4) // if facebook sortie
		        theHead += "<img src=\"" + imagetext + "\" alt=\"" + headText + "\" align=\"left\" style=\"margin-right:5px; height:24px; width:38px\" />";
		    else
		        theHead += "<img src=\"" + imagetext + "\" alt=\"" + headText + "\" align=\"left\" style=\"margin-right:5px;\" />";
		}
		if (isWeather)
		    theHead += "<img src=\"" + imagetext + "\" alt=\"" + headText + "\" align=\"left\" style=\"margin-right:5px; height:24px; width:24px;\" />";
		theHead += "<h6 style=\"float:left;\"><span id=\"spnHeaderText" + this.sortieIndex + 
			"\">" + headText + "</span>&nbsp;<span id=\"imgLoad" + this.sortieIndex + 
			"\" style=\"width:15px; height:15px;\" class=\"loadingHidden\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></h6>";
		theHead += "<div style=\"float:right;\">";
		if (this.type == 4) // if facebook sortie
		    theHead += "<span id=\"fbLogout" + this.sortieIndex + "\" class=\"fbLogout\" style=\"display:none\" onclick=\"doFbLogout()\">Logout</span> ";
		theHead += "<img id=\"imgRefresh" + this.sortieIndex + "\" src=\"" + 
			this.refreshImage + "\" style=\"width:15px; height:15px; margin:0px; cursor:pointer; display:none;\" onclick=\"reloadSortie(" + 
			this.sortieIndex + ")\" alt=\"refresh content\">&nbsp;&nbsp;";
		theHead += "<span id=\"spnEdit" + this.sortieIndex + 
			"\" class=\"addedit2\" style=\" cursor:pointer; display:none;\" onclick=\"toggleEdit(" + 
			this.sortieIndex + ", " + true + ");\">" + editText + "</span>&nbsp;&nbsp;";
		theHead += "<img id=\"imgClose" + this.sortieIndex + "\" src=\"" + 
			this.closeImage + "\" style=\"width:15px; height:15px; cursor:pointer; display:none;\" onclick=\"closeSortie(" + 
			this.sortieIndex + ")\" alt=\"close sortie\"><img id=\"imgClose_" + this.sortieIndex + "\" src=\"" + 
			this.closeImage + "\" style=\"width:15px; height:15px; cursor:pointer; display:none;\" onclick=\"closeSortie(" + 
			this.sortieIndex + ")\" alt=\"close sortie\"></div>";
		theHead += "<div style=\"clear: both; visibility: hidden; line-height0px: height:0px;\"></div>";
	    
		return theHead;
	};
	
	this.facebookFail = function() {
	    try {
	    var div = document.getElementById('dvHeader' + this.sortieIndex);
	    div.innerHTML = this.makeHeader("Facebook", "", null, false);
	    div = document.getElementById("dvContent" + this.sortieIndex);
	    div.innerHTML = "Facebook is experiencing technical difficulties and was too slow to respond. Please refresh this box or page to try again.";
	    }
	    catch(err) {}
	};
	
	this.createContainer = function() {
		var maindiv = document.createElement('DIV');
		
		maindiv.className = 'sContainer';
		
		maindiv.id = 'dvBox' + this.sortieIndex;

		maindiv.onmouseover = this.onMouseOverBox;
		maindiv.onmouseout = this.onMouseOutBox;

		var div = document.createElement('DIV');
		div.className='sHeader';
		div.id = 'dvHeader' + this.sortieIndex;
		div.innerHTML = this.makeHeader("Loading...", "", null, false);

		div.onmouseover = this.onMouseOverBoxHeader;
		div.onmouseout = this.onMouseOutBoxHeader;
		maindiv.appendChild(div);

		var innerDiv = document.createElement('DIV');
		innerDiv.id = "dvContent" + this.sortieIndex;
		innerDiv.innerHTML = "<img src='" + homedirectory + "images/ajax-loader.gif' alt='Loading...' />";
		maindiv.appendChild(innerDiv);

		var editDiv = document.createElement('DIV');
		editDiv.id = "dvEdit" + this.sortieIndex;
		editDiv.style.display = 'none';
		maindiv.appendChild(editDiv);

		var obj = this.docelm("dvColumn" + this.columnIndex);

		var subs = obj.getElementsByTagName('DIV');

		if(subs.length>0) {
		    obj.insertBefore(maindiv, subs[subs.length-1].nextSibling);
		}
		else {
		    obj.appendChild(maindiv);
		}

		var dvHead = maindiv.firstChild;
		
		dvHead.onmousedown = this.initDragDropBox;
		dvHead.style.cursor = 'move';

		this.containerObj = maindiv;
		this.headObj = maindiv.childNodes[0];
		this.contentObj = maindiv.childNodes[1];
		this.editObj = maindiv.childNodes[2];
		this.parentObj = maindiv.parentNode;
	};
	
	this.reloadSortie = function() {
		this.sackObj = new sack();
		this.sackObj.requestFile = this.sortieFile;
		this.sackObj.onCompletion = this.onCompletion;
		this.sackObj.setVar('theParams', this.vars);
		this.sackObj.runAJAX();
	};
	
	this.setNumRowsDisplayed = function(rowsDisplayed) {
	    if (this.numRows > rowsDisplayed)
	        this.setNumRows(rowsDisplayed);
	    this.numRowsDisplayed = rowsDisplayed;
	};
	
	this.setNumRows = function(numRows) {
	    if (numRows > 0 && numRows <= maxRows) {
	        this.numRows = numRows;
	        this.vars = 'bx:' + this.sortieIndex + ',code:' + this.contentType + ',type:' + this.type + ',num:' + this.numRows;
	    }
	};
	
	this.createSortie = function() {
	    this.createContainer();

		this.sackObj = new sack();
		this.sackObj.requestFile = this.sortieFile;
		this.sackObj.onCompletion = this.onCompletion;
		this.sackObj.setVar('theParams', this.vars);
		this.sackObj.runAJAX();
	};
	
	this.reset();
}