var productInAnimation = false;
var productVisible = false;

$(document).ready(function() {
	var slideSpeed = 1250;
	var page = "home";
	var aVisited = [];
	var bVisited = false;
	var preload = new Image();
	preload.src = "./images/backgrounds/home.jpg";
	preload.src = "./images/products/black-label.jpg";
	preload.src = "./images/products/couture-clothing.jpg";
	preload.src = "./images/products/couture-shirts.jpg";
	preload.src = "./images/products/perfetto.jpg";
	preload.src = "./images/products/sonny-yuen.jpg";
	
	$("#copyright").hide();
	$("#loading").hide();
	$("dd").hide();
	$("dl:first").hide();
	$("#maincontent").ajaxSuccess(function() {
		if(!bVisited) {
			$("#loading").fadeOut(750, function() {
				$("#maincontent").fadeIn(750);
			});
		} else {
			$("#maincontent").fadeIn(750);
		}
		bVisited = false;
		$("#loading").hide();
	});
	
	$("dl:first").fadeIn(2500);
	$("#copyright").fadeIn(2500);
	
	$("dt a, li a").click(function(){
		if($(this).parent().next().is("dd") && !$(this).parent().next().is(":visible")) {
			$("#navigation dd:visible").slideUp(slideSpeed);
			$(this).parent().next().slideDown(slideSpeed);
		}
		if($(this).parent().parent().parent().is("#mens")) {
			if($(this).attr("href") == page) {
				hideProduct();
				$.changePage(page, aVisited, bVisited);
			} else {
				if($(this).attr("href") == productVisible && productVisible != false) {
					hideProduct();
					page = $(this).attr("href");
					$.changePage(page, aVisited, bVisited);
				} else {
					$.changePage("blank", aVisited, bVisited);
					showProduct($(this).attr("href"));
				}
			}
		} else {
			if($(this).attr("href") != page && $(this).attr("href") != "mens") {
				page = $(this).attr("href");
				$.changePage(page, aVisited, bVisited);
			}
			if($(this).attr("href") == "home") {
				page = "home";
				hideProduct();
				$.changePage(page, aVisited, bVisited);
			}
		}
		return false;
	});
	$("#copyright a").click(function() {
		if($(this).attr("href") != page) {
			page = $(this).attr("href");
			$.changePage(page, aVisited, bVisited);
		}
		return false;
	});
	$(".internal-link").live("click", function() {
		if(productVisible != false)
			hideProduct();
		if($(this).attr("href") != page) {
			page = $(this).attr("href");
			$.changePage(page, aVisited, bVisited);
		}
		if(($(this).attr("href") == "black-label" || $(this).attr("href") == "couture-shirts" || $(this).attr("href") == "perfetto") && !$("#mens").is(":visible")) {
			$("#navigation dd:visible").slideUp(slideSpeed);
			$("dd#mens").slideDown(slideSpeed);
		}
		return false;
	});
	$(".home-link, .couture-link").live("click", function() {
		if($(this).attr("href") == "couture-menu") {
			$.changePage("couture-menu", aVisited, bVisited);
		} else {
			$.changePage("blank", aVisited, bVisited);
			showProduct($(this).attr("href"));
			if(!$("#mens").is(":visible")) {
				$("#navigation dd:visible").slideUp(slideSpeed);
				$("dd#mens").slideDown(slideSpeed);
			}
		}
		return false;
	});
	
	$.changePage(page, aVisited, bVisited);
});

$.changePage = function(szPage, aVisited, bVisited) {
	for(c = 0; c < aVisited.length; c++) {
		if(szPage == aVisited[c]) {
			bVisited = true;
			break;
		}
	}
	$("#maincontent").hide();
	$("#maincontentholder").hide();
	$("#maincontentholder").css("background-image", "url(./images/backgrounds/"+szPage+".jpg)");
	setTimeout("$('#maincontentholder').fadeIn(750)", 500);
	if(szPage != "index") {
		if(!bVisited) {
			$("#loading").show();
			aVisited.push(szPage);
		}
		if(szPage != "blank")
			document.title = $.capitalise(szPage.replace("-", " ")) + " - Andy Yuen";
		$("#maincontent").load("./pages/"+szPage+".html");
	} else {
		document.title = "Welcome - Andy Yuen";
		$("#maincontent").load("./pages/"+szPage+".html");
	}
}

$.capitalise = function() {
	temp = "";
	str = arguments[0].split(" ");
	for(c = 0; c < str.length; c++) {
		temp += str[c].substring(0, 1).toUpperCase() + str[c].substring(1, str[c].length) + " ";
	}
	return temp;
}

function showProduct(product) {
	if(productInAnimation) {
		setTimeout(function() { showProduct(product); }, 500);
	} else if(productVisible != false) {
		hideProduct();
		setTimeout(function() { showProduct(product); }, 500);
	} else {
		showProduct2(product);
	}
}

function showProduct2(product) {
	productInAnimation = true;
	productVisible = product;
	$("#product img").attr("src", "./images/products/" + product + ".jpg");
	$("#product a").attr("href", product);
	$("#product").fadeIn(500, function() {
		productInAnimation = false;
	});
}

function hideProduct() {
	if(productInAnimation) {
		setTimeout(function() { hideProduct(); }, 500);
	} else {
		hideProduct2();
	}
}

function hideProduct2() {
	productInAnimation = true;
	$("#product").fadeOut(500, function() {
		productInAnimation = false;
		productVisible = false;
	});
}
