/*

//////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//////////////////
//////////////////  Media Viewer
//////////////////
////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

*/

// Prepare all items for the media Viewer.  Uses the master.js file's items associative array with the keys id, url, desc, title
function prepareMediaViewer()
{
	// Setup base variables
	navDisabled = false;
	
	// Create a master window and a window blackener and attach them to the body
	mediaViewerMaster = document.createElement('div');
	$(mediaViewerMaster).attr('id','CT_MediaViewerMaster');
	$(mediaViewerMaster).html('\n<div id="CT_MediaViewerBlack"></div>\n<div class="IM_Loading"><img src="images/ANIM_Loading_Black.gif"></div><div id="CT_MediaViewerControls"><a id="BT_ItemLink" href="" target="_blank">visit website</a><span id="BT_MVPrev">previous</span> - <span id="BT_MVNext">next</span> - <span id="BT_MVClose">close</span></div>\n<div id="IM_MediaViewer_Current" class="IM_MediaViewer"><img></div>\n\n');
	$(mediaViewerMaster).prependTo('body');
	
	// Create an event to close the window based on clicking the black
	$("#CT_MediaViewerBlack").click(function(){ $(mediaViewerMaster).fadeOut(500); });
	
	$('#BT_MVPrev').click(function() {
		
		// Cancel if the button is disabled
		if (navDisabled) { return; }

		launchMediaViewer('-1');
	});
	
	$('#BT_MVNext').click(function() {
		
		// Cancel if the button is disabled
		if (navDisabled) { return; }
		
		launchMediaViewer('+1');
	});
	
	$('#BT_MVClose').click(function() {
		$(mediaViewerMaster).fadeOut(500);
	});
}

