
var modalwindowone;
var replacementlist = new Array();

Event.observe(window, 'load', onload_solata8, false);

// load initial html bar replacements
function onload_solata8() {

    modalwindowone = new Control.Modal($('modal-link-one'),{
        overlayCloseOnClick: false,
        opacity: 0.8,
        position: 'relative'
    });
	
	s8_add_to_replacement_list('pr-pageheader');
	s8_add_to_replacement_list('pr-leftmenu');
	
	s8_onload_replacements();
}

function s8_add_to_replacement_list(id) {
	replacementlist.push(id);
}

// replace all onload replacements now.
function s8_onload_replacements() {
	// get all divs with classes onload-replacement
	var replacements = $$(".onload-replacement");
	
	// with each one, move its content to the dest.
	for(var i=0;i<replacements.length;i++) {
		var srcid = replacements[i].id;
		var destid = srcid.substr(6);		// remove "onload-"
		s8_html_switch(srcid, destid);
	}
}

// replace all items set for page-replacement
function s8_page_replacements() {
	
	// faster implementation. use named list instead of $$.  $$ is really really really slow in IE6.
	replacementlist.each(function(destid){
		//var destid = destitem.id;
		//var destid = $(destitem).id;
		var srcid = "replacement-"+destid
		var odest = $(destid);
		var osrc = $(srcid);
		if(!osrc && !odest) return;	// if none of them exist, dont break the page
		if(!osrc) {
			// if there is nothing to replace it with, blank out the content and hide it.
			Element.update(odest, "");
			Element.hide(odest);
		}
		else {
			// replace the contents, and make it visible
			s8_html_switch(srcid, destid);
			Element.show(odest);	
		}

		//alert(destid.id);
		//alert(odest);alert(osrc);
	});
	return;

	// OLD 2007.04.13
	// this is the data that needs to be replaced.
	// get all divs with classes page-replaceable
	//alert(destlist);
	//alert(destlist.length);
	// with each one, move its content to the dest.
	var destlist = $$(".page-replaceable");
	for(var i=0;i<destlist.length;i++) {
		var destid = destlist[i].id;
		var srcid = "replacement-"+destid
		var odest = $(destid);
		var osrc = $(srcid);

		if(!osrc) {
			// if there is nothing to replace it with, blank out the content and hide it.
			Element.update(odest, "");
			Element.hide(odest);
		}
		else {
			// replace the contents, and make it visible
			s8_html_switch(srcid, destid);
			Element.show(odest);	
		}
	}
//alert('4');
	// leave unused items in the pr-buffer until next time. TODO remove innerhtml if not needed this may be useful though in the future for secret items
}

// add debug text to the debug element, if there is some sent out here
function s8_add_debug(json) {
	if(!json.debug) return;
	if(!$('page-debug')) return;
	h = $A(json.debug);
	h.each(function(node){
		new Insertion.Top('page-debug', '<div class="debugitem">'+node+'</div>');
	});
}

function s8_add_error(message) {
	if(!$('page-debug')) return;
	new Insertion.Top('page-debug', '<div class="erroritem"><pre>'+message+'</pre></div>');
}


// take content out of srcid, and put it in destid.
// copy content to string, delete srcid content, the place it.
function s8_html_switch(srcid, destid) {
	var osrc = $(srcid);
	var odest = $(destid);
	if(!osrc || !odest) return;
	var buf = osrc.innerHTML;
	Element.update(osrc, "");
	Element.update(odest, buf);
}

// replace contents of destid with srcid.
function s8_html_copy(srcid, destid) {
	var osrc = $(srcid);
	var odest = $(destid);
	if(!osrc || !odest) return;
	Element.update(odest, osrc.innerHTML);
}

// common function to call ajrefresh with the current url
// upd 20070414
function s8_call_ajrefresh() {
	s8_simple_callback(s8_tokenurl('ajrefresh'), 's8_modal_wait_hide', 'Please Wait, loading...');
	return false;
}
function s8_call_ajgetsearchform() {
	s8_simple_callback(s8_tokenurl('ajgetsearchform'), 's8_modal_wait_hide', 'Please Wait, loading...');
	return false;
}
function s8_call_ajpostsearchform(formname) {
	s8_simple_callbackform(s8_tokenurl('ajpostsearchform'), formname, 's8_after_ajpostsearchform', 'Searching, Pleae Wait...');
	return false;
}
function s8_after_ajpostsearchform() {
	// if searchcomplete, show the listbrowse.
	// otherwise we're done.
	if(requestbucket["a"].json.searchcomplete) {
		return s8_call_ajlistbrowse();
	}
	else {
		return s8_modal_wait_hide();
	}
}

// upd 20070414
function s8_call_ajlistbrowse() {
	s8_simple_callback(s8_tokenurl('ajlistbrowse'), 's8_modal_wait_hide', 'Loading Results...');
	return false;
}

// upd 20070414
function s8_call_ajlistbrowse_changepage(newrow) {
	s8_simple_callbackparams(s8_tokenurl('ajlistbrowse'), 'rown='+newrow, 's8_modal_wait_hide', 'Loading Results...');
	return false;
}

// replaces url_s8_ajtoken's url (ajtoken) 
function s8_tokenurl(ajfuncname) {
	newurl = url_s8_ajtoken;
	newurl = newurl.replace('ajtoken', ajfuncname);
	return newurl;	
}

