// JavaScript Document

function ValidateMediaCat(form, labels)
{
	if(RemoveSpaces(form.elements["TitleA"].value) == "")
		return Error(labels.titleA, form.elements["TitleA"]);
	if(RemoveSpaces(form.elements["TitleE"].value) == "")
		return Error(labels.titleE, form.elements["TitleE"]);
	return true;
}

function ValidateMediaItem(form, validateFile, labels)
{
	if(RemoveSpaces(form.elements["TitleA"].value) == "")
		return Error(labels.titleA, form.elements["TitleA"]);
	if(RemoveSpaces(form.elements["TitleE"].value) == "")
		return Error(labels.titleE, form.elements["TitleE"]);
	if(validateFile)
	{
		element = (form.elements["Upload"].checked) ? form.elements["File"][0] : form.elements["File"][1];
		msg = (form.elements["Upload"].checked) ? labels.file : labels.url;
		if(RemoveSpaces(element.value) == "")
			return Error(msg, element);
	}
	return true;
}

function SwitchFileStat(element)
{
	fields = element.form.elements["File"];
	fields[0].disabled = ! element.checked;
	fields[0].className = (fields[0].disabled) ? "FormText Width150 Disabled" : "FormText Width150";
	fields[1].disabled = element.checked;
	fields[1].className = (fields[1].disabled) ? "FormText Width150 Disabled" : "FormText Width150";
}

// POPUPS