// Launches the media viewer with the item ID as given.  Uses the master.js file's items array
// Passing it a discrete value hides the current and shows the new id
// Passing it the value "+x" or "-x" animates the current one out and the given index in. 
function launchMediaViewer(itemID)
{
	// Make sure that the action doesn't go out of bounds
	theID = parseInt($('#IM_MediaViewer_Current img').attr('id'));
	if (theID+1 >= items.length && itemID.substring(0,1) == "+") { return; }
	if (theID-1 < 0 && itemID.substring(0,1) == "-") { return; }
	
	navDisabled = true;
	var fadeOut = false;
	$('#BT_MVNext').css('opacity','1.0');
	$('#BT_MVPrev').css('opacity','1.0');
	
	$("#IM_MediaViewer_Current").stop(true).animate({opacity: 0}, 250, function() { fadeOut = true; });
	
	// If we are scrolling forward
	if (itemID.substring(0,1) == "+") {
		
		var waitForFadeout = setInterval(function() {
												 
			if (fadeOut)
			{
												 
				// Determine the newID
				theID = parseInt($('#IM_MediaViewer_Current img').attr('id'));
				theNewID = theID + 1;
				if (theNewID+1 >= items.length) { $('#BT_MVNext').css('opacity','0.2'); }
				
				// Fade out the image and reveal the previous button
				theCurrent = $('#IM_MediaViewer_Current img'); theCurrentCT = $('#IM_MediaViewer_Current');
				$('#BT_MVPrev').css('opacity','1.0');
				
				// Fade the loading animation in
				$('#CT_MediaViewerMaster .IM_Loading').stop().animate({opacity:0.5},2000);
				
				// Set a timer to change the src and fade in and out the loading animation
		
					// Change the src of the image
					$(theCurrent).attr('src',items[theNewID]['url']);
					$(theCurrent).attr('id',theNewID);
					
					// If the image has a link item attached, show the link button
					if (items[theNewID]['link'] != "")
					{
						$("#BT_ItemLink").attr('href',items[theNewID]['link']);
						$("#BT_ItemLink").css('visibility','visible');
					} else {
						$("#BT_ItemLink").css('visibility','hidden');
					}
					
					// Fade in and out on the new image load
					$(theCurrent).load(function()
					{
						navDisabled = false;
						$(theCurrentCT).animate({opacity: 1.0}, 350);
						$('#CT_MediaViewerMaster .IM_Loading').stop().animate({opacity:0},200);
					});
		
				
				// Check if going next will enter the next range of images and cycle if so
				if (theNewID > range2)
				{
					$(".BT_PgnNext").trigger('click');
				}
				
				clearInterval(waitForFadeout);
				
			}		
			
		}, 250);
	
	// If we are scrolling backward
	} else if (itemID.substring(0,1) == "-") {
		
		var waitForFadeout = setInterval(function() {
												 
			if (fadeOut)
			{
				
				// Determine the newID
				theID = parseInt($('#IM_MediaViewer_Current img').attr('id'));
				theNewID = theID - 1;
				if (theNewID <= 0) { $('#BT_MVPrev').css('opacity','0.2'); }
				
				// Fade out the image and reveal the next button
				theCurrent = $('#IM_MediaViewer_Current img'); theCurrentCT = $('#IM_MediaViewer_Current');
				$('#BT_MVNext').css('opacity','1.0');
				
				// Fade the loading animation in
				$('#CT_MediaViewerMaster .IM_Loading').stop().animate({opacity:0.5},2000);
				
				// Set a timer to change the src and fade in and out the loading animation
		
					// Change the src of the image
					$(theCurrent).attr('src',items[theNewID]['url']);
					$(theCurrent).attr('id',theNewID);
					
					// If the image has a link item attached, show the link button
					if (items[theNewID]['link'] != "")
					{
						$("#BT_ItemLink").attr('href',items[theNewID]['link']);
						$("#BT_ItemLink").css('visibility','visible');
					} else {
						$("#BT_ItemLink").css('visibility','hidden');
					}
				
					// Fade in and out on the new image load
					$(theCurrent).load(function()
					{
						navDisabled = false;
						$(theCurrentCT).animate({opacity: 1.0}, 350);
						$('#CT_MediaViewerMaster .IM_Loading').stop().animate({opacity:0},200);
					});
				
				// Check if going previous will enter the previous range of images and cycle if so
				if (theNewID < range1)
				{
					$(".BT_PgnPrev").trigger('click');
				}
				
				clearInterval(waitForFadeout);
				
			}
			
		}, 250);
		
	} else {
				
		// Setup a loading animation
		$('#CT_MediaViewerMaster .IM_Loading').css('zIndex',10).animate({opacity:0.7},250);
		
		// Hide the image
		$('#IM_MediaViewer_Current').css('visibility','hidden');
		$('#IM_MediaViewer_Prev').css('visibility','hidden');
		$('#IM_MediaViewer_Next').css('visibility','hidden');
		
		// Set the id
		theID = itemID.substring(4);
		theCurrentSRC = items[theID]['url'];
		if (parseInt(theID)+1 >= items.length) { $('#BT_MVNext').css('opacity','0.2'); }
		if (parseInt(theID) <= 0) { $('#BT_MVPrev').css('opacity','0.2'); }
		
		$('#IM_MediaViewer_Current img').attr('src','');
		$('#IM_MediaViewer_Current img').attr('src',theCurrentSRC);
		$('#IM_MediaViewer_Current img').attr('id',theID);
		
		// If the image has a link item attached, show the link button
		if (items[theID]['link'] != "")
		{
			$("#BT_ItemLink").attr('href',items[theID]['link']);
			$("#BT_ItemLink").css('visibility','visible');
		} else {
			$("#BT_ItemLink").css('visibility','hidden');
		}
		
		// Reveal the items when the first one has loaded
		$('#IM_MediaViewer_Current img').bind('load',function()
		{
			navDisabled = false;
			theCurrentWidth = $('#IM_MediaViewer_Current img').width();
			$('#IM_MediaViewer_Current').css('opacity','0').css('visibility','visible').animate({opacity: 1.0}, 500);
			$('#CT_MediaViewerMaster .IM_Loading').animate({opacity:0},200);
		});
		
	}
	
	// Display the media viewer master
	$(mediaViewerMaster).fadeIn(80);
}
