function QuickEdit()
{
	this.oAjaxThreads = new Array();
	this.loadingThreads = 0;
	this.temp = new Array();
	
	this.currentObject = '';
	this.currentContent = '';
	this.lastEvent = new Array();
	this.currentClassName = '';
	this.currentAttribute = '';
	this.currentId = '';
	this.fckeditor = false;
	this.alternativeCss = "";
	this.iframe = false;
	this.selectHtmlName = false;
	this.selectContent = false;
	this.selectSelectedValue = false;
}

QuickEdit.prototype.getNextThread = function()
{
	var i = 0;
	
	if (this.oAjaxThreads.length == 0)
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}

	while (this.oAjaxThreads[i] && this.oAjaxThreads[i].working && i < this.oAjaxThreads.length )
	{
		i++;
	}
	
	if (!this.oAjaxThreads[i])
	{
		var oAjax = new AJAX();
		this.oAjaxThreads.push(oAjax);		
	}
	
	return this.oAjaxThreads[i];
}


QuickEdit.prototype.quickEdit = function(event, object, className, attribute, id, fckeditor, alternativeCss, iframe, forceCurrentElement, truncate, strip_tags, selectHtmlName)
{
	this.currentObject = object;
	
	//IE hack
	// Calculate pageX/Y if missing and clientX/Y available
	if ( event.pageX == undefined && event.clientX != undefined ) {
		var e = document.documentElement, b = document.body;
		event.pageX = event.clientX + (e.scrollLeft || b.scrollLeft);
		event.pageY = event.clientY + (e.scrollTop || b.scrollTop);
	}
	
	this.lastEvent['pageX'] = event.pageX;
	this.lastEvent['pageY'] = event.pageY;
	this.currentClassName = className;
	this.currentAttribute = attribute;
	this.currentId = id;
	this.fckeditor = fckeditor;
	this.alternativeCss = alternativeCss;
	this.universalLoad(className, attribute, id);
	this.iframe = iframe;
	this.forceCurrentElement = forceCurrentElement;
	this.truncate = truncate;
	this.strip_tags = strip_tags;
	this.selectHtmlName = selectHtmlName;
}

QuickEdit.prototype.close = function()
{
	if(document.getElementById('quickedit'))
	{
		document.getElementsByTagName('body')[0].removeChild(document.getElementById('quickedit'));
	}
	this.currentElement = '';
	this.currentObject = '';
	this.currentContent = '';
	this.lastEvent = new Array();
	this.fckeditor = false;
}

QuickEdit.prototype.universalLoad = function(className, attribute, id)
{
	var oAjax = this.getNextThread();
	oAjax.url = '/quickEdit/universalLoad/'+className+'/'+attribute+'/'+id;
	oAjax.method = 'post';
	oAjax.data = '';
	oAjax.addRequestListener(this, this.universalLoaded, false);
	oAjax.open();
}

