// $Id: progress.js 4508 2006-09-21 11:43:48Z slip $
/*
 * <pre>
 * Copyright (c) 2006 by SouthProjects
 * http://www.southprojects.com
 * Santiago, Chile,
 * Version 1.2 Dec 2006.
 * All rights reserved.
 * </pre>
 */

/**
 * southprojects modal object
 */
southprojects.Modal = function (objArgs) {
	if(arguments.length == 0){
		objArgs = {};
	}

	southprojects.Modal.SUPERconstructor.call(this, objArgs);
}

southprojects.Modal.id = "southprojects.Progress";

// Inherit SuperClass
southprojects.inherit(southprojects.Modal, southprojects.Widget);

southprojects.Modal.prototype.init = function(objArgs){
	this.config.zIndex = 1000;
	this.config.x = null;
	this.config.y = null;
	this.config.width = null;
	this.config.height = null;
	this.config.container = null;
	this.config.scroll = true;

	// processing Widget functionality
	southprojects.Modal.SUPERclass.init.call(this, objArgs);
};

southprojects.Modal.prototype.create = function (zIndex) {
	zIndex = zIndex || this.config.zIndex;

	this.WCH = southprojects.Utils.createWCH();

	this.container = southprojects.Utils.createElement("div", document.body);
	var br = {};

	if(this.config.x != null && this.config.y != null){
		br.x = this.config.x;
		br.y = this.config.y;
	} else {
		br.y = southprojects.Utils.getPageScrollY();
		br.x = southprojects.Utils.getPageScrollX();
	}
	var st = this.container.style;
	st.dispaly = "none";
	st.position = "absolute";
	st.top = br.y + "px";
	st.left = br.x + "px";
	var dim = southprojects.Utils.getWindowSize();

	if(this.config.width != null){
		dim.width = this.config.width;
	}

	if(this.config.height != null){
		dim.height = this.config.height;
	}

	st.width = dim.width + "px";
	st.height = dim.height + "px";

	st.zIndex = zIndex;

	this.container.className = this.getClassName({prefix: "zpModal" + (southprojects.is_opera ? "Opera" : "")})

	if(this.config.scroll == true){
		southprojects.ScrollWithWindow.stickiness = 1;
		southprojects.ScrollWithWindow.register(this.container);
		if (this.WCH){
			this.WCH.style.zIndex = zIndex - 1;
			southprojects.ScrollWithWindow.register(this.WCH);
		}
	}
};

southprojects.Modal.prototype.show = function (zIndex) {
	if (!this.container) {
		this.create(zIndex);
	} else {
		zIndex = zIndex || this.config.zIndex;
		this.container.style.zIndex = zIndex;
		if (this.WCH) this.WCH.style.zIndex = zIndex - 1;
	}
	this.container.style.display = "block";
	southprojects.Utils.setupWCH(this.WCH, parseInt(this.container.style.left, 10), parseInt(this.container.style.top, 10), parseInt(this.container.style.width, 10), parseInt(this.container.style.height, 10));
};

southprojects.Modal.prototype.hide = function (destroy) {
	if (destroy) {
		southprojects.Utils.destroy(this.container);
		southprojects.Utils.destroy(this.WCH);
		this.container = null;
	} else {
		if (this.container) this.container.style.display = "none";
		southprojects.Utils.hideWCH(this.WCH);
	}
};

/**
 * southprojects loader object
 */
southprojects.Progress = function (objArgs) {
	if(arguments.length == 0){
		objArgs = {};
	}

	southprojects.Progress.SUPERconstructor.call(this, objArgs);
}

southprojects.Progress.id = "southprojects.Progress";

// Inherit SuperClass
southprojects.inherit(southprojects.Progress, southprojects.Widget);

southprojects.Progress.prototype.init = function(objArgs){
	this.config.zIndex = 1000;
	this.config.x = null;
	this.config.y = null;
	this.config.width = null;
	this.config.height = null;
	this.config.container = null;

	// processing Widget functionality
	southprojects.Progress.SUPERclass.init.call(this, objArgs);

	var modalConfig = {};

	if(typeof(this.config.container) == 'string'){
		this.config.container = document.getElementById(this.config.container);
	}

	if(this.config.container != null){
		var size = southprojects.Utils.getAbsolutePos(this.config.container);

		this.config.x = size.x;
		this.config.y = size.y;
		this.config.width = this.config.container.offsetWidth;
		this.config.height = this.config.container.offsetHeight;
		modalConfig.scroll = false;
	}

	modalConfig.x = this.config.x;
	modalConfig.y = this.config.y;
	modalConfig.width = this.config.width;
	modalConfig.height = this.config.height;
	modalConfig.zIndex = this.config.zIndex - 1;
	modalConfig.theme = this.config.theme;
	modalConfig.themePath = this.config.themePath;

	this.modal = new southprojects.Modal(modalConfig);
	this.active = false;
};

southprojects.Progress.prototype.start = function (message) {
	this.active = true;
	this.modal.show();
	this.message = southprojects.Utils.createElement("div", document.body);
	this.message.className = this.getClassName({prefix: "zpProgress"});
	var st = this.message.style;
	st.position = "absolute";
	st.zIndex = this.config.zIndex;
	st.backgroundColor = "#aaaaaa";
	this.message.innerHTML = message;

	var size = southprojects.Utils.getWindowSize();

	if(this.config.x != null && this.config.y != null){
		size.x = this.config.x;
		size.y = this.config.y;
	}

	if(this.config.width != null && this.config.height != null){
		size.width = this.config.width;
		size.height = this.config.height;
	}

	var br = southprojects.Utils.getAbsolutePos(this.modal.container);
	x = (size.width -  this.message.offsetWidth)/2 + br.x; 
	y = (size.height - this.message.offsetHeight)/2 + br.y;
	st.left = x + "px";
	st.top = y + "px";
};

southprojects.Progress.prototype.stop = function () {
	this.active = false;
	this.modal.hide(true);
	southprojects.Utils.destroy(this.message);
	this.message = null;
};

southprojects.Progress.prototype.isActive = function () {
	return this.active;
}
