$(function() {

	/*	videoplayer	*/

	$('a.video').click(function(){
		$('<div class="body_overlay video"></div>')
			.click(function(evt) {
				if (evt.target.nodeName.toLowerCase() != 'object') {
					$(this).remove();
				}
			})
			.flash({
				swf : '_sys/swf/flvplayer.swf',
				flashvars : {
					videoURL : '../../' + $(this).attr('href'),
					smoothing : true
				},
				height: 394,
				width: 696
			})
			.end()
			.appendTo('body')
			.append(
				$('<button>X</button>').click(function(){
					$(this).parent().remove();
				})
			);
		return false;
	});


	/*	song player	*/
	var playerCount = 0;
	$('a[href$=".mp3"]').each(function() {
		var soundURL = $(this).attr('href');
		var playName = '_simpplay_' + ++playerCount;

		$('<span id="span' + playName + '"></span>')
			.prependTo(this)
			.flash({
				allowScriptAccess : 'always',
				swf : '_app/swf/simp3l.swf',
				id : 'flash' + playName,
				flashvars : {
					soundURL : soundURL,
					btnCol : 0xF9EAA4,
					btnURL : '_app/img/nuotti.png'
				},
				height : 18,
				width : 14,
				wmode : 'transparent'
			});
		$(this).click(function(evt){
			evt.preventDefault();
			$(this).find('object').get(0).playPauseSong();
		})
	});


	/*	home page news selector */
	var news_length = 0;
	if (news_length = $('body.etusivu div#news_container li').length) {
		var d = $('<div class="news_selector"></div>');
		for (var i = 0; i < news_length; i++) {
			if (i) {
				d.append('<span> - </span>');
			}
			d.append(
				$('<span' + (!i ? ' class="selected"' : '') + '>[ ' + (i + 1) + ' ]</span>')
					.data('n', (i + 1))
					.click(function() {
						$(this)
							.addClass('selected')
							.siblings().removeClass('selected');
						$('#news_container ul li:nth-child(' + $(this).data('n') + ')')
							.show(500)
							.siblings(':visible').hide(300);
				})
			);
		}
		$('div#news_container').prepend(d);
		$('#news_container ul li:nth-child(1)')
			.show()
			.siblings(':visible').hide();

	}


	/*	kädentaitajat slider	*/
/*
	$('#k_taitajat li')
		.mouseenter(function() {
			var active_id = this.id;
			$(this).siblings().each(function() {
				$(this)
					.removeClass('active')
					.find('blockquote:visible').hide(300);
				if ($(this).prev().attr('id') == active_id || $(this).next().attr('id') == active_id) {
					$(this)
						.removeClass('mini')
						.find('img:hidden').show(300);
				}
				else {
					$(this)
						.addClass('mini')
						.find('img:visible').hide(300).end()
						.find('a:visible').hide(300);
				}
			});
			$(this)
				.removeClass('mini').addClass('active')
				.find('img:hidden').show(300).end()
				.find('blockquote:hidden').show(300).end()
				.find('a:hidden').show(300);
		})
		.each(function(index) {
			if (index == 0) {
				$(this).addClass('active');
			}
			if (index > 0) {
				$(this).find('blockquote').hide();
			}
			if (index > 1) {
				$(this)
					.addClass('mini')
					.find('img').hide().end()
					.find('a:visible').hide(500);
			}
		});*/

	$('body.kadentaitajat').mousemove(function(e) {
		var w = $('#k_taitajat_wrap');
		w.data({
			mousex : e.pageX - w.offset().left,
			mousey : e.pageY - w.offset().top
		});
	});

	$('#k_taitajat_wrap.horiz')
		.css('overflow-x', 'hidden')
		.everyTime(20, function() {
			var x = $(this).data('mousex');
			var y = $(this).data('mousey');
			if (y > 0 && y < 300) {
				var ulx = $(this).scrollLeft();
				if (x < 0) { x = 0; }
				if (x > 1000) { x = 1000; }
				x = x - 500;
				$(this).scrollLeft(ulx + (x * x * x / 9000000));
			}
		})
		.animate(
			{ scrollLeft : ($('#k_taitajat li').length * 214) },
			2000
		);

	$('#k_taitajat_wrap.horiz #k_taitajat')
		.css(
			'width',
			($('#k_taitajat li').length * 214) + 'px');

	$('#k_taitajat_wrap.verti')
		.css('overflow-y', 'hidden')
		.everyTime(20, function() {
			var x = $(this).data('mousex');
			var y = $(this).data('mousey');
			if (x > 0 && x < 450) {
				var uly = $(this).scrollTop();
				if (y < 0) { y = 0; }
				if (y > 600) { y = 600; }
				y = y - 300;
				$(this).scrollTop(uly + (y * y * y / 9000000));

			}
		});


	/*	kädentaita title images	*/

	$('#k_taitaja_title_thumbs img')
		.each(function() {
			var src = $(this).attr('src').split('?')[0];
			(new Image()).src = src;
		})
		.click(function(ev) {
			var src = $(this).attr('src').split('?')[0];
			$(this).parent().siblings().find('img').removeClass('selected');
			$('#k_taitaja_title_image').fadeOut(600, function() {
				$(this).attr('src', src);
				$(this).fadeIn(600);
			});
			$(this).addClass('selected');
			ev.preventDefault();
		});


	/*	pysy hereillä slider	*/

	$('body.kahvitauko.hereilla ul#slides li').each(function(index) {
		$(this).css('position: absolute');
		if (index) {
			$(this).fadeOut();
		}
	});

	$('body.kahvitauko.hereilla ul#controls a')
		.click(function() {
			var a = $(this);
			var li = $('ul#slides li').eq(a.text() - 1);
			a.addClass('selected').parent().siblings().find('a').removeClass('selected');
			li.fadeIn(300).siblings(':visible').fadeOut(300);
			return false;
		})
		.eq(0).addClass('selected');


	/*	oivallus paginator	*/
	$('#oivallus_paginator form')
		.find('select').change(function() {
			this.form.submit();
		}).end()
		.find('input[type="submit"]').remove();

	/*	tarina images	*/

	$('body.tarina div#historical p.selector a').click(function(ev) {
		var qs = $(this).attr('href').split('?')[1];
		var img_src = $(this).find('img').attr('src').split('?')[0];
		$('#historical div.main').fadeOut(600);
		$(this).siblings().removeClass('selected');
		$('body.tarina div#historical img.main').fadeOut(600, function() {
			$(this).attr('src', img_src);
			$(this).fadeIn(600);
			$('#historical div.main').load('tarina/get_item.php?' + qs);
			$('#historical div.main').fadeIn(600);
		});
		$(this).addClass('selected');
		ev.preventDefault();
	});

});
