/*!______________________________________________________________________*\
||                                                                      ||
|| __   __                        				            	  ||			
|| \ \ / /__  _ __   ___ __ _ 		- Fırat Ağdaş                 ||
||  \ V / _ \| '_ \ / __/ _` |		- Yonca New Media             ||
||   | | (_) | | | | (_| (_| |		- firat@yonca-ad.com          ||
||   |_|\___/|_| |_|\___\__,_|                                          ||
||                                                                      ||
||______________________________________________________________________||
\*                                                                      */

function Yonca_Dialogs()
{
	this.mask = null;
	
	/*
	<div class="yncbox">
		<div class="hd"></div>
		<div class="bd"></div>
		<div class="ft"></div>
	</div>
	*/
	this.main_format = null;
	this.active_dialog = false;
	
	this.icons = {
		WARNING:    'icon_warning.gif',
		INFO:       'icon_info.gif',
		BLOCK:      'icon_block.gif',
		QUESTION:   'icon_question.gif',
		LOCKED:     'icon_locked.gif'
	};
	
	this.buttons = new Array();
	
	this.is_modal = false;
	
	this.scroll_timer = null;
}

Yonca_Dialogs.prototype.render = function(varname, context, config)
{
	if (this.active_dialog)
	{
		return false;
	}
	
	config['modaldialog'] = config['modaldialog'] || false;
	config['icon'] = config['icon'] || Yonca.dialog.icons.QUESTION;
	config['visible'] = Lang.isUndefined(config['visible']) ? true : config['visible'];
	
	this.is_modal = config['modaldialog'] ? true : false;
	
	if (!Yonca.isWinLoaded)
	{
		if (CP_AREA)
		{
			Event.addListener(window, 'load', function(){ this.confirm(varname, context, config); }, this, true);
		}
		else
		{
			Event.addListener(window, 'load', function(){ this.render(varname, context, config); }, this, true);
		}
		
		return false;
	}
	
	this.set_footer('');
	if (Lang.isArray(this.buttons[varname]))
	{
		this.buttons[varname] = new Array();
	}
	
	config['buttons'] = config['buttons'] || new Array();
	var _this = this, ft = this.fetch_block('ft'), buttons = this.construct_buttons(varname, config['buttons']);
	
	if (config['icon'] !== false)
	{
		this.set_header('<img src="' + STYLEDIR + 'images/icons/' + config['icon'] + '" border="0" />');
	}
	else
	{
		this.set_header('');
	}
	
	this.set_body(context);
	for (var i = 0; i < buttons.length; i++)
	{
		ft.appendChild(buttons[i].get('element'));
	}
	
	this.fetch_format().className = '';
	Dom.addClass(this.fetch_format(), 'modaldialog');
	
	if (config['visible'])
	{
		this.show();
	}
};

Yonca_Dialogs.prototype.loading_render = function()
{
	this.is_modal = false;
	this.render('loadingbox', '<img src="' + STYLEDIR + 'images/loading.gif" border="0" />', { modaldialog: true, visible: false });
	this.set_header('<div class="loading"> YÜKLENİYOR <div> İstasyon içeriği yükleniyor. lütfen bekleyin </div> </div>');
	this.set_footer('<div class="lfooter"><br /> LÜTFEN BEKLEYİNİZ </div>');
	
	Dom.replaceClass(this.fetch_format(), 'modaldialog', 'redirect');
	Dom.addClass(this.fetch_format(), 'dialog-redirect');
	return this;
};

Yonca_Dialogs.prototype.site_loading_render = function()
{
	this.is_modal = false;
	var loading_render = '<div class="l_ind"> <img src="' + STYLEDIR + 'images/loading.gif" border="0" /> </div>'
		+ '<div class="l_desc"> ' + phrase['isleminiz_gerceklestiriliyor'] + ' <br /> ' + phrase['lutfen_bekleyin_ajax'] + ' </div> ';
		
	this.render('loadingbox', loading_render, { modaldialog: false, visible: false });
	Dom.replaceClass(this.fetch_format(), 'modaldialog', 'loadingbox');
	return this;
};

Yonca_Dialogs.prototype.show = function()
{	
	Dom.addClass(document.body, 'ie6overlayfix');
	
	if (this.is_modal)
	{
		this.resize_mask();
		Dom.setStyle(this.mask, 'display', 'block');
	}
	
	Dom.setStyle(this.main_format, 'display', '');

	this.locate_format();
	this.active_dialog = true;
	
	Event.addListener(window, 'resize', Yonca_Dialogs.prototype.resize_mask, this, true);
	Event.addListener(window, 'scroll', Yonca_Dialogs.prototype.scroll_locate_format, this, true);
};

Yonca_Dialogs.prototype.hide = function()
{
	Dom.removeClass(document.body, 'ie6overlayfix');
	Dom.setStyle(this.mask, 'display', 'none');
	Dom.setStyle(this.main_format, 'display', 'none');
	this.active_dialog = false;
	
	Event.removeListener(window, 'resize', Yonca_Dialogs.prototype.resize_mask);
	Event.removeListener(window, 'scroll', Yonca_Dialogs.prototype.scroll_locate_format);
};

Yonca_Dialogs.prototype.locate_format = function()
{
	center_object(this.fetch_format());
};

Yonca_Dialogs.prototype.scroll_locate_format = function()
{
	clearTimeout(this.scroll_timer);
	if (Yonca.scroll_dragging)
	{
		var _this = this;
		this.scroll_timer = setTimeout(function(){ _this.scroll_locate_format(); }, 100);
	}
	else
	{
		this.locate_format();
	}
};

