var animationInProgress = false;
// var list_all_link = false;

Event.observe(window, 'load', function(e) {
	/*
	Event.observe(document, 'mousemove', function(event) {
		mouseY = Event.pointerY(event);
		mouseX = Event.pointerX(event);
		
		$('ajax_loader').setStyle({ top: (mouseY - 10) + 'px', left: (mouseX + 10) + 'px' });
	});
	*/
	$$('.aanestys_right img').invoke('observe', 'click', submitFrontPageVoteForm);
	$$('#vote_stars li').invoke('observe', 'mouseover', voteMouseOver);
	$$('#vote_stars li').invoke('observe', 'click', voteClick);
	$$('.clearable').invoke('observe', 'focus', inputFocus);
	$$('#small_images_list li').invoke('observe', 'click', imageClick);
	$$('#delete_button').invoke('observe', 'click', imageDelete);
	$$('#description').invoke('observe', 'keydown', descriptionCount);
	$$('#description').invoke('observe', 'keyup', descriptionCount);
	/*
	$$('#navi li').invoke('observe', 'mouseover', naviMouseOver);
	$$('#navi').invoke('observe', 'mouseover', naviMouseOut);
	*/
	$$('.flash_video').invoke('observe', 'click', loadFlashVideo);
	$$('#popup').invoke('observe', 'click', loadPopup);
	$$('#move_frame_left').invoke('observe', 'click', moveFrameLeft);
	$$('#move_frame_right').invoke('observe', 'click', moveFrameRight);
	$$('.error').each(function(e) { new Effect.Highlight(e); });
	$$('.msg').each(function(e) { new Effect.Highlight(e); });
	$$('.selectable').invoke('observe', 'click', changeImage);
	if($('movable_frame')) {
		pos = 0;
		$('movable_frame').select('li').each(function(e) { if(e.down('img').next('img').title == $('frame_name').value) { moveFrameToPos(pos); } else { pos++; } });
	}
	if($('frame'))
		$('frame').observe('click', openLargeFrame);
	if($('main_large_image'))
		$('main_large_image').observe('click', openLargeImage);
	$$('.vote_finalist').invoke('observe', 'mouseenter', function(e) { this.select('a').each(function(e) { e.show(); }); } );
	$$('.vote_finalist').invoke('observe', 'mouseleave', function(e) { this.select('a').each(function(e) { e.hide(); }); } );
	$$('.vote_girl').invoke('observe', 'click', doFinalistVote);
	$$('.redo_vote').invoke('observe', 'click', returnToVote);
	if($('finalists_name'))
		new Lightbox();
});

returnToVote = function(e) {
	$('vote_form').hide();
	$('finalist_list').show();
}

doFinalistVote = function(e) { 
	$('finalists_name').innerHTML = this.title;
	$('finalist_value').value = this.title;
	var clonedNode = this.previous('img').cloneNode(false);
	if($('finalist_placeholder').down('img'))
		$('finalist_placeholder').down('img').remove();
	$('finalist_placeholder').insert(clonedNode);
	$('finalist_list').hide(); $('vote_form').show();
}

voteMouseOver = function(e) {
	// alert('jee');
	_this = this;
	_flag = false;
	this.up('ul').select('li').each(function(e) { e.removeClassName('selected') });
	this.up('ul').select('li').each(function(e) { if(_flag != true) {  if(e == _this) { _flag = true; e.addClassName('selected'); return; } e.addClassName('selected') } });
}

voteClick = function(e) {
	_count = 0;
	$$('#vote_stars li').invoke('stopObserving', 'mouseover');
	this.up('ul').select('li').each(function(e) { if(e.className == 'selected') _count++; } );
	$('grade').value = _count;
	$('vote_form').submit();
}

inputFocus = function(e) {
	if(!this.hasClassName('cleared')) {
		this.value = '';
		this.addClassName('cleared');
		if(this.hasClassName('password')) {
			clone = this.cloneNode(false);
			clone.type = 'password';
			this.parentNode.replaceChild(clone, this);
			$('login_form_password').focus();
		}
	}
}

imageClick = function(e) {
	// alert('jee');
	small_params = this.down('img').src.toQueryParams();
	// alert(params);
	big_params = $('main_large_image').src.toQueryParams();
	big_params.src = small_params.src;
	$('main_large_image').src = 'phpThumb/phpThumb.php?src=' + escape(big_params.src) + '&w=' + big_params.w;
}

imageDelete = function(e) {
	if(confirm('Haluatko varmasti tuhota kyseisen kuvan?')) {
		_target = this.up('div');
		window.location = 'delete_image.php?id=' + _target.down('img').title
	}
}

descriptionCount = function(e) {
	limit = 500;
	if(this.value.length > limit)
		this.value = this.value.substring(0, limit);

}

naviMouseOver = function(e) {
	Event.stop(e);
	this.up('ul').select('li').each(function(e) { if(e.className != 'selected') { e.down('img').src = e.down('img').src.replace('_selected', ''); } });
	if(this.className != 'selected')
		this.down('img').src = this.down('img').src.replace('.gif', '_selected.gif');
}

