function updateFCK()
{
	for ( i = 0; i < parent.frames.length; ++i )
	if ( parent.frames[i].FCK )
		parent.frames[i].FCK.UpdateLinkedField();
}




createFCK = function(a, div, textareaId, height, toolbarset)
{
	div.removeChild(a);
	textarea = $('textareaId');

	var oFCKeditor = new FCKeditor(textareaId) ;
	oFCKeditor.BasePath	= '/_includes/FCKeditor/';
	oFCKeditor.Height = height ;
	oFCKeditor.ToolbarSet = toolbarset;
	oFCKeditor.ReplaceTextarea() ;
}


var Message = Class.create({

	initialize: function(response) {
		if (typeof response == 'string')
			response = {message: response};

		if (response.redirect)
			afterHide = function() { location.href = response.redirect };
		else if (response.reload)
			afterHide = function() { location.reload() };
		else
			afterHide = function() {};
		
		Modalbox.message(response.message, afterHide);
	}

});

var ErrorMessage = Class.create({

	initialize: function(errors) {
		str = '<ul>';
		for (i=0; i<errors.length; i++)
			str += '<li>' + errors[i] + '</li>';
		str += '</ul>';
		Modalbox.alert(str);
	}

});




var FormElementObserver = Class.create({

	initialize: function(elements, events, func) {
		if (typeof elements == 'string')
			elements = [elements];

		if (typeof events == 'string')
			events = [events];
	
		for (var i=0; i<elements.length; i++)
			for (var j=0; j<events.length; j++)
				if ($(elements[i]))
					$(elements[i]).observe(events[j], func);
	}

});



var CheckboxToggler = Class.create({

	initialize: function(inputId, divId) {
		var input = $(inputId);
		if (!input)
			return false;
		
		var div = $(divId);

		if (!input.checked)
			div.hide();

		input.observe('click', function(){
			if (this.checked)
				div.show();
			else
				div.hide();
		});
	}

});


var InputDefaultValue = Class.create({

	initialize: function(fieldId, defaultValue) 
	{
		field = $(fieldId);
		if (!field)
			return false;

		field.observe('blur', function(){
			if (!$F(this))
				this.value = defaultValue;
		});

		field.observe('focus', function(){
			if ($F(this) == defaultValue)
				this.value = '';
		});

		if (!$F(field))
			field.value = defaultValue;
	}

});




var TabMenuFromDL = Class.create({

	initialize: function(fieldId, options)
	{
		this.id = Math.round(100000 * Math.random());
		
		this.dl = $(fieldId);
		this.dts = this.dl.select('dt');
		this.count = this.dts.length;

		this.ids = new Array();
		this.dls = new Array()

		for (var i=0; i<this.count; i++)
		{
			this.ids.push(this.dts[i].next().id);
			this.dls.push(this.dts[i].next());
		}

		this.readOptions(options);
		this.createTabs();
		
		this.show(this.index);
	},
	
	
	readOptions: function(options)
	{
		if (!options)
		{
			this.index = 0;
			return;
		}

		this.index = (options.index) ? options.index : 0;
		if (options.prevButton)
			this.prevButton = $(options.prevButton);
		if (options.nextButton)
			this.nextButton = $(options.nextButton);
	},
	
	
	createTabs: function()
	{
		this.dts.invoke('hide');

		this.tabMenu = new Element('div', {'class' : 'noprint'});
		var tabMenuUl = new Element('ul', {'class' : 'tabnav'});

		for (var i=0; i<this.count; i++)
		{
			this.dls[i].hide();

			tabMenuUlLi = new Element('li').update(new Element('a', {rel: i, id: this.ids[i] + this.id}).update(this.dts[i].innerHTML));
			tabMenuUl.insert(tabMenuUlLi);
		}
		
		this.tabMenu.insert(tabMenuUl);
		this.dl.insert({Before: this.tabMenu});

		tabMenuUl.select('a').each(function(element){
			element.observe('click', function(){
				this.show(element.rel);
			}.bindAsEventListener(this, element));
		}.bind(this));

		if (this.prevButton)
		{
			this.prevButton.observe('click', function(){
				this.show(--this.index);
			}.bindAsEventListener(this));
		}
		
		if (this.nextButton)
		{
			this.nextButton.observe('click', function(){
				this.show(++this.index);
			}.bindAsEventListener(this));
		}
	},
	
	
	show: function(index)
	{
		this.index = index;
		
		for (var i=0; i<this.count; i++)
			if (this.ids[i] == index)
				this.index = i;
		
		this.tabMenu.select('li.on').each(function(element){
			element.removeClassName('on');
		});
		this.tabMenu.select('li')[this.index].addClassName('on');

		for (var i=0; i<this.count; i++)
		{
			if (i == this.index)
				this.dts[i].next().show();
			else
				this.dts[i].next().hide();
		}
		
		if (this.prevButton)
		{
			if (this.index == 0)
			{
				this.prevButton.addClassName('disabled');
				this.prevButton.disable();
			}
			else
			{
				this.prevButton.removeClassName('disabled');
				this.prevButton.enable();
			}
		}

		if (this.nextButton)
		{
			if (this.index == this.count-1)
			{
				this.nextButton.addClassName('disabled');
				this.nextButton.disable();
			}
			else
			{
				this.nextButton.removeClassName('disabled');
				this.nextButton.enable();
			}
		}
	},
	
	addTabFunction: function(tabId, func) {

		if ($(tabId + this.id))
		{
			$(tabId + this.id).observe('click', func);
		}
	}

});