Yonca_Dialogs.prototype.construct_buttons = function(varname, buttons)
{
	var _this = this;
	if (Lang.isUndefined(this.buttons[varname]))
	{
		this.buttons[varname] = new Array();
		var cb = this.buttons[varname];
		for (var i = 0; i < buttons.length; i++)
		{
			var title = buttons[i].label || 'Button #' + i;
			var handler = buttons[i].handler || Yonca_Dialogs.prototype.dialog_hide_handler
			var isDefault = buttons[i].isDefault || false;
			
			var button = new YAHOO.widget.Button({ label: title, onclick: { fn: handler, scope: _this, obj: _this } });
			if (isDefault)
			{
				Dom.addClass(button, 'default');
			}
			
			this.buttons[varname][this.buttons[varname].length] = button;
		}
	}
	else
	{
		var limit = Math.max(this.buttons[varname].length, buttons.length), ft = this.fetch_block('ft');
		for (var i = 0; i < limit; i++)
		{
			if (Lang.isUndefined(buttons[i]))
			{
				this.buttons[varname][i].parentNode.removeChild(cb[i]);
				delete this.buttons[varname][i];
				continue;
			}
			
			var title = buttons[i].label || 'Button #' + i;
			var handler = buttons[i].handler || Yonca_Dialogs.prototype.dialog_hide_handler
			var isDefault = buttons[i].isDefault || false;
				
			var button = new YAHOO.widget.Button({ label: title, onclick: { fn: handler, scope: _this, obj: _this } });
			if (isDefault)
			{
				Dom.addClass(button, 'default');
			}
				
			this.buttons[varname][i] = button;
			ft.appendChild(this.buttons[varname][i].get('element'));
		}
	}
	
	return this.buttons[varname];
};

Yonca_Dialogs.prototype.fetch_icon = function(iconid)
{
	if (Lang.isUndefined(this.icons[iconid]))
	{
		iconid = parseInt(iconid, 10) || 3;
		if (isNaN(iconid) || iconid > 3 || iconid < 0)
		{
			iconid = 3;
		}
		
		var count = -1;
		for (var iconvar in this.icons)
		{
			if (++count == iconid)
			{
				return this.icons[iconvar];
			}
		}
		
		return '';
	}
	
	return this.icons[iconid];
};

Yonca_Dialogs.prototype.dialog_hide_handler = function()
{
	if (Lang.isFunction(this.hide))
	{
		this.hide();
	}
};

Yonca_Dialogs.prototype.fetch_mask = function()
{
	if (this.mask === null)
	{
		var mask = document.createElement('div');
		Dom.addClass(mask, 'modalmask');
		Dom.innerHTML = '&#160;'; 				// Nam-ı Değer &nbsp;
		Dom.setStyle(mask, 'display', 'none');
		
		document.body.insertBefore(mask, document.body.firstChild);
		
		this.mask = mask;
	}
	
	return this.mask;
};

Yonca_Dialogs.prototype.resize_mask = function()
{
	var mask = this.fetch_mask(), vpx = Dom.getViewportWidth(), vpy = Dom.getViewportHeight();
	if (mask.offsetWidth > vpx)
	{
		Dom.setStyle(mask, 'width', vpx + 'px');
	}
	
	if (mask.offsetHeight > vpy)
	{
		Dom.setStyle(mask, 'height', vpy + 'px');
	}
	
	Dom.setStyle(mask, 'width', Dom.getDocumentWidth() + 'px');
	Dom.setStyle(mask, 'height', Dom.getDocumentHeight() + 'px');
};

Yonca_Dialogs.prototype.fetch_format = function()
{
	if (this.main_format == null)
	{
		var divs = ['modaldialog', 'hd', 'bd', 'ft'], stack = new Array();
		for (var i in divs)
		{
			stack[i] = document.createElement('div');
			Dom.addClass(stack[i], divs[i]);
			stack[i].innerHTML = '&#160;';
			
			if (i > 0)
			{
				stack[0].appendChild(stack[i]);
				delete stack[i];
			}
		}
		
		this.main_format = stack[0];
		Dom.setStyle(this.main_format, 'display', 'none');
		Dom.get('yncboxarea').appendChild(this.main_format);
	}
	
	return this.main_format;
};

Yonca_Dialogs.prototype.fetch_block = function(blockcss)
{
	var div = Dom.getElementsBy(function(divobj){ return Dom.hasClass(divobj, blockcss); }, 'div', this.fetch_format());
	return div ? div[0] : div;
};

Yonca_Dialogs.prototype.set_block = function(blockcss, inHTML, addcss)
{
	addcss = addcss || false;
	var block = this.fetch_block(blockcss);
	
	if (addcss !== false && Lang.isString(addcss) && !Dom.hasClass(block, addcss))
	{
		Dom.addClass(block, addcss);
	}
	
	block.innerHTML = inHTML;
};

Yonca_Dialogs.prototype.set_header = function(inHTML, addcss)
{
	this.set_block('hd', inHTML, addcss);
};

Yonca_Dialogs.prototype.set_body = function(inHTML, addcss)
{
	this.set_block('bd', inHTML, addcss);
};

Yonca_Dialogs.prototype.set_footer = function(inHTML, addcss)
{
	this.set_block('ft', inHTML, addcss);
};

Yonca.dialog = new Yonca_Dialogs();


