js.module('visual');

var visual =
{

editorToolbars : 'PlexSafe',

fadeSpaceOnly : false,

fxloaded : undefined,

loadEditor : function(target, id, height, value, toolbar, html)
{
	if(value == undefined || !value) value = '';
	if(toolbar == undefined || !toolbar) toolbar = visual.editorToolbars;
	
	var fck = new FCKeditor(id);
	fck.BasePath = "/admin/fckeditor/";
	fck.Config["SkinPath"] = "/admin/fckeditor/editor/skins/office2003/";
	fck.Config["CustomConfigurationsPath"] = "/admin/fckeditor/ourconfig.js?" + (new Date() * 1);
	fck.Height = height;
	fck.Value = value;
	fck.ToolbarSet = toolbar;
	
	if(!html) fck.Config["ForcePasteAsPlainText"] = true;
	
	fck.ToolbarSet = toolbar ? toolbar : 'Empty';
	
	$$(target, fck.CreateHtml());
	return fck;
},
windowWidth : function()
{
	return window.innerWidth != null ? window.innerWidth : document.body != null ? document.body.clientWidth : null;
},
windowHeight : function()
{
	return window.innerHeight != null ? window.innerHeight : document.body != null ? document.body.clientHeight : null;
},
showWait : function()
{
	visual.showMsg(js.strings.wait);
},
hideWait : function()
{
	if($$('msgdiv_td') == js.strings.wait)
	{
		visual.hideMsg();
	}
},
showMsg : function(msg)
{
	if($('msgdiv_td'))
	{
		visual.setColor($('msgdiv'), '');
		visual.setClass($('msgdiv'), 'message_div_enabled');
		$$('msgdiv_td', msg);
		visual.show($('msgdiv'));
	}
},
showErr : function(msg)
{
	if($('msgdiv_td'))
	{
		visual.setColor($('msgdiv'), 'red');
		visual.setClass($('msgdiv'), 'message_div_enabled');
		$$('msgdiv_td', msg);
		visual.show($('msgdiv'));
	}
},
hideMsg : function()
{
	if($('msgdiv'))
	{
		visual.hide($('msgdiv'));
		$$('msgdiv_td', '&nbsp;');
	}
},
fade : function(msg)
{
	$('fade_window').style.width = common.checkBrowser('ie') ? document.body.clientWidth : document.width;
	$('fade_window').style.height = common.checkBrowser('ie') ? document.body.clientHeight : document.height;
	
	$$('fade_window_text', msg ? msg : '&nbsp;');
	visual.show($('fade_window'));
},
unfade : function()
{
	visual.hide($('fade_window'));
},
showConfirm : function(caption, onok, subcaption, oncancel, okcaption, cancelcaption)
{
	$('confirm_window').style.width = common.checkBrowser('ie') ? document.body.clientWidth : document.width;
	$('confirm_window').style.height = common.checkBrowser('ie') ? document.body.clientHeight : document.height;
	
	$$('confirm_window_confirm_caption', caption ? caption : '');
	$$('confirm_window_confirm_subcaption', subcaption ? subcaption + '<br /><br />' : '');
	
	ajb.setText('confirm_window_button_ok', okcaption ? okcaption : 'Да');
	ajb.setText('confirm_window_button_cancel', cancelcaption ? cancelcaption : 'Нет');
	ajb.setClickAct('confirm_window_button_ok', (onok ? (onok + "; visual.hideConfirm();") : "visual.hideConfirm(); return false;"));
	ajb.setClickAct('confirm_window_button_cancel', (oncancel ? (oncancel + "; visual.hideConfirm();") : "visual.hideConfirm(); return false;"));
	
	this.show($('confirm_window'));
},
hideConfirm : function()
{
	this.hide($('confirm_window'));
},
getInput : function()
{
	return $$('input_window_input_text');
},
showInput : function(caption, onok, subcaption, oncancel, text, okcaption, cancelcaption, pwd)
{
	$('input_window').style.width = common.checkBrowser('ie') ? document.body.clientWidth : document.width;
	$('input_window').style.height = common.checkBrowser('ie') ? document.body.clientHeight : document.height;
	
	$$('input_window_input_caption', caption ? caption : '');
	$$('input_window_input_subcaption', subcaption ? subcaption + '<br /><br />' : '');
	
	ajb.setText('input_window_button_ok', okcaption ? okcaption : 'OK');
	ajb.setText('input_window_button_cancel', cancelcaption ? cancelcaption : 'Отмена');
	ajb.setClickAct('input_window_button_ok', (onok ? (onok + "; visual.hideInput();") : "visual.hideInput(); return false;"));
	ajb.setClickAct('input_window_button_cancel', (oncancel ? (oncancel + "; visual.hideInput();") : "visual.hideInput(); return false;"));
	
	$$('input_window_input_text', text ? text : "");
	
	$A('input_window_input_text', 'type', (pwd != undefined && pwd == true ? 'password' : 'text'));
	
	this.show($('input_window'));
},
hideInput : function()
{
	this.hide($('input_window'));
},
setDisabled : function(el, flag)
{
	if($(el)) $(el).disabled = flag ? 'true' : '';
},
toggle : function(el)
{
	if(visual.visible($(el)))
		visual.hide($(el));
	else
		visual.show($(el));
},
itoggle : function(el)
{
	if(visual.visible($(el)))
		visual.hide($(el));
	else
		visual.ishow($(el));
},
show : function(el)
{
	if($(el) && $(el).style) $(el).style.display = '';
},
ishow : function(el)
{
	if($(el) && $(el).style) $(el).style.display = 'inline';
},
hide : function(el)
{
	if($(el) && $(el).style) $(el).style.display = 'none';
},
visible : function(el)
{
	if($(el)) return $(el).style.display != 'none';
},
setContent : function(el, content)
{
	if($(el)) $(el).innerHTML = content;
},
getContent : function(el)
{
	if($(el)) return $(el).innerHTML;
},
setColor : function(el, col)
{
	if($(el)) $(el).style.color = col;
},
setBgColor : function(el, col)
{
	if($(el)) $(el).style.backgroundColor = col;
},
setBorderColor : function(el, col)
{
	if($(el)) $(el).style.borderColor = col;
},
getBgColor : function(el)
{
	if($(el)) return $(el).style.backgroundColor;
},
getBorderColor : function(el)
{
	if($(el)) return $(el).style.borderColor;
},
getClass : function(el)
{
	if($(el)) return $(el).className;
},
setClass : function(el, cls)
{
	if($(el)) $(el).className = cls;
},
popUp : function(url)
{
	var newwin = window
			.open(url, "_blank", "toolbar=0,resizable=Yes,scrollbars=Yes");
	setTimeout('newwin.focus()', 250);
},
popUpSized : function(url, w, h)
{
	newwin = window
			.open(url, "_blank", "toolbar=0,resizable=No,scrollbars=Yes, width=" + w + ", height=" + h);
	setTimeout('newwin.focus()', 250);
},
imgPopUp : function(url)
{
	jscall = 'var neww=this.width+50; var newh=this.height+100; if(neww>screen.availWidth) {neww=screen.availWidth;} if(newh>screen.availHeight) {newh=screen.availHeight;} window.resizeTo(neww, newh);';
	
	newwin = window
			.open("", "Blank", "toolbar=0,resizable=No,scrollbars=Yes,width=100,height=100");
	newwin.document
			.write("<html><head><\/head><body topmargin='0' leftmargin='0' marginheight='0' marginwidth='0' style='padding:0px; margin:0px;'><table cellpadding='0' cellspacing='0' border='0' style='width:100%; height:100%;'><tr><td style='width:100%; height:100%; text-align:center; vertical-align:middle'><img src='" + url + "' \/ onload=\"" + jscall + "\"><\/tr><\/td><\/table><\/body><\/html>");
	setTimeout('newwin.focus()', 250);
},
enlarge : function(path, w, h)
{
	newwin = window
			.open('about:blank', "_blank", "toolbar=0,resizable=No,scrollbars=No,width=" + w + ",height=" + h);
	newwin.document
			.write("<html><head><\/head><body><img src='" + path + "' \/><\/body><\/html>")
	with (newwin.document.body.style)
	{
		margin = "1";
		padding = "0";
	}
	setTimeout('newwin.focus()', 250);
},
redirect : function(url)
{
	window.location = url;
},
reload : function()
{
	location.reload(true);
},
toAnchor : function(anc)
{
	visual.redirect(window.location.href.replace(/#.*$/i, '') + '#' + anc);
},
insertText : function(opentext, closetext, issingle, tofield)
{
	if(common.checkBrowser('ie'))
	{
		if($(tofield).isTextEdit)
		{
			$(tofield).focus();
			
			var sel = document.selection;
			
			var rng = sel.createRange();
			rng.colapse;
			
			if((sel.type == "Text" || sel.type == "None") && rng != null && rng.text)
			{
				if(closetext != "" && rng.text.length > 0)
				{
					opentext += rng.text + closetext;
				}
				else if(issingle)
				{
					has_closed = true;
				}
				
				rng.text = opentext;
			}
			else
			{
				$(tofield).value += opentext + closetext;
				has_closed = true;
			}
		}
		else
		{
			$(tofield).value += opentext + closetext;
			has_closed = true;
		}
		
		$(tofield).focus();
	}
	else if(common.checkBrowser('opera') || common.checkBrowser('mozilla') || common
			.checkBrowser('safari'))
	{
		var ss = $(tofield).selectionStart;
		var st = $(tofield).scrollTop;
		var es = $(tofield).selectionEnd;
		
		if(es <= 0)
		{
			es = $(tofield).textLength;
		}
		
		var start = ($$(tofield)).substring(0, ss);
		var middle = ($$(tofield)).substring(ss, es);
		var end = ($$(tofield)).substring(es, $(tofield).textLength);
		
		// -----------------------------------
		// text range?
		// -----------------------------------
		
		if($(tofield).selectionEnd - $(tofield).selectionStart > 0)
		{
			middle = opentext + middle + closetext;
		}
		else
		{
			middle = opentext + middle + closetext; // not sure
			
			if(issingle)
			{
				has_closed = true;
			}
		}
		
		$(tofield).value = start + middle + end;
		
		var cpos = ss + (middle.length);
		
		$(tofield).selectionStart = cpos;
		$(tofield).selectionEnd = cpos;
		$(tofield).scrollTop = st;
		
		$(tofield).focus();
	}
},
disableSelection : function(target, cursor)
{
	target = $(target);
	
	if(typeof target.onselectstart != "undefined") // IE route
		target.onselectstart = function()
		{
			return false
		};
	else if(typeof target.style.MozUserSelect != "undefined") // Firefox route
		target.style.MozUserSelect = "none";
	else
		// All other route (ie: Opera)
		target.onmousedown = function()
		{
			return false
		};
	target.style.cursor = cursor == undefined ? 'default' : cursor;
},
enableSelection : function(target)
{
	target = $(target);
	
	if(typeof target.onselectstart != "undefined") // IE route
		target.onselectstart = null;
	else if(typeof target.style.MozUserSelect != "undefined") // Firefox route
		target.style.MozUserSelect = "";
	else
		// All other route (ie: Opera)
		target.onmousedown = null;
	
	// target.style.cursor = cursor==undefined ? 'default' : cursor;
},
showFade : function(w, h, s, user_func, s_is_html)
{
	$$('blend_td', '&nbsp;');

	if(s_is_html == undefined)
		s_is_html = false;

	if(visual.fxloaded == undefined)
	{
		var fxl = true;
		if(!$('blend_table')) fxl = false;
		if(!$('blend_con')) fxl = false;
		if(!$('blend_milk')) fxl = false;
		
		var test = new Fx.Morph('blend_milk');
		if(!test) fxl = false;
		test = null;
		
		visual.fxloaded = fxl;
	}
	
	if(!visual.fxloaded) return false;
	
	if(w != undefined) $A('blend_table', 'width', w);
	if(h != undefined) $A('blend_table', 'height', h);
	
	visual.fadeSpaceOnly = (w==undefined&&h==undefined);
	
	var oh = $A('blend_table', 'height');
	var ow = $A('blend_table', 'width');
	
	var el = new Fx.Morph('blend_milk',
	{
	duration : 350,
	transition : Fx.Transitions.Back.easeOut
	}).set(
	{
		'opacity' : 0
	});
	
	var el2 = new Fx.Morph('blend_table',
	{
	duration : 350,
	transition : Fx.Transitions.Back.easeOut
	}).set(
	{
	'opacity' : 0,
	'height' : 10,
	'width' : 20
	});
	
	if(common.checkBrowser('ie'))
	{
		$('blend_con').style.height = (document.body.scrollHeight ? document.body.scrollHeight : visual
				.getWindowHeight()) + 'px';
		
		$('blend_space').style.top = (document.body.scrollTop ? document.body.scrollTop : 0) + 'px';
		
		visual.show('blend_con');
		visual.show('blend_space');
	}
	else
	{
		visual.show('blend_con');
	}
	
	el.start(
	{
		'opacity' : .85
	});
	
	if(!visual.fadeSpaceOnly)
	{
		if(s == undefined)
			el2.start(
			{
			'opacity' : 1,
			'height' : oh,
			'width' : ow
			});
		else
			el2.start(
			{
			'opacity' : 1,
			'height' : oh,
			'width' : ow
			}).chain(function()
			{
				$$('blend_td', (s_is_html ? s : $$(s)));
				if(user_func != undefined) user_func();
			});
	}
	return true;
},
hideFade : function(func)
{
	if(visual.fxloaded != true) return false;
	
	var el = new Fx.Morph('blend_milk',
	{
	duration : 350,
	transition : Fx.Transitions.Back.easeIn
	});
	var el2 = new Fx.Morph('blend_table',
	{
	duration : 350,
	transition : Fx.Transitions.Back.easeIn
	});
	
	el.start(
	{
		'opacity' : 0
	}).chain(function(){
		visual.hide('blend_con');
		if(common.checkBrowser('ie'))
			visual.hide('blend_space');
	});
	
	if(!visual.fadeSpaceOnly)
	{
		if(common.checkBrowser('ie'))
		{
			el2.start(
			{
			'opacity' : 0,
			'height' : 10,
			'width' : 20
			}).chain(function()
			{
				visual.hide('blend_con');
				visual.hide('blend_space');
				if(func != undefined) func();
			});
		}
		else
		{
			el2.start(
			{
			'opacity' : 0,
			'height' : 10,
			'width' : 20
			}).chain(function()
			{
				visual.hide('blend_con');
				if(func != undefined) func();
			});
		}
	}
	return true;
},
findPos : function(obj)
{
	var curleft = curtop = 0;
	
	if(obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	return [ curleft, curtop ];
}

};