var VTips = {};
VTips.Size = {
	width:null,
	height:null
};

VTips.WM = { // Window manager
	activeModal:null
};
VTips.WM.Window = function(id, size) {
	this.id = id;
	this.size = size;
	this.content = null;
}
VTips.WM.ModalWindow = function(vtips_wm_window, opts) { // allow escape close, whether ESC or clicking outside
	this.window = vtips_wm_window;
	//this.allowClickOutside = allow_click_outside;
	this.opts = opts;
}
VTips.WM.ModalWindow.prototype.getWindow = function() { return this.window; }
VTips.WM.ModalWindow.prototype.allowClickOutside = function() { return this.opts.allowClickOutside; }

VTips.WM.Window.createWindowWithContent = function(id, width, height, content) {
	window_size = VTips.Size;
	window_size.width = width;
	window_size.height = height;
	the_window = new VTips.WM.Window(id, window_size);
	the_window.content = content;
	return the_window;
}
VTips.WM.Window.prototype.display = function(opts) {
	var width = this.size.width;
	var height = this.size.height;
	
	if (typeof(opts.animate) == 'undefined') opts.animate = false;
	
	animateSpeed = null;
	if (opts.animate) {
		animateSpeed = 'fast';
	}
	
	/*if (!width) width = jQuery(window).width() - 50;
	if (!width) width = 800;
	if (!height) height = jQuery(window).height() - 50;
	if (!height) height = 500;}
	*/
	var css_obj = {
		zIndex:opts.zIndex
	};
	
	if (width) {
		css_obj.width = width + 'px';
		css_obj.marginLeft = -(width/2) + 'px'
	}
	if (height) {
		css_obj.height = height + 'px';
		css_obj.marginTop = -(height/2) + 'px';
	}

	if ($('#'+this.id).length > 0) { // Already exists
		// prepare window object
		var window_obj = $('#' + this.id);
		if (!window_obj.hasClass('window-view')) window_obj.addClass('window-view');
		
		window_obj.css(css_obj);
		if (this.content) {
			window_obj.html(this.content);
		}
		$('body').append(window_obj);
		window_obj.show(animateSpeed); //
	} else { // Create from scratch
		/*
		window_obj = $('<div></div>').
				hide().
				attr('id', this.id).
				addClass('window-view').
				css(css_obj).
				css('width', css_obj.width).
				html(this.content).
				show(animateSpeed);
		*/
		window_obj = $('<div></div>').
				css('visibility', 'hidden').
				attr('id', this.id).
				addClass('window-view').
				html(this.content).
				hide();
		$('body').append(window_obj);
		window_obj.css('visibility', 'visible');
		window_obj.css(css_obj);
		window_obj.show(animateSpeed);
	}
}

VTips.WM.Window.makeContainerAWindow = function(id, width, height) {
	window_size = VTips.Size;
	window_size.width = width;
	window_size.height = height;
	the_window = new VTips.WM.Window(id, window_size);
	the_window.content = $('#' + id).html();
	return the_window;
}

VTips.WM.Modal = function() {
	this.windowStack = [];
	this.overlayObj = null;
}
VTips.WM.Modal.prototype.close = function() {
	this.popAllWindows();
	jQuery(".modal-overlay").remove();
	VTips.WM.activeModal = null;
}

VTips.WM.Modal.prototype.getCurrentWindowIndex = function() { return this.windowStack.length-1; }

VTips.WM.Modal.prototype.removeWindowAtIndex = function(index) {
	if (index > 0 && index  < this.windowStack.length) {
		this.windowStack.splice(index, 1);
	}
}

VTips.WM.Modal.prototype.popAllWindows = function() {
	if (this.windowStack.length > 0) {
		for(i=this.windowStack.length-1; i >= 0; i--) {
			jQuery('#' + this.windowStack[i].getWindow().id).hide();
		}
		this.windowStack = [];
	}
	return true;
}
VTips.WM.Modal.prototype.getOverlayObj = function() { return this.overlayObj; }

VTips.WM.Modal.prototype.addOverlay = function(allow_click_outside) {
	if ($('.modal-overlay').length == 0) {
		var overlay = $('<div></div>');
		overlay.addClass('modal-overlay');
		if (allow_click_outside) {
			overlay.click(function() {
					VTips.WM.activeModal.close();
				});
		}
		this.overlayObj = overlay;
		$('body').append(overlay);
	}
}

VTips.WM.Modal.prototype.pushWindow = function(the_window, append_options, animate) {
	var opts = {
		allowClickOutside : false,
		addOverlay : true
	}
	jQuery.extend(opts, append_options);
	
	if (typeof(opts.allowClickOutside) == 'undefined') opts.allowClickOutside = false;
	if (typeof(opts.addOverlay) == 'undefined') opts.addOverlay = true;
	if (typeof(animate) == 'undefined') animate = true;
	
	var displayOptions = {
		animate : animate
	};
		
	
	var modal_window = new VTips.WM.ModalWindow(the_window, opts)
	if (opts.addOverlay) this.addOverlay(opts.allowClickOutside);
	/*if ($('.modal-overlay').length == 0) {
		$('body').append(
			$('<div></div>')
				.addClass('modal-overlay')
				.click(function() {
					VTips.WM.activeModal.close();
				})
		);
	}*/	

	
	if (this.windowStack.length > 0) {
		$('#' + this.windowStack[this.windowStack.length-1].getWindow().id).hide(); 
	}
	this.windowStack.push(modal_window);
	
	if (VTips.WM.activeModal) {
		displayOptions.zIndex = 101 + this.windowStack.length;
		modal_window.getWindow().display(displayOptions);
		//$("body").append(modal_window.getWindow().display(displayOptions));
	}
}
VTips.WM.Modal.prototype.pushWindows = function(the_windows, append_options) {
	if (typeof(the_windows) == 'object') {
		for(i=0; i < the_windows.length; i++) {
			var animate = (i == the_windows.length-1);
			this.pushWindow(the_windows[i], append_options, animate);
		}
	}
}
VTips.WM.Modal.prototype.popWindow = function(close_overlay) {
	var length = this.windowStack.length;
	if (length > 0) {
		$('#' + this.windowStack[length-1].getWindow().id).hide('fast');
		
		this.windowStack.pop();
		length--;
		
		if (length > 0) {
			$('#' + this.windowStack[length-1].getWindow().id).show();
		} else {
			if (typeof(close_overlay) == 'undefined' || (typeof(close_overlay) != 'undefined' && close_overlay))
				this.close(); // Close modal view
		}
	}

}
VTips.WM.getModalManager = function() {
	if (!VTips.WM.activeModal) {
		VTips.WM.activeModal = new VTips.WM.Modal();
	}
	return VTips.WM.activeModal;
}
