var gallery_slide_offset = 119;
var gallery_slide_duration = 1300;
var article_slide_offset = 75;
var article_slide_duration = 700;
var count = 0;
var it = 0;

var track_count = 0;
var track_current_it = 0;
var track_current_id = 0;

function player_play() {
	if(track_count) {
		$("DIV.track_title").hide(1, function() {
			$("#TrackTitle_"+track_current_id).show(1);
		});
	}
}
var eq_hidden = true;
function eq_show(id) {
	if(eq_hidden) {
		$("div.eq"+id).show(1, function() {
			eq_hidden = false;
		});
	} else {
		$("div.eq").hide(1, function() {
			$("div.eq"+id).show(1, function() {
				eq_hidden = false;
			});
		});
	}
}
function eq_hide() {
	$("div.eq").hide(1, function() {
		eq_hidden = true;
	});
}
/*function setTimeStamp(time) {
	$("#Progress").hide(500);
	$("DIV.player DIV.info DIV.time").html("("+time+")");
}*/
function setProgress(pro) {
	$("#Progress").slideUp(500);
}
function progress(id) {
	$("#Mp3Player").css({opacity:1.0});
	if(id==0) {
		var offset = $("A.play").offset();
	} else {
		var offset = $("#PlayerPlayNavi"+id).offset();
	}
	$("#Progress").css("left",offset.left-34).css("top",offset.top+25).slideDown(500);
	$("#Mp3Player").css("left",offset.left-39).css("top",offset.top+20);
}
$(window).load(function() {
	$("DIV#Person DIV#Content DIV.portrait DIV.person DIV.title DIV.bg").css({opacity:0.5});;
	$("DIV.player A").css({opacity:0.0});
	$("DIV.player A").hover(function(){
		$(this).css({opacity:0.5});
	}, function(){
		$(this).css({opacity:0.0});
	});
	$(function(){
		$("A:not(.player_navi)").click(function() {
			$("#player").remove();
		});
	});
	count = $("DIV.count").attr("count");
	$("DIV.portrait DIV.person").hover(function() {
		$("DIV.portrait DIV.person DIV.title").show();
	}, function() {
		$("DIV.portrait DIV.person DIV.title").hide();
	});
	if(count > 1) {
		$("DIV.portrait DIV.person").hover(function() {
			$("DIV.portrait DIV.next").show();
			$("DIV.portrait DIV.person DIV.title").show(100);
		}, function() {
			$("DIV.portrait DIV.next").hide();
			$("DIV.portrait DIV.person DIV.title").hide(100);
		});
		$("DIV.portrait DIV.person").click(function() {
			$("#Image_"+(it%count)).fadeOut(500,function() {
				it++;
				$("#Image_"+(it%count)).fadeIn(500);
			});
		});
	}
	$("#Content .slide-up").click(function() {
		$("#Content .slide-up").fadeOut(1, function() {
			$("#Content .slide-down").fadeOut(1, function() {
				var pos_slider = $("#Content .slider").position();
				if(pos_slider.top < 0) {
					$("#Content .slider").animate({
						top: "+="+article_slide_offset
					}, article_slide_duration, function() {
						$("#Content .slide-up").fadeIn(1);
						$("#Content .slide-down").fadeIn(1);
					});
				} else {
					$("#Content .slide-up").fadeIn(1);
					$("#Content .slide-down").fadeIn(1);
				}
			})
		})
	});
	$("#Content .slide-down").click(function() {
		$("#Content .slide-down").fadeOut(1, function() {
			$("#Content .slide-up").fadeOut(1, function() {
				var pos_slider = $("#Content .slider").position();
				var height_slider = $("#Content .slider").height();
				var height_article = $("#Content .article").height();
				if((height_slider + pos_slider.top) > height_article) {
					$("#Content .slider").animate({
						top: "-="+article_slide_offset
					}, article_slide_duration, function() {
						$("#Content .slide-up").fadeIn(1);
						$("#Content .slide-down").fadeIn(1);
					});
				} else {
					$("#Content .slide-up").fadeIn(1);
					$("#Content .slide-down").fadeIn(1);
				}
			})
		})
	});
	// Slider galerii
	$("#Gallery .slide-right").click(function() {
		$("#Gallery .slide-right").fadeOut(1, function() {
			$("#Gallery .slide-left").fadeOut(1, function() {
				var pos_slider = $("#Gallery .slider").position();
				var width_slider = $("#Gallery .slider").width();
				var width_images = $("#Gallery .images").width();
				var max_iter = parseInt((width_slider - width_images) / gallery_slide_offset);
				var curr_iter = parseInt((-pos_slider.left) / gallery_slide_offset);
				if(curr_iter != max_iter) {
					var iter = 0;
					for(var i=0; i<4; i++) {
						if((curr_iter + iter) < max_iter) {
							iter++;
						}
					}
					var offset = iter * gallery_slide_offset;
					$("#Gallery .slider").animate({
						left: "-="+offset
					}, gallery_slide_duration, "swing", function() {
						$("#Mp3Player").css({opacity:0.0});
						$("#Gallery .slide-left").fadeIn(1);
						$("#Gallery .slide-right").fadeIn(1);
					});
				} else {
					var m = parseInt(max_iter / 5);
					$("#Gallery .slider").animate({
						left: "0px"
					}, (m * gallery_slide_duration), "swing", function() {
						$("#Mp3Player").css({opacity:0.0});
						$("#Gallery .slide-left").fadeIn(1);
						$("#Gallery .slide-right").fadeIn(1);
					});
				}
			});
		});
	});
	$("#Gallery .slide-left").click(function() {
		$("#Gallery .slide-left").fadeOut(1, function() {
			$("#Gallery .slide-right").fadeOut(1, function() {
				var pos_slider = $("#Gallery .slider").position();
				var width_slider = $("#Gallery .slider").width();
				var curr_iter = parseInt((-pos_slider.left) / gallery_slide_offset);
				if(curr_iter) {
					var iter = 0;
					for(var i=0; i<4; i++) {
						if((curr_iter - iter) > 0) {
							iter++;
						}
					}
					var offset = iter * gallery_slide_offset;
					$("#Gallery .slider").animate({
						left: "+="+offset
					}, gallery_slide_duration, "swing", function() {
						$("#Mp3Player").css({opacity:0.0});
						$("#Gallery .slide-left").fadeIn(1);
						$("#Gallery .slide-right").fadeIn(1);
					});
				} else {
					var width_images = $("#Gallery .images").width();
					var max_iter = parseInt((width_slider - width_images) / gallery_slide_offset);
					var offset = max_iter * gallery_slide_offset;
					var m = parseInt(max_iter / 5);
					$("#Gallery .slider").animate({
						left: "-="+offset
					}, (m * gallery_slide_duration), "swing", function() {
						$("#Mp3Player").css({opacity:0.0});
						$("#Gallery .slide-left").fadeIn(1);
						$("#Gallery .slide-right").fadeIn(1);
					});
				}
			});
		});
	});
	track_count = $("DIV.track_count").attr('count');
	track_current_id = $("DIV.track").eq(0).attr('track');
	$("IMG.player_next").click(function () {
		track_current_it = (track_current_it+1) % track_count;
		track_current_id = $("DIV.track").eq(track_current_it).attr('track');
		progress(0);
		document.vinyl.nextMP3(track_current_it+1);
		document.player.playMP3('/people/getmp3/' + track_current_id);
		player_play();
	});
	$("IMG.player_prev").click(function () {
		if(track_current_it-1 < 0) {
			track_current_it = track_count-1;
		} else {
			track_current_it = track_current_it-1;
		}
		track_current_id = $("DIV.track").eq(track_current_it).attr('track');
		progress(0);
		document.vinyl.nextMP3(track_current_it+1);
		document.player.playMP3('/people/getmp3/' + track_current_id);
		player_play();
	});
	// Animacja
	$("#Onload").fadeOut(500, function() {
		$("#Bg").fadeIn(1000, function() {
			$("#Bg DIV.bg").fadeIn(500);
			$("#Main").slideDown(1000);
			$("#Wartopamietac").slideDown(1000);
			$("#Header").fadeIn(500, function() {
				$("#Content").fadeIn(500, function(){
					if($("#Content .slider").height() > $("#Content .article").height()) {
						$("#Content .slide").fadeIn(500);
					}
					if($("#Gallery").length) {
						$("#Gallery").fadeIn(500, function() {
							if($("#Gallery .slider").width() > $("#Gallery .images").width()) {
								$("#Gallery .slide").fadeIn(500);
									$('DIV.player').fadeIn(1000);
							} else {
								$('DIV.player').fadeIn(1000);
							}
						});
					} else {
						$('DIV.player').fadeIn(1000);
					}
				});
				$("#Footer").fadeIn(500);
			});
		});
	});
});