QuickEdit.prototype.universalLoaded = function(xmlDocument, text)
{
	var content = text;
	var className = this.currentClassName;
	var attribute = this.currentAttribute;
	var id = this.currentId;
	
	var oDOMHelper = new DOMHelper();
	
	if(document.getElementById('quickedit'))
	{
		document.getElementsByTagName('bodyalert')[0].removeChild(document.getElementById('quickedit'));
	}

	var x = this.lastEvent['pageX']+'px';
	var y = this.lastEvent['pageY']+'px';
	var div = document.createElement('div');
	div.setAttribute('id', 'quickedit');
	div.setAttribute('class', 'quick-edit');
	div.className = 'quick-edit';
	div.style.left = x;
	div.style.top = y;
	if(this.iframe)
	{
		div.style.left = 0;
	}
	div.style.display = '';
	
	var classInput = document.createElement('input');
	classInput.setAttribute('name', 'className');
	classInput.setAttribute('id', 'className');
	classInput.setAttribute('type', 'hidden');
	classInput.setAttribute('value', className);
	div.appendChild(classInput);
	
	var objectAttribute = document.createElement('input');
	objectAttribute.setAttribute('name', 'objectAttribute');
	objectAttribute.setAttribute('id', 'objectAttribute');
	objectAttribute.setAttribute('type', 'hidden');
	objectAttribute.setAttribute('value', attribute);
	div.appendChild(objectAttribute);
	
	var objectId = document.createElement('input');
	objectId.setAttribute('name', 'objectId');
	objectId.setAttribute('id', 'objectId');
	objectId.setAttribute('type', 'hidden');
	objectId.setAttribute('value', id);
	div.appendChild(objectId);
	
	if(this.forceCurrentElement)
	{
		this.currentElement = document.getElementById(this.forceCurrentElement);
	}
	else
	{
		this.currentElement = this.currentObject.parentNode;
	}
	
	if (this.fckeditor) 
	{
		div.style.width = '600px';
		div.style.height = '500px';
	}
	else
	{
		div.style.width = '400px';
		div.style.height = '180px';		
	}
	
	if(this.iframe)
	{
		div.style.width = '220px';
	}
	
	if(this.selectHtmlName)
	{
		div.style.width = '200px';
		div.style.height = '140px';
		this.selectContent = "<div id='selectContent'>"+$("#"+this.selectHtmlName).html()+"</div>";
		$("#"+this.selectHtmlName).html("");
	}
	else
	{
		var inputElement = document.createElement('textarea');
		inputElement.setAttribute('cols', '40');
		if(this.iframe)
		{
			inputElement.setAttribute('cols', '25');
		}
		inputElement.setAttribute('rows', '5');
		
		inputElement.setAttribute('id', 'quickEditContent');
		inputElement.value = content;
	}
	
	var closerDiv = document.createElement('div');
	closerDiv.style.width = '100%';
	closerDiv.style.textAlign = 'right';
	
	var closerTag = document.createElement('a');
	closerTag.setAttribute('href', 'javascript:void(0)');
	closerTag.innerHTML = '<b>[x]</b>&nbsp;';
	closerTag.onclick = function(){oQuickEdit.close();};
	closerDiv.appendChild(closerTag);
	
	var h3 = document.createElement('h3');
	h3.appendChild(document.createTextNode('Quick Edit'));
	
	var saveStatus = document.createElement("span");
	saveStatus.setAttribute("id", "quickEditStatus");
	
	h3.appendChild(saveStatus);
	
	var saverDiv = document.createElement('div');
	saverDiv.style.width = '100%';
	saverDiv.style.textAlign = 'center';
	saverDiv.appendChild(document.createElement('br'));
	
	var saver = document.createElement('input');
	saver.setAttribute('type', 'button');
	saver.setAttribute('value', 'Save');
	saver.onclick = function() { oQuickEdit.save(); };
	saverDiv.appendChild(saver);
	
	div.appendChild(closerDiv);
	div.appendChild(h3);
	
	if(this.selectHtmlName)
	{
		div.innerHTML = div.innerHTML+this.selectContent;		
	}
	else
	{
		div.appendChild(inputElement);
	}
	
	div.appendChild(saverDiv);
	document.getElementsByTagName('body')[0].appendChild(div);
	
	if(this.selectSelectedValue)
	{
		$("#countryId").val(this.selectSelectedValue);
	}
	
	if (this.fckeditor) 
	{
		var oFCKeditor = new FCKeditor('quickEditContent');
		oFCKeditor.ToolbarSet = 'QuickEdit';
		oFCKeditor.BasePath = "/FCKeditor26/";
		oFCKeditor.Height = 400;
		oFCKeditor.ReplaceTextarea();
	}
}

QuickEdit.prototype.save = function()
{
	document.getElementById("quickEditStatus").innerHTML = " - <i>Saving...</i>";
	
	if(this.fckeditor)
	{
		var newContent = FCKeditorAPI.GetInstance( 'quickEditContent' ).GetHTML();
	}
	else if(this.selectHtmlName)
	{
		var newContent = $('#countryId option:selected').html();
	}
	else
	{
		var newContent = document.getElementById('quickEditContent').value;
	}
	
	var className = document.getElementById('className').value;
	var objectAttribute = document.getElementById('objectAttribute').value;
	var objectId = document.getElementById('objectId').value;
	
	if(this.strip_tags)
	{
		newContent = oUtils.strip_tags(newContent);
	}
	
	if(this.truncate)
	{
		newContent = oUtils.truncate(newContent, this.truncate);
	}
	
	if(!this.forceCurrentElement)
	{
		newContent += ' <img width="10" class="'+this.alternativeCss+'" height="10" src="/images/quickedit.gif" onclick="javascript: oQuickEdit.quickEdit(event, this, \''+className+'\', \''+objectAttribute+'\', \''+objectId+'\', \''+this.fckeditor+'\', \''+this.alternativeCss+'\', \''+this.iframe+'\', \'\', \''+this.truncate+'\', \''+this.strip_tags+'\', \''+this.selectHtmlName+'\');"/>';
	}
	
	//IE Hack
	if (oBrowser.isIE()) 
	{
		var div = document.createElement("div");
		div.setAttribute("id", "quckEditContainer");
		div.style.display = "inline";
		div.innerHTML = newContent;
		
		this.currentElement.innerHTML = "";
		this.currentElement.appendChild(div);
	}
	else
	{
		this.currentElement.innerHTML = newContent;
	}
	
	this.universalSave(className, objectAttribute, objectId);	
}

