YAHOO.namespace("masi.module");
YAHOO.masi.module.Module = function(unique_id) {
	YAHOO.masi.util.base.call(this,unique_id);
	// internal status variables
	this._loaded = false;
	this._complete = false;
	this._inprocess = false;
	this._preloading = false;

	// config options
	this.preload_flag = false;
	this.no_cache_flag = false;
	this.required_steps = false;
	this.status = 'incomplete';
}
YAHOO.masi.module.Module.prototype = new YAHOO.masi.util.base();
YAHOO.masi.module.Module.prototype.constructor = YAHOO.masi.module.Module;
YAHOO.masi.module.Module.prototype.set_config = function(config) {
	if ( config.panel_config ) {
		this.Panel = new YAHOO.widget.Panel("module_id_"+this.unique_id, config.panel_config);
	} else {
		this.Panel = new YAHOO.widget.Module("module_id_"+this.unique_id);
	}

	// required configurations
	this.source_url = config.source_url;
	this.parameters = config.parameters;
	this.display_name = config.display_name;

	// optional configurations
	this.step_label = config.step_label;
	this.step_label_div = config.step_label_div;
	this.file_upload = config.file_upload;

	this.step_type = config.step_type;
	this.page_number = config.page_number;
	this.form_name = config.form_name;
	this.target_div = config.target_div;
	this.static_html = config.static_html;
	this.preload_flag = config.preload;
	if ( this.preload_flag && this.preload_flag != '' && this.preload_flag != '0' ) {
		this.preload_flag = true;
	} else {
		this.preload_flag = false;
	}
	this.no_cache_flag = config.no_cache;
	if ( this.no_cache_flag && this.no_cache_flag != '' && this.no_cache_flag != '0' ) {
		this.no_cache_flag = true;
	} else {
		this.no_cache_flag = false;
	}
	this.required_steps = config.required_steps;
	this.status = config.status;
}
YAHOO.masi.module.Module.prototype.display = function() {
	if ( this._inprocess ) {
		return;
	}
	if ( ! this._loaded ) {
		this.load_content();
		return;
	}

	if ( this.module_render_div ) {
		this.Panel.render(document.getElementById(this.module_render_div));
	} else {
		this.Panel.render(document.body);
	}

	if ( YAHOO.masi.calendar ) {
		// check for date fields that need a calendar
		YAHOO.masi.calendar.setup_calendars();
	}

	this.Panel.show();

	if ( this.step_label_div && document.getElementById(this.step_label_div) ) {
		document.getElementById(this.step_label_div).innerHTML = this.step_label;
	}

	if ( ! this.crumbcontroller.current_page_number ) {
		this.crumbcontroller.current_page_number = this.page_number;
	}
	if ( this.page_number != this.crumbcontroller.current_page_number ) {
		this.crumbcontroller.current_page_number = this.page_number;
		this.redisplay_preview();
	}
}
YAHOO.masi.module.Module.prototype.preload = function() {
	this._preloading = true;
	this.load_content();
}
YAHOO.masi.module.Module.prototype.handle_preview = function(content) {
	document.getElementById(this.target_div).innerHTML = content;
}
YAHOO.masi.module.Module.prototype.redisplay_preview = function() {
	// startup check for completion
	var config = {
		unique_id: this.unique_id,
		url: this.source_url,
		param_string: 'func=display_clickable_work_area&spread=' + this.page_number,
		callback_method: 'handle_preview'
	};
	YAHOO.masi.util.request_passthrough(config);
}
YAHOO.masi.module.Module.prototype.load_content = function() {
	if ( this._inprocess ) {
		return;
	}
	this._inprocess = true;
	if ( ! this._preloading ) {
		this.crumbcontroller.crumb_change_name(this.unique_id,'Loading...');
		document.body.style.cursor = "progress";
	}

	// startup check for completion
	var config = {
		unique_id: this.unique_id,
		url: this.source_url,
		param_string: this.parameters,
		callback_method: 'handle_content'
	};
	if ( this._preloading ) {
		config.param_string += '&preloading=1';
	}

	if ( this.static_html ) {
		config.request_method = 'GET';
		YAHOO.masi.util.request_passthrough(config);
	} else {
		YAHOO.masi.util.request_passthrough(config);
	}
	return;
}
YAHOO.masi.module.Module.prototype.handle_content = function(content) {
	this.Panel.setBody(content);

	this._loaded = true;
	this._inprocess = false;

	if ( this._preloading ) {
		this.Panel.hide();
		if ( this.module_render_div ) {
			this.Panel.render(document.getElementById(this.module_render_div));
		} else {
			this.Panel.render(document.body);
		}

		if ( YAHOO.masi.calendar ) {
			// check for date fields that need a calendar
			YAHOO.masi.calendar.setup_calendars();
		}

		this._preloading = false;
		return;
	}
	if ( YAHOO.masi.calendar ) {
		// check for date fields that need a calendar
		YAHOO.masi.calendar.setup_calendars();
	}

	this.crumbcontroller.crumb_change_name(this.unique_id,this.display_name);
	document.body.style.cursor = "default";
	this.display();

	if ( this.no_cache_flag ) {
		this._loaded = false;
	}
}
YAHOO.masi.module.Module.prototype.hide = function() {
	this.Panel.hide();
}
YAHOO.masi.module.Module.prototype.destroy = function() {
	// remove it from the DOM as well
	this.Panel.destroy();
	YAHOO.masi.util.delete_object(this.unique_id);
}
YAHOO.masi.module.Module.prototype.submit = function() {
	if ( this._inprocess ) {
		return false;
	}
	this.crumbcontroller.crumb_change_name(this.unique_id,'Processing...');
	document.body.style.cursor = "progress";
	this._inprocess = true;

	//param_string: this.parameters,
	var config = {
		unique_id: this.unique_id,
		url: this.source_url,
		form_name: this.form_name,
		callback_method: 'handle_submit'
	};
	if ( this.file_upload && this.file_upload != '0' ) {
		config.file_upload = 1;
	}

	YAHOO.masi.util.request_passthrough(config);
	return false;
}
YAHOO.masi.module.Module.prototype.handle_submit = function(content) {
	var new_content = String(content);
	var regex = /__masi_analytics__/;
	var regex_result = regex.exec(new_content);
	if ( regex_result ) {
		var content_array = new_content.split('__masi_analytics__');
		var pageTracker = YAHOO.masi.util.get_object('pagetracker');
		if ( pageTracker ) {
			try {
				pageTracker._trackPageview(content_array[0]);
			} catch(e) {
				//alert('failed to use pageTracker');
			}
		} else {
			//alert('failed to get pageTracker');
		}
		document.getElementById(this.target_div).innerHTML = content_array[1];
	} else {
		document.getElementById(this.target_div).innerHTML = new_content;
	}
	this.status = 'complete';
	this._inprocess = false;
	this._complete = true;
	this.crumbcontroller.check_required_steps();
	this.crumbcontroller.crumb_change_name(this.unique_id,this.display_name);
	document.body.style.cursor = "default";
	this.hide();
}
// accepts the list of completed step numbers and may update our status
YAHOO.masi.module.Module.prototype.set_status_on_completes = function(completed_array) {
	if ( ! this.required_steps || this.status == 'complete' ) {
		// do nothing if we don't have any required steps, or of we're already complete
		return;
	}
	var ready_obj = {};
	var required_steps = this.required_steps.split(',');
	for(var j=0;j<required_steps.length;j++){
		var required_step = required_steps[j];
		var tmp_obj = YAHOO.masi.util.get_object( required_step );
		if ( tmp_obj ) { // only check completed status on steps that exist in task list
			eval('ready_obj.id_' + required_step + ' = false;');
			for(var i=0;i<completed_array.length;i++){
				var step_number = completed_array[i];
				if ( step_number == required_step ) {
					eval('ready_obj.id_' + required_step + ' = true;');
				}
			}
		}
	}
	var ready = true;
	for ( var attribute in ready_obj ) {
		eval('var check = ready_obj.' + attribute);
		if ( check == false ) {
			ready = false;
			break;
		}
	}
	if ( ready ) {
		this.status = 'ready';
	} else {
		this.status = 'inactive';
	}
	return ready;
}
YAHOO.masi.module.Module.prototype.select_photo_category = function(id,level) {
	var el = document.getElementById(id);
	var selected_id = el[ el.selectedIndex ].value;

	var config = {
		unique_id: this.unique_id,
		url: this.source_url,
		param_string: 'func=display_photo&base_id=' + selected_id + '&level=' + level + '&step_number=' + this.unique_id,
		callback_method: 'handle_content'
	};
	YAHOO.masi.util.request_passthrough(config);
}


