Event.observe(window, 'load', function(event){
	var count=0;

	if($('sw')!='null' && $('sw')!=undefined) $('sw').value=screen.width;
	if($('sh')!='null' && $('sh')!=undefined) $('sh').value=screen.height;
	if($('scd')!='null' && $('scd')!=undefined) $('scd').value=screen.colorDepth;
	if($('np')!='null' && $('np')!=undefined) $('np').value=navigator.platform;

	/*
	$A($$('button.cal')).each(function(button){
		var input=$('cal_input');
		input.id=input.id+'_'+count;
		button.id = button.id+'_'+count;
		
		Calendar.setup({
			inputField		:	input.id,
			ifFormat		:	"%Y-%m-%d %H:%M:%S",
			showsTime		:	true,
			button			:	button.id,
			singleClick		:	true,
			step			:	1
		});
		count++;
	});
	*/
	setHiddenCheck();
	//setDependencies();
});

Event.observe(window, 'change', function(){
	setDependencies();
});

function fAjaxSetLang(alias){
	//alert(alias);
	new Ajax.Request(
		'/inc/ajax.php?action=setLang&arg_1=' + alias, 
		{
		method: 'get',
		onComplete: function(request){
			//alert(request.responseText);
			location.reload(true);
		}
	}); 
	return false;
}

function fAjaxSetSearch(search_str){
	//alert(search_str);
	new Ajax.Request(
		'/inc/ajax.php?action=setSearch&str=' + search_str, 
		{
		method: 'get',
		onComplete: function(request){
			//alert(request.responseText);
			//location.reload(true);
		}
	}); 
	return false;
}

function emoticon(text) {
	var txtarea = $$('textarea.emoticon')[0];
	//text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
};

function setHiddenCheck(){
	$$('input[type=checkbox]').each(function(chk){
		chk.observe('click', function(e){
			chk.checked=this.checked;
			$$('input[type=hidden]').each(function(hid){
				if(hid.name==chk.name){
					hid.removeAttribute('value');
					hid.writeAttribute('value', chk.checked ? '1' : '0');
				}
			});
			setDependencies();
		});
		var hid=document.createElement('input');
		hid.writeAttribute('name', chk.name);
		hid.writeAttribute('type', 'hidden');
		hid.writeAttribute('value', chk.checked ? '1' : '0');
		chk.parentNode.appendChild(hid);
	});
}

function setActiveTabs(self){
	var obj=$$('.mc').each(function(obj){
		if(obj.hasClassName('active_tabs')){
			obj.removeClassName('active_tabs');
		}
	});
	self.addClassName('active_tabs');
	$('subtree').addClassName('active_tabs');
}

function loadTab(tab) {
	new Ajax.Updater('subtree', tab, { method: 'get' } );
}

function setDependencies(){
	var obj=$$('.master').each(function(obj){
		if(obj.id){
			switch(obj.type){
				case 'checkbox':
					$$('#'+obj.id+':not([class=master])').each(function(el){
						obj.checked==true ? el.show() : el.hide();
					});
				break;

				case 'select-one':
					$$('#'+obj.id+':not([class=master])').each(function(el){
						$A(el.childNodes).each(function(sub_el){
							if(sub_el.id){
								if(sub_el.hasAttribute('class')){
									var class_ids=$w(sub_el.readAttribute('class'));
									if(class_ids){
										if(inArray(obj.value, class_ids)){
											sub_el.show();
										}
										else{
											$A(sub_el.childNodes).each(function(sub_sub_el){
												if(sub_sub_el.type=='checkbox' || sub_sub_el.type=='hidden'){
													sub_sub_el.checked=false;
													//sub_sub_el.value='0';
													sub_sub_el.removeAttribute('value');
													sub_sub_el.writeAttribute('value', '0');
													sub_sub_el.removeAttribute('checked');
												}
											});
											sub_el.hide();
										}
									}
								}
								else{
									if(sub_el.id==obj.value){
										sub_el.show();
									}
									else{
										$A(sub_el.childNodes).each(function(sub_sub_el){
											if(sub_sub_el.type=='checkbox' || sub_sub_el.type=='hidden'){
												sub_sub_el.checked=false;
												//sub_sub_el.value='0';
												sub_sub_el.removeAttribute('value');
												sub_sub_el.writeAttribute('value', '0');
												sub_sub_el.removeAttribute('checked');
											}
										});
										sub_el.hide();
									}
								}
							}
						});
					});
				break;

				default:
				alert(obj.type);
				break;
			}
		}
	});
}

function inArray(needle, haystack, strict) {
	var found = false, key, strict = !!strict;
	for (key in haystack) {
		if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			found = true;
			break;
		}
	}
	return found;
}