QuickEdit.prototype.universalSave = function(className, attribute, id)
{
	var oAjax = this.getNextThread();
	oAjax.url = '/quickEdit/universalSave/'+className+'/'+attribute+'/'+id;
	oAjax.method = 'post';
	if(this.fckeditor)
	{
		oAjax.data = 'content='+escape(FCKeditorAPI.GetInstance( 'quickEditContent' ).GetXHTML());
	}
	else if(this.selectHtmlName)
	{
		oAjax.data = 'content='+$("#countryId").val();
		this.selectSelectedValue = $("#countryId").val();
		$("#selectCountryContainer").html($("#selectContent").html());
	}
	else
	{
		oAjax.data = 'content='+escape(document.getElementById('quickEditContent').value);
	}
	oAjax.addRequestListener(this, this.universalSaved, false);
	oAjax.open();
}

QuickEdit.prototype.universalSaved = function(xmlDocument, text)
{
	document.getElementById("quickEditStatus").innerHTML = " - <i>Saved !</i>";
	
	this.close();
}

QuickEdit.prototype.debug = function(object)
{
	var oDebug = new Debug();
	oDebug.debugObject(object);
}

QuickEdit.prototype.activeFeatureBoxChangeOrder = function()
{
	$('#allFeaturedBoxes').sortable(
	{
		items : 		'.sortableitem',
		opacity: 		0.8,
		axis:			'y',
		revert:			true,
		cursor: 		'move',
		//cursor: 		'pointer',
		stop: function()
		{
			var dragEls = $(".sortableitem"); 
		    var els = ''; 
		    jQuery.each(dragEls, function (){ 
		        var cur_id = $(this).attr('id').split('-').shift(); 
		        els += "featuredBoxes[]="+cur_id+"&"; 
		    }); 
		    
		    $.ajax({
			   type: "POST",
			   url: "/quickEdit/featuredBox",
			   data: els,
			   success: function(msg){}
			});
		}
	});
}

QuickEdit.prototype.allContentSortable = function(divSortable)
{
	$('#'+divSortable).sortable(
	{
		items : 		'.all_content_sortable',
		opacity: 		0.8,
		axis:			'y',
		revert:			true,
		stop: function(e, ui)
		{
			var prevId = ui.item.prev().attr("id");
			var movedId = ui.item.attr("id");
			var nextId = ui.item.next().attr("id");
			
			$.ajax({
			   type: "POST",
			   url: "/quickEdit/allContentOrder",
			   data: "prevId="+prevId+"&movedId="+movedId+"&nextId="+nextId,
			   success: function(msg){
			   }
			});
		}
	});
}

QuickEdit.prototype.changeZspacePhoto = function()
{
	setTimeout("oQuickEdit.changeZspacePhotoTimeout();", 3000);
}

QuickEdit.prototype.changeZspacePhotoTimeout = function()
{
	var randImage = "/images/memberspics/"+$("#accountId").val()+"_"+$("#uploaderZspaceId").val()+".jpg";
	
	var img = new Image();
	img.src = randImage;
	img.onload = function()
	{
		$("#zspacePhotoContainer").css("width", img.width+"px");
		$("#zspacePhotoContainer").css("height", img.height+"px");
	}
	
	$("#zspacePhotoContainer").css("backgroundImage", "url("+randImage+")");
	$('#uploadStatus').html('');
	$('#zspacePhotoUpload').css('display','none');
	
	$("#uploadPhoto")[0].reset();
}

QuickEdit.prototype.changeZmagTopCover = function()
{
	setTimeout("oQuickEdit.changeZmagTopCoverTimeout();", 3000);
}

QuickEdit.prototype.changeZmagTopCoverTimeout = function()
{
	var randImage = "/images/siteSettings/"+$("#uploaderZmagTopId").val()+".jpg";
	
	var img = new Image();
	img.src = randImage;
	img.onload = function()
	{
		$("#zmagTopCoverContainer").css("width", img.width+"px");
		$("#zmagTopCoverContainer").css("height", img.height+"px");
	}
	
	$("#zmagTopCoverContainer").css("backgroundImage", "url("+randImage+")");
	$('#uploadStatus').html('');
	$('#zmagTopCover').css('display','none');
	
	$("#formZmagTopCover")[0].reset();
}

QuickEdit.prototype.changeZvideoTopCover = function()
{
	setTimeout("oQuickEdit.changeZvideoTopCoverTimeout();", 3000);
}

QuickEdit.prototype.changeZvideoTopCoverTimeout = function()
{
	var randImage = "/images/siteSettings/"+$("#uploaderZvideoTopId").val()+".jpg";
	
	var img = new Image();
	img.src = randImage;
	img.onload = function()
	{
		$("#zvideoTopCoverContainer").css("width", img.width+"px");
		$("#zvideoTopCoverContainer").css("height", img.height+"px");
	}
	
	$("#zvideoTopCoverContainer").css("backgroundImage", "url("+randImage+")");
	$('#uploadStatus').html('');
	$('#zvideoTopCover').css('display','none');
	
	$("#formZvideoTopCover")[0].reset();
}


var oQuickEdit = new QuickEdit();
