function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y));
	content.removeEvents();
	handle.removeEvents();
	scrollbar.removeEvents();
	if(steps <= 0){
		handle.setStyle('display','none');
		scrollbar.setStyle('display','none');
	} else {
		handle.setStyle('display','block');
		scrollbar.setStyle('display','block');
	}
	var slider = new Slider(scrollbar, handle, {
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the
		// content or the scrollbar element.
		$$(content, scrollbar).removeEvent('mousewheel');
		$$(content, scrollbar).addEvent('mousewheel', function(e){
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;
			slider.set(step);
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	//$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

function createImageRotators(){
	var images = $('mood').getChildren();
	if(images.length > 1){
		var i = 0;
		for(i = 0; i < images.length; i++){
			if(i > 0){
				images[i].setStyle('opacity', 0);
			} else {
				images[i].setStyle('opacity', 1);
				images[i].setProperty('alt', 'on');
			}
		}

		setInterval(function(){
			var i = 0;
			for(i = 0; i < images.length; i++){
				if(images[i].getProperty('alt') == 'on'){
					//images[i].setStyle('opacity', 0);
					new Fx.Morph(images[i], {duration: 2000} ).start({'opacity': [1,0]});
					images[i].setProperty('alt', 'off');
					if(i == images.length-1){
						//images[0].setStyle('opacity', 1);
						new Fx.Morph(images[0], {duration: 2000} ).start({'opacity': [0,1]});
						images[0].setProperty('alt', 'on');
					} else {
						//images[i+1].setStyle('opacity', 1);
						new Fx.Morph(images[i+1], {duration: 2000} ).start({'opacity': [0,1]});
						images[i+1].setProperty('alt', 'on');
					}
					break;
				}
			}
		}, (5000));
	}
}

function fix(){
	if($('body').getSize().x < $('site').getSize().x){
		$('container').setStyle('marginLeft', "0px"); 
		$('container').setStyle('left', "0px");
	} else {
		$('container').setStyle('marginLeft', (-1 * Math.round($('container').getSize().x/2)) + "px"); 
		$('container').setStyle('left', "50%");
	}
	if($('body').getSize().y < $('site').getSize().y){
		$('container').setStyle('marginTop', "0px"); 
		$('container').setStyle('top', "0px");
	} else {
		$('container').setStyle('marginTop', (-1 * Math.round($('container').getSize().y/2)) + "px"); 
		$('container').setStyle('top', "50%");
	}
} 

function initJs(){
	createScrollbar();
	createImageRotators();
	$$('#popup_button').each(function(item){
		item.addEvent('click', function(){
			window.open(SITE_ROOT + 'page=site.popup_player', 'popup_player', 'width=270,height=474,scrollbars=no,toolbar=no,location=no');
		});
	});
	fix();
}

function createScrollbar(){
	makeScrollbar($('content'), $('scroller'), $('scrollbar_handle'));
}

function createPlayer(file){
	var params = {};
	var flashvars = {
		allowfullscreen: 'true',
		displayheight: '32',
		backcolor: '0x000000',
		frontcolor: '0xffffff',
		lightcolor: '0x808080',
		screencolor: '0x000000',
		width: '356',
		playlist: 'none',
		playlistsize: '0',
		overstretch: 'uniform',
		autostart: 'false',
		repeat: 'false',
		shuffle: 'false',
		skin: SITE_ROOT + 'swf/thin.swf',
		file: file,
		playerready: 'player_init'
	};
	var attributes = {
		name: 'player'
	};
	swfobject.embedSWF(SITE_ROOT + "swf/player.swf", "player", "356", "32", "9.0.0", SITE_ROOT + "js/expressInstall.swf", flashvars, params, attributes);	
}

var current_play = 0;
var playlist;
var plyr;

function player_init(){
	plyr = new JWPlayer();
	playlist = plyr.get_playlist();
	plyr.player.addViewListener("NEXT", 'next');
	plyr.player.addViewListener("PREV", 'prev');
}

function next(){	
	current_play++;
	if(current_play == playlist.length){
		current_play = 0;
	}
	$('playlistitem').set('html', playlist[current_play]["author"]+' - '+playlist[current_play]["title"]+'<br />'+playlist[current_play]["description"]);
}

function prev(){	
	current_play--;
	if(current_play <= 0){
		current_play = playlist.length-1;
	}
	$('playlistitem').set('html', playlist[current_play]["author"]+' - '+playlist[current_play]["title"]+'<br />'+playlist[current_play]["description"]);
}

Shadowbox.init({
	language: "en",
	players: ["img", "iframe"],
	overlayOpacity: 0.8
});

window.addEvent('domready', initJs);
window.addEvent('load', createScrollbar);
window.addEvent('resize', fix);
