function startSlideshow(data, extra) {
	for(var i=0; i<data.length; i++) {
		$(document.createElement("img"))
		    .attr({ src: extra + '/' + data[i], title: 'Image '+i, id: 'slide_'+i })
		    .addClass("slide")
		    .appendTo($('#main'))
			.css({"display":"none", "position":"absolute", "z-index":100-i})
			.load(function() {
				var w = $(window).width();
				var h = $(window).height() - 75;
				$(this).css({
					'position':'absolute',
					'max-width':w,
					'max-height':h,
				});
				// Now get width and set left
				var l = (w - $(this).width()) / 2;
				$(this).css('left', l);
				// Fade in first slide and start slideshow
				if (this.id == 'slide_0') {
					$(this).addClass('active');
					$(this).fadeIn();
					removeLoader();
					slideInt = setInterval( "iterateSlideshow()", 5000 );
				}
			})
	}
}
function iterateSlideshow() {
   var active = $('#main img');
   var currentImage = $('#main img.active');
   
   var nextImage = currentImage.next().length ? currentImage.next() : $('#main img:first');
   
   currentImage.removeClass('active');
   nextImage.addClass('active');
   
   // nextImage.css('left', (940-nextImage.width())/2);
   // nextImage.css('top', (600-nextImage.height())/2);
   
   currentImage.fadeOut();
   nextImage.fadeIn();
}
resizeMainWindow = function() {
	var main = $('#main');
	// Get window width and height
	var windowW = $(window).width();
	var windowH = $(window).height();
	// Header height
	var headerH = $('#header').height();
	var thumbH = $('#thumbs').height();
	// Set width and height
	main.width(windowW);
	main.height(windowH - headerH - thumbH); // Height is window height minus header height
	// Position top
	main.css('top',headerH);
	
	// Iterate through images and resize / reposition
	$('#main img').each(function() {
		$(this).css({
			'position' : 'absolute',
			'max-width' : windowW,
			'max-height' : (windowH - headerH - thumbH),
			'top' : 0
		});
		// Now get width and set left
		var l = (windowW - $(this).width()) / 2;
		$(this).css('left', l);
	});
}
showLoader = function() {
	var l = $(window).width() / 2 - 110;
	var t = $(window).height() / 2 - 10;
	var ldr = $('<div id="loading"></div>')
		.appendTo($('body'))
		.css({'left' : l, 'top' : t})
}
removeLoader  = function() {
	$('#loading').remove();
}
showGalleries = function(categoryID) {
	// Remove existing list
	$('.gallery_list').remove();
	// Append a UL to the LI of the category
	var list = $('<ul class="gallery_list"></ul>')
		.appendTo($('#category_' + categoryID))
	// Loop through galleries and append LIs to the UL
	$.each(siteData.galleries, function(count, item) {
		// Make sure it matches the category id
		if (item.category_id == categoryID && item.gallery_name.length > 0 && item.gallery_hidden == '0' && item.password.length == 0) {
			var galleryItem = $('<li class="gallery_item"><a href="#gallery">' + item.gallery_name + '</a></li>')
				.appendTo($(list))
			$(galleryItem).data('gallery',item)
		}
	})
}
startAnchored = function(categoryID) {
	$.each(siteData.galleries, function(count, item) {
		// Make sure it matches the category id
		if (item.category_id == categoryID && item.gallery_name.length > 0 && item.gallery_hidden == '0' && item.password.length == 0) {
			startGallery(item);
			return false;
		}
	})
}
startGallery = function(data) {
	// Hide menu
	$('#portfolio_menu').hide();
	// Clear out the main window and shorten it 
	$('#main img').remove();
	$('#main').height($(window).height() - 180); // Main is now the height of window minus header/thumbs
	// Stop slideshow 
	if (typeof(slideInt) !== 'undefined') {
		clearInterval(slideInt);
	}
	// Remove page if it's there
	$('#page').remove();
	// Set the gallery index
	galleryIndex = 0;
	// Loader 
	showLoader();
	// Make thumb bar (remove if existing)
	$('#thumbs').remove();
	var tBar = $('<div id="thumbs"><div id="thumbs_inner"></div></div>')
		.css({'background' : '#000', 'position' : 'absolute', 'width' : '100%', 'height' : '100px', 'bottom' : '0px', 'z-index' : '1200'})
		.appendTo($('body'))
	// Swipe on thumbs div
	$('#thumbs').swipe({
		swipeRight:function(){
			$('#thumbs_inner').animate({left:'+=500'},500,function() {
				if ($(this).offset().left > 0) {
					$('#thumbs_inner').animate({left:0},250);
				}
			});
		},
		swipeLeft:function() {
			$('#thumbs_inner').animate({left:'-=500'},500);
		}
	})
	// Load thumbs 
	$.each(data.images, function(count, item) {
		var img = $('<img class="thumb" id="thumb_' + count + '" />')
			.attr({src : url + '/gallery/thumb/' + item.image_file})
			.appendTo($('#thumbs_inner'))
			.css({'display' : 'none'})
			.data('image', {fileName : item.image_file, index : count})
			.load(function() {
				$(this).css('margin-top',function() {
					return (100-$(this).height()) / 2;
				});
				$(this).fadeIn();
			})
	});
	$('#thumb_0').addClass('active');
	// Load first image
	$(document.createElement("img"))
	    .attr({ src: url + '/gallery/large/' + data.images[0].image_file, title: 'Gallery Image' })
		.css({'max-width' : '100%', 'max-height' : '100%', 'display' : 'none', 'position' : 'absolute'})
		.addClass('galleryImage')
	    .appendTo($('#main'))
		.load(function() {
			removeLoader();
			var l = ($(window).width() - $(this).width()) / 2; // Now get width and set left
			$(this).css('left', l);
			$(this).fadeIn();
			// Initialize swipe
			$('#main').swipe({
				swipeRight: function() { },
				swipeLeft: function() { 
					removeLoader();
					var imgData = $('#thumb_1').data('image');
					$('.galleryImage').animate({left : '-=250', opacity : 0.0}, 200, function() {
						galleryImage(imgData, $('#thumb_1'));
					})
				},
			});
		})
}
galleryImage = function(data, item) {
	// Add active class (and remove from old thumb)
	$('#thumbs_inner .active').removeClass('active');
	$(item).addClass('active');
	// Set index
	galleryIndex = data.index;
	// Loader 
	showLoader();
	// Clear out the main window
	$('#main img').remove();
	$(document.createElement("img"))
	    .attr({ src: url + '/gallery/large/' + data.fileName, title: 'Gallery Image' })
		.css({'max-width' : '100%', 'max-height' : '100%', 'display' : 'none', 'position' : 'absolute'})
		.addClass('galleryImage')
	    .appendTo($('#main'))
		.load(function() {
			removeLoader();
			
			var l = ($(window).width() - $(this).width()) / 2; // Now get width and set left
			$(this).css('left', l);
			
			$(this).fadeIn();
			// Initialize swipe
			$('#main').swipe({
				swipeLeft: function() {
					removeLoader();
					var imgData = $('#thumb_'+(galleryIndex + 1)).data('image');
					$('.galleryImage').animate({left : '-=250', opacity : 0.0}, 200, function() {
						galleryImage(imgData, $('#thumb_'+(galleryIndex + 1)));
					})
				},
				swipeRight: function() {
					removeLoader();
					var imgData = $('#thumb_'+(galleryIndex - 1)).data('image');
					$('.galleryImage').animate({left : '+=250', opacity : 0.0}, 200, function() {
						galleryImage(imgData, $('#thumb_'+(galleryIndex - 1)));
					})
				}
			});
		})
}

