/*
 * $ jQuery Site Overlay
 * Version 1.3.1 (2008-01-30)
 *
 * $Id$
 *
 * Copyright (c) 2008 That's-id Multimedia
 *
 */

function gallerySelect(gal_Id, page)
{
	if (!page) page=1;
	$.get('/dynamics/gallery.php', { gal_Id: gal_Id, page: page},function(data)
	{
		$('#galContent').html(data);
		$('#galPopupLink').setZoomHandler();
		$('a[rel=gallery]').setGalleryHandler();
	});
}



function showPub(pub_Id, action)
{
	$.overlay.status.overlayPub = pub_Id;
	$.overlay.status.overlayAction = action;
	$.overlay.showOverlay();
	if (action) $('#overlay_popup').addClass(action);
	$.ajax({
		url:	'/dynamics/getoverlay.php',
		data:	{pub_Id: pub_Id, action: action},
		success: function(content)
		{
			$('#popup').html(content);
			$.overlay.showPopup($.overlay.popupWidth, $.overlay.popupHeight);
			if (typeof $.validator != 'undefined') $('#overlay form').validatorEnable({hdlCallback: setForm});
		},
		error: $.overlay.removeOverlay
	});
}

function setForm(validatedOK,form)
{
	if (validatedOK)
	{
		$.overlay.showOverlay();
		$.ajax({
			type: 'POST',
			url: '/dynamics/getoverlay.php?pub_Id='+$.overlay.status.overlayPub+'&action='+$.overlay.status.overlayAction,
			data: $(':input', form).serialize(),
			success: function(content){
				if (content=='')
				{
					$.overlay.removeOverlay();
				}
				else
				{
					$('#popup').html(content);
					$.overlay.showPopup($.overlay.popupWidth, $.overlay.popupHeight);
					if (typeof $.validator != 'undefined') $('#overlay form').validatorEnable({hdlCallback: setForm});
				}
			},
			error: $.overlay.removeOverlay
		});
	}
	return false;
}


/*------------------------------------------------------------------------------
	Event handlers
------------------------------------------------------------------------------*/

jQuery.fn.extend({
	setZoomHandler: function(animation)
	{
		$.each(this, function()
		{
			this.animation = animation;
			$(this)
				.click(function(){
					var img =  $('img', this);
					var src = img.attr('src').replace(/&thumbnail=1$/, '&thumbnail=2').replace(/&thumb_nr=\d$/, '');
					var title = img.attr('title');
					$.overlay.imagePopup(src, title, this.animation);
					return false;
				});
		});
		return this;
	},
	setGalleryHandler: function()
	{
		$(this)
			.click(function(){
				var img =  $('img', this);
				var src = img.attr('src').replace(/&thumbnail=0$/, '&thumbnail=1');
				var title = img.attr('title');
				//var timeout = setTimeout(showImage, 1000);
				$('img#galImg').parents('a').hide();

				var newimg = new Image();
				newimg.onload = function()
				{
					//if (timeout) clearTimeout(timeout);
					showImage();
					newimg.onload = null;
				};
				newimg.src= src;

				function showImage()
				{
					$('img#galImg')
						.attr('src',src)
						.attr('title',title);
					$('img#galImg')
						.parents('a')
							.fadeIn();
				}

				return false;
			});
		return this;
	}
});


/*------------------------------------------------------------------------------
	CORE
------------------------------------------------------------------------------*/

