﻿function LoadChildren(parent) {

	if ( parent.childrenLoaded ) {
		// Already done, so they must really want to go there
		return true;
	}

	var contentDiv = document.createElement('div');
	contentDiv.id = parent.id+'_children';
	contentDiv.className = 'children';
	contentDiv.innerHTML = "<img src='/images/spinner.gif' alt='Loading' /> Loading ...";
	parent.parentNode.appendChild(contentDiv);

	TradesmenBids.WebGui.bid.services.bid_service.GetChildren( parent.href, loadChildren_Success, loadChildren_Failure, parent.id );

	CancelEvent(); // No need to navigate to it, because we'll fill it instead
	return false;
}

function loadChildren_Success(results, context) {

	var parent = $get(context);
	parent.childrenLoaded = true;

	var content = new Sys.StringBuilder();
	if ( results && results.length > 0 ) {

		content.append("<ul>");

		for ( var i = 0; i < results.length; i++ ) {
			var trade = results[i];
			content.append("<li");
			if ( trade.HasChildren ) {
				content.append(" class='more'");
			} else {
				content.append(" class='no_more'");
			}
			content.append("><a href='"+trade.TradeUrl+"'");
			if ( trade.HasChildren ) {
				content.append(" onmouseover='return LoadChildren(this);'");
			}
			content.append(">");
			content.append( trade.TradeName ); // HtmlEncode()'d previously
			content.append("</a></li>");
		}

		content.append("</ul>");

	}
	var contentDiv = $get(context+'_children');
	contentDiv.innerHTML = content.toString();
}

function loadChildren_Failure(result) {
	var mgr = Kazi.Logging.ExceptionManager.getInstance();
	mgr.publishException("Error loading page: "+result.get_message());
}

// Cancel an event
function CancelEvent(e) {
	if ( !e ) {
		e = window.event;
	}
	if ( e ) {
		if ( e.stopPropagation ) {
			e.stopPropagation();
		}
		e.cancelBubble = true;
		if ( e.preventDefault ) {
			e.preventDefault();
		}
		e.returnValue = false;
	}
}

if (typeof(Sys) != 'undefined') {
	Sys.Application.notifyScriptLoaded();
}