naviMouseOut = function(e) {
	// alert('out');
	this.select('li').each(function(e) { if(e.className != 'selected') { e.down('img').src = e.down('img').src.replace('_selected', ''); } });
}

moveFrameToPos = function(pos) {
	width = $('movable_frame').down('li').getWidth();
	if(pos == 0) {
		$('move_frame_right').addClassName('unclickable');
	} else if(pos == ($('movable_frame').select('li').length - 1)) {
		$('move_frame_left').addClassName('unclickable');
	}
	setAnimationTrue();
	new Effect.Move($('movable_frame'), { afterFinish: setAnimationFalse, x: (-width * pos) } );
}

setAnimationFalse = function() {
	animationInProgress = false;	
}
setAnimationTrue = function() {
	animationInProgress = true;	
}

moveFrameLeft = function(e) {
	pos = $('movable_frame').positionedOffset();
	width = $('movable_frame').down('li').getWidth();
	count = $('movable_frame').select('li').length;
	cal_pos = Math.abs(Math.round(pos[0] / width));
	if(cal_pos < (count - 1) && animationInProgress == false) {
		setAnimationTrue();
		new Effect.Move($('movable_frame'),
			{ x: -width, afterFinish: function(e)
				{
					setAnimationFalse();
					$('frame_name').value = $('movable_frame').select('li')[cal_pos + 1].down('img').next('img').title;
					if($('move_frame_right').hasClassName('unclickable'))
						$('move_frame_right').removeClassName('unclickable');
					if(cal_pos == (count - 2)) {
						$('move_frame_left').addClassName('unclickable');
					}
				}
			}
		);
	}
}

moveFrameRight = function(e) {
	pos = $('movable_frame').positionedOffset();
	width = $('movable_frame').down('li').getWidth();
	count = $('movable_frame').select('li').length;
	cal_pos = Math.abs(Math.round(pos[0] / width));
	// alert(cal_pos);
	if(cal_pos > 0 && animationInProgress == false) {
		setAnimationTrue();
		new Effect.Move($('movable_frame'), { x: width, afterFinish: function(e)
				{
					setAnimationFalse();
					$('frame_name').value = $('movable_frame').select('li')[cal_pos - 1].down('img').next('img').title;
					if($('move_frame_left').hasClassName('unclickable'))
						$('move_frame_left').removeClassName('unclickable');
					if(cal_pos == 1) {
						$('move_frame_right').addClassName('unclickable');
					}
				}
			}
		);
	}
}

changeImage = function(e) {
	src = this.src;
	q_str = src.toQueryParams();
	$('image_id').value = this.title;
	$$('.changeable_image').each(function(e) { big_params = e.src.toQueryParams(); e.src = 'phpThumb/phpThumb.php?src=' + escape(q_str.src) + '&w=' + big_params.w + '&h=' + big_params.h + '&zc=1'; });
}

loadFlashVideo = function(e) {
	if(e)
		Event.stop(e);
	if(!e)
		var _this = $('main_container');
	else
		var _this = this;
	$$('.hide_flash').each(function(e) { e.style.visibility = 'hidden' });
	var href = "flash/flash_video.html";
	if(!$('overlay')) {
		_this.up('body').insert('<div id="overlay" style="display: none;"></div>');
		_this.up('body').insert('<div id="flash_container"><div style="margin: 0 auto; text-align: center; width: 682px; height: 410px; background: url(images/videobox.png) no-repeat; padding-top: 15px;"></div></div>');
		$('flash_container').setStyle({ position: 'absolute', height: '410px', zIndex: 100, width: '100%'});
	}
	$('overlay').style.height = _this.up('body').getHeight()+'px';
	var pos = document.viewport.getScrollOffsets();
	$('flash_container').style.top = (pos.top + 50) + 'px';
		
	new Effect.toggle($('overlay'), 'appear', { from: 0, to: 0.8, duration: 0.5, afterFinish: function(e) { $('flash_container').down('div').insert('<iframe scrolling="no" src="video/malliksi_paivassa_teaser.html" style="width: 640px; height: 360px;" frameborder="0"></iframe><img class="close" src="images/suljeikkuna.png" alt="Sulje ikkuna">'); $$('.close').invoke('observe', 'click', function(e) { $('flash_container').remove(); $$('.hide_flash').each(function(e) { e.style.visibility = 'visible' }); if($('flash_preview')) $('flash_preview').style.visibility = 'visible'; $('overlay').remove(); }); } });
}

