var changingMorph = false;
var timeoutID = false;

var rollAutomatically = function() {
	var cur = $('top').getElement('.ponuka_hover');
	var el = false;
	
	if (!cur.getParent().getNext('li'))
		el = $('top').getElement('.ponuka');
	else
		el = cur.getParent().getNext('li').getElement('.ponuka');
	
	cur.removeClass('ponuka_hover');
	el.addClass('ponuka_hover');
	roll(el, 1000);
	timeoutID = rollAutomatically.delay(8000);
}

var roll = function(thisElement, duration) {
	var i = 0;
	
	$('top').getElements('a').each(function(element) {	if (element == thisElement) thisElementID = i; else i++;});
	
	var changing = new Element('div', {
		'class' : 'img_changing',
		'styles' :  {
			'opacity' : 0.1,
			'background-image': 'url(/pub/local/img/0'+(thisElementID+1)+'.jpg)'
		}
	});
	
	if($('top').getLast('.img_changing'))
		changing.inject($('top').getLast('.img_changing'), 'after');
	else
		changing.inject($('top'), 'top');

	if (changingMorph !== false) {
		changingMorph.cancel();
	}
	
	changingMorph = new Fx.Morph(changing, {
		'duration' : duration,
		'onComplete' : function(el) {
			el.setStyle('opacity', 1);
			el.getParent().setStyle('background-image', 'url(/pub/local/img/0'+(thisElementID+1)+'.jpg)')
			changing.getParent().getChildren('div.img_changing').each(function(eee){eee.destroy()});
		}
	}).start({
		'opacity' : '1'
	});
}

window.addEvent('domready', function() {
	timeoutID = rollAutomatically.delay(3000);
	$$('.ponuka').each(function (el){
		el.addEvent('mouseover', function(e) {
			
			$clear(timeoutID);
			$('top').getElement('.ponuka_hover').removeClass('ponuka_hover');
			this.addClass('ponuka_hover');
			roll(this, 500);
		});
		el.addEvent('mouseleave', function() {
			timeoutID = rollAutomatically.delay(8000);
		});
	});
	
	ReMooz.assign('#gallery a', {
		'origin': 'img',
		'zIndex' : 500,
		'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
		'resizeFactor': 0.8, // resize to maximum 80% of screen size
		'cutOut': false, // don't hide the original
		'opacityResize': 0.4, // opaque resize
		'dragging': false, // disable dragging
		'centered': true // resize to center of the screen, not relative to the source element
		
	});
	if ($('m_form')) {
		var morph = false;
		$('m_form_open').addEvent('click', function (){
			if (!this.hasClass('opened')) {
				$('m_form_open').set('src', '/pub/local/img/Bspat.png');
				var height = (($('content').getSize().y > $('m_form').getSize().y)?$('content').getSize().y:$('m_form').getSize().y)+'px';
				$('m_form').setStyles({
					'display': 'block',
					'height' : '65px',
					'width' : '0',
					'overflow' : 'hidden'
				});
				if(morph)
					morph.cancel();
				morph = new Fx.Morph($('m_form'), {
					'duration' : 200
				}).start({
					'width' : '570px'
				}).chain(function() {
					morph = new Fx.Morph($('m_form'), {
						'duration' : 200
					}).start({
						'height' : height
					})
				});
				
				this.addClass('opened');
			} else {
				if(morph)
					morph.cancel();
				morph = new Fx.Morph($('m_form'), {
					'onComplete' : function() {
						$('m_form').setStyles({
							'display': 'none',
							'height' : 'auto'
						});
					}
				}).start({
					'width' : '0',
					'height' : '0'
				});
				$('m_form_open').set('src', '/pub/local/img/Bpridajtesaknam.png');
				
				
				
				this.removeClass('opened');
			}
			
		});
	}
});









