
tor = {};

tor.modal = function (params) {
	var _defaults = {
		title: 'Title',
		content: 'Message',
		width: 350,
		height: false,
		show: true,
		cssclass: 'torModal',
		overlayColor: '#000',
		icon: false,
		iconWidth: 64,
		iconHeight: 64,
		buttons: [
			{
				title: 'Tamam',
				cls: 'okButton',
				close: true
			}
		],
		load: function () { },
		close: function () { }
	};
	this.opts = $.extend({}, _defaults, params);

	// modal container in body
	if ($('#TorModals').length == 0) {
		$('body').append('<div id="TorModals" style="display: none;"></div>');
	}

	// clearing same instance
	if (this.opts.clear) {
		$('#TorModals').remove();
	}

	// creating this instance
	this.modal = $('<div>').attr('id', 'ActiveModal').addClass('torModalBox').css('width', this.opts.width).appendTo('#TorModals');
	if( this.opts.height ){
		this.modal.css('height', this.opts.height);
	}
	if( this.opts.cssclass ){
		this.modal.addClass(this.opts.cssclass);
	}

	// title
	this.title = $('<div>').addClass('modalTitle').html('<h2>' + this.opts.title + '</h2>');
	
	this.content = $('<div>').addClass('modal-content').html(this.opts.content);
	
	// icon
	if (this.opts.icon) {
		// covering content with h3
		this.content.html('<h3 class="center">' + this.content.html() + '</center>');
		icon = '<img src="'+ this.opts.icon +'" width="'+ this.opts.iconWidth +'" height="'+ this.opts.iconHeight +'" border="0" alt="Icon" />';
		// adding icon div to beginning
		this.content.prepend('<div class="modalIcon">' + icon + '</div>');
	}

	// injecting title and content
	this.modal.append(this.title);
	this.modal.append(this.content);

	// if there is close button?
	this.title.append('<a href="javascript:;" class="close">X</a>');
	
	// if there is buttons
	if (this.opts.buttons.length > 0) {
		// adding container
		this.buttons = $('<ul>');
		this.buttonContainer = $('<div>').addClass('modalButtons').append(this.buttons);
		this.modal.append(this.buttonContainer);
		// adding buttons
		for (var i = 0; i < this.opts.buttons.length; i++) {
			button = this.opts.buttons[i];
			// button link
			buttonLink = $('<a>').addClass('modalButton').attr('href', 'javascript:;').html('<span>' + button.title + '</span>');

			// button event
			if (button.click) {
				buttonLink.bind('click', {modalObject: this, buttonObject: button}, function(e){
					// if not disabled
					if( !e.data.modalObject.content.find('.modalButtons-' + (i+1)).hasClass('disabled') ){
						// callback (passing modal object)
						e.data.buttonObject.click( e.data.modalObject );
					}
				});
			}

			// custom button class
			buttonLink.addClass('modalButtons-' + (i+1));
			if (button.cssclass) {
				buttonLink.addClass(button.cssclass);
			}

			// if button is one of close modal button
			if (button.close) {
				buttonLink.addClass('close');
			}

			// list object
			buttonObject = $('<li>').append(buttonLink);

			// adding to the button list
			this.buttons.append(buttonObject);
		}
	}

	// close modal
	this.close = function(){
		$(document).unbind('keydown');
		$.unblockUI();
	}

	// binding close links
	this.modal.find('.close').bind('click', {modalObject: this}, function(e){
		modal = e.data.modalObject;
		if(modal.opts.close !== false){ // running callback
			modal.close();
		}
	});

	// show method
	this.show = function () {
		// load callback
		this.opts.load(this);

		// todo: multi instance lightbox clone araştırılıp entegre edilecek.

		// cloning and injecting to body for calculating size
		clone = this.modal.clone().css({
			position: 'absolute',
			top: -10000,
			left: -10000
		});
		$('body').append(clone);
		// getting size
		calculatedWidth = clone.width();
		calculatedHeight = clone.height();
		clone.remove();

		// showing
		$.blockUI({
			message: this.modal,
			css: {
				width: calculatedWidth + 'px',
				height: calculatedHeight + 'px',
				top:  ($(window).height() - calculatedHeight) / 2 + 'px',
                left: ($(window).width() - calculatedWidth) / 2 + 'px',
				background: 'transparent',
				border: 'none'
			},
			overlayCSS: {
				backgroundColor: this.opts.overlayColor
			}
		});
	}

	// if show parameter is given
	if (this.opts.show) {
		this.show();
	}

	return this;
}
