var public_releases_by_company_rules = {
		
//	'.press-release h2 a' : function(el)
//	{
//		var parent = el.up().up();
//		var offset = parent.viewportOffset();
//		var elttip = parent.down('.tooltip');
//		if (elttip) {
//			new Tooltip(el, elttip, {
//				min_distance_x: 15,
//				min_distance_y: 5/*,
//				delta_x: -offset[0],
//				delta_y: -offset[1]
//				*/
//			});
//		}
//	},

	'body' : function(el)
	{
		var label = $$('.sidebar-block-updates form label')[0];
		var title = label.childNodes[0].nodeValue.strip();

		label.hide();

		if ('' == $('form_email').getValue()) {
			$('form_email').setValue(title);
		}

		// hide messages
		if ($('notices')) {
			setTimeout(function(){
				$('notices').fade();
			}, 15000);
		}
	},

	'#form_email' : function(el)
	{
		var _title = $$('.sidebar-block-updates form label')[0].childNodes[0].nodeValue.strip();
		
		el.handleChange = function(e)
		{
			e.stop();

			if ('' == el.getValue()) {
				el.setValue(_title);
			}
		};

		el.handleFocus = function(e)
		{
			e.stop();

			if (_title == el.getValue()) {
				el.setValue('');
			}
		};

		el.observe('change', el.handleChange);
		el.observe('blur', el.handleChange);
		el.observe('focus', el.handleFocus);
	},

	'.itemsperpage-list' : function(el)
	{
		el.onchange = function(event)
		{
			var value = event.target.value;
			var loc = window.location.href.replace(new RegExp(/\?.*/), '');
			window.location = loc + '?items=' + value;
		}
	},

	'div.sidebar-block-content-slider div.slider a.next' : function(el)
	{
		el.handleResult = function(req)
		{
			if (req.responseJSON.is_error) {
				el.hide();
				return;
			}

			var content = $(req.responseJSON.result.type).down('.items')
			var newCell = content.down('.item').clone(true);
			newCell.down('a').writeAttribute('href', req.responseJSON.result.link);
			newCell.down('img').writeAttribute('alt', req.responseJSON.result.title);
			newCell.down('img').writeAttribute('title', req.responseJSON.result.title);
			newCell.down('img').writeAttribute('src', req.responseJSON.result.thumb);

			content.insert({
				bottom: newCell
			});

			_loading[req.responseJSON.result.type] = false;
			
			Behaviour.applySheet({
				'#media-graphics .content .items .item a' : public_releases_by_company_rules['#media-graphics .content .items .item a']
			});

			myLightWindow._resetLinks();
			myLightWindow._setupLinks();
			myLightWindow._setupActions();
		};

		el.handleClick = function(e)
		{
			e.stop();

			var content = el.up().down('.items');
			var margin = content.offsetLeft;
			var count = parseInt(el.up().down('.content').down('.items-count').innerHTML, 10);
			var exists = el.up().down('.content').down('.items').childElements().length;
			var imagesTotal = (Math.abs(margin) / _offset) + _imgs;

			if (count > imagesTotal) {
				var type = el.up('.slider').up().readAttribute('id');
				
				if (exists < count && imagesTotal + 1 > exists && !_loading[type]) {
					_loading[type] = true;
					
					new Ajax.Request("/public/company/55/js", {
						parameters: {
							type: type,
							exists: exists
						},
						onSuccess: el.handleResult,
						evalJSON: 'force',
						evalJS: false
					});
				}
				
				if (0 != margin % _offset) {
					margin = Math.floor(margin / _offset) * _offset;
				}

				new Effect.Move(content, {
					x: (margin - _offset),
					mode: 'absolute',
					transition: Effect.Transitions.linear,
					queue: 'end'
				});
				
				el.up().down('a.prev').show();

				if (count <= imagesTotal + 1) {
					el.hide();
				}
			} else {
				el.hide();
			}
		};

		el.observe('click', el.handleClick);

		if (el.up().down('.content').down('.items').childElements().length > _imgs) {
			el.show();
		}
	},

	'div.sidebar-block-content-slider div.slider a.prev' : function(el)
	{
		el.handleClick = function(e)
		{
			e.stop();

			var content = el.up().down('.items');
			var margin = content.offsetLeft;
			var newMargin = margin + _offset;

			if (0 != newMargin % _offset) {
				newMargin = Math.ceil(newMargin / _offset) * _offset;
			}

			new Effect.Move(content, {
				x: newMargin,
				mode: 'absolute',
				transition: Effect.Transitions.linear,
				queue: 'end',
				afterFinish: function (effect) {
					if (effect.originalLeft + _offset >= 0) {
						el.hide();
					}
				}
			});
			
			el.up().down('a.next').show();
		};

		el.observe('click', el.handleClick);
	},

	'#media-graphics .content .items .item a' : function(el)
	{
		el.handleMouseEnter = function(e)
		{
			e.stop();

			_showDialog(e, el.down('img').readAttribute('alt'));
		};

		el.handleMouseLeave = function(e)
		{
			e.stop();

			_hideDialog();
		};

		el.observe('mouseover', el.handleMouseEnter);
		el.observe('mouseleave', el.handleMouseLeave);
	}
};

Behaviour.register(public_releases_by_company_rules);

var _offset = 100;
var _imgs = 3;
var _loading = new Array();

var _showDialog = function(e, title)
{
	var dialog = $('graphics-info');

	if (!dialog) {
		dialog = new Element('div', {id: 'graphics-info', 'class': 'tooltip'}).insert({
			top: new Element('span'),
			bottom: new Element('div', {'class': 'arrow'})
		});

		$('media-graphics').insert({
			bottom: dialog
		});
	}

	dialog.down('span').update(title.replace(/\n/, '<br />'));

	dialog.setStyle({
		position: 'absolute',
		top: (e.pointerY() - 20) + 'px',
		left: (e.pointerX() + 20) + 'px'
	});
	
	dialog.show();
};

var _hideDialog = function()
{
	var dialog = $('graphics-info');
	if (dialog) {
		dialog.hide();
	}
}
