	       	
var popupOpen = false;
var currentContentID = '';

var wheight = 600;

var popupNoContentHeight = 0;
var stop = parseInt(wheight/2);
var ptop = 0;

var currentPopup = 0;
var numPopups = 7;
var popupID = 'popup';

$(document).ready(function() {
	
	$('.openPopup').click(function(){
		openPopup($(this).attr('rel'));
		return false;
	});
	
	$('#popup .close').click(function(){
		closePopup();
		return false;
	});
	
	wheight = $(window).height();
	if (wheight > 700) wheight = 700;
	
});

function nextPopup() {
	openPopup(popupID+(parseInt(currentPopup)+1));
	return false;
}
function previousPopup() {
	openPopup(popupID+(parseInt(currentPopup)-1));
	return false;
}

function openPopup(contentID, isChaining) {
	if (currentContentID == contentID) return;
		if (!popupOpen) {
			currentContentID = contentID;
			
		currentPopup = contentID.toString().substr(popupID.length);
		//alert(contentID + ' - ' + currentPopup);
		if (currentPopup <= 1) $('#popup #content-bottom .previous').hide();
		else $('#popup #content-bottom .previous').show();
		if (currentPopup >= numPopups) $('#popup #content-bottom .next').hide();
		else $('#popup #content-bottom .next').show();
		
		if (isChaining) {
			var sheight = $('#popup .content').height();
			$('#popup .content').css('height','auto');
			$('#popup .content').html($('#'+contentID).html() + $('#popup #content-bottom').html());
			var nheight = $('#popup .content').height();
			$('#popup .content').css('height',sheight);
			$('#popup .content').children().animate({opacity: 0},0).animate({opacity: 1},300);
			$('#popup .content').animate({'height': nheight}, 500);
			var ntop = parseInt((wheight - nheight - popupNoContentHeight) / 2) - 30;
			$('#popup').animate({'top': ntop+'px'},500);
		}
		else {
			$('#popup .content').html($('#'+contentID).html() + $('#popup #content-bottom').html());
			
			var nheight = $('#popup .content').height();
			stop = parseInt(wheight/2) - 30;
			var ntop = parseInt((wheight - nheight - popupNoContentHeight) / 2) - 30;
			$('#popup').css('margin-left', '-201px');
			if (ieBrowser || isChaining) {
				$('#popup .content').animate({'height': '0px'},0);
				$('#popup .content').animate({'height': nheight},500);
				$('#popup').animate({'top': stop+'px'},0);
				$('#popup').animate({'top': ntop+'px'},500);
			}
			else {
				$('#popup .content').animate({'height': '0px'},0);
				$('#popup .content').animate({'height': nheight}, 500);
				$('#popup').css('top', stop+'px');
				$('#popup').fadeOut(0);
				$('#popup').animate({'top': ntop+'px', opacity: 'show'},500);
			}			
		}
		popupOpen = true;
	}
	else {
		closePopup(contentID)
	}
	return false;
}

function closePopup(contentID) {
	if (contentID) {
		$('#popup .content').css('height', $('#popup .content').height())
		$('#popup .content').children().animate({opacity: 0},300,function(){
			openPopup(contentID, true);
		});
		//alert('ok');
	}
	else if (ieBrowser) {
		$('#popup .content').animate({'height': '0px'},300,function(){
			$('#popup').css('margin-left', '-9000px');
			$('#popup .content').css('height', 'auto');
			if (contentID) openPopup(contentID, true);
		});
		$('#popup').animate({'top': stop+'px'},300);
	}
	else {
		$('#popup .content').animate({'height': '0px'},500, function() {
			$('#popup').css('margin-left', '-9000px');
			$('#popup .content').css('height', 'auto');
			if (contentID) openPopup(contentID, true);
		});
		$('#popup').animate({'top': stop+'px', opacity: 'hide'},500,function(){$(this).show();});
	}
	popupOpen = false;
	currentContentID = '';
	return false;
}