$.overlay = {

	// internal
	status : {
		overlayLoaded:	false,
		popupVisible:	false,
		overlayPub:		null,
		overlayAction:	null,
		scrollHeight: 0,
		scrollWidth: 0,
		windowHeight: 0,
		windowWidth: 0
	},

	// configurable
	overlayMask: 		0.6,
	overlayAnimation: 	'fade',
	overlayAnimationTime:500,
	popupWidth:			800,
	popupHeight:		600,
	popupAutoMaxSize: 	true,
	popupValign: 		'center',
	popupAnimation:		'slide',
	popupAnimationTime:	500,
	popupCloseButton: 	true,


 	imagePopup:	function(src, title, animation)
	{
		if (animation) $.overlay.popupAnimation = animation;

		// show overlay
		$.overlay.showOverlay();

		popupWidth = $.overlay.popupWidth;
		popupHeight = $.overlay.popupHeight;
		maxHeight = $.overlay.status.scrollHeight - 50;
		maxWidth = $.overlay.status.scrollWidth - 50;
		if ($.overlay.popupAutoMaxSize || popupHeight > maxHeight || popupWidth > maxWidth)
		{
			popupWidth = maxWidth;
			popupHeight = maxHeight;
		}

		// preload image
		imgPopup = new Image();
		imgPopup.onload = function()
		{
			var imgWidth = this.width;
			var imgHeight = this.height;

			if (imgWidth > popupWidth)
			{
				imgHeight = imgHeight * (popupWidth / imgWidth);
				imgWidth = popupWidth;
			}
			if (imgHeight > popupHeight)
			{
				imgWidth = imgWidth * (popupHeight / imgHeight);
				imgHeight = popupHeight;
			}

			// add image to popup
			$('#popup').html('<img src="'+src+'" id="popupImg" width="'+imgWidth+'" height="'+imgHeight+'">' + (title ? '<span id="popupImgText">'+title+'</span>' : ''));

			// animate popup
			$.overlay.showPopup(imgWidth, imgHeight);

			imgPopup.onload=null;
		};
		imgPopup.src=src;
	},

	showPopup: function(pWidth, pHeight)
	{
		if (!$.overlay.status.popupVisible)
		{
			$.overlay.status.popupVisible = true;

			$('#overlay_load').hide();

			// position popup with scroll height
			$('#overlay').css('padding-top', document.documentElement.scrollTop+'px');

			// set vertical align
			var top = document.documentElement.scrollTop;
			switch ($.overlay.popupValign)
			{
				case 'top' : break;
				case 'bottom' : top += $.overlay.status.windowHeight-pHeight; break;
				default : top += ($.overlay.status.windowHeight-pHeight)/2; break;
			}

			$('#overlay_popup').css('top', parseInt(top)+'px');

			// set popup animation
			switch ($.overlay.popupAnimation)
			{
				case 'slide':
					$('#overlay_popup').height(5).width(0).animate({width: pWidth}, $.overlay.popupAnimationTime, null, function(){
						$('#overlay_popup').animate({height: pHeight}, $.overlay.popupAnimationTime, null, function()
						{
							$('#popup'+($.overlay.popupCloseButton?', #popupClose':'')).fadeIn($.overlay.popupAnimationTime);
						});
					});
					break;
				case 'fade':
					$('#overlay_popup').width(pWidth).height(pHeight);
					$('#overlay_popup, #popup'+($.overlay.popupCloseButton?', #popupClose':'')).fadeIn($.overlay.popupAnimationTime);
					break;
				default :
					$('#overlay_popup').width(pWidth).height(pHeight).show();
					$('#popup'+($.overlay.popupCloseButton?', #popupClose':'')).show();
					break;
			}
		}
	},

 	hidePopup: function()
	{
		switch ($.overlay.popupAnimation)
		{
			case 'slide' :
				if ($.overlay.popupCloseButton) $('#popupClose').fadeOut($.overlay.popupAnimationTime);
				$('#popup').fadeOut($.overlay.popupAnimationTime, function()
				{
					$('#overlay_popup').empty().animate({height:5}, $.overlay.popupAnimationTime, null, function()
					{
						$('#overlay_popup').animate({width:'toggle'}, $.overlay.popupAnimationTime, null, $.overlay.hideOverlay);
					});
				});
				break;
			case 'fade' :
				$('#overlay_popup').fadeOut($.overlay.popupAnimationTime,  $.overlay.hideOverlay);
				break;
			default :
				$.overlay.removeOverlay();
				break;
		}
	},

 	showOverlay: function()
	{
		if (!$.overlay.status.overlayLoaded)
		{
			$.overlay.status.overlayLoaded = true;

			$.overlay.setSizes();

			// show overlay
			$('<div id="overlay"><div id="overlay_mask"></div><div id="overlay_load"></div><div id="overlay_popup"><a href="/" id="popupClose"></a><div id="popup"></div></div></div>').appendTo('body');

			$('#overlay').show();
			// bind event to close button
			$('#popupClose').click(function(){ $.overlay.hidePopup(); return false; });

			// stretch to full height & width
			if ($.overlay.overlayMask > 0)
			{
				$('#overlay_mask')
					.hide()
					.height($.overlay.status.scrollHeight)
					.css({opacity: $.overlay.overlayMask})
					.click(function(){ $.overlay.removeOverlay(); return false; });

				switch ($.overlay.overlayAnimation)
				{
					case 'fade' :
						$('#overlay_mask').fadeIn($.overlay.overlayAnimationTime)
						break;
					default :
						$('#overlay_mask').show();
						break;
				}
			}

			// ie6 specific
			if($.browser.msie && $.browser.version < 7)  $(':input').css('visibility', 'hidden');
		}
		else
		{
			$('#overlay_load').show()
		}
	},

 	hideOverlay: function()
	{
		if ($.overlay.overlayMask > 0)
		{
			switch ($.overlay.overlayAnimation)
			{
				case 'fade' :
					$('#overlay_mask').fadeOut($.overlay.overlayAnimationTime, $.overlay.removeOverlay);
					break;
				default :
					$.overlay.removeOverlay();
					break;
			}
		}
		else
		{
			$.overlay.removeOverlay();
		}
	},

	removeOverlay: function()
	{
		if ($.overlay.status.overlayLoaded)
		{
			$.overlay.status.overlayLoaded = false;
			$.overlay.status.popupVisible = false;

			$('#overlay').remove();
			if($.browser.msie && $.browser.version < 7) $(':input').css('visibility', 'visible');
		}
	},

	setSizes: function()
	{
		$.overlay.status.windowHeight = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight > document.body.offsetHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
		$.overlay.status.windowWidth = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth > document.body.offsetWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
		$.overlay.status.scrollHeight = (document.body.scrollHeight > document.documentElement.scrollHeight ? document.body.scrollHeight : document.documentElement.scrollHeight);
		$.overlay.status.scrollWidth = (document.body.scrollWidth > document.documentElement.scrollWidth ? document.body.scrollWidth : document.documentElement.scrollWidth);
	}

}


