/**************************************************************************
Script:	jPart.queue.js
Author:	SharejPoint.com
Purpose:  Displays a queue like auto refreshing list
Dependencies:
	jPointLoader.js has to be on the same page or form editor webpart
*************************************************************************/
//Add include extension
//Include CSS and JS
jP.includePath = '/src/';
jP.include(['jquery-ui-1.7.1.redmond/css/redmond/jquery-ui-1.7.1.redmond.css', 
	'jpoint/plugins/queue/Queue.css'
]);
jP.include([
	'jpoint/plugins/queue/Queue.js'
	,'jpoint/plugins/queue/Queue_Data.js'
	,'jpoint/plugins/queue/Queue_Style.js'
	,'jquery/jquery-ui/jquery-ui.js'
	,'jquery/plugins/jquery.contextMenu.js'
]);

myjPart.ShowConfigDefaults = [	
["SiteURL","Absolute path to list site location",""]
,["ListName","List name hosting chat","jPartChatBoxList"]
,["ViewName","View to use (blank to use default)",""]
,["CAML","Custom CAML", null]
,["MaxDisplay","Number of lines to display","20"]
,["Refresh","Refresh interval in seconds","5"]
,["FieldName","Field name to store message (default is LinkTitle because default list view has hover edit menu)","LinkTitle"]
,["Image", "Queue Row Image", ""]
,["Dragable","Popup dialog title","false"]
];

myjPart.setjPartOptions({Plugin:{Name:'Queue', Version:'2.0', Origin:'SharejPoint', Description:'Allows sharepoint administrator display contents of a SharePoint list in a queue', Developer:'Samir Sijercic'}});
//Start Webpart logic code ----------------------------------- 
//Supplement functions
function addQueue(QueueOptions, oldQueue)
{
	try
	{
		var newQueue = oldQueue || new Queue(QueueOptions);
		newQueue.QueueOptions = QueueOptions;
		
		var queueData = new Queue_Data({
		Debug: QueueOptions.Debug
	    ,LogLevel: QueueOptions.LogLevel
		});
		var testJSONObj;

		if(QueueOptions.Qtype == "jpoint")
		{
			testJSONObj= queueData.getjPointData(QueueOptions.SiteURL, QueueOptions.ListName, QueueOptions.ViewName, QueueOptions.CAML, QueueOptions.MaxDisplay, QueueOptions.ViewFields);
			var queueStyle = new Queue_Style({
			Debug: QueueOptions.Debug
		    ,LogLevel: QueueOptions.LogLevel
		    ,RowClass: QueueOptions.RowClass || ''
			});

			formattedData = queueStyle.formatData(testJSONObj, QueueOptions.Columns, QueueOptions.Draggable);
			testJSONObj = null;
		}
		else
			formattedData = queueData.getMLData(QueueOptions.MLURI, QueueOptions.MLXQYURI, QueueOptions.MLParams);

		newQueue.setData(formattedData);
		newQueue.renderDivs();
		//Apply format to jpoint queue
		if(QueueOptions.Qtype == "jpoint")
			queueStyle.formatjpointQueue(QueueOptions.DivID, QueueOptions.DivSelector, QueueOptions.SiteURL, QueueOptions.ListName, QueueOptions.FieldName, QueueOptions.Draggable, QueueOptions.Image);
		
		if(oldQueue == null)
			this.Queues.push(newQueue);
	}
	catch(err)
	{
		alert("addQueue() Error:" + err);
	}

}
//Refreshes all queues stored in the Page Driver
function refreshQueues()
{
	window.status = "Loading...";
	for(var queue=0; queue< PageDriver.Queues.length; queue++)
	{
		$('#' + PageDriver.Queues[queue].QueueOptions.DivID).empty();
		PageDriver.addQueue(PageDriver.Queues[queue].QueueOptions, PageDriver.Queues[queue]);	//.refresh()
	}
	window.status = "Finished";
}
function startRefreshQueues(bStartQueue, refreshQueueInterval)
{
	if(bStartQueue != null && bStartQueue && !DraggingQueueItem)
		PageDriver.refreshQueues();
	if(refreshQueueInterval != null && refreshQueueInterval > 0)
		setTimeout(function(){PageDriver.startRefreshQueues(true, refreshQueueInterval)},refreshQueueInterval);
}
//Main function
var PageDriver = null;
var DraggingQueueItem = false;

$(document).ready(function() { //Wait page to load and then apply webpart logic
	if(myjParts[JpartProcessing] != null)
	{
		var thisWebpart = myjParts[JpartProcessing];
		try
		{
			//jPart user code goes here ---------------------------
			PageDriver = this;
			this.Queues = new Array();
			//Methods
			this.addQueue=addQueue;
			this.refreshQueues=refreshQueues;
			this.startRefreshQueues=startRefreshQueues;
			var thisWebpart = myjWebparts['Queue'];
			//Appending queue to results div
			//thisWebpart.MainDiv.append("<p>test</p>");
			if(thisWebpart.Options.Local != null)
			{
				//Init Pending Open
				var QueueOptions = {
			    DivID:thisWebpart.MainDivID
			    ,TableClass:'tablesorter'
			    ,Qtype:'jpoint'
			    ,RowClass: 'FeedbackRow'
			    ,DivSelector: '> .FeedbackRow'
			    ,Columns:[{name:"ID", css:"FeedID"},{name:"Comments", css:"FeedbackTitle"}]
			    ,ViewFields:[{title:"ID", internalName:"ID"},{title:"Comments", internalName:"Comments"}]
			    };
				
				$.each(thisWebpart.Options.Local, function (idx, item) {
					QueueOptions[idx] = item;
					
				});
				PageDriver.addQueue(QueueOptions);
				var RefreshQueueInterval = parseInt((QueueOptions.Refresh || "0")) * 1000;
				//Do not allow queue refresh in edit mode
				if($("span:contains('Add a Web Part')").length == 0)
					PageDriver.startRefreshQueues(false, RefreshQueueInterval);
			}
			//-----------------------------------------------------
		}catch(err){};
		JpartProcessing = JpartProcessing + 1;
	}
});
//End Webpart logic code -----------------------------------