/**
 * FIX IE6 IMAGE FLICKER PROBLEM
 *
 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


$(document).ready(function() {
	$('a[rel="photo"]').click(function() {
		PhotoGallery.init(this);
		return false;
	});
});

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;

		if($('div.popup').size() < 1)
		{
			$('<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');
		$('div.popup').css('top', parseInt(this.offsetToTop + 25) + 'px');
	}
};



var PhotoGallery = {
	// 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.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;


		PopupOverlay.init('PhotoGallery');
		PopupOverlay.loadContent(this._buildTemplate());

		/*
		Setup events for closing the photo gallery.
		*/
		$('#PhotoGallery_Close').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}, 1000);
   		}

		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());
	},


	_buildTemplate : function() {
		return '<div id="PhotoGallery_Wrapper">' +
					'<a href="#" title="Close" id="PhotoGallery_Close">Close</a>' +
					'<div id="PhotoGallery_Image"></div>' +
					'<a href="#" id="PhotoGallery_Prev">Previous</a>' +
					'<a href="#" id="PhotoGallery_Next">Next</a>' +
					'<div id="PhotoGallery_Description"></div>' +
					'<span class="usagerights">' + $("p.usagerights").html() + '</span>' +
				'</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;
		});
	},


};



