/*
 * $ site Overlay
 * Version 1.2.2 (2007-12-14)
 *
 * $Id$
 *
 * Copyright (c) 2007 That's-id Multimedia
 */


function galSelect(gal_Id, page)
{
	if (!page) page=1;
	$.get('/dynamics/gallery.php', { gal_Id: gal_Id, page: page},function(data)
	{
		$('#galContent').html(data);
	});
}



function showPub(pub_Id, action)
{
	overlayPub = pub_Id;
	overlayAction = action;

	$.ajax({
		url:	'/dynamics/getoverlay.php',
		data:	{pub_Id: pub_Id, action: action},
		success: function(content)
		{
			showOverlay();
			$('#popup').html(content);
			showPopup(popupWidth, popupHeight);
			$('#overlay form').validatorEnable({ajaxHandler: setForm});
		},
		error: ajaxError

	});
}

function setForm(form_Id)
{
	$.ajax({
		type: 'POST',
		url: '/dynamics/getoverlay.php?pub_Id='+overlayPub+'&action='+overlayAction,
		data: $('form#'+form_Id+' :input').serialize(),
		success: function(content){
			$('#popup').html(content);
			showPopup(popupWidth, popupHeight);
			$('#overlay form').validatorEnable({ajaxHandler: setForm});
		},
		error: ajaxError
	});
	return false;
}

function ajaxError()
{
	alert('Failed to execute ajax request');
}


/*------------------------------------------------------------------------------
	BASIC BEHAVIOR
------------------------------------------------------------------------------*/


var overlayLoaded = false;
var popupVisible = false;
var popupWidth = 800;
var popupHeight = 600;
var popupAnimation;

var overlayPub;
var overlayAction;


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');
					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;
	}
});

function imagePopup(src, title, animation)
{
	popupAnimation = animation;

	// show overlay
	showOverlay();

	// preload image
	imgPopup = new Image();
	imgPopup.onload = function()
	{

		var imgWidth = imgPopup.width;
		var imgHeight = imgPopup.height;

		if (imgWidth > popupWidth)
		{
			imgWidth = popupWidth;
			imgHeight = imgHeight * (imgWidth / popupWidth);
		}
		if (imgHeight > popupHeight)
		{
			imgHeight = popupHeight;
			imgWidth = imgWidth * (imgHeight / popupHeight);
		}

		// img tag
		var content = '<img src="'+src+'" id="popupImg" width="'+imgWidth+'" height="'+imgHeight+'">';

		// when text is available
		if (title)
		{
			content += '<span id="popupImgText">'+title+'</span>';
			imgHeight += 20;
		}

		// add image to popup
		$('#popup').html(content);

		// animate popup
		showPopup(imgWidth, imgHeight);

		imgPopup.onload=null;	//	clear onLoad, IE behaves irratically with animated gifs otherwise
	}
	imgPopup.src=src;
}


function showOverlay()
{
	if (!overlayLoaded)
	{
	
		overlayLoaded = true;


		// 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"> Loading...</div></div></div>').appendTo('body');

		//$('#overlay_mask').click(function(){ removeOverlay(); return false; });
		$('#popupClose').click(function(){ hidePopup(); return false; });

		// stretch to full height & width
		$('#overlay_mask')
			.height(document.documentElement.scrollHeight > $('html').height() ? document.documentElement.scrollHeight : $('html').height())
			.width(document.documentElement.scrollWidth);
			
		$('#overlay')
			.css('padding-top', document.documentElement.scrollTop+'px');

		// ie6 specific
		if($.browser.msie || popupAnimation=='none')
		{
			if ($.browser.msie) $(':input').css('visibility', 'hidden');
			$('#overlay_mask').show();
			
		}
		else
		{
			$('#overlay_mask').fadeIn(500);
		}
	}
}

function showPopup(pWidth, pHeight)
{

	if (!popupVisible)
	{
		popupVisible = true;

		var marginTop = parseInt(($('html').height() - pHeight) / 2);
		$('#overlay_load').hide();

		switch (popupAnimation)
		{
			case 'none':
				$('#overlay_popup').css({top: marginTop+'px'}).width(pWidth).height(pHeight).show();
				$('#popup, #popupImg, #popupClose, #popupImgText').show();
				break;
			default:
				// set margin top
				$('#overlay_popup').css({top: marginTop+'px'}).height(5).width(0);

				// show popup
				$('#overlay_popup').animate({
					width: pWidth
				}, null, null, function(){
					$('#overlay_popup').animate({height: pHeight}, null, null, function()
					{
						$('#popup').show();
						//$('#popup').height(pHeight).width(pWidth);

						$('#popupImg, #popupClose, #popupImgText').animate({opacity: 'toggle'});
					});
				});
				break;
		}
	}
	else
	{
		$('#popup').show();
	}
}

function hidePopup()
{
	switch (popupAnimation)
	{
		case 'none' :
			removeOverlay();
			break;
		default :
			var width = $('#overlay_popup').width();
			var height = $('#overlay_popup').height();

			$('#popup').animate({opacity: 'toggle'}, null, null, function()
			{
				$('#overlay_popup').html('').animate({height: 5}, null, null, function()
				{
					$('#overlay_popup').animate({width: 'toggle'}, null, null, function()
					{
						hideOverlay();
					});
				});
			});
			break;
	}
}

function hideOverlay()
{
	if($.browser.msie || popupAnimation=='none')
	{
		removeOverlay();
	}
	else
	{
		$('#overlay_mask').fadeOut(500, function()
		{
			removeOverlay();
		});
	}
}

function removeOverlay()
{
	if (overlayLoaded)
	{
		overlayLoaded = false;
		popupVisible = false;

		// remove overlay
		$('#overlay').remove();
		if($.browser.msie) $(':input').css('visibility', 'visible');
	}
}
