//
//@author Vladimir Loshchin <screw@gorodok.net>

// This function create new hidden div for order 
// additional information
function createAdditionalInfoElement(divElement) 
{
	var fullInfoDiv = divElement.cloneNode(true);
	fullInfoDiv.setAttribute('id', null);
	divElement.parentNode.appendChild(fullInfoDiv);
	$(fullInfoDiv).css('display', 'none');
}

function showHideDetailInformation(p_orderID) {
	var orderContainer = $('#order_'+p_orderID);
	var infoContainer = $('div.additional_info', orderContainer);
	
	if (infoContainer.css('display') == 'none') {
		infoContainer.show().next().hide();
	} else {
		infoContainer.hide().next().show();
	}
	
}

function onOrderRemove(p_data)
{
	var jqErrorList = $('errorList', p_data);
	
	if (jqErrorList.length) {
		alert($('error', jqErrorList).attr('message'));
		return;
	}
	
	var jqOrder = $('order', p_data);
	
	if (jqOrder.length) {
		$('#order_'+jqOrder.attr('orderID')).remove();
		return;	
	}
	
	alert('Controller returned unknown type of response! Please notify support!');
}

// This function remove order action
function rmOrder(p_orderID) {
	if (confirm("Are you sure?")) {
		$.post('/orders/'+p_orderID+'/remove/', {}, onOrderRemove);
	}
}

function showHideOrderStatusSwitcher(p_orderID)
{
	var orderContainer = $('#order_'+p_orderID);
	var statusSwitcher = $('ul.orderStatusSwitcher', orderContainer);
	
	if (statusSwitcher.css('display') == 'none') {
		statusSwitcher.show();
	} 
	else {
		statusSwitcher.hide();
	}
}

function activateOrderCallback(data) 
{
	var errors = $('error', data);
	
	if (errors.length) 
	{
		alert(errors.attr('message'));
		return;
	}
	
	var order = $('order', data);
	
	if (!order.length) {
		alert('Controll returned unexpected document type. Please email the support.')
		return;
	}
	
	var orderID = order.attr('orderID');
	
	var orderContainer = $('#order_'+orderID);
	var currentStatusContainer = $('a.currentStatus', orderContainer);
	
	var statusSwitcher = $('ul.orderStatusSwitcher', orderContainer);
	
	$('li', statusSwitcher).show();
	
	var activeItem = $('li.active', statusSwitcher);
	var activeText = activeItem.text();
	activeItem.hide();
		 
	currentStatusContainer.text(activeText);
	statusSwitcher.hide();
}

function activateOrder(p_orderID)
{
	$.post('/orders/'+p_orderID+'/activate/', {}, activateOrderCallback);
}

function deactivateOrderCallback(data) 
{
	var errors = $('error', data);
	
	if (errors.length) 
	{
		alert(errors.attr('message'));
		return;
	}
	
	var order = $('order', data);
	
	if (!order.length) {
		alert('Controll returned unexpected document type. Please email the support.')
		return;
	}
	
	var orderID = order.attr('orderID');
	
	var orderContainer = $('#order_'+orderID);
	var currentStatusContainer = $('a.currentStatus', orderContainer);
	
	var statusSwitcher = $('ul.orderStatusSwitcher', orderContainer);
	
	$('li', statusSwitcher).show();
	
	var inactiveItem = $('li.inactive', statusSwitcher);
	var inactiveText = inactiveItem.text();
	inactiveItem.hide();
	
	currentStatusContainer.text(inactiveText);
	statusSwitcher.hide();
}

function deactivateOrder(p_orderID)
{
	$.post('/orders/'+p_orderID+'/deactivate/', {}, deactivateOrderCallback);
}


function rejectOrderCallback(data)
{
	var errors = $('error', data);
	
	if (errors.length) 
	{
		alert(errors.attr('message'));
		return;
	}
	
	var order = $('order', data);
	
	if (!order.length) {
		alert('Controll returned unexpected document type. Please email the support.')
		return;
	}
	
	var orderID = order.attr('orderID');
	
	var orderContainer = $('#order_'+orderID);
	var currentStatusContainer = $('a.currentStatus', orderContainer);
	
	var statusSwitcher = $('ul.orderStatusSwitcher', orderContainer);
	
	$('li', statusSwitcher).show();
	
	var rejectItem = $('li.reject', statusSwitcher);
	var rejectText = rejectItem.text();
	rejectItem.hide();
	 
	currentStatusContainer.text(rejectText);
	statusSwitcher.hide();
}

function rejectOrder(p_orderID)
{
	$.post('/orders/'+p_orderID+'/reject/', {}, rejectOrderCallback);
}

function closeOrderCallback(data)
{
	var errors = $('error', data);
	
	if (errors.length) 
	{
		alert(errors.attr('message'));
		return;
	}
	
	var order = $('order', data);
	
	if (!order.length) {
		alert('Controll returned unexpected document type. Please email the support.')
		return;
	}
	
	var orderID = order.attr('orderID');
	
	var orderContainer = $('#order_'+orderID);
	var currentStatusContainer = $('a.currentStatus', orderContainer);
	
	var statusSwitcher = $('ul.orderStatusSwitcher', orderContainer);
	
	$('li', statusSwitcher).show();
	
	var closeItem = $('li.close', statusSwitcher);
	var closeText = closeItem.text();
	closeItem.hide();
	 
	currentStatusContainer.text(closeText);
	statusSwitcher.hide();
}

