document.observe('dom:loaded', function() {
	
	//	disable non-js solution and switch to js enhanced onces
	$(document.body).removeClassName('nojs').addClassName('js');
	
	//	animate submenu states
	$$('.menu ul li').each( function (el) {
		if (ul = el.down('ul')) {
			ul.hide();
			el.observe('mouseover', function (e) {
				if (this.effect) this.effect.cancel();
				this.effect = new Effect.Appear(this.down('ul'), {
					duration: 0.5,
					delay: 0.5
				});
			});
			el.observe('mouseout', function (e) {
				this.effect = new Effect.Fade(this.down('ul'), {
					duration: 0.5
				});
			});
		}
	});
	
	if (t = $('gallery')) {
		if (t.down('.thumbnails'))	Gallery.init('gallery');	
	}
  
  function showCodeForm() {
    new Effect.Fade($('register'), {
      duration: 1
    });
    new Effect.Appear($('regcode'), {
      duration: 1,
      delay: 1
    });
  }

  if ($('entercode')) {
    $('entercode').observe('click', function (e) {
      showCodeForm();
      Event.stop(e);
    });
  }
  
  
  progressArticleAdInit();
});

var Gallery = {
	imgArray: new Array(),
	current: 0,
	
	init: function (cont) {
		//	define elements
		this.container = $(cont);
		this.topImg = this.container.down('.top-img');
		this.bottomImg = this.container.down('.bottom-img');
		this.thumbs = this.container.select('.thumbnail');
		this.slider = this.container.down('.thumbnails');
		this.num_thumbs = this.thumbs.length;
		this.prevBtn = this.container.down('.prev');
		this.nextBtn = this.container.down('.next');
		
		//	init functions
		this.thumbNum=0;
		this.thumbs.each( function (tmb, i) {
			this.setupThumb(tmb, i);
			this.thumbNum++;
		}.bind(this));
		this.nextBtn.observe('click', function() {
			this.slide(4);
		}.bind(this));
		this.prevBtn.observe('click', function() {
			this.slide(-4);
		}.bind(this));
		this.setupNavHover();
		if (this.thumbNum < 5) this.nextBtn.hide();
		this.prevBtn.hide();
		this.slider.setStyle('left: 0px');
	},
	
	setupThumb: function (tmb, i) {
		//	preload img
		this.imgArray[i] = new Image();
		this.imgArray[i].src = tmb.href;
		
		//	assign img switching
		tmb.observe('click', function(e) {
			e.stop();
			var btn = e.element();
      if (!btn.onclick)
      {
        var mp = $('mediaplayer');
        if (mp) mp.remove();
      }
			var nextImg = btn.href;
			var prevImg = this.topImg.style.backgroundImage;
			this.bottomImg.setStyle('opacity: 1; background-image:' + nextImg);
			this.topImg.setStyle('opacity: 0; background-image: url('+nextImg + ')');
			if (this.effect) this.effect.cancel();
			this.effect = new Effect.Parallel(
				[
					new Effect.Morph(this.topImg, {
						style: 'opacity: 1',
						sync: true
					}),
					new Effect.Morph(this.bottomImg, {
						style: 'opacity: 0',
						sync: true
					})
				], 
				{
					duration: 0.5
				}
			);
		}.bind(this));
	},
	
	setupNavHover: function() {
		this.container.select('.btn').each( function (btn) {
			btn.setStyle({opacity: 0.2});
			btn.observe('mouseover',function() {
				if (this.effect) this.effect.cancel();
				this.effect = new Effect.Morph(this, {
					style: 'opacity: 1',
					duration: 0.2
				});
			});
			btn.observe('mouseout', function() {
				if (this.effect) this.effect.cancel();
				this.effect = new Effect.Morph(this, {
					style: 'opacity: 0.2',
					duration: 0.4
				});
			});
		});
	},
	
	slide: function(n) {
		var new_num = this.current + n;
		if (new_num < 1) new_num = 0;
		if (new_num > this.num_thumbs-4) new_num = this.num_thumbs-4;
		if (new_num != this.current) {
			this.current = new_num;
			if (this.current > 0) this.prevBtn.show(); else this.prevBtn.hide();
			if (this.current < this.num_thumbs-4) this.nextBtn.show(); else this.nextBtn.hide();
			new Effect.Morph( this.slider, {
				style: 'left: ' + (0 - (new_num) * 115) + 'px',
				duration: 0.5
			});
		}
	}
}



function progressArticleAdInit()
{
	if($('varinex-article-ad'))
	{
		progressArticleAd();
		
		Event.observe(window, 'scroll', function(){
			progressArticleAd();
		});
	}
}


function progressArticleAd()
{
	var scroll = document.viewport.getScrollOffsets();
	var ad = $('scroll-end').positionedOffset();
	
	var height = document.viewport.getHeight();
	var body = $('page-wrapper').getDimensions();
	
	if((height+scroll[1]) >= ad[1])
	{
		showArticleAd();
	}
	else
	{
		hideArticleAd();
	}
}

function showArticleAd()
{
	$('varinex-article-ad').show();
	new Effect.Morph($('varinex-article-ad'),
	{	style:'right:10px; bottom:40px;',
		duration:0.2});
}


function hideArticleAd()
{
	new Effect.Morph($('varinex-article-ad'),
			{	style:'right:-410px; bottom:-100px;',
				duration:0.2});
	//$("varinex-article-ad").hide();
}
