var last_x;
var current_x;
var position=0;
var pano_width=7747;
var active=false;
var current_page="";
var http_request;
var animate=0;
var animate_to=0;
var animate_side=1;
var animate_interval=10;
var animate_speed=20;
var animate_power=1.05;
var animate_temp=0;

var pagesx={
	"about"		:0,
	"news"		:1,
	"menu"		:2,
	"map"		:3,
	"album"		:4,
	"dj"		:5,
	"contacts"	:6
};

var pagesy=[
	"about",
	"news",
	"menu",
	"map",
	"album",
	"dj",
	"contacts",
];

var coordinates={
	974:"about",
	2470:"news",
	3972:"menu",
	5123:"map",
	5950:"album",
	6870:"dj",
	7747:"contacts"
};

var pages={
	about:"pages/about.html",
	news:"pages/news.html",
	menu:"pages/menu.html",
	map:"pages/map.html",
	album:"pages/album.html",
	dj:"pages/dj.html",
	contacts:"pages/contacts.html"
};

function animate_move(){
	animate_temp*=animate_power;
	position+=(animate_temp*animate_side);
	set_position();

	var current=get_current();

	if(current>=(animate_to-animate_temp) & current<=(animate_to+animate_temp)){
		position=animate_to;
		set_position();
		clearInterval(animate);	
		animate=0;
	}
}

function get_current(){
	var current=position;
	if(current<0)
		current=pano_width+current;
	return current;
}

function select_page(id){
	if(animate | current_page==id)return;

	animate_temp=animate_speed;
	animate_to=0;

	for(var index in coordinates){
		if(coordinates[index]==id)
			break;
		animate_to=parseInt(index);
	}

	var current=get_current();
	animate_side=current>=animate_to ? -1 : +1;
	animate=setInterval(animate_move, animate_interval);
	active=false;
	load_page(id);
}

function load_page(id){
	if(current_page==id)
		return false;
	var bool=true;
	$("#main").fadeOut("fast");
	$("#waiter").fadeIn("fast",
		function(evt) {
			$.get(
				pages[current_page=id],
				function(content){
					$("#main").html(content);
					var ol=$(".im-bigger, .im-small, .image").mouseover(
						function(evt) {
							$(this).css({
								outlineColor: "#974202",
								outlineStyle: "dotted"
							}).animate({
								outlineWidth: 5
							});
						}
					).mouseout(
						function(evt) {
							$(this).animate({
								outlineWidth: 0
							});
						}
					);
					if(ol.length) {
						$("#main").hide("fast");
						ol.prev().hide("fast");
						ol.hide("fast").
							map(
								function() {
									var uri=$(this).css("background-image");
									var t=this;
									$(document).append(
										$("<img>").css({
											"display":"none"
										}).attr("src", uri.substring(5, uri.length-2)).load(
											function() {
												$("#waiter").fadeOut("fast");
												$("#main").show("slow");
												$(t).show("slow").prev().show("fast");
												$(this).remove();
											}
										).error(
											function() {
												$("#waiter").fadeOut("fast");
												$("#main").show("slow");
												$(t).show("slow").prev().show("fast");
												$(this).remove();
											}
										)
									);
								}
							);
					} else {
						$("#waiter").fadeOut("fast");
						$("#main").fadeIn("fast");
					}
					$("a.lightbox").lightBox();
				}
			);
		}
	);
	return true;
}

function page_change() {
	var current=get_current();
	var page="";
	for(var index in coordinates)
		if(current<=index){
			page=coordinates[index];
			break;
		}
	return load_page(page);
}

function get_mouse_x(evt){
	if(!evt)evt=event;
	if(evt.pageX)
		return evt.pageX;
	if(evt.clientX)with(document)
		return evt.clientX+(documentElement.scrollLeft || body.scrollLeft)-documentElement.clientLeft;
}

function set_position() {
	if(Math.abs(position)>pano_width)
		position%=pano_width;
	$("#pano").css({backgroundPosition: (-position).toString()+"px 0"});
}

function move(evt) {
	evt.cancelBubble=true;
	if(!active)return;

	current_x=get_mouse_x(evt);
	var i=pagesx[current_page]+(current_x<last_x?1:-1);
	i=(i%=pagesy.length)<0?pagesy.length-i-2:i;
	var p=pagesy[i];
	select_page(p);
	active=false;
	last_x=current_x;
}

function move_enable(evt){
	if(animate)return;

	last_x=get_mouse_x(evt);
	active=true;

	return false;
}

function move_disable() {
	active=false;

	return false;
}


function onCmdPanoLoad(evt) {
	$("#pano").fadeIn("slow");
}

AudioPlayer.setup("../swf/player.swf", {
	autostart: "yes",
	width: 290,
	initialvolume: 100,
	transparentpagebg: "yes",
	bg: "f4ae61",
	leftbg: "471e00",
	lefticon: "f4ae61",
	rightbg: "471e00",
	rightbghover: "471e00",
	righticon: "f4ae61",
	righticonhover: "f4ae61",
	text: "271e1e",
	slider: "e75744",
	track: "f4ae61",
	border: "f4ae61",
	loader: "e75744"
});

window.onload=function(evt){
	load_page("about");
};
