// This is the jQuery Javascript for Little Forever

// totalImages specifies how many images there are in a particular gallery //
// gallery images are named PicN; totalImages a multiple of 8 is very good //

//var pageNames = new Array('Home', 'Galleries', 'Info', 'Sessions', 'Resources', 'Contact');
var pageIndex = 0, pageName = '', menuItemId = '', menuIndex = 0, menuItemName = '';
var menuItems = new Array(); 
	menuItems[2] = 6; menuItems[3] = 6; menuItems[4] = 3; menuItems[5] = 3;

var galleryNames = new Array('Pregnancies', 'Newborns', 'Infants', 'Toddlers', '18 to 5', 'Families');
var gallerySizes = new Array(19, 24, 24, 24, 32, 16); 
var gallery = '', galleryIndex = 0, thumbIndex = 0, thumbFile = "";
var showInterval, imageIndex = 0, imageFile = '', lastImage = 0, totalImages = 24;
var loc = 0, autoStart = 1, s = '', hContents = 586;

function LCode() {
	S = String.fromCharCode(60,97,32,104,114,101,102,61,34,109,97,105,108,116,111);
	S = S + String.fromCharCode(58,76,105,110,100,97,64,76,105,110,100,97,115,76,101);
	S = S + String.fromCharCode(110,115,101,115,46,99,111,109,34,62,76,105,110,100,97);
	S = S + String.fromCharCode(64,76,105,110,100,97,115,76,101,110,115,101,115,46,99);
	S = S + String.fromCharCode(111,109,60,47,97,62);
	return S;
}

function loadGallery() {
	galleryName = galleryNames[galleryIndex];
	$('#menuItemName').text(galleryName);
	$('#currentMenuItem').show();
	stopSlideshow();
	for (loc = 1; loc < 9; loc++) { thumbId = '#thumb' + loc; $(thumbId).css('opacity',0); }
	$('#mainPanel').hide();	$('#slideshowPanel').show(); 
	$('#show-start-stop').text('Pause').show().fadeTo('fast',0.5);
	totalImages = gallerySizes[galleryIndex];
	gallery = 'Galleries/' + galleryName + '/';
	if (totalImages == 0) { alert('no images for ' + gallery); return; }
	autoStart = 1; thumbIndex = 0; imageIndex = 0; startSlideshow();
}

function playSlideshow() {
	imageIndex = (imageIndex % totalImages) + 1;
	imageFile = gallery + 'pic' + imageIndex + '.jpg';
  $('#image').hide().load( function(){$(this).fadeIn(750);} ).attr('src',imageFile);
	if (autoStart) { autoStart = 0; thumbIndex = 0; loadThumbs(); }
}

function startSlideshow() {
	playSlideshow(); showInterval = setInterval('playSlideshow()',2500);
}

function loadThumbs() {
	for (loc = 1; loc < 9; loc++) {
		thumbIndex = (thumbIndex % totalImages) + 1;
		thumbFile = gallery + 'thumbs/pic' + thumbIndex + '.jpg';
	  $('#thumb' + loc).hide().load(function(){$(this).show()}).css('opacity',0.5).attr('src',thumbFile);
	}
}

function stopSlideshow() {
	clearInterval(showInterval);
}

function showSingleImage(thumbImageFile) {
	stopSlideshow(); $('#show-start-stop').text('Resume');
	imageFile = gallery + thumbImageFile.substr(thumbImageFile.lastIndexOf('/')+1);
  var image = $('#image');
  image.fadeOut(50,function() {
  	image.load( function(){$(this).fadeIn(400);} ).attr('src',imageFile);
  });
	imageIndex = imageFile.substring(imageFile.lastIndexOf('pic')+3,imageFile.lastIndexOf('.jpg'));
}

var vWidth = 580, vHeight = 360;
function playYouTubeVideo(youTubeCode) {
	// Open and focus a new popup window
  var windowWidth = vWidth, windowHeight = vHeight;
	var left = (window.screen.width - (windowWidth + 10))/2;   // plus two 5-pixel borders
	var top = (window.screen.height - (windowHeight + 50))/2;  // plus title and status bars
	var popWin = window.open("dummy.htm", "myWin", "left="+left+",top="+top+",width="+windowWidth+",height="+windowHeight);
	if (window.focus) { popWin.focus() }
	// Create the popup window contents using the youTubeCode
	var popDoc = popWin.document;
	var uRef = 'http://www.youtube.com/v/' + youTubeCode + '&hl=en_US&fs=1&rel=0&border=1&hd=1&rel=0';
	popDoc.write("<html><head><title>Linda's Lenses YouTube Videos</title></head>");
	popDoc.write('<body bgcolor=0 style="margin:0">');
	popDoc.write('<object width="' + vWidth + '" height="' + vHeight + '">');
	popDoc.write('<param name="movie" value="' + uRef + '"></param>');
	popDoc.write('<param name="allowFullScreen" value="true"></param>');
	popDoc.write('<embed src="' + uRef + '" type="application/x-shockwave-flash" allowfullscreen="true" width="' + vWidth + '" height="' + vHeight + '">');
	popDoc.write('</embed></object></body></html>');
	popDoc.close();
}

function ShowFlash(Item) {
  var windowWidth = 890, windowHeight = 580;
	HREF = Item + "/index.html"
	var left = (window.screen.width - (windowWidth + 10))/2;   // plus two 5-pixel borders
	var top = (window.screen.height - (windowHeight + 50))/2;  // plus title and status bars
	window.open(HREF, "",     "left="+left+",top="+top+",    width="+windowWidth+",height="+windowHeight)
}

function Startup() {
	// Managing the buttons (all pages have buttons)
  $('.buttons')
  	.css('opacity',0.5)	// start with all buttons at half mast
		.mouseover(function() { $(this).fadeTo("fast",1.0) })
		.mouseout(function() { $(this).fadeTo("fast",0.5)
	});
	// Managing the arrows (all pages have arrows)
  $('.arrows')
  	.css('opacity',0.5)	// start with all arrows at half mast
		.mouseover(function() {	$(this).fadeTo("fast",1.0) })
		.mouseout(function() { $(this).fadeTo("fast",0.5)
	});
	// These arrows are only for the Galleries page
	$('#leftArrow').click(function() {
		galleryIndex = (galleryIndex + 5) % 6;
		loadGallery(); return false;
	});
	$('#rightArrow').click(function() {
		galleryIndex = (galleryIndex + 1) % 6;
		loadGallery(); return false;
	});
	$('#upArrow').click(function() {
		thumbIndex = (thumbIndex - 16 + totalImages) % totalImages;	loadThumbs();
	});
	$('#downArrow').click(function() {
		loadThumbs();
	});
	// Managing a Gallery click
	$('.gallery').click(function() {
		pageIndex = 2;
		galleryIndex = $(this).attr('id').substr(7,1)-1;
		loadGallery();
	});
	// Managing the slideshow
	$('#show-start-stop').mouseover(function() { 
		$(this).fadeTo("fast",1.0);
	}).mouseout(function() {
		$(this).fadeTo("fast",0.5);
	}).click(function(event) {
		event.preventDefault();
		var x = $(this);
		if (x.text() == 'Resume') { startSlideshow(); x.text('Pause') }
		else { stopSlideshow(); x.text('Resume') }
	});
// Managing the gallery thumbnails
	$('.thumbs').mouseover(function() { 
		$(this).fadeTo("fast",1.0);
	}).mouseout(function() {
		$(this).fadeTo("fast",0.5);
	}).click(function() { 
		showSingleImage($(this).attr('src'));
	});
}
