$("document").ready(function () {
	
	ua = navigator.userAgent.toLowerCase();
	isOpera = (ua.indexOf('opera')  > -1);
	isIE = (!isOpera && ua.indexOf('msie') > -1);

	
	bView = $(".b-view");
	bWrapper = $(".b-wrapper");
	bLoader = $(".b-loader");

	documentHeight = getDocumentHeight(); 
	windowHeight = getViewportHeight();		
	globalDocumentHeight = documentHeight;
	globalWindowHeight = windowHeight;
	
	bLoader.css({height: globalDocumentHeight});
	
	$(window).resize(function () {
		documentHeight = getDocumentHeight(); 
		windowHeight = getViewportHeight();		
		bLoader.css({height: globalDocumentHeight});
	});
	
	
	$(".b-piece-item a").click(function () {
		$(".b-piece-item a").removeClass('active-piece');
		$(this).addClass('active-piece');
		var href = $(this).attr('href');
		var pieceTitle = $(this).attr('title');
		var bg = '#' + $(this).find("span.bg").html();
		var h = globalDocumentHeight;
		if(documentHeight > globalDocumentHeight) {
			h = documentHeight;
		}
		bLoader.css({height: h});
		bLoader.css("opacity", 0.7).animate({"left": 0}, 500, function () {
		
			var image = new Image();
			image.onload = function () {
				iHeight = parseInt(image.height);
				if (iHeight <= windowHeight) {
					viewHeight = windowHeight;
				} else {
					if (iHeight > documentHeight) {
						viewHeight = iHeight;
					}	else {
						viewHeight = documentHeight;
					}
				}
				viewHeight += 'px';
				
				$(".b-control .b-piece-title p span").html(pieceTitle);
				
				
				
				bView.css({opacity: '0', display: 'block', height: viewHeight, background: bg + ' url("' + image.src + '") no-repeat center'}).animate({"opacity": 1}, 500, function () {
					bWrapper.css({overflow: 'hidden', height: windowHeight});
					bLoader.css({height: windowHeight});
				});
			};
			image.src = href;			
		});		
		return false;
	});
	
	bView.click(function () {
		closePiece();
	});
	
	bLoader.click(function () {
		$(this).stop().animate({left: "0"}, 300);
		closePiece();
	});	
	
	$(".b-control").css({opacity: '0'});
	bView.mousemove(function (e) {
		if (e.clientY < 200) {
			$(".b-control").css({"opacity": 0.5});
		} else {
			$(".b-control").css({"opacity": 0});
		}
	});
	
	$('.a-close').click (function () {
		closePiece();
		return false;
	});
	
});

document.onkeydown = function(e){ 	
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	if(keycode == 27){ // close
		closePiece();
	}	

}

function closePiece() {
	bView.animate({opacity: 0}, 300, function () {
		var documentHeight = getDocumentHeight(); 
		$(this).css({display: "none", height: documentHeight});
		bLoader.animate({left: "-100%"}, 500);
		bWrapper.css({overflow: 'visible', height: globalDocumentHeight});
		bLoader.css({height: globalDocumentHeight});
	});
}

	 
function getDocumentHeight() {
  return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}


function getViewportHeight() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

