// JavaScript Document
var picWidth = 2400;
var picHeight = 1616;

//wspolrzedzne srodkowego
var newHeight;
var newWidth;
var newTop;
var newLeft;

//obrazki srodowy lewy i prawy
var imgCenter = null;
var imgLeft = null;
var imgRight = null;

$(window).load(function(){
	$('#loader').hide();					
	//$(".mp3").jmp3({repeat: true});					
	$("div.pasek, div.red").show().css({'opacity':0.80});	
	$("div.bottombar").show().css({'opacity':0.90});
	$("#splayer,div.bottombar,div.bottombar_textleft,div.autor,div.topmenubar,div.mainmenu").show();
	
	$("div.prevbutton, div.nextbutton").css({'opacity':0.75});
	$("div.prevbutton, div.nextbutton").hover(
		function(){ $(this).css({'opacity':0.95}); },
		function(){ $(this).css({'opacity':0.75}); }
	);
	
	$("div.fullimage img").hide();
	imgCenter=$("div.fullimage img:first");
	resizeImages();
	startAutoView();
	turnOnNextPrevButton();
});

$(window).resize(function(){
	resizeImages();
});


function resizeImages() {
	var navWidth = $(window).width();
	var navHeight = $(window).height();
	
	if(navWidth<800)navWidth=800;
	
	var navRatio = navWidth / navHeight;
	var picRatio = picWidth / picHeight;
	
	if (navRatio > picRatio) {
			newHeight = (navWidth / picWidth) * picHeight;
			newWidth = navWidth;
	} else {
			newHeight = navHeight;
			newWidth = (navHeight / picHeight) * picWidth;
	}
	
	newTop = 0 - ((newHeight - navHeight) / 2);
	newLeft =  0 - ((newWidth - navWidth) / 2);
	newTop=60;

	$('div.fullimage').css({height: navHeight, width: navWidth});
	$('div.fullimage').css({visibility:"visible", display:"block"});
	$('div.fullimage img').css({height: newHeight, width: newWidth});
	$('body').css({height: navHeight, width: navWidth});
	
	setPagePosition(); //kartka pokazujaca sie z pliku showcontent.js
	setImagesPosition();
}



function moveLeft(){
	turnOffNextPrevButton();	
	var centerLeftEnd = -1*(newWidth-newLeft);
	$(imgRight).show();
	$(imgCenter).animate({left:centerLeftEnd},1000,"expoEaseIn", function(){} );
	$(imgRight).animate({left:newLeft},1000,"expoEaseIn", function(){ imgCenter = imgRight;	setImagesPosition(); turnOnNextPrevButton(); startAutoView(); });
	
}

function moveRight(){
	turnOffNextPrevButton();	
	var centerLeftEnd = (newWidth+newLeft);
	$(imgLeft).show();
	$(imgCenter).animate({left:centerLeftEnd},1000,"expoEaseIn", function(){});
	$(imgLeft).animate({left:newLeft}, 1000,"expoEaseIn", function(){ imgCenter = imgLeft; 	setImagesPosition(); turnOnNextPrevButton(); startAutoView(); });
	
}

function setImagesPosition(){
	var tmpright=$(imgCenter).next("img");
	var tmpleft=$(imgCenter).prev("img");
	
	if( !$(tmpright).is('img')){tmpright=$("div.fullimage img:first");  }
	if( !$(tmpleft).is('img') ){tmpleft=$("div.fullimage img:last"); }
	
	imgRight=tmpright;
	imgLeft=tmpleft;
	
	$(imgRight).hide();
	$(imgLeft).hide();
	$(imgCenter).show();
	
	$(imgCenter).css({height: newHeight, width: newWidth});
	$(imgLeft).css({height: newHeight, width: newWidth});
	$(imgRight).css({height: newHeight, width: newWidth});
	
	$(imgCenter).css({top: newTop, left: newLeft});
	$(imgLeft).css({top: newTop, left: newLeft-newWidth});
	$(imgRight).css({top: newTop, left: newLeft+newWidth});
	
	configureNextPrevButton();
}

function configureNextPrevButton(){
	if( $(imgLeft).is('img') )$('div.prevbutton').show();
	else $('div.prevbutton').hide();
	if( $(imgRight).is('img'))$('div.nextbutton').show();
	else $('div.nextbutton').hide();
}
function turnOffNextPrevButton(){
	$('div.nextbutton, div.prevbutton').hide();
	$('div.nextbutton, div.prevbutton').unbind('click');
}
function turnOnNextPrevButton(){
	$('div.nextbutton, div.prevbutton').show();
	$("div.nextbutton").click(function(event){ event.preventDefault(); $("div.red").stop(true,false); moveLeft();  });
	$("div.prevbutton").click(function(event){ event.preventDefault(); $("div.red").stop(true,false); moveRight(); });	
}

function startAutoView(){
	var navWidth = $(window).width();
	$("div.red").css({width:0}).animate({width:navWidth},15000,function(){ moveLeft();});	
}