// call item focus, then if successful redirect to the url spefified in ajitemdocuredirect
// upd 2007.04.14
function s8_call_ajfocusredirect_key(gkey) {
	s8_simple_callbackparams(s8_tokenurl('ajitemfocus'), 'gkey='+gkey, 's8_after_ajfocusredirect', 'Preparing Item, please wait...');
	return false;
}
function s8_call_ajfocusredirect_pos(gpos) {
	s8_simple_callbackparams(s8_tokenurl('ajitemfocus'), 'gpos='+gpos, 's8_after_ajfocusredirect', 'Preparing Item, please wait...');
	return false;
}
function s8_after_ajfocusredirect() {
	// if itemloaded, redirect out.
	// otherwise we're done. (error!) TODO
	if(requestbucket["a"].json.loadcomplete) {
		window.location = url_s8_ajitemfocusredirect;
		return false;
	}
	else {
		return s8_modal_wait_hide();
	}
}

function s8_call_ajfocusrefresh_pos(gpos) {
	s8_simple_callbackparams(s8_tokenurl('ajitemfocus'), 'gpos='+gpos, 's8_after_ajfocusrefresh', 'Preparing Item, please wait...');
	return false;
}
function s8_after_ajfocusrefresh() {
	// if itemloaded, refresh the UI
	// otherwise we're done. (error!) TODO
	if(requestbucket["a"].json.loadcomplete) {
		s8_call_ajrefresh();
		return false;
	}
	else {
		return s8_modal_wait_hide();
	}
}

function s8_call_ajposteditform(formname) {
	s8_simple_callbackform(s8_tokenurl('ajposteditform'), formname, 's8_after_ajposteditform', 'Saving, Please Wait...');
	return false;
}
function s8_after_ajposteditform() {
	// if savecomplete, show somewhere else
	// otherwise we're done.
	if(requestbucket["a"].json.savecomplete) {
		return s8_simple_callback(s8_tokenurl('ajposteditformcomplete'), 's8_modal_wait_hide', 'Loading Page...');
	}
	else {
		return s8_modal_wait_hide();
	}
}

function s8_call_aj(ajfunc, message) {
	desturl = url_s8_ajcall.replace("@@ajcall@@", ajfunc);
	s8_simple_callback(desturl, 's8_modal_wait_hide', message);
	return false;
}
function s8_call_ajparams(ajfunc, params, message) {
	desturl = url_s8_ajcall.replace("@@ajcall@@", ajfunc);
	s8_simple_callbackparams(desturl, params, 's8_modal_wait_hide', message);
	return false;
}
function s8_call_ajparamsform(ajfunc, params, formname, message) {
	desturl = url_s8_ajcall.replace("@@ajcall@@", ajfunc);

	s8_modal_wait_show(message);
	
	requestbucket["a"] = {
		url: desturl,
		parameters: params,
		addform: formname,
		callbackfunction: 's8_modal_wait_hide',
		callbackstate: '0'
	};
	call_bucket("a");

	return false;
}
function s8_call_ajform(ajfunc, formname, message) {
	desturl = url_s8_ajcall.replace("@@ajcall@@", ajfunc);

	s8_modal_wait_show(message);
	
	requestbucket["a"] = {
		url: desturl,
		addform: formname,
		callbackfunction: 's8_modal_wait_hide',
		callbackstate: '0'
	};
	call_bucket("a");

	return false;
}


function s8_ri(o) {
	Element.addClassName(o, 'thover');
	//dojo.html.prependClass(o, 'thover');
}
function s8_ro(o) {
	Element.removeClassName(o, 'thover');
	//dojo.html.removeClass(o, 'thover');
}


var s8modalwaitvisible = false;

/**
 * dialog wait message / popup handler
 * pop the dialog if it is not showing. if it is showing, update the message.
 *
 *
 */
function s8_modal_wait_show(messagehtml) {
	modalwindowone.open();
	modalwindowone.update(messagehtml);
	return false;	
	
	// OLDcode 2007.04.13
	if(!s8modalwaitvisible) {	
		s8modalwaitvisible = true;
		// NOTE in px. TODO calculate approx ems to handle screen resizing
		Dialog.info(messagehtml, {windowParameters: {className: "alert", opacity: 1.00, top: 5, left: 525, width:250, height:26, showEffect: Effect.Appear, hideEffect: Element.hide}, showProgress: true});
	}
	else {
		Dialog.setInfoMessage(messagehtml);
	}
}

// closees the popup window
function s8_modal_wait_hide() {
	modalwindowone.close();
	return false;	
	
	// OLDcode 2007.04.13
	Dialog.closeInfo();
	s8modalwaitvisible = false;
	return false;
}

/**
 * calls the url provided, in bucket a, with the return callback function and message
 *
 */
function s8_token_callback(tokenname, callbackfunction, message) {
	url = s8_tokenurl(tokenname);
	s8_modal_wait_show(message);
	requestbucket["a"] = {
		url: url,
		callbackfunction: callbackfunction,
		callbackstate: '0'
	};
	call_bucket("a");
}
function s8_token_callbackform(tokenname, formname, callbackfunction, message) {
	url = s8_tokenurl(tokenname);
	return s8_simple_callbackform(url, formname, callbackfunction, message);
}
function s8_simple_callback(url, callbackfunction, message) {
	s8_modal_wait_show(message);
	requestbucket["a"] = {
		url: url,
		callbackfunction: callbackfunction,
		callbackstate: '0'
	};
	call_bucket("a");
}
function s8_simple_callbackform(url, formname, callbackfunction, message) {
	s8_modal_wait_show(message);
	
	requestbucket["a"] = {
		url: url,
		addform: formname,
		callbackfunction: callbackfunction,
		callbackstate: '0'
	};
	call_bucket("a");
	return false;
}
function s8_simple_callbackparams(url, params, callbackfunction, message) {
	s8_modal_wait_show(message);
	
	requestbucket["a"] = {
		url: url,
		parameters: params,
		callbackfunction: callbackfunction,
		callbackstate: '0'
	};
	call_bucket("a");
}