var getPopup = function(id)
{
	// get or create element
	element = $(id);
	if(element == null)
		element = new Element('div', {'id': id});
	else
		if(element.retrieve('popup') == true)
			return element;
	// create and organize additional elements
	/**
	the sturucture becomes
	div.popup-container
	|- div.popup-bg
	|- div.popup-win
	   |- div.popup-title-bar
	      |- a.popup-close-link
	         |- img.popup-close-img
	      |- span.popup-title
	   |- div.popup-frame#{id} --> the actual element with the request object
	*/
	container = new Element('div', {'class': 'popup-container closed'});
	bg = new Element('div', {'class': 'popup-bg'});
	bg.setStyle('opacity', 0.5);
	win = new Element('div', {'class': 'popup-win'});
	titlebar = new Element('div', {'class': 'popup-title-bar'});
	closeLink = new Element('a', {'class': 'popup-close-link', 'href': window.location.href});
	closeLink.addEvent('click', function(e)
	{
		e.stop();
		this.getParent('.popup-container').getElement('.popup-frame').close();
	});
	closeImg = new Element('img', {'class': 'popup-close-img', 'src': 'popup-close.gif'});
	titleEl = new Element('span', {'class': 'popup-title'});
	if(! element.hasClass('popup-frame'))
		element.addClass('popup-frame');
	// create the structure by injecting elements
	closeImg.inject(closeLink);
	closeLink.inject(titlebar);
	titleEl.inject(titlebar);
	titlebar.inject(win);
	element.inject(win);
	bg.inject(container);
	win.inject(container);
	container.inject(document.body);
	
	container.resize = function()
	{
		// reset pos
		win = this.getElement('.popup-win');
		win.erase('style');
		side = ($(document.body).getStyle('direction') == 'rtl') ? 'right' : 'left';
		win.setStyles({side: '0px', 'top': '0px'});
		win.setStyle(side, '0px');
		// hide the popup
		this.setStyles({'display': 'none'});
		// resize while popup is hidden
		docSize = $(document.body).getScrollSize();
		this.setStyles({'display': 'block', 'height': docSize.y + 'px', 'width': docSize.x + 'px'});
		// resize again while the popup is shown
		docSize = $(document.body).getScrollSize();
		this.setStyles({'display': null, 'height': docSize.y + 'px', 'width': docSize.x + 'px'});
		win.repos();
	}
	
	win.repos = function()
	{
		// reset pos
		side = (document.body.getStyle('direction') == 'rtl') ? 'right' : 'left';
		this.setStyles({side: '0px', 'top': '0px'});
		win.setStyle(side, '0px');
		// set pos
		docSize = $(document.body).getScrollSize();
		winSize = this.getSize();
		if(docSize.x > winSize.x)
			this.setStyle(side, ((docSize.x - winSize.x)/2).round() + 'px');
		docHeight = document.body.getHeight();
		if(docHeight > winSize.y)
			this.setStyle('top', ((docHeight - winSize.y)/2).round() + 'px');
	}
	
	element.fixSize = function()
	{
		//element = this.getElement('.popup-frame');
		this.setStyles({'overflow': 'auto'});
		elSize = this.getSize();
		if(! Browser.Engine.trident)
		{
			elSize.x -= this.getStyle('padding-left').toInt();
			elSize.x -= this.getStyle('padding-right').toInt();
			elSize.y -= this.getStyle('padding-bottom').toInt();
			elSize.y -= this.getStyle('padding-top').toInt();
		}
		this.setStyles({'height': elSize.y + 'px', 'overflow': 'hidden', 'width': elSize.x + 'px'});
	}
	
	// set popup title
	element.setTitle = function(t)
	{
		this.getParent('.popup-container').getElement('.popup-title').set('html', t);
		return this;
	}
	
	// close popup (cancels current request)
	element.close = function()
	{
		sc = this.retrieve('scroll');
		$(document.body).scrollTo(sc.x, sc.y);
		this.get('load').cancel();
		this.empty();
		container = this.getParent('.popup-container');
		container.get('morph').addEvent('complete', function()
		{
			//this.subject.getElement('.popup-frame').empty();
			this.subject.addClass('closed');
			this.removeEvents('complete');
		});
		container.morph({'opacity': 0});
	}
	
	element.expand = function()
	{
		// paddings
		pBottom = this.getStyle('padding-bottom').toInt();
		pLeft = this.getStyle('padding-left').toInt();
		pRight = this.getStyle('padding-right').toInt();
		pTop = this.getStyle('padding-top').toInt();
		// currenct size
		currentSize = this.getSize();
		if(! Browser.Engine.trident)
		{
			currentSize.x -= pLeft;
			currentSize.x -= pRight;
			currentSize.y -= pBottom;
			currentSize.y -= pTop;
		}
		// new size
		this.setStyles({'height': 'auto', 'width': 'auto'});
		newSize = this.getSize();
		if(! Browser.Engine.trident)
		{
			newSize.x -= pLeft;
			newSize.x -= pRight;
			newSize.y -= pBottom;
			newSize.y -= pTop;
		}
		// coordinates
		docSize = $(document.body).getSize();
		win = this.getParent('.popup-win');
		winSize = win.getSize();
		side = (document.body.getStyle('direction') == 'rtl') ? 'right' : 'left';
		newSide = 0;
		if(docSize.x > winSize.x)
			newSide = ((docSize.x - winSize.x)/2).round() + 'px';
		newTop = 0;
		if(docSize.y > winSize.y)
			newTop = ((docSize.y - winSize.y)/2).round() + 'px';
		// restore current size
		this.setStyles({'height': currentSize.y + 'px', 'width': currentSize.x + 'px'});
		// animate
		this.get('morph').addEvent('complete', function()
		{
			this.subject.getParent('.popup-container').resize();
			this.removeEvents('complete');
		});
		this.morph({'height': newSize.y + 'px', 'width': newSize.x + 'px'});
		win.morph({side: newSide + 'px', 'top': newTop + 'px'});
	}
	
	// open popup on request
	element.get('load').addEvent('request', function()
	{
		element = this.options.data;
		element.store('scroll', $(document.body).getScroll());
		$(document.body).scrollTo(0, 0);
		// remove old data and view (Loading ...)
		element.empty();
		element.erase('style');
		loading = new Element('div', {'class': 'popup-loading'});
		element.adopt(loading);
		// resize container
		container = element.getParent('.popup-container');
		container.setStyle('opacity', 0);
		container.resize();
		container.removeClass('closed');
		container.morph({'opacity': 1});
		container.resize();
		element.fixSize();
		container.getElement('.popup-win').repos();
	});
	
	element.get('load').addEvent('success', function()
	{
		element = this.options.data;
		element.expand();
	});
	// return
	element.store('popup', true);
	return element;
}

function resizePopups()
{
	$$('.popup-container').each(function(element)
	{
		element.resize();
	});
}
window.addEvent('resize', resizePopups);