
/**
 * Popup Overlay
 *
 * Grabs URL within anchor tag and loads the page, filtering out all
 * content except what has been provided as an anchor target.
 *
 * i.e. <a href="popuppage.html#targetID"> ... </a>
 *
 */
var PopupOverlay = {
	offsetToTop : 0,
	offsetOfElement : 0,
	popupID : '',
	status : false,

	init: function(id) {
		var obj = this;

		$('<div id="overlay"></div>').appendTo('body');
		$('#overlay').css({
			height: $(document).height() + 'px',
			width: $(window).width() + 'px'
		});

		this.popupID = (id !== '') ? id : 'PopupDefault';

		//$(window).scroll(this._offset);
		$(window).resize(function(){
			$('#overlay').css({
				height: $(document).height() + 'px',
				width: $(window).width() + 'px'
			});

			obj._offset();
		});

		this.status = true;

		$('<div class="popup"></div>').attr('id', this.popupID).appendTo('body');

		/*
		"Anywhere" click closing function.
		*/
		$('#overlay').click(function(){
			obj.close('obj.init');
		});
	},

	loadContent: function(e) {
		$('div.popup').html(e).show();
		this._offset();
	},

	close: function(evtfunc) {
		$('div.popup').remove();
		$("#overlay").remove();

		this.status = false;

		$(document).unbind('keydown', evtfunc);

		return false;
	},

	loadMessage: function() {
		$('div.popup').html('<p class="tcenter"><strong>Loading... please wait.</strong></p>')
		$('div.popup').show();

		this._offset();
	},

	_offset: function() {
		this.offsetToTop = $(window).scrollTop();
		this.offsetOfElement = parseInt(($(window).height() - $('div.popup').height()) / 2);
		$('div.popup').css('top', parseInt(this.offsetToTop + this.offsetOfElement) + 'px');
	}
};




var PhotoGalleryImage = {
	// Number of photos in the gallery.
	_numPhotosInGallery : 0,

	// Current (selected) photo in the gallery.
	_currPhotoInGallery : 0,

	// Current photo gallery
	_currGallery : 0,

	_currLang : '',


	init : function(e) {this._langStrings('previous');
		// Determine parent of gallery.
		this._currGallery = $(e).parents('div.box');

		this._numPhotosInGallery = $('a[rel="photo"]', this._currGallery).size();
		this._currPhotoInGallery = (e) ? $('a[rel="photo"]', this._currGallery).index(e) : 0;

		// Retrieve the current language, as set on our wrapper DIV.
		this._currLang = $('#wrapper').attr('class');
		


		PopupOverlay.init('PhotoGallery');
		PopupOverlay.loadContent(this._buildTemplate());
		PopupOverlay.close();
		PopupOverlay.init('PhotoGallery');
		PopupOverlay.loadContent(this._buildTemplate());
		
		/*
		Setup events for closing the photo gallery.
		*/
		$('#PhotoGallery_Banner a').click(function(e){
			return PopupOverlay.close();
		});
		$(document).bind('keydown', function ClosePopup(e){
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			(key == 27) ? PopupOverlay.close(ClosePopup) : false;
		});
		
		this.loadImage(this._currPhotoInGallery, this._currGallery);
		this._initNavigation();
		this._navigationControls();
	},

	loadImage : function(imgID) {

   		var newImg = $(document.createElement('img')).attr('src', $('a[rel="photo"]', this._currGallery).eq(imgID).attr('href')).appendTo('#PhotoGallery_Image').css({opacity: 0}).load(FadeInOutImage);

   		function FadeInOutImage()
   		{
   			if($('#PhotoGallery_Image img').size() > 1)
   			{
   				$('#PhotoGallery_Image img:first').animate({opacity: 0}, 2000, function(){
   					$(this).remove();
   				});
   			}

   			$(newImg).animate({opacity: 1}, 2000);
   		}

		if(newImg[0].complete)
		{
			FadeInOutImage();
		}

		// Caption/description of the current image.
		$('#PhotoGallery_Description').html($('a[rel="photo"]', this._currGallery).eq(imgID).children('span.description').html());
	},

	/*
	Creates static HTML template that will be used for displaying the photo
	viewer.
	*/
	_buildTemplate : function() {
		var tmp=$('p.usagerights').html();
		if((null == tmp ) || ( "null" == tmp )){tmp="";} 
		
		return '<div id="PhotoGallery_Wrapper" class="' + $('#wrapper').attr('class') + '">' +
					'<h1 id="PhotoGallery_Banner" title="' + this._langStrings('title') + '">' +
					this._langStrings('title') +
					'<a href="#" title="' + this._langStrings('close') + '">' + this._langStrings('close') + '</a>' +
					'</h1>' +
					'<div id="PhotoGallery_Image"></div>' +
					'<div class="clearfix"><a href="#" id="PhotoGallery_Prev">' + this._langStrings('previous') + '</a>' +
					'<a href="#" id="PhotoGallery_Next">' + this._langStrings('next') + '</a></div>' +
					'<div id="PhotoGallery_Description"></div>' +
					'<div id="PhotoGallery_Usage">' + tmp + '</div>' +		
					'</div>';
		
	},

	_initNavigation : function() {
		(this._currPhotoInGallery == 0) ? $('#PhotoGallery_Prev').hide() : $('#PhotoGallery_Prev').show();
		(this._currPhotoInGallery < this._numPhotosInGallery - 1) ? $('#PhotoGallery_Next').show() : $('#PhotoGallery_Next').hide();
	},

	_navigationControls : function() {
		var obj = this;

		$('#PhotoGallery_Prev').click(function(){
			obj.loadImage(obj._currPhotoInGallery - 1, obj._currGallery);
			obj._currPhotoInGallery--;
			obj._initNavigation();
			return false;
		});
		$('#PhotoGallery_Next').click(function(){
			obj.loadImage(obj._currPhotoInGallery + 1, obj._currGallery);
			obj._currPhotoInGallery++;
			obj._initNavigation();
			return false;
		});
	},

	_langStrings : function(tag) {
		var lang_en = {
			title : 'Photo Gallery',
			close : 'Close',
			next : 'Next',
			previous : 'Previous'
		}

		var lang_fr = {
			title : 'Galerie de photos',
			close : 'Fermer',
			next : 'Suivant',
			previous : 'Précédent'
		}

		return (this._currLang == 'en') ? lang_en[tag] : lang_fr[tag] ;
	}

};




/*
Create a popup window at a specificed location on the screen.
*/
function Popup(href, windowName, y, x){
	//var popup = window.open(href, windowName, 'width=415,height=340,location=no,menubar=no,status=no,scrollbars=yes,top='+ (parseInt(y) + 30) +',left='+ (parseInt(x) - 20));
	var popup = window.open(href, windowName, 'width=415,height=450,location=no,menubar=no,status=no,scrollbars=no,top='+ 40 +',left='+ (parseInt(x) - 20));
	popup.focus();
}