function showPage(data) {
	// Empty out main div
	$('#main img').each(function(index) {
		$(this).remove();
	})
	// Remove thumbs
	$('#thumbs').remove();
	// If a div for page already exists, empty it
	$('#page').remove();
	// If not, create a div for the page
	pageContent = (data.alt_html == "1") ? data.alt_content : data.page_content_html
	$('<div id="page"></div>')
		.css({'position':'absolute','z-index':300,'width':'100%'})
		.appendTo($('body'))
		.append('<h2 class="title">' + data.page_title + '</h2>')
		.append('<div id="sub_pages"></div>')
		.append('<div id="page_body">' + nl2br(pageContent) + '</div>')
	
	if (data.parent_id == 0) {
		showSubPages(data.page_id);
	} else {
		showParentLink(data.parent_id)
	}
}
function showParentLink(pid) {
	var subNav = $('<ul></ul>');
	$.each(siteData.pages, function(count, item) {
		if(item.page_id == pid && item.page_title.length > 0 && item.page_title.toLowerCase() != 'facebook' && item.include_html != '0') {
			if (item.alt_html == "1") {
				var newItem = $('<li><a href="' + count + '">Return to ' + item.alt_title + '</a></li>');    
			} else {
				var newItem = $('<li><a href="' + count + '">Return to ' + item.page_title + '</a></li>');    
			}	
			subNav.append(newItem);
			newItem.data('page', item);
			return false;
		}			
	});
	$('#sub_pages').append(subNav);
}
function showSubPages(pid) {
	
	var subNav = $('<ul></ul>');
	var pageCount = 0;
	$.each(siteData.pages, function(count, item) {
		if(item.parent_id == pid && item.page_title.length > 0 && item.page_title.toLowerCase() != 'facebook' && item.include_html != '0') {
			if (item.alt_html == "1") {
				var newItem = $('<li><a href="' + count + '">' + item.alt_title + '</a></li>');    
			} else {
				var newItem = $('<li><a href="' + count + '">' + item.page_title + '</a></li>');    
			}	
			pageCount++;				
			subNav.append(newItem);
			newItem.data('page', item);
		}			
	});
	if (pageCount > 0) {
		$('#sub_pages').append(subNav);
	}
}
function nl2br (str, is_xhtml) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'

    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}