// Display links as a lightbox when in the format of
// <a href="http://somesite.com" class="modalbutton" rel="640x480">Click here</a>

jQuery(function ($) {
	$('.modalbutton').click(function (e) {
		
		// Get the dimesions from the rel attribute.
		// If the rel attribute is formatted incorrectly,
		// This call will fail and the modal will not load.
		var modalSizeData = $(this).attr('rel');
		var modalSize = modalSizeData.split('x');
		var modalWidth = modalSize[0];
		var modalHeight = modalSize[1];
		var modalStyling = '';
		if (modalSize.length == 3)
		{
			if(modalSize[2] == 'noscroll')
			{
				modalStyling = 'overflow: visible;';
			}
		}
		
		// Call up the modal, using an iframe to hold the link contents
		$.modal('<iframe src="'+ this.href + '" width="'+modalWidth+'" height="'+modalHeight+'" style="border: 0px solid black; margin-left: 5px; margin-top: 5px; ' + modalStyling + '"></iframe>', 
			{
				// Opening Animation
				onOpen: function (dialog) {
					dialog.overlay.fadeIn('slow', function () {
						dialog.container.slideDown('slow', function () {
							dialog.data.fadeIn('slow');
						});
					});
				},
				
				// Closing Animation
				onClose: function (dialog) {
					dialog.data.fadeOut('slow', function () {
						dialog.container.slideUp('slow', function () {
							dialog.overlay.fadeOut('slow', function () {
								$.modal.close();	
							});
						});
					});
				},
				
				// A little last-minute styling.
				// Extra 10px to width and height to avoid scrollbars.
				containerCss:{
					backgroundColor:"#fff",
					borderColor:"#fff",
					height:(parseInt(modalHeight, 10) + 10),
					padding:0,
					width:(parseInt(modalWidth, 10) + 10)	
				}
			}
		);
		return false;
	});
});
