var showSplash = true;
function loadPage() {
	if (showSplash) {
		$("#splash_bokeh").delay(500).fadeIn(2000, function() {
			if (showSplash) {
				$("#splash_vivaluma").fadeIn(2000, function() {
					if (showSplash) {
						$("#splash_wherelightlives").fadeIn(2000, function() {
							if (showSplash) {
								$("#splash_container").delay(500).fadeOut(2000, function() {
									showView();
								});
							} else {
								showView();
							}
						});
					} else {
						showView();
					}
				});
			} else {
				showView();
			}
		});
	} else {
		showView();
	}
	
}


function showView() {
	if ($("#splash_container").is(":visible")) {
		$("#splash_container").hide();
	};
	
	addMenuRollovers();
	addMenuClicks();
	//figure out maximum height and width;
	var width = 900;
	var height = 600;
	//center it on the page
	
	var viewContainerWidth = width+10+17;
	var viewContainerHeight = height + 10 + 38;
	
	var centerHeight = Math.min($(window).height(), $("#splash_background").height());
	var viewContainerTop = (centerHeight - viewContainerHeight)/2;
	if (viewContainerTop < 15) {
		viewContainerTop = 15;
	}
	var viewContainerLeft = ($(window).width() - viewContainerWidth)/2;
	if (viewContainerLeft < 0) {
		viewContainerLeft = 0;
	}
	$("#view_container").css({
		"left":  viewContainerLeft + 'px',
		"top":   viewContainerTop+ 'px'
	});
	$("#view_container").fadeIn(1000, function() {
		showCategory("home");
	});
}

function addMenuRollovers() {
	$('.menuButton').mouseenter(function() {
		$(this).addClass("menuButtonHighlighted");
		$("#button_highlight").css("left", $(this).position().left+($(this).width()-$("#button_highlight").width())/2+"px");
		$("#button_highlight").show();
	});
	$('.menuButton').mouseleave(function() {
		$(this).removeClass("menuButtonHighlighted");
		$("#button_highlight").hide();
	});
	
	$('.submenuButton').mouseenter(function() {
		$(this).addClass("menuButtonHighlighted");
		$(".submenuButtonHighlight").css("top", $(this).position().top+2+"px");
		$(".submenuButtonHighlight").show();
	});
	$('.submenuButton').mouseleave(function() {
		$(this).removeClass("menuButtonHighlighted");
		$(".submenuButtonHighlight").hide();
	});
}

function addMenuClicks() {
	$("#button_home").click(function() {
		showCategory("home");
	});
	$("#button_portfolio").click(function() {
		showPopupMenu("#submenu_portfolio");
	});
	$("#button_wedding").click(function() {
		showCategory("wedding");
	});
	$("#button_engagement").click(function() {
		showCategory("engagement");
	});
	$("#button_senior").click(function() {
		showCategory("senior");
	});
	$("#button_family").click(function() {
		showCategory("family");
	});
	$("#button_newborn").click(function() {
		showCategory("newborn");
	});
	$("#button_about").click(function() {
		showPopupMenu("#submenu_about");
	});
	$("#button_whoweare").click(function() {
		showPage("whoWeAre.html");
	});
	$("#button_sessions").click(function() {
		showPage("sessions.html");
	});
	$("#button_products").click(function() {
		showPage("products.html");
	});
	$("#button_contact").click(function() {
		showPage("contact.html");
	});
	$("#button_clients").click(function() {
		document.location.href = "http://www.vivaluma.com/c/";
	});
	$("#button_facebook").click(function() {
		document.location.href = "http://www.facebook.com/vivaluma/";
	});
}

function showPopupMenu(menuName) {
	if ($(menuName).is(":hidden")) {
		hidePopupMenu();
		$(menuName).show('slide', {direction: "down"}, 500);
	} else {
		hidePopupMenu();
	}
}


function hidePopupMenu() {
	if ($(".submenu").is(":visible")) {
		$(".submenu:visible").hide('slide', {direction: "down"}, 500);
	}
}

function shuffle(array) {
    var tmp, current, top = array.length;

    if(top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }

    return array;
}

function showPage(pageName) {
	hidePopupMenu();
	$("#image_navigation").html("");
	$("#image_navigation").hide();
	$("#view_container_center").load("pages/"+pageName);
}

var currentInterval;
function showCategory(categoryName) {
	$("#view_container_center").html("");
	$("#image_navigation").html("");
	$("#image_navigation").hide();
	stopInterval();
	$.getJSON("scripts/galleries/"+categoryName+".js?"+new Date().getTime(), function (data) {
		var panels = data.panels;
		if (data.shuffle) {
			panels = shuffle(panels);
		}
		$.each(panels, function(i,panel) {
			//set up panel container
			var imagePanel = $("<div class='imagePanel loading' id='imagePanel_"+i+"'/>").appendTo("#view_container_center");
				
			//add navigation
			var navImage = $("<img id='imagePanel_"+i+"_nav' class='imagePanelNav' src='images/opencircle.png'/>");
			$(navImage).click(function() {
				stopInterval();
				showPanel("imagePanel_"+i);
			});
			$(navImage).appendTo("#image_navigation");

			//load images
			$.each(panel.images, function(j, image) {
	            $("<img class='"+image.imageClass+"'/>").attr("src", "images/galleries/"+image.imageSource).load(function() {
					$(imagePanel).append($(this));
					var panelImages = $(imagePanel).children("img").length;
					//if all images for the panel are loaded, show the nav, if it is the first panel, show it
					if (panel.images.length == panelImages) {
						$(imagePanel).removeClass("loading");
						$(navImage).show();
						if (i == 0) {
							showPanel2("imagePanel_"+i);
						}
						if ($(".imagePanel:not(.loading)").length > 1) {
							$("#image_navigation").show();
						}
						
					}
				});
			});
        });
		if (data.autoplay) {
			currentInterval = window.setInterval(nextPanel, 4000);
		}
	});
	hidePopupMenu();
}

function stopInterval() {
	if (currentInterval) {
		window.clearInterval(currentInterval);
	}
	currentInterval = "";
}

function nextPanel() {
	var newPanel = $(".currentPanel").next(":not(.loading)");
	
	if (newPanel.length == 0) {
		newPanel = $("#view_container_center .imagePanel:not(.loading):first");
	}
	showPanel($(newPanel).attr("id"));
}

function previousPanel() {
	var newPanel = $(".currentPanel").prev(":not(.loading)");
	
	if (newPanel.length == 0) {
		newPanel = $("#view_container_center .imagePanel:not(.loading):last");
	}
	showPanel($(newPanel).attr("id"));
}


function showPanel(panelId) {
	var currentPanel = $(".currentPanel");
	if (currentPanel.attr("id") != panelId) {
		if (currentPanel.length > 0) {
			$(currentPanel).fadeOut(500, function() {
				var currentId = $(currentPanel).attr("id");
				$(currentPanel).removeClass("currentPanel");
			
				showPanel2(panelId);
			});
		} else {
			showPanel2(panelId);
		}
	}
}

function showPanel2(panelId) {
	var imagePanel = $("#"+panelId);
	$(imagePanel).addClass("currentPanel");
	$(imagePanel).fadeIn(500);
	selectNav(panelId);
}

function selectNav(panelId) {
	$(".currentNav").attr("src", "images/opencircle.png").removeClass("currentNav");
	$("#"+panelId+"_nav").attr("src", "images/closedcircle.png").addClass("currentNav");
	
}

