$(document).ready(
	function()
	{
		$('div.popup').Draggable(
		{
			zIndex: 	1000,
			ghosting:	true,
			opacity: 	0.2
		});

		$('img.thumb').click(function()
		{
			activatePopup(this);
			this.blur();
			return false;
		});

		$('img.windowClose').click(function()
		{
			var popupId = $(this).attr('id').replace('close','popup');
			$('#'+popupId).TransferTo(
			{
				to:popupId,
				className:'transferer2',
				duration: 400
			}).hide();
		});

		$('img.printerPic').click(function()
		{
			var baseId = $(this).attr('id').replace('print_','');
			insertInIframe(baseId);
			top.print_frame.print();
		});

		$('img.nextPic').click(function()
		{
			var baseId = + $(this).attr('id').replace('next_','');
			activatePopup($('#thumb_' + baseId));
		});

		$('img.previousPic').click(function()
		{
			var baseId = + $(this).attr('id').replace('prev_','');
			activatePopup($('#thumb_' + baseId));
		});
        
    if(document.getElementById('map')) {  
      var cwp  = {
        mapId         : 'map',
        mapWidth      : 400,
        mapHeight     : 300,
        latitude      : 49.647469,
        longitude     : 5.9554,
        zoom          : 13,
        departAddress : "20 Rue d'Arlon, 8399 Koerich, Capellen, Luxembourg, Luxembourg"
      }
      new simpleGmap(cwp);
    } 
	}
);



function activateWindow(sender)  {
  $('div.popup').css('zIndex', 300);
  $(sender).css('zIndex', 500);
}

function insertInIframe(mynum) {
  var iFrame                     = top.print_frame;
  iFrame.document.body.innerHTML = '' ;
  iFrame.tmp                     = window.document.getElementById('popup_' + mynum).innerHTML;
  iFrame.document.body.innerHTML = '<div id="popup_' + mynum + '">' + iFrame.tmp + '</div>';
}

function activatePopup(sender)
{
	var baseId = $(sender).attr('id').replace('thumb_','');
	if (!document.getElementById('pic_' + baseId))
	{
		myPic     = new Image();
		myPic.id  = "pic_" + baseId;
		myPic.src = $(sender).attr('src').replace('/thumbs/', '/pictures/');

		myPic.onload = function()
		{
			document.getElementById("picbox_" + baseId).appendChild(myPic);
			var descriptionHeight = parseInt($('div.descriptionbox').css('height'));
			var popupWidth  = myPic.width;
			var popupHeight = myPic.height + descriptionHeight;
			$('#popup_' + baseId).css('width',popupWidth).css('height', popupHeight);
		}
	}

	/*-----show img-------- */
	// get size of #contents
	// move block to the middle

	if($('#popup_' + baseId).css('display') == 'none')
	{
		posx = 150 + Math.round(Math.random() * 50);
		$('#popup_' + baseId).css('left', posx);
		$(sender).TransferTo(
		{
			to:'popup_' + baseId,
			className:'transferer2',
			duration: 400,
			complete: function()
			{
				$('#popup_' + baseId).css('border','1px solid silver').show();
				activateWindow($('#popup_' + baseId));
				$('#popup_' + baseId).mousedown(function()
				{
					activateWindow($('#popup_' + baseId));
				})
			}
		});
	} else {
		activateWindow($('#popup_' + baseId));
	}
}
