$(document).ready(function() {
	var popupStatus = 0;

	function ___getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};
	
	function ___getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};
	
	function _resize_container(width, height){
		var arrPageSizes = ___getPageSize();
		var arrPageScroll = ___getPageScroll();
		
		if (arrPageSizes[3] - height < 0){
			height = arrPageSizes[3];
		}

		if (arrPageSizes[2] - width < 0){
			width = arrPageSizes[2];
		}	
		
		$('#containerCenter').css({
			'position':'absolute',
			'top': arrPageScroll[1] + ((arrPageSizes[3] - height) / 2),
			'left': arrPageScroll[0] + ((arrPageSizes[2] - width) / 2),
			'z-index':'2'
		});
	}
	
	function showPopup(imgUrl){
		if (popupStatus == 0){
			$("#backgroundPopup").remove();
			$('#containerCenter').remove();
		
			$('body').append('<div id="backgroundPopup"></div>');
			var arrPageSizes = ___getPageSize();
			$("#backgroundPopup").css({
				 'opacity': '0.7',
				 'position':'fixed',  
				 '_position':'absolute', /* hack for internet explorer 6*/  
				 'width': arrPageSizes[0],
				 'height': arrPageSizes[1],
				 'top':'0',  
				 'left':'0',  
				 'background':'#000000',  
				 'border':'1px solid #cecece',
				 'z-index':'1'
			});
			$("#backgroundPopup").fadeIn("slow")
		
			$("#backgroundPopup").click(function(){
				closePopup();
			});
			
			$('body').append('<div id="containerCenter"><div id="popupClose"><span>Close</span></div><img id="imgContainer"></div>');
			
			var objImagePreloader = new Image();
			objImagePreloader.onload = function() {
				$('#imgContainer').attr('src', imgUrl);

				_resize_container(objImagePreloader.width,objImagePreloader.height);
				
				objImagePreloader.onload=function(){};
			};
			objImagePreloader.src = imgUrl;
			
			$('#imgContainer').css({
				'border': 'solid 2px #ffffff'
			});
			
			$('#popupClose').css({
				'text-align':'right',
				'font-weight':'bold',
				'width':'100%',
				'position':'absolute',
				'right':'5px',
				'padding':'10px'
			});

			$('#popupClose span').css({
				'cursor':'pointer'
			});

			
			$("#popupClose").click(function(){
				closePopup();
			});
			
			$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$("#backgroundPopup").css({
					 'opacity': '0.7',
					 'position':'fixed',  
					 '_position':'absolute', /* hack for internet explorer 6*/  
					 'width': arrPageSizes[0],
					 'height': arrPageSizes[1],
					 'top':'0',  
					 'left':'0',  
					 'background':'#000000',  
					 'border':'1px solid #cecece',
					 'z-index':'1'
				});
				
				var arrPageScroll = ___getPageScroll();
				if (arrPageSizes[3] - $('#imgContainer').attr('height') < 0){
					height = arrPageSizes[3];
				}else{
					height = $('#imgContainer').attr('height');
				}

				if (arrPageSizes[2] - $('#imgContainer').attr('width') < 0){
					width = arrPageSizes[2];
				}else{
					width = $('#imgContainer').attr('width');
				}
				
				$('#containerCenter').css({
					'position':'absolute',
					'top': arrPageScroll[1] + ((arrPageSizes[3] - $('#imgContainer').attr('height')) / 2),
					'left': arrPageScroll[0] + ((arrPageSizes[2] - $('#imgContainer').attr('width')) / 2),
					'z-index':'2'
				});
			});
			
			$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$("#backgroundPopup").css({
					 'opacity': '0.7',
					 'position':'fixed',  
					 '_position':'absolute', /* hack for internet explorer 6*/  
					 'width': arrPageSizes[0],
					 'height': arrPageSizes[1],
					 'top':'0',  
					 'left':'0',  
					 'background':'#000000',  
					 'border':'1px solid #cecece',
					 'z-index':'1'
				});
				
				var arrPageScroll = ___getPageScroll();
				if (arrPageSizes[3] - $('#imgContainer').attr('height') < 0){
					height = arrPageSizes[3];
				}else{
					height = $('#imgContainer').attr('height');
				}

				if (arrPageSizes[2] - $('#imgContainer').attr('width') < 0){
					width = arrPageSizes[2];
				}else{
					width = $('#imgContainer').attr('width');
				}

				$('#containerCenter').css({
					'position':'absolute',
					'top': arrPageScroll[1] + ((arrPageSizes[3] - $('#imgContainer').attr('height')) / 2),
					'left': arrPageScroll[0] + ((arrPageSizes[2] - $('#imgContainer').attr('width')) / 2),
					'z-index':'2'
				});
			});
			
			popupStatus = 1;
		}
	}
	
	function closePopup(){
		if (popupStatus == 1){
			$("#backgroundPopup").fadeOut("slow");
			$('#containerCenter').fadeOut("slow");
			popupStatus = 0;
		}
	}

	$('.playerboxlink').click(function(){
		showPopup(this.href);
		return false;
	});

	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			closePopup();
		}
	});
	
	$('#videobutton').click(function(){
		$('#video').css({
			'display':'block'
		});
		
		$('#pictures').css({
			'display':'none'
		});
	});	
	
	$('#picturebutton').click(function(){
		$('#pictures').css({
			'display':'block'
		});
		
		$('#video').css({
			'display':'none'
		});
	});	

});

