/*
 * GeckTOP.net - object of dialog window 
 *
 * (based on jQuery)
 *
 * $Date: 2008-06-06
 */

// class of Dialog Window
function DialogWindow(options) {
	//jquery wrapper of dialog window DOM model
	this.dialog = null;
	//jquery wrapper of background page overlay
	this.cover = null;
	//object properties
	this.possX = 'center';
	this.possY = 'center';
	this.width = 'auto'; 
	this.title = 'GeckTOP dialog window';
	this.icon  = baseUrl+'/public/icon/default/16x16/gecktop.gif';
	this.button  = new Array(); //array of jQuery wrappers of created buttons (for bind 'onClick' event outside of this object) 
	this.buttons = {ok: 'Ok'};	//array of buttons to create (by default 'ok' button only)
	this.hideOnClick = true;    //automaticaly hide dialog on any button click
	this.speed     = 300;		//show/hide speed
	this.color     = '#EE3338'; //frame color
	this.colorAlt  = 'yellow'; 	//alernate frame color (for blinking)
	this.draggable = false;     //drag, move and leave :)
	this.coverBack = true 		//create page covering background (disable page functionality except dialog window)
	this.colorBack = 'transparent'; //color of cover background
	//launch object initialization
	this.init(this.defaults,options);
}

// constructor of object
function dialogWindowInit(defaultOptions,objectOptions) {
	var def = defaultOptions == undefined ? {}:defaultOptions;
	var obj = objectOptions  == undefined ? {}:objectOptions;
	
	//names of properties for update
	var properties = ['hideOnClick','possX','possY','title','icon','speed','color','colorAlt','draggable','width','coverBack','colorBack','buttons'];
	//set new properties by options / global defaults
	for(key in properties) {
		property = properties[key];

		if(obj[property] != undefined) {
			//set object option if its set
			this[property] = obj[property];
		} else if (def[property] != undefined) {
			//set default object option if its set
			this[property] = def[property];
		}
	}
}

DialogWindow.prototype.init		= dialogWindowInit;
DialogWindow.prototype.create	= dialogWindowCreate;
DialogWindow.prototype.show		= dialogWindowShow;
DialogWindow.prototype.hide		= dialogWindowHide;
DialogWindow.prototype.destroy	= dialogWindowDestroy;
DialogWindow.prototype.blink	= dialogWindowBlink;
DialogWindow.prototype.setContent = dialogWindowSetContent;
DialogWindow.prototype.getContentWrap = dialogWindowGetContentWrap;

// creates dialog window to body element
function dialogWindowCreate() {
	if (this.dialog == null) {
		//this object holder (for events)
		var thisDialog = this;
		//create page covering background
		if (this.coverBack) {
			var pageCover = $('<div></div>')
				.addClass('dialogBackgroundPageCover')
				.css({
					display:		 'none',
					backgroundColor: this.colorBack,
					width: 			 $(document).width(),
					height:			 $(document).height()
				})
				.click(function(){ thisDialog.blink(); })
				.disableTextSelect()
				.appendTo('body');
			this.cover = pageCover;
		}
		//create main parts in dialog body
		var dialogBody = $('<div></div>').addClass('dialog').appendTo('body');
		var dialogTop = $('<div></div>').addClass('top').appendTo(dialogBody);
		var dialogMiddle = $('<div></div>').addClass('middle').appendTo(dialogBody);
		var dialogBottom = $('<div></div>').addClass('bottom').appendTo(dialogBody);
		//all parts (top, middle, bottom) has same div with classes (background1,background2,left,right)
		var dialogParts = [dialogTop, dialogMiddle, dialogBottom];
		for (key in dialogParts) {
			dialogParts[key].append('<div class="background1"></div>');
			dialogParts[key].append('<div class="background2"></div>');
			dialogParts[key].append('<div class="left"></div>');
			dialogParts[key].append('<div class="right"></div>');
		}
		//contents(=main) wrap for header(=top) and body(=middle)
		var dialogTopMain = $('<div></div>').addClass('main').appendTo(dialogTop);
		var dialogMiddleMain = $('<div></div>').addClass('main').appendTo(dialogMiddle);
		var dialogBottomMain = $('<div></div>').addClass('main').appendTo(dialogBottom);
		
		$('<img />').addClass('icon').attr('src',this.icon).appendTo(dialogTopMain);
		$('<span></span>').addClass('title').text(this.title).appendTo(dialogTopMain);

		//set CSS for body of dialog (position and hidden)
		dialogBody.css({
			'width': this.width,
			'display':'none'
		});
		//set CSS for dialog frame properties (color and transparenty)
		$('.background1, .background2', dialogBody).css({
			'backgroundColor': this.color
		});
		
		//if is draggable
		if (this.draggable == true) {
			$(dialogBody).draggable({
				handle: '.top',
				cursor: 'move'
			});
			$(dialogTop).css('cursor','move');
		}
		//create buttons in the dialog window
		if (this.buttons != null) {
			var buttonsFrame = $('<div></div>').addClass('dialogButtons').appendTo(dialogMiddle);
			var buttons = this.buttons;
			for(key in buttons) {
				button = $('<button></button>').text(buttons[key]).appendTo(buttonsFrame);
				this.button[key] = button;
				if (this.hideOnClick) {
					$(button).click( function(){
						thisDialog.hide(null,true);
					});
				} 
			}
		}
		//put dialog window jQuery wrapper to object
		this.dialog = dialogBody;
	}
	
	return this;
}