var ExclusiveCheckboxControl = Class.create({

	initialize: function(fields) 
	{
		var checkboxes = new Array();
		
		for (var i=0; i<fields.length; i++)
		{
			if (field = $(fields[i]))
				checkboxes.push(field);
		}

		for (var i=0; i<checkboxes.length; i++)
		{
			checkboxes[i].observe('click', function(){
				if (this.checked)
				{
					for (var j=0; j<checkboxes.length; j++)
					{
						if (this != checkboxes[j] && checkboxes[j].checked)
							checkboxes[j].checked = false;
					}
				}
			});
		}
	}

});






var VisibilityToggler = Class.create({

	initialize: function(linkId, layerId, showText, hideText) 
	{
		this.link = $(linkId);
		this.layer = $(layerId);
		this.showText = showText;
		this.hideText = hideText;
		this.visible = this.layer.visible();
		
		if (this.visible)
			this.link.update(hideText);
		else
			this.link.update(showText);
		
		this.link.observe('click', this.aaa.bindAsEventListener(this));
	},
	
	aaa: function()
	{
		this.layer.toggle();
		this.visible = !this.visible;

		if (this.visible)
			this.link.update(this.hideText);
		else
			this.link.update(this.showText);
	}

});




var Overlay = Class.create({

	initialize: function(color, opacity) 
	{
		this.zIndex = 3000;

		this.overlay = new Element('div');
		var opacityMoz = opacity / 100;
		this.overlay.setStyle({
			zoom: 1,
			position: 'absolute',
			left: '0px',
			top: '0px', 
			width: '100%',
			height: this.getPageSize()[1] + 'px',
			opacity: opacityMoz,
			backgroundColor: color,
			zIndex: this.zIndex
		});
		document.body.insert(this.overlay);
	},
	
	
	getZindex: function() {
		return this.zIndex;
	},
	
	
	remove: function() {
		this.overlay.remove();
	},

		
	getPageSize: function() {

		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}

});



if (!window.UploadManager)
	var UploadManager = new Object();

