$(function() {
	/*
	 * Image Gallery links
	 * see http://github.com/mjijackson/shadowbox/blob/master/examples/script-gallery.html
	 */
	// Look for view_gallery links and add the launchShadowbox class just in case it wasn't added in the cms
	$('#container a').each(function() {
		var href = $(this).attr('href');
		
		if (href !== undefined)
		{
			if (href.match(/library\/view_gallery/))
				$(this).addClass('launchShadowbox');
		}
	});
	
	// SB links
	$('a.launchShadowbox').click(function() {
		var options = {
			continuous: true,
			slideshowDelay: 5
		};
		
		// Get the images from the gallery
		var href = $(this).attr('href');
		var title = '';
		
		if ($(this).attr('title').length > 0)
			title = $(this).attr('title');
		
		$.get(href, function(data) {
			// Split the data into an array
			var dataA = data.split(',');
			var sbA = [];
			
			for (var i=0; i < dataA.length; i++)
			{
				var images = {
					player: "img",
					title: title,
					content: dataA[i],
					options: options
				}
				
				sbA.push(images);
			}
			
			Shadowbox.open(sbA);
		});
		
		return false;
	});
	
	/*
	 * Messaging
	 */
	$('#closeMsg').click(function() {
		$('#msg').slideUp(200);
	});

	/*
	 * Folder Tree
	 */
	if ($('#folderTree').length > 0) {
		$("#folderTree").treeview();
	}
	
	if ($('#lpFolderTree').length > 0) {
		$("#lpFolderTree").treeview();
	}

	/*
	 * Page previews
	 */
	$('#preview').click(function() {
		NewWindow($(this).attr('href'), 'preview', 1024, 768, 'yes');
		return false;
	})

	// Disable links in previews
	var url = window.location.href;
	if (url.indexOf('?preview') > -1) {
		$('a:not("#previewMsg a")').click(function() {
			return false;
		});
		
		$('input[type="submit"]').click(function() {
			return false;
		});
	}
	
	/*
	 * More info links (popup)
	 */
	$('.moreInfo').click(function() {
		NewWindow($(this).attr('href'), 'moreInfo', 400, 270, 'yes');
		return false;
	})
});

/* NewWindow popup functionality */
function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function SwapImage(image, filename) {
    image.src = filename;
}
