toolbar_ready=false;
target_window=null;

function return_toolbar_ready(){
	return toolbar_ready;
}

function button_over(caller_obj){
	if(caller_obj.src){
		button_image_cache[caller_obj.id]['default'].src=caller_obj.src;
		caller_obj.src=button_image_cache[caller_obj.id]['on'].src;
	}
}

function button_out(caller_obj){
	if(caller_obj.src){
		caller_obj.src=button_image_cache[caller_obj.id]['default'].src;
	}
}

function button_actions(caller_object, action_type, action){
	var extra_args="";

	if(window.event!=null) window.event.cancelBubble=true;

	if(action_type=="window"){
		if(action=="Keyword Editor") {
			extra_args="&section="+target_window.return_section();
		}

		if(screen_help_array[0]=="disabled") var help_enabled=0;
		else var help_enabled=1;

		window.open("/res/toolbar/elements/dialog_frameset.php?dialog_name="+action+extra_args, "ToolbarDialog", "width="+default_dialog_width+", height="+default_dialog_height+", left="+((screen.availWidth/2)-(default_dialog_width/2))+", top="+((screen.availHeight/2)-(default_dialog_height/2)));
	} else if(action_type=="file"){
		if(action=="revert_live"){
			if(confirm('This will undo any changes you have made since your last save and show you the currently live version.')){
				target_window.document.location.href=target_window.document.location.href;
			}
		}
	}
	update_buttons();
}

function update_buttons(){
	for(var i=0;i<button_action_array.length;i+=2){
		// Disable revert button if the disable_revert_button() function has been called on the page
		if(button_action_array[i+1]=='revert_live'){
			if(target_window.return_revert_disabled()){
				disable_button(document.getElementById(button_action_array[i]));
			} else {
				enable_button(document.getElementById(button_action_array[i]));
			}

		// Disabling keyword editor if no section is set
		} else if (button_action_array[i]=='search_engine_keywords') {

			if(!target_window.return_section()){
				disable_button(document.getElementById(button_action_array[i]));
			} else {
				enable_button(document.getElementById(button_action_array[i]));
			}

		// Else button is enabled
		} else {
			// See above
			if(document.getElementById(button_action_array[i])){
				enable_button(document.getElementById(button_action_array[i]));
			} else {
				eval(button_action_array[i]+'_disabled=false');
			}
		}
	}

}

function disable_button(button){
	button.style.visibility='hidden';
	button.disabled=true;
}

function enable_button(button){
	if(button.src==button_image_cache[button.id]['on'].src){
		button.src=button_image_cache[button.id]['default'].src;
	}
	button.style.visibility='visible';
	button.disabled=false;
}

function disable_toolbar(){
	target_window=null;
	for(var i=0;i<button_action_array.length;i+=2){
		if(document.getElementById(button_action_array[i])){
			disable_button(document.getElementById(button_action_array[i]));
		}
	}
}

function key_handler(){
	if(window.event.keyCode==46 || window.event.keyCode==8){
		target_window.document.execCommand("delete");
	}
}

function preload_all_images(){
	button_image_cache=new Array();

	for(i=0;i<document.images.length;i++){
		preload_single_image(document.images[i].id);
	}

	for(i=0;i<button_action_array.length;i+=2){
		if(typeof(button_image_cache[button_action_array[i]])=="undefined"){
			preload_single_image(button_action_array[i]);
		}
	}
}

function preload_single_image(image_id){
	button_image_cache[image_id]=new Array();
	button_image_cache[image_id]['default']=new Image();
	button_image_cache[image_id]['on']=new Image();
	button_image_cache[image_id]['on'].src=toolbar_files_path+'images/'+image_id+'_on.gif';
}

function set_target_window(caller_obj){
	target_window=caller_obj;
}

function initialise_toolbar(leave_disabled){
	if(!toolbar_ready){
		preload_all_images();
		document.body.onkeydown=key_handler;
	}

	if(leave_disabled){
		disable_toolbar();
	} else {
		update_buttons();
		target_window.set_toolbar_ready(true);
	}
	toolbar_ready=true;
}