UploadManager.Methods = {

	fields: new Array(),
	uploadInProgress: false,

	Observe: function(elementId, afterUpload) {
		element = $(elementId);
		if (!element)
			return false;

		element.wrap('div', {id: element.id + '-wrap'});

		if (!afterUpload)
			afterUpload = function() {};

		element.observe('change', function() {
			UploadManager.AddToList(this, afterUpload)
		});
		//UploadManager.fields.push(element);
	},
	
	AddToList: function(element, afterUpload)
	{
		UploadManager.fields.push({field: element, afterUpload: afterUpload});
		element.disable();
		element.stopObserving('change');
		UploadManager.UploadControl();
	},
	
	UploadControl: function() {
		if (UploadManager.uploadInProgress)
			return;
		else
		{
			if (element = UploadManager.fields.shift())
			{
				element.field.enable();
				UploadManager.Submit(element.field, element.afterUpload);
			}
		}
	},
	
	Submit: function(field, afterUpload)
	{
		args = $A(arguments);
		afterUpload = args[1];
		
		UploadManager.uploadInProgress = true;
		
		iframeId = 'iframe_' + field.id;
		var iframe = new Element('iframe', {id: iframeId, name: iframeId/*, height: 0, width: 0*/});
		$(document.body).insert(iframe);

		var form = $(field.form);
		
		var originalAction = (form.action) ? form.action : "";
		var originalTarget = (form.target) ? form.target : "";

		form.action = '/upload';
		form.target = iframeId;

		document.observe(field.id + ':done', function(event){
			UploadManager.Done.bindAsEventListener(field, afterUpload)();
			document.stopObserving(field.id + ':done');
		}.bindAsEventListener(field));

		setcookie('inputname', field.name, 1, '/');

		form.submit();
		field.up().update(new Element('span', {'class': 'loading'}).update('Kis türelmet, feltöltés folyamatban...'));
		
		form.action = originalAction;
		form.target = originalTarget;
	},
	
	
	Done: function()
	{
		args = $A(arguments);
		afterUpload = args[1];

		parts = getcookie(this.id).split(';');
		UploadManager.uploadInProgress = false;

		$(this.id + '-wrap').update(parts[0] + ' (' + parts[2] + ') - ');
		$(this.id + '-wrap').insert(a = new Element('a').update('Törlés'));

		a.observe('click', function(){
			$('iframe_' + this.id).remove();
			$(this.id + '-wrap').update(new Element('input', {type: 'file', id: this.id, name: this.name}));
			UploadManager.Observe(this.id);
		}.bindAsEventListener(this));

		$(this.id + '-wrap').insert(new Element('input', {type: 'hidden', name: this.name, value: parts[1]}));

		afterUpload.bind(this)();
		UploadManager.UploadControl();
		deletecookie(this.id);
	}

}

Object.extend(UploadManager, UploadManager.Methods);



function setcookie(name, value, expires, path, domain, secure) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());

	// if the expires variable is set, make the correct 
	// expires time, the current script below will set 
	// it for x number of days, to make it for hours, 
	// delete * 24, for minutes, delete * 60 * 24
	if (expires)
	{
		expires = expires * 1000 * 60;
	}
	var expires_date = new Date(today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


function getcookie(name) 
{
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length)))
	{
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape( document.cookie.substring(len, end));
}



function deletecookie(name, path, domain) 
{
	if (getcookie(name)) 
		document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



selectMethods = {

	add: function(element, value, label)
	{
		element.insert(new Element('option', {value: value}).update(label));
	},
	
	sort: function(element) 
	{
		var lb = element;
		arrTexts = new Array();
		arrValues = new Array();
		arrOldTexts = new Array();
	
		for(i=0; i<lb.length; i++)
		{
			arrTexts[i] = lb.options[i].text;
			arrValues[i] = lb.options[i].value;
	
			arrOldTexts[i] = lb.options[i].text;
		}
	
		arrTexts.sort();
	
		for(i=0; i<lb.length; i++)
		{
			lb.options[i].text = arrTexts[i];
			for(j=0; j<lb.length; j++)
			{
				if (arrTexts[i] == arrOldTexts[j])
				{
					lb.options[i].value = arrValues[j];
					j = lb.length;
				}
			}
		}
	}
}
	
Element.addMethods('SELECT', selectMethods);


textareaMethods = {

	convertToFCK: function(element, options)
	{
		options = options || {};
		if (!options.ToolbarSet)
			options.ToolbarSet = 'Default';
		if (!options.Height)
			options.Height = element.getHeight();
		
		var oFCKeditor = new FCKeditor(element.id) ;
		oFCKeditor.BasePath	= '/_includes/FCKeditor/';
		oFCKeditor.Height = options.Height;
		oFCKeditor.ToolbarSet = options.ToolbarSet;
		oFCKeditor.ReplaceTextarea() ;
	}
}
	
Element.addMethods('TEXTAREA', textareaMethods);

