/**
 * args:
 * - min
 * - max
 * - cookieName
 */

geck.requireStyle('css/dialog/simple.css');
geck.requireScript('js/jquery.ui.js');

// class of Dialog Window
function DialogWindow( options ) {
	
	this.args = options || {};
	
	var t = this; 
	var defaults = {
		possX: 'center',
		possY: 'center',
		possZ: '10000',
		width: '500', 
		title: '',
		icon:  null,
		content: '',
		contentIcon:  null,        //special icon in content
		modal:        true,        //top priority window (must be resolved)
		buttons:      {ok: 'Ok'},  //array of buttons to create (by default 'ok' button only)
		closeOnClick: true,        //automaticaly hide dialog on any button click
		speed:        500,		   //show/hide speed
		color:       'blue',       //frame color
		draggable:    true,        //drag, move and leave :)
		resizable:    true,        //drag side, and resize 
		coverBack:    true, 	   //create page covering background (disable page functionality except dialog window)
		colorBack:   'transparent' //color of cover background
	};
	
	t.dialog = null; //jquery wrapper of dialog window DOM model
	t.button = [];   //array of jQuery wrappers of created buttons (for bind 'onClick' event outside of this object)
	
	this.args = jQuery.extend(defaults, this.args);
	
	//create DOM of dialog window
	t.dialog = {};
	t.dialog.body    = $('<div>').addClass('geckDialog').addClass(t.args.color);
	t.dialog.top     = $('<div>').addClass('top').appendTo(t.dialog.body);
	t.dialog.middle  = $('<div>').addClass('middle').appendTo(t.dialog.body);
	t.dialog.bottom  = $('<div>').addClass('bottom').appendTo(t.dialog.body);
	t.dialog.header  = $('<div>').addClass('header').appendTo(t.dialog.body);
	t.dialog.content = $('<div>').addClass('content-wrap');
	
	//all parts (top, middle, bottom) has same div with classes (left,main,right)
	var parts = ['top','middle', 'bottom'];
	for (var key in parts) {
		t.dialog[parts[key]].append('<div class="left"></div>');
		t.dialog[parts[key]].append('<div class="main"></div>');
		t.dialog[parts[key]].append('<div class="right"></div>');
	}
	
	//set icon, texts, content
	t.dialog.header.append(t.args.title);
	t.dialog.content.appendTo($('.main',t.dialog.middle));
	if (t.args.contentIcon != null) {
		t.dialog.content.html(
			'<table class="content-tab"><tr>' +
			'<td class="content-icon '+t.args.contentIcon+'"><div></div></td>' +
			'<td class="content">'+t.args.content+'</td>' +
			'</tr></table>');
		t.dialog.content = $('.content',t.dialog.content);
	}
	
	//head buttons
	t.dialog.buttons = {};
	t.dialog.buttons['close'] = $('<div>').addClass('btnClose').appendTo(t.dialog.header);
	for (var key in t.dialog.buttons) {
		t.dialog.buttons[key].hover( function() {
			$(this).stop().animate({opacity:0.7},500);
		}, function() {
			$(this).stop().animate({opacity:0.3},500);
		}).click( function(){ 
			t.close();
		});
	}
	
	//create buttons in the dialog window
	if (t.args.buttons != null) {
		var buttonsFrame = $('<div>').addClass('dialogButtons').appendTo(t.dialog.content);
		for(key in t.args.buttons) {
			t.button[key] = $('<div class="button body">');
			t.button[key].html(
			      '<div class="button left"></div>'
			    + '<div class="button main"><span>' + t.args.buttons[key] + '</span></div>'
			    + '<div class="button right"></div>');
			t.button[key].appendTo(buttonsFrame);
			t.button[key].bind('mouseenter mouseleave', function(){ $(this).toggleClass('hover'); });
			if (t.args.closeOnClick) {
				t.button[key].click( function(){ t.close(); });
			}
		}
	}
	
	t.dialog.content.append('<div class="dialog-clear"></div>');
	
	//set width
	t.dialog.body.css({'width':t.args.width});
	
	//set proper possitions
	if (t.args.possX == 'center') {
		dialogWidth = $(t.dialog.body).width();
		windowWidth = $(window).width();
		t.dialog.body.css('left', (windowWidth - dialogWidth)/3  );		
	} else {
		t.dialog.body.css('left', t.args.possX );	
	}
	if (t.args.possY == 'center') {
		dialogHeight = $(t.dialog.body).height();
		windowHeight = $(window).height();
		t.dialog.body.css('top', (windowHeight - dialogHeight)/10);
	}
	else {
		t.dialog.body.css('top', t.args.possY );
	}
	
	//if is draggable
	if (t.args.draggable == true) {
		$(t.dialog.body).draggable({
			handle: t.dialog.header,
			cursor: 'move'
		});
		$(t.dialog.header).css('cursor','move');
	}
	
	//if is resizable
	if (t.args.resizable == true) {
//		$(dialog.body).resizable({
//			handles: 'all',
//			containment: 'document',
//			minWidth:  '200',
//			minHeight: '200'
//		});
	}
	
	//finaly show dialog
	t.dialog.body.hide().appendTo('body');
	t.dialog.body.show('drop', {direction:"down"}, (t.args.speed));
	
	//create page covering background
	if (t.args.modal) {
		t.dialog.overlay = geck.toggleOverlay({speed:(t.args.speed*2)});
		t.dialog.overlay.click( function() {
			t.dialog.body.effect("shake", { times:3, distance:5 }, 75);
		});
	}
	
	//close window function
	t.close = function() {
		$(t.dialog.body).hide('drop', {direction:'up'}, (t.args.speed*2), function() {
			t.dialog.body.remove();
			t.dialog = null;
			t = null;
		});
		
		if (t.args.modal)
			geck.toggleOverlay({speed:(t.args.speed)});
	};
}