// show dialog window 
function dialogWindowShow(speed) {
	if(this.dialog != null) {
		//if is IE just display else fade in
		if ($.browser.msie) {
			$(this.dialog).css('display', 'block');
		} else {
			if (speed == null) {speed = this.speed}
			$(this.dialog).fadeIn(speed)
		}
		
		middleWidth = $('.middle',this.dialog).width();
		titleWidth  = $('.top',   this.dialog).width();
		$(this.dialog).css({
			'width': middleWidth > titleWidth ? middleWidth:titleWidth
		});
		//show page covering background
		$(this.cover).css('display','block');
		
		//set proper possitions
		if (this.possX == 'center') {
			dialogWidth = $(this.dialog).width();
			windowWidth = $(window).width();
			this.dialog.css('left', (windowWidth - dialogWidth)/2  );		
		} else {
			this.dialog.css('left', this.possX );	
		}
		if (this.possY == 'center') {
			dialogHeight = $(this.dialog).height();
			windowHeight = $(window).height();
			this.dialog.css('top', (windowHeight - dialogHeight) / 3);
		}
		else {
			this.dialog.css('top', this.possY );
		}
	}
	return this;
}

// hide dialog window
function dialogWindowHide(speed, destroy) {
	if(this.dialog != null) {
		//if is IE just hide else fade out
		if ($.browser.msie) {
			$(this.dialog).css('display', 'none');
		} else {
			if (speed == null) {speed = this.speed}
			//$(this.dialog).fadeOut(speed)
			$(this.dialog).hide("drop", {direction:"up"}, speed);
		}
		
		$(this.cover).css('display','none');
		if (destroy) {
			setTimeout(this.destroy, speed);
		}
	}
	return this;
}

// hide dialog window
function dialogWindowDestroy() {
	this.dialog.remove();
	this.cover.remove();
	this.dialog = null;
}

// blink window frame (twice)
function dialogWindowBlink() {
	var blinkingSpeed = 60; 
	if(this.dialog != true) {
		$('.background1, .background2', this.dialog)
		.animate({'backgroundColor':this.colorAlt}, blinkingSpeed)
		.animate({'backgroundColor':this.color}, 	blinkingSpeed)
		.animate({'backgroundColor':this.colorAlt}, blinkingSpeed)
		.animate({'backgroundColor':this.color}, 	blinkingSpeed)
	}
	return this;
}

// set title in header of dialog window
function dialogWindowSetTitle(title) {
	$('.title', this.dialog).html(title);
}

// set content to dialog window
function dialogWindowSetContent(html) {
	if (this.dialog != true) {
		var middleMain = $('.middle .main', this.dialog);
		$('.contentWrap', middleMain).remove();
		$('<div></div>').addClass('contentWrap').html(html).appendTo(middleMain);
	} 
	return this;
}

// get content wrap
function dialogWindowGetContentWrap(){
	if (this.dialog != true) {
		return $('.middle .main', this.dialog);
	} else {
		return false;
	} 
}