/*
 * ficia dialog manager for jquery plugin
 */
if (typeof(Ficia) == 'undefined') Ficia = {};
if (typeof(Ficia.jQuery) == 'undefined') Ficia.jQuery = {};


;(function ($) {
	var getHeight = function(){
		var height = 0;
		if (document.documentElement) {
			height = document.documentElement.clientHeight;
		} else if (document.body) {
			height = document.body.clientHeight;
		} else {
			height = window.innerHeight;
		}
		return height;
	};
	var getWidth = function(){
		var width = 0;
		if (document.documentElement) {
			width = document.documentElement.clientWidth;
		} else if (document.body) {
			width = document.body.clientWidth;
		} else {
			width = window.innerWidth;
		}
		return width;
	};

	var centering = function(obj){
		var height = getHeight();
		var width  = getWidth();
		var tHeight = obj.height();
		var tWidth  = obj.width();

		var dHeight = tHeight;
		var dWidth  = tWidth;
		if (height - 20 < tHeight) {
			dHeight = height - 40;
		}
		if (width - 20 < tWidth) {
			dWidth = width - 40;
		}

		var setHeight = 10;
		var setWidth  = 10;
		if (height > (tHeight*1)+20) {
			setHeight = Math.floor((height/2) - (tHeight/2));
		}
		if (width > (tWidth*1)+20) {
			setWidth = Math.floor((width/2) - (tWidth/2));
		}

		return {
			position: { height: setHeight, width: setWidth },
			dialog: { height: dHeight, width: dWidth }
		};
	};

	var dialog = {
		target: null,
		modal: null,
		containerId: 'simplemodal-container',
		closeClass: 'simplemodal-close',
		closeHTML: '<a class="modalCloseImg" title="Close"></a>',
		open: function(target, args){
			var self = this;
			if (self.target) self.closeEvent();

			if (args === undefined) args = {};
			if (args.overlayClose === undefined) args.overlayClose = true;

			self.target = target;

			var pos = centering(target);
			self.modal = target.modal({
				overlayClose: true,
				autoResize: false,
				containerId: self.containerId,
				closeClass: self.closeClass,
				closeHTML: self.closeHTML,
				//position: [ pos.position.height, pos.position.width ],
				overlayClose: args.overlayClose,
				persist: true,
				onShow: function(){ self.showEvent() },
				onClose: function(){ self.closeEvent() }
			});
			//$('#'+self.containerId).find('.dialog_scroll').height(pos.dialog.height);
			//// $('#'+self.containerId).find('.dialog_scroll').width(pos.dialog.width);
		},
		reopen: function(){
			if (this.target) this.open(this.target);
		},
		close: function(){
			$.modal.close();
		},
		showEvent: function(){
		},
		closeEvent: function(){
			this.target = null;
			this.close();
		},
		appendClose: function(target){
			target.append($(this.closeHTML).addClass(this.closeClass));
		},
		resizeEvent: function(){
			var self = this;
			if (self.target === null) return;

			var target = $('#'+self.containerId);

			var pos = centering(target);
			self.modal.opts.position = [ pos.position.height, pos.position.width ];     // set to modal object
			target.css({ top: pos.position.height, left: pos.position.width });

			target.find('.dialog_scroll').height(pos.dialog.height);
			// target.find('.dialog_scroll').width(pos.dialog.width);
		},
		isOpen: function(){
			return (this.target === null) ? false : true;
		},
		name: 'dialog'
	};

	$.dialog = dialog;
	$.fn.dialog_open = function(args){
		dialog.open($(this), args);
	};

	// resize
	$(window).resize(function (e) {
		//dialog.resizeEvent();
		dialog.reopen();
	});

})(jQuery);