function closeOrder(p_orderID)
{
	$.post('/orders/'+p_orderID+'/close/', {}, closeOrderCallback);
}

function addFavouriteCallback(data)
{
	var errors = $('error', data);
	
	if (errors.length) 
	{
		alert(errors.attr('message'));
		return;
	}
	else {
		var order = $('order', data);
		var orderID = order.attr('orderID');
		
		var orderContainer = $('#order_'+orderID);
		var orderMenu = $('ul.orderMenu', orderContainer);
		$('li.favourite', orderMenu).hide().prev().css('border', 'none');
	}
}

function addFavourite(p_orderID) {
	$.post('/orders/'+p_orderID+'/addfavourite/', {order_id: p_orderID}, addFavouriteCallback);
}
						
$(document).ready(function(){
	$("div.additional_info").each(function (index) 
	{
		if (!this.childNodes.length) {
			return;
		}
									
		if (this.childNodes.length == 1 && this.childNodes[0].nodeType == 3) 
		{
			text = this.childNodes[0].nodeValue;
			// Searching end line symbols in string
			var endlIndex = text.indexOf('\n');
									
			if (endlIndex > 0) 
			{
				text = text.substr(0, endlIndex);
				createAdditionalInfoElement(this);
											
				$(this).text(text);

				$(this).parent().prev('td').children(':first-child').show();
			} 
			else 
			{
				$(this).text(text);
			}
		} 
		else 
		{
			var text = $(this).children(':first-child').text();
			if ($(this).children('p').size() > 1) 
			{
				createAdditionalInfoElement(this);
				$(this).parent().prev('td').children(':first-child').show();	
			}
			$(this).text(text);
		}
	});
});

function onLoadManagerList(data)
{
	var errors = $('error', data);
	
	if (errors.length) 
	{
		alert(errors.attr('message'));
		return;
	}
	
	var userList = $('user', data);
	
	userList.each(function (index, domElement) {
		$('#managerList').append('<li><a id="manager_'+domElement.getAttribute('userID')+'">'+domElement.getAttribute('alias')+'</a></li>');
	});
	
	initManagerListHref();
}

function onSetManager(data) 
{
	var errors = $('error', data);
	
	if (errors.length) 
	{
		alert(errors.attr('message'));
		return;
	}
	
}

function setManager(p_orderID, p_managerID) {
	$.post('/orders/'+p_orderID+'/setmanager/', {manager_id: p_managerID}, onSetManager);
}

function initManagerListHref() 
{
	var managerList = $('#managerList');
	var orderDiv = managerList.parent().parent().parent().parent().parent().parent().parent().parent().parent();
	var orderID = orderDiv.attr('id').substr(6);
	
	
	$('a', managerList).each(function (index, domElement) {
		var managerID = domElement.getAttribute('id').substr(8);
		domElement.setAttribute('href', 'javascript:setManager('+orderID+', '+managerID+');');
	});
}

function showHideManagerList(p_orderID)
{
	var orderContainer = $('#order_'+p_orderID);
	var managerList = $('#managerList');
	
	var currentManagerContainer = $('a.currentManager', orderContainer);
	currentManagerContainer.parent().append(managerList);
	
	if (!$('li' , managerList).length) {
		$.get('/user/managerlist/', {}, onLoadManagerList);
	}
	
	managerList = $('#managerList');
	var managersLinks = $('a', managerList[0]);
	
	managersLinks.each(function (index, domElement) {
		var managerID = domElement.getAttribute('id').substr(8);
		domElement.setAttribute('href', 'javascript:setManager('+p_orderID+', '+managerID+');');
	});
}

function onLoadPutOrderOnFrontPageResponse(p_data) {
	var jqOrder = $('order', p_data);
	var iOrderID = jqOrder.attr('orderID');
	
	var jqOrderContainer = $('#order_'+iOrderID);
	$('a.putOnFrontPageButton', jqOrderContainer).hide();
	$('a.rmFromFrontPageButton', jqOrderContainer).show();
}

function putOrderOnFrontPage(p_orderID) {
	$.post('/orders/'+p_orderID+'/onfrontpage/', {}, onLoadPutOrderOnFrontPageResponse);
}

function onLoadRmOrderFromFrontPageResponse(p_data) {
	var jqOrder = $('order', p_data);
	var iOrderID = jqOrder.attr('orderID');
	
	var jqOrderContainer = $('#order_'+iOrderID);
	$('a.putOnFrontPageButton', jqOrderContainer).show();
	$('a.rmFromFrontPageButton', jqOrderContainer).hide();
}

function rmOrderFromFrontPage(p_orderID) {
	$.post('/orders/'+p_orderID+'/rmfromfrontpage/', {}, onLoadRmOrderFromFrontPageResponse);
}
