/**
 * JavaScript for JamesHanton.com - requires JQuery and JQuery Cycle plugin
 */
var generalSlides = new Array();
generalSlides[0] = { "styleDef": "slide", "src": "images/gallery/bbpics_01.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[1] = { "styleDef": "slide", "src": "images/gallery/bbpics_02.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[2] = { "styleDef": "slide", "src": "images/gallery/bbpics_03.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[3] = { "styleDef": "slide", "src": "images/gallery/bbpics_04.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[4] = { "styleDef": "slide", "src": "images/gallery/bbpics_05.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[5] = { "styleDef": "slide", "src": "images/gallery/bbpics_06.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "400", "iheight": "600" };
generalSlides[6] = { "styleDef": "slide", "src": "images/gallery/bbpics_07.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[7] = { "styleDef": "slide", "src": "images/gallery/bbpics_08.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[8] = { "styleDef": "slide", "src": "images/gallery/bbpics_09.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "400", "iheight": "600" };
generalSlides[9] = { "styleDef": "slide", "src": "images/gallery/bbpics_10.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[10] = { "styleDef": "slide", "src": "images/gallery/bbpics_11.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "600", "iheight": "400" };
generalSlides[11] = { "styleDef": "slide", "src": "images/gallery/mg_01.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[12] = { "styleDef": "slide", "src": "images/gallery/mg_02.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[13] = { "styleDef": "slide", "src": "images/gallery/mg_03.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[14] = { "styleDef": "slide", "src": "images/gallery/mg_04.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[15] = { "styleDef": "slide", "src": "images/gallery/mg_05.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[16] = { "styleDef": "slide", "src": "images/gallery/mg_06.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[17] = { "styleDef": "slide", "src": "images/gallery/mg_07.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "500", "iheight": "332" };
generalSlides[18] = { "styleDef": "slide", "src": "images/gallery/mg_08.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[19] = { "styleDef": "slide", "src": "images/gallery/mg_09.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[20] = { "styleDef": "slide", "src": "images/gallery/mg_10.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "332", "iheight": "500" };
generalSlides[21] = { "styleDef": "slide", "src": "images/gallery/mu_01.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "400", "iheight": "600" };
generalSlides[22] = { "styleDef": "slide", "src": "images/gallery/mu_02.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "400", "iheight": "600" };
generalSlides[23] = { "styleDef": "slide", "src": "images/gallery/mu_03.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "400", "iheight": "600" };
generalSlides[24] = { "styleDef": "slide", "src": "images/gallery/mu_04.jpg", "alt": "JamesHanton.com Gallery Photo", "iwidth": "400", "iheight": "600" };

var loadCompleted;
var intID;
function showGallery(showID, arrayName) {
	var slideshowContent;
	var slides = eval(arrayName);

	loadCompleted = false;
	
	$('#show-background').show();
	$('#show-placeholder').show()

	slideshowContent = '<div id="' + showID + '" class="slideshow">';
	for (slide in slides) {
		slideshowContent += '<img class="' + slides[slide].styleDef + '" src="' + slides[slide].src + '" alt="' + slides[slide].alt + '" iwidth="' + slides[slide].iwidth + '" iheight="' + slides[slide].iheight + '" />\n';
	}
	slideshowContent += '</div>';
	$('#show-container').append(slideshowContent);
	$('#show-nav-header').html('<span onclick="closeShow(\'' + showID + '\');">CLOSE&nbsp;X</span>');
	
	if (loadCompleted) {
		$('#show-container').show();
		$('#show-placeholder').fadeOut("slow");
		
		startShow(showID);
	} else {
		intID = setInterval('checkLoadStatus(\'' + showID + '\')', 500);		
	}
}

function checkLoadStatus(showID) {
	loadComplete = true;
	$('.slide').each( function() {
		if (!($(this)[0].complete)) {
        	loadComplete = false;
   		}
	});
	
	if (loadComplete) {
		clearInterval(intID);

		$('#show-container').show();
		$('#show-placeholder').fadeOut("slow");
		
		startShow(showID);
	}
}

function startShow(showID) {
	$('#' + showID).cycle({
		fx: 'fade',
		timeout: 5000,
		before: function () {
			var showW, showH, marginStr;
			showW = $('.slideshow').css("width");
			showH = $('.slideshow').css("height");
			if (browser == "Microsoft Internet Explorer") {
				marginStr = Math.round((parseInt(showH) - this.iheight) / 2) + 'px ' + Math.round((parseInt(showW) - this.iwidth) / 2) + 'px ' + Math.round((parseInt(showH) - this.iheight) / 2) + 'px ' + Math.round((parseInt(showW) - this.iwidth) / 2) + 'px';
			} else {
				marginStr = Math.round((parseInt(showH) - this.height) / 2) + 'px ' + Math.round((parseInt(showW) - this.width) / 2) + 'px ' + Math.round((parseInt(showH) - this.height) / 2) + 'px ' + Math.round((parseInt(showW) - this.width) / 2) + 'px';
			}
			$(this).css("margin", marginStr);
			$(this).css("visibility", "visible");
		},
		pager: '#thumb-wrapper',
		// callback fn that creates a thumbnail to use as pager anchor
		pagerAnchorBuilder: function(idx, slide) {
			var slideSrc, slideW, slideH, slideDx, anchorDim;
			slideSrc = $(slide).attr("src");
			if (browser == "Microsoft Internet Explorer") {
				slideW = $(slide).attr("iwidth");
				slideH = $(slide).attr("iheight");
			} else {
				slideW = $(slide).attr("width");
				slideH = $(slide).attr("height");
			}
			
			if (slideW > slideH) {
				slideDx = slideW / slideH;
				slideH = 50;
				slideW = Math.round(slideH * slideDx);
				anchorDim = slideW;
			} else {
				slideDx = slideH / slideW;
				slideW = 50;
				slideH = Math.round(slideW * slideDx);
				anchorDim = slideH;
			}
			
			return '<a href="#" style="width: ' + anchorDim + 'px; height:' + anchorDim + 'px;"><img src="' + slideSrc + '" width="' + slideW + '" height="' + slideH + '" style="margin: ' + Math.round((anchorDim - slideH) / 2) + 'px ' + Math.round((anchorDim - slideW) / 2) + 'px ' + Math.round((anchorDim - slideH) / 2) + 'px ' + Math.round((anchorDim - slideW) / 2) + 'px;" /></a>';
		},
		pagerEvent: 'mouseover'
	});
}

function closeShow(showID) {
	$('#show-background').hide();
	$('#show-container').hide();
	$('#' + showID).remove();
	$('#show-nav-header').html("");
	$('#thumb-wrapper').html("");
}