var Galleries = {
	rsv3: {
		width: 685,
		height: 514,
		marginTop: '-257px',
		marginLeft: '-324px',
		images: [
			{
				title: 'Rosetta Stone V3: main menu'
			},
			{
				title: 'Rosetta Stone V3: course'
			},
			{
				title: 'Rosetta Stone V3: course'
			},
			{
				title: 'Rosetta Stone V3: main menu'
			}
		]
	},
	rsv4TotalE: {
		width: 812,
		height: 474,
		marginTop: '-237px',
		marginLeft: '-406px',
		images: [
			{
				title: 'Totale: Main menu'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Course'
			},
			{
				title: 'Totale: Main menu, lesson completed'
			}
		]
	},
	rsv4TotalEMobile: {
		width: 320,
		height: 480,
		marginTop: '-240px',
		marginLeft: '-160px',
		images: [
			{
				title: 'Totale Mobile Companion: Menu'
			},
			{
				title: 'Totale Mobile Companion: Course'
			},
			{
				title: 'Totale Mobile Companion: Course'
			},
			{
				title: 'Totale Mobile Companion: Course'
			},
			{
				title: 'Totale Mobile Companion: Lesson Index'
			},
			{
				title: 'Totale Mobile Companion: Next Lesson'
			}
		]
	},
	rsReflex: {
		width: 800,
		height: 440,
		marginTop: '-220px',
		marginLeft: '-400px',
		images: [
			{
				title: 'Reflex beta: log-in screen'
			},
			{
				title: 'Reflex beta: home screen'
			},
			{
				title: 'Reflex beta Rehearsal: Intro'
			},
			{
				title: 'Reflex beta Rehearsal: Intro'
			},
			{
				title: 'Reflex beta Rehearsal: Up Next'
			},
			{
				title: 'Reflex beta Rehearsal: NPC speaks'
			},
			{
				title: 'Reflex beta Rehearsal: Learner speaks'
			}
		]
	},
	leapfrog: {
		width: 685,
		height: 514,
		marginTop: '-257px',
		marginLeft: '-342px',
		images: [
			{
				title: 'Leaptrack wiresketch student list'
			},
			{
				title: 'Leaptrack wiresketch dashboard'
			},
			{
				title: 'Leaptrack interactive prototype student list'
			},
			{
				title: 'Leaptrack wiresketch report'
			},
			{
				title: 'Leaptrack interactive prototype report menu'
			},
			{
				title: 'Leaptrack interactive prototype sample report'
			},
			{
				title: 'Leaptrack interactive prototype sample report'
			}
		]
	},
	canwest: {
		width: 600,
		height: 514,
		marginTop: '-257px',
		marginLeft: '-300px',
		images: [
			{
				title: 'Vancouver Sun wireframe'
			},
			{
				title: 'Vancouver Sun redesigned'
			},
			{
				title: 'Calgary Herald wireframe'
			},
			{
				title: 'Calgary Herald redesigned'
			}
		]
	}
}

var rsImages = [];
var rsSrcs = [
	'images/rsv3-11.png',
	'images/rsv3-0.png',
	'images/rsv3-1.png',
	
	'images/rsv4TotalE-10.png',
	'images/rsv4TotalE-0.png',
	'images/rsv4TotalE-1.png',
	
	'images/rsv4TotalEMobile-11.png',
	'images/rsv4TotalEMobile-0.png',
	'images/rsv4TotalEMobile-1.png',
	
	'images/rsReflex-6.png',
	'images/rsReflex-0.png',
	'images/rsReflex-1.png',
	
	'images/leapfrog-15.jpg',
	'images/leapfrog-0.jpg',
	'images/leapfrog-1.jpg',
	
	'images/canwest-3.jpg',
	'images/canwest-0.jpg',
	'images/canwest-1.jpg'
];
function preloadRs() {
	for (var i = 0; i < rsSrcs.length; i++) {
		rsImages[i] = new Image();
		rsImages[i].src = rsSrcs[i];
	}
}
preloadRs();