loadPopup = function(e) {
	if(e)
		Event.stop(e);
	if(!e)
		var _this = $('main_container');
	else
		var _this = this;
	$$('.hide_flash').each(function(e) { e.style.visibility = 'hidden' });
	var href = "popup/popup.php";
	if(!$('overlay')) {
		_this.up('body').insert('<div id="overlay" style="display: none;"></div>');
		_this.up('body').insert('<div id="popup_container"><div style="margin: 0 auto; text-align: center; width: 682px; height: 410px; background: url(images/videobox.png) no-repeat; padding-top: 15px;"></div></div>');
		$('popup_container').setStyle({ position: 'absolute', height: '410px', zIndex: 100, width: '100%'});
	}
	$('overlay').style.height = _this.up('body').getHeight()+'px';
	var pos = document.viewport.getScrollOffsets();
	$('popup_container').style.top = (pos.top + 50) + 'px';
		
	new Effect.toggle($('overlay'), 'appear', { from: 0, to: 0.8, duration: 0.5, afterFinish: function(e) { $('popup_container').down('div').insert('<iframe scrolling="no" src="' + href + '" style="width: 640px; height: 360px;" frameborder="0"></iframe><img class="close" src="images/suljeikkuna.png" alt="Sulje ikkuna">'); $$('.close').invoke('observe', 'click', function(e) { $('popup_container').remove(); $$('.hide_flash').each(function(e) { e.style.visibility = 'visible' }); $('overlay').remove(); }); } });
}

openLargeFrame = function(e) {
	if(e)
		Event.stop(e);
	if(!e)
		var _this = $('main_container');
	else
		var _this = this;
	$$('.hide_flash').each(function(e) { e.style.visibility = 'hidden' });
	
	if(!$('overlay')) {
		_this.up('body').insert('<div id="overlay" style="display: none;"></div>');
		_this.up('body').insert('<div id="popup_container"><div style="margin: 0 auto; background: white no-repeat; padding: 25px; position: relative;"></div></div>');
		$('popup_container').setStyle({ position: 'absolute', height: '410px', zIndex: 100, width: '100%'});
	}
	$('overlay').style.height = _this.up('body').getHeight()+'px';
	var pos = document.viewport.getScrollOffsets();
	$('popup_container').style.top = (pos.top + 50) + 'px';
	_i = 0;
	var image;
	var frame;
	this.select('img').each(
		function(e) {
			if(_i == 0) {
				image = e;
			} else {
				frame = e;
			}
			_i++;
		}
	);
	image_params = image.src.toQueryParams();
	frame_params = frame.src.toQueryParams();
	_top = image.style.top;
	_left = image.style.left;
	bigimage = '<img src="phpThumb/phpThumb.php?src=' + image_params.src + '&amp;w=' + (image_params.w * 3) + '&amp;h=' + (image_params.h * 3) + '&amp;zc=T&amp;aoe=1" style="';
	bigimage += 'top: ' + _top + ';';
	bigimage += 'left: ' + _left + ';';
	bigimage += '" />';
	$('popup_container').down('div').setStyle({ width: (frame_params.w * 3) + 'px', height: ((frame_params.h * 3) + 20) + 'px' });
	bigframe = '<img src="phpThumb/phpThumb.php?src=' + frame_params.src + '&amp;w=' + (frame_params.w * 3) + '&amp;h=' + (frame_params.h * 3) + '&amp;f=png&amp;zc=1"  />';
	new Effect.toggle($('overlay'), 'appear', { from: 0, to: 0.8, duration: 0.5, afterFinish: function(e) { $('popup_container').down('div').insert('<div class="frame" style="width: ' + (frame_params.w * 3) + 'px; height: ' + (frame_params.h * 3) + 'px;">' + bigimage + bigframe + '</div><img class="close_frame" src="images/suljeikkuna.png" alt="Sulje ikkuna">'); $$('.close_frame').invoke('observe', 'click', function(e) { $('popup_container').remove(); $$('.hide_flash').each(function(e) { e.style.visibility = 'visible' }); $('overlay').remove(); }); } });
}

openLargeImage = function(e) {
	if(e)
		Event.stop(e);
	if(!e)
		var _this = $('main_container');
	else
		var _this = this;
	$$('.hide_flash').each(function(e) { e.style.visibility = 'hidden' });
	
	if(!$('overlay')) {
		_this.up('body').insert('<div id="overlay" style="display: none;"></div>');
		_this.up('body').insert('<div id="popup_container"><div style="margin: 0 auto; background: white no-repeat; padding: 25px; position: relative; width: 869px; text-align: center;"></div></div>');
		$('popup_container').setStyle({ position: 'absolute', height: '410px', zIndex: 100, width: '100%'});
	}
	$('overlay').style.height = _this.up('body').getHeight()+'px';
	var pos = document.viewport.getScrollOffsets();
	$('popup_container').style.top = (pos.top + 50) + 'px';
	_i = 0;
	image = this;
	image_params = image.src.toQueryParams();
	
	bigimage = '<img src="phpThumb/phpThumb.php?src=' + image_params.src + '&amp;w=829&amp;h=514&amp;aoe=1" />';
	new Effect.toggle($('overlay'), 'appear', { from: 0, to: 0.8, duration: 0.5, afterFinish: function(e) { $('popup_container').down('div').insert(bigimage + '<br /><br /><img class="close_frame" src="images/suljeikkuna.png" alt="Sulje ikkuna">'); $$('.close_frame').invoke('observe', 'click', function(e) { $('popup_container').remove(); $$('.hide_flash').each(function(e) { e.style.visibility = 'visible' }); $('overlay').remove(); }); } });
}

submitFrontPageVoteForm = function(e) {
	var val = this.title;
	$('hidden_frontpage_grade').value = val;
	$('vote_frontpage').submit();
}