var NiceLightboxJQuery = {	
	width: null,
	height: null,
	marginTop: null,
	marginLeft: null,
	photos: [],// ALERT: Whenever a new gallery is opended, this value has to be emptied.
	lastPhoto: null,
	prevPhoto: null,
	currPhoto: 0,
	nextPhoto: null,
	transitioning: false,
	getPhotos: function(id) {
		var path = null;
		switch (id) {
			case 'rsv3' : 
				NiceLightboxJQuery.width = Galleries.rsv3.width;
				NiceLightboxJQuery.height = Galleries.rsv3.height;
				NiceLightboxJQuery.marginTop = Galleries.rsv3.marginTop;
				NiceLightboxJQuery.marginLeft = Galleries.rsv3.marginLeft;
				path = Galleries.rsv3.images;
				break;
			case 'rsv4TotalE' : 
				NiceLightboxJQuery.width = Galleries.rsv4TotalE.width;
				NiceLightboxJQuery.height = Galleries.rsv4TotalE.height;
				NiceLightboxJQuery.marginTop = Galleries.rsv4TotalE.marginTop;
				NiceLightboxJQuery.marginLeft = Galleries.rsv4TotalE.marginLeft;
				path = Galleries.rsv4TotalE.images;
				break;
			case 'rsv4TotalEMobile' : 
				NiceLightboxJQuery.width = Galleries.rsv4TotalEMobile.width;
				NiceLightboxJQuery.height = Galleries.rsv4TotalEMobile.height;
				NiceLightboxJQuery.marginTop = Galleries.rsv4TotalEMobile.marginTop;
				NiceLightboxJQuery.marginLeft = Galleries.rsv4TotalEMobile.marginLeft;
				path = Galleries.rsv4TotalEMobile.images;
				break;
			case 'rsReflex' : 
				NiceLightboxJQuery.width = Galleries.rsReflex.width;
				NiceLightboxJQuery.height = Galleries.rsReflex.height;
				NiceLightboxJQuery.marginTop = Galleries.rsReflex.marginTop;
				NiceLightboxJQuery.marginLeft = Galleries.rsReflex.marginLeft;
				path = Galleries.rsReflex.images;
				break;
			case 'leapfrog' : 
				NiceLightboxJQuery.width = Galleries.leapfrog.width;
				NiceLightboxJQuery.height = Galleries.leapfrog.height;
				NiceLightboxJQuery.marginTop = Galleries.leapfrog.marginTop;
				NiceLightboxJQuery.marginLeft = Galleries.leapfrog.marginLeft;
				path = Galleries.leapfrog.images;
				break;
			case 'canwest' : 
				NiceLightboxJQuery.width = Galleries.canwest.width;
				NiceLightboxJQuery.height = Galleries.canwest.height;
				NiceLightboxJQuery.marginTop = Galleries.canwest.marginTop;
				NiceLightboxJQuery.marginLeft = Galleries.canwest.marginLeft;
				path = Galleries.canwest.images;
				break;
		}
		for (var i = 0; i < path.length; i++) {
			NiceLightboxJQuery.photos[i] = {};
			NiceLightboxJQuery.photos[i].image = 'images/' + id + '-' + i + '.png';
			NiceLightboxJQuery.photos[i].text = path[i].title;
			// $(this).attr('id', 'a-' + i);
		}
		NiceLightboxJQuery.lastPhoto = NiceLightboxJQuery.photos.length - 1;
		this.insertNiceLightbox();
	},
	setImgNumber: function() {
		$('#imgNumber').html(NiceLightboxJQuery.currPhoto + 1);
	},
	prevReset: function() {
		$('#img-' + this.nextPhoto).remove();
		this.currPhoto = this.prevPhoto;
		this.setPrev();
		this.setNext();
		this.photoPara();
		$('#photoContainer').prepend('<div id="img-' + NiceLightboxJQuery.prevPhoto + '" style="left: 812px;"><img src="' + NiceLightboxJQuery.photos[NiceLightboxJQuery.prevPhoto].image + '" width="' + NiceLightboxJQuery.width + '" height="' + NiceLightboxJQuery.height + '" style="margin-top: ' + NiceLightboxJQuery.marginTop + '; margin-left: ' + NiceLightboxJQuery.marginLeft + ';"></div>');
		// NiceLightboxJQuery.margins('#img-' + NiceLightboxJQuery.prevPhoto + ' img');
		NiceLightboxJQuery.setImgNumber();
		setTimeout(function() {NiceLightboxJQuery.transitioning = false;}, 500);
	},
	prevImage: function() {
		if (!this.transitioning) {
			NiceLightboxJQuery.transitioning = true;
			// if ($('#img-' + NiceLightboxJQuery.prevPhoto + ' img').load()) {
				$('#photoContainer div').each(function() {
					$(this).animate({
						'left': (parseInt($(this).css('left').split('px')[0]) + 812) + 'px'
					}, 500);
				});
				setTimeout('NiceLightboxJQuery.prevReset();', 500);
			// }
		}
	},
	nextReset: function() {
		$('#img-' + this.prevPhoto).remove();
		this.currPhoto = this.nextPhoto;
		this.setPrev();
		this.setNext();
		this.photoPara();
		$('#photoContainer').append('<div id="img-' + NiceLightboxJQuery.nextPhoto + '" style="left: 2436px;"><img src="' + NiceLightboxJQuery.photos[NiceLightboxJQuery.nextPhoto].image + '" width="' + NiceLightboxJQuery.width + '" height="' + NiceLightboxJQuery.height + '" style="margin-top: ' + NiceLightboxJQuery.marginTop + '; margin-left: ' + NiceLightboxJQuery.marginLeft + ';"></div>');
		// NiceLightboxJQuery.margins('#img-' + NiceLightboxJQuery.nextPhoto + ' img');
		NiceLightboxJQuery.setImgNumber();
		setTimeout(function() {NiceLightboxJQuery.transitioning = false;}, 500);
	},
	nextImage: function() {
		if (!this.transitioning) {
			NiceLightboxJQuery.transitioning = true;
			// if ($('#img-' + NiceLightboxJQuery.nextPhoto + ' img').load()) {
				$('#photoContainer div').each(function() {
					$(this).animate({
						'left': (parseInt($(this).css('left').split('px')[0]) - 812) + 'px'
					}, 500);
				});
				setTimeout('NiceLightboxJQuery.nextReset();', 500);
			// }
		}
	},
	insertNiceLightbox: function() {
		$('body').append('<div id="screen" style="display: none;">&nbsp;</div>\
		<div id="nLWrapper" style="display: none;">\
			<div id="blind" style="display: none;">&nbsp;</div>\
			<div id="niceLightbox" style="display: none;">\
				<div id="photoWindow" style="display: none;">\
					<div id="photoContainer"></div>\
				</div>\
			</div>\
			<div id="descPrevNext" style="display: none;">\
				<p class="description">&ldquo;It&rsquo;s never too early for war&rdquo;</p>\
				<p class="imgCounter"><span id="imgNumber">1</span>/<span id="imgTotal">7</span></p>\
				<ul class="floatRight clearfloat" id="photoPrevNext">\
					<li class="prev"><a href="javascript:NiceLightboxJQuery.prevImage();" title="&laquo; Prev"><span>&nbsp;</span></a></li>\
					<li class="next"><a href="javascript:NiceLightboxJQuery.nextImage();" title="Next &raquo;"><span>&nbsp;</span></a></li>\
				</ul>\
				<a href="javascript:NiceLightboxJQuery.closeNiceLightbox();" title="Close window" id="nlbClose" style="display: none;"><span>Close window</span></a>\
			</div>\
		</div>');
		this.niceLightbox();
	},
	setPrev: function() {
		if (NiceLightboxJQuery.currPhoto == 0) 
			NiceLightboxJQuery.prevPhoto = NiceLightboxJQuery.lastPhoto;
		else 
			NiceLightboxJQuery.prevPhoto = NiceLightboxJQuery.currPhoto - 1;
	},
	setNext: function() {
		if (NiceLightboxJQuery.currPhoto == NiceLightboxJQuery.lastPhoto) 
			NiceLightboxJQuery.nextPhoto = 0;
		else 
			NiceLightboxJQuery.nextPhoto = NiceLightboxJQuery.currPhoto + 1;
	},
	photoPara: function() {
		$('#descPrevNext p.description').html('&ldquo;' + NiceLightboxJQuery.photos[NiceLightboxJQuery.currPhoto].text + '&rdquo;');
	},
	margins: function(path) {
		if (path == '#photoContainer div img') {
			setTimeout(function() {
				$(path).each(function() {
					$(this).css({
						'margin-top': '-' + (Math.round($(this).height() / 2)) + 'px',
						'margin-left': '-' + (Math.round($(this).width() / 2)) + 'px'
					});
				});
			}, 550);
		} else {
			$(path).load(function() {
				$(this).css({
					'margin-top': '-' + (Math.round($(this).height() / 2)) + 'px',
					'margin-left': '-' + (Math.round($(this).width() / 2)) + 'px'
				});
			});
		}
		// window.alert(path);
	},
	niceLightbox: function() {
		NiceLightboxJQuery.setPrev();
		NiceLightboxJQuery.setNext();
		$('#photoContainer').html('<div id="img-' + NiceLightboxJQuery.prevPhoto + '" style="display: none; left: 812px;"><img src="' + NiceLightboxJQuery.photos[NiceLightboxJQuery.prevPhoto].image + '" width="' + NiceLightboxJQuery.width + '" height="' + NiceLightboxJQuery.height + '" style="margin-top: ' + NiceLightboxJQuery.marginTop + '; margin-left: ' + NiceLightboxJQuery.marginLeft + ';"></div>\
			<div id="img-' + NiceLightboxJQuery.currPhoto + '" style="display: none; left: 1624px;"><img src="' + NiceLightboxJQuery.photos[NiceLightboxJQuery.currPhoto].image + '" width="' + NiceLightboxJQuery.width + '" height="' + NiceLightboxJQuery.height + '" style="margin-top: ' + NiceLightboxJQuery.marginTop + '; margin-left: ' + NiceLightboxJQuery.marginLeft + ';"></div>\
			<div id="img-' + NiceLightboxJQuery.nextPhoto + '" style="display: none; left: 2436px;"><img src="' + NiceLightboxJQuery.photos[NiceLightboxJQuery.nextPhoto].image + '" width="' + NiceLightboxJQuery.width + '" height="' + NiceLightboxJQuery.height + '" style="margin-top: ' + NiceLightboxJQuery.marginTop + '; margin-left: ' + NiceLightboxJQuery.marginLeft + ';"></div>');
		// NiceLightboxJQuery.margins('#photoContainer div img');
		// NiceLightboxJQuery.margins('#img-' + NiceLightboxJQuery.prevPhoto);
		// NiceLightboxJQuery.margins('#img-' + NiceLightboxJQuery.currPhoto);
		// NiceLightboxJQuery.margins('#img-' + NiceLightboxJQuery.nextPhoto);
		NiceLightboxJQuery.photoPara();
		NiceLightboxJQuery.setImgNumber();
		$('#imgTotal').html(NiceLightboxJQuery.photos.length);
		var pageSize = getPageSize();
		var getScroll = getScrollXY();
		$('#img-' + NiceLightboxJQuery.nextPhoto)
		$("#screen").css({
			'height': pageSize[1] + 'px',
			'opacity': '0.8'
		}).fadeIn(500);
		$("#nLWrapper").css({
			'top': getScroll[1] + 'px'
		}).show();
		$('#niceLightbox').show().css({
			'width': '1px',
			'height': '1px',
			'margin': '0',
			'opacity': '0.0'
		}).animate({
			'width': '812px',
			'height': '514px',
			'margin-top': '-272px',
			'margin-left': '-406px',
			'opacity': '1.0'
		}, 500, function() {
			$('#blind').show();
			$('#photoWindow').show();
			$('#photoContainer div').fadeIn(500);
			$('#descPrevNext').fadeIn(500);
			$('#nlbClose').fadeIn(500);
		});
	},
	closeNiceLightbox: function() {
		$('#photoContainer div').fadeOut(500);
		$('#descPrevNext').fadeOut(500);
		$('#nlbClose').fadeOut(500, function() {
			$('#photoWindow').hide();
			$('#blind').hide();
			$('#screen').fadeOut(500, function() {
				$('#screen').hide();
			});
			$("#nLWrapper").fadeOut(500, function() {
				$('#nLWrapper').hide();
			});
			$('#niceLightbox').animate({
				'width': '1px',
				'height': '1px',
				'margin-top': '0',
				'margin-left': '0',
				'opacity': '0.0'
			}, 500, function() {
				$('#niceLightbox').hide();
				NiceLightboxJQuery.currPhoto = 0;
			});
		});
	},
	triggerNLB: function(path) {
		$(path).click(function() {
			var id = $(this).attr('id');
			NiceLightboxJQuery.photos = [];
			NiceLightboxJQuery.getPhotos(id);
			return false;
		});
	},
	init: function(path) {
		this.triggerNLB(path);
	}
};
