/***********/
/*  Legal  */
/***********/

function loadOnlineOrderingPolicy (agree) {
	var content = "<div id=\"online_ordering_policy\"><h1>Return / Refund / Cancellation Policy</h1><div><p>Your online order will arrive at your selected location in about an hour. If changes to this order are necessary, please contact the store immediately. Any orders not picked up by the end of the selected pick-up day will be assumed to be abandoned and will be donated with no refund given. Cancellation of an order submitted online will incur a handling fee. Defective product, errors in orders, or any other mistakes on The Cup's part will be provided a reissue or reimbursement. If for some reason, you are not satisfied with our product, please contact the manager at your local Cup. We do not offer shipping at this time. Sign-up for our mailing list to be alerted when shipping becomes available.</p></div></div>";

	var closeButton = false;
	if (agree)
		content += "<div class=\"center\" id=\"order_control\"><img src=\"/images/back_button.png\" class=\"clickable\" onclick=\"closeSubwindow('online_ordering_policy')\" />&nbsp;<img class=\"clickable\" src=\"/images/agree_button.jpg\" ondblclick=\"void(0)\" onclick=\"verifyCheckout(true)\" /></div>";
	else closeButton = true;

	createSubwindow("online_ordering_policy", content, closeButton);
}

/***************/
/*  Subwindow  */
/***************/

function createSubwindow (windowID, content, closeButton) {
	if ($("#" + windowID).size() > 0)
		$("#" + windowID + "_container").html(content);
	else {
		$("body").append("<div class=\"window\" id=\"" + windowID + "\"><div class=\"window_border\"><div class=\"right\"><img src=\"/images/close.gif\" class=\"clickable\" alt=\"close\" onclick=\"closeSubwindow('" + windowID + "')\" /></div><div id=\"" + windowID + "_container\">" + content + "</div></div></div>");
	}
	if (typeof(closeButton) != "undefined" && closeButton) {
		$("#" + windowID + "_container").append("<div class=\"center\"><img src=\"/images/close_button.png\" class=\"clickable\" onclick=\"closeSubwindow('" + windowID + "')\"</div>");
	}
	var left = Math.floor($(window).width()/2)-130;
	var top = Math.floor($(window).height()/2)-150;
	$("#" + windowID).css("left", left + "px").css("top", top + "px");
}

function closeSubwindow (windowID) {
	$("#" + windowID).remove();
}

/**********/
/*  Menu  */
/**********/

function showMenus () {
//	$("#location_menus").slideDown("slow");
	$.post("/ajax/get_menus.php", { }, function (response) {
		$("body").append("<div class=\"screen\" onClick=\"hideMenus()\" ></div><div class=\"location_menus\"><div class=\"location_menus_border\"><img src=\"/images/close.png\" class=\"clickable window_control\" onClick=\"hideMenus()\" />" + response + "</div></div>");
	});
}

function hideMenus () {
//	$("#location_menus").slideUp("slow");
	$(".screen, .location_menus").fadeOut("slow", function () {
		$(".screen, .location_menus").remove();
	});
}

/******************/
/*  Subscription  */
/******************/

function validateSubscriptionForm () {
	var firstName = $("#first_name");
	var lastName = $("#last_name");
	var email = $("#email");
	var location = $("#location");
	var errors = "";

	if (trim(firstName.val()) == "") {
		firstName.addClass("error_input");
		errors += "<li>Please supply your first name</li>";
	}
	else firstName.removeClass("error_input");

	if (trim(lastName.val()) == "") {
		lastName.addClass("error_input");
		errors += "<li>Please supply your last name</li>";
	}
	else lastName.removeClass("error_input");

	if (!isValidEmail(email.val())) {
		email.addClass("error_input");
		errors += "<li>Please supply a valid email</li>";
	}
	else email.removeClass("error_input");

	if (location.val() == "") {
		location.addClass("error_input");
		errors += "<li>Please select a location</li>";
	}
	else location.removeClass("error_input");

	if (errors.length > 0) {
		$("#error").html("Oops! It looks like you may have forgotten to give us some info!<ul>" + errors + "</ul>").slideDown("slow");
		return false;
	}

	$("#subscribe_form").attr("action", "subscription_script.php");
	return true;
}

var unloadTimeout = null;

function subscribe (cupcakeID) {
	if (typeof(cupcakeID) == "undefined")
		cupcakeID = "";

	var email = $("#subscription_email" + cupcakeID).val();

	if (!isValidEmail(email)) {
		$("#subscription_message" + cupcakeID).addClass("error").html("Please enter a valid email.").slideDown("slow");
		return;
	}

	$.post("/ajax/add_subscriber.php", { email: email, cupcake_ID: cupcakeID }, function () {
		$("#subscription_email" + cupcakeID).val("");
		// $("#subscription_message" + cupcakeID).removeClass("error").addClass("success").html("You have subscribed successfully!").slideDown("slow");
		$("#subscription_" + cupcakeID).addClass("success").html("<p>You have subscribed successfully!</p><p>Would you like to join our birthday mailing list?</p><a href=\"/subscribe.php\"><img src=\"/images/yes.png\"></a> <a href=\"javascript: unloadSubscriptionForm()\"><img src=\"/images/no_thanks.png\"></a>");
		unloadTimeout = setTimeout(function () { unloadSubscriptionForm(); }, 10000);
	});
}

function loadSubscriptionForm (cupcakeID) {
	$.post("/ajax/get_subscription_form.php", { cupcake_ID: cupcakeID }, function (response) {
		$("body").append("<div class=\"screen\" onClick=\"unloadSubscriptionForm()\" ></div><div class=\"subscribe_form\" id=\"cupcake_subscribe\"><div class=\"subscribe_border\"><img src=\"/images/close.png\" class=\"clickable window_control\" onClick=\"unloadSubscriptionForm()\" />" + response + "</div></div>");
	});
}

function unloadSubscriptionForm () {
	clearTimeout(unloadTimeout);
	$(".screen, .subscribe_form").fadeOut("slow", function () {
		$(".screen, .subscribe_form").remove();
	});
}

/*******************/
/*  Splash Images  */
/*******************/

var splashTimeout = null;

function cueSplashScroll (currentImage, numImages) {
	splashTimeout = setTimeout(function () { rotateSplash(currentImage, numImages); }, 6000);
	$(".splash_control").removeClass("splash_control_current");
	$("#splash_control_" + currentImage).addClass("splash_control_current");
}

function rotateSplash (currentImage, numImages) {
/*
	$("#splash_carrier").children(":first").fadeOut("slow", function () {

	});
*/
	var nextImage = Number(currentImage)+1;
	if (nextImage > numImages)
		nextImage = 1;

	$("#splash_carrier").children(":first").animate({ width: "0px" }, 2000, "", function () {
		removeAndAppend("splash_carrier", "", 1.0, 950);
		cueSplashScroll(nextImage, numImages);
	});
}

function loadSplash (splashNum) {
	clearTimeout(splashTimeout);
	$(".splash_image").css("display", "none");
	$("#splash_" + splashNum).css("display", "block");
	$(".splash_control").removeClass("splash_control_current");
	$("#splash_control_" + splashNum).addClass("splash_control_current");
}

/*************/
/*  Alerts   */
/*************/

var alertTimeout = null;

function loadAlerts () {
	initializeCity();
	//var geo = google.gears.factory.create('beta.geolocation');
	//geo.getCurrentPosition(updatePosition, handleError);
}

function initializeCity () {
	var initialLocation = null;
	var siberia = new google.maps.LatLng(60, 105);
	var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
	
	// Try W3C Geolocation (Preferred)
	if(navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition( function(position) {
			initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
		});
	}
	
	return initialLocation;
}


function cueAlertScroll () {
	alertTimeout = setTimeout(function () { scrollAlerts(); }, 5000);
}

function scrollAlerts () {
	nextAlert();
	cueAlertScroll();
}

function previousAlert () {
	clearTimeout(alertTimeout);
	$(".alert:first").fadeOut("slow", function () {
		removeAndPrepend("alerts");
		$(".alert:first").fadeIn("slow");
	});
}

function nextAlert () {
	clearTimeout(alertTimeout);
	$(".alert:first").fadeOut("slow", function () {
		removeAndAppend("alerts");
		$(".alert:first").fadeIn("slow");
	});
}

function closeAlerts () {
	$("#alert_frame").fadeOut("slow");
}


/******************/
/*  Social Media  */
/******************/
function toggleSocialMedia () {
	if ($("#social_media_links").css("display") == "none") {
		showSocialMedia();
		return true;
	}
	else {
		hideSocialMedia();
		return false;
	}
}

function showSocialMedia () {
	$("#social_media_links").slideDown("slow");
	$(".social_media_icon, #social_media_links").css("border-width", "1px");
	$(".social_media_icon").css("border-top-width", "0px");
}

function hideSocialMedia () {
	//$("#facebook_icon").css("border-top", "");
	//$("#twitter_icon").css("border-top", "");
	$("#social_media_links").slideUp("slow");
	$(".social_media_icon, #social_media_links").css("border-width", "0px");
}

function viewTwitterLinks () {
	bringSMToTop("twitter");
}

function viewFacebookLinks () {
	bringSMToTop("facebook");
}

function bringSMToTop (id) {
	var closed = false;
	if ($("#" + id + "_links").css("display") == $("#social_media_links").css("display") || $("#social_media_links").css("display") == "none") {
		closed = !toggleSocialMedia();
	}
	$(".link_container").css("display", "none");
	$("#" + id + "_links").css("display", "block");
	if (!closed)
		$(".social_media_icon").css("border-top", "solid #90D9E2 1px");
	$("#" + id + "_icon").css("border-top", "");
}

/**********************/
/*  Cupcake Calendar  */
/**********************/
function toggleCupcakeCalendar () {
	var contentContainerID = "cupcake_calendar_content";
	if ($("#" + contentContainerID).css("display") == "none")
		showFooterSubmenu(contentContainerID, "cupcake_calendar_border");
	else hideFooterSubmenu(contentContainerID, "cupcake_calendar_border");
}

function loadCupcakeCalendarDateChooser (month, year) {
	$.post("ajax/load_cupcake_calendar.php", { month: month, year: year }, function (response) {
		createSubwindow("date_chooser", response);
	});
}

function loadAvailableCupcakes (day, month, year) {
	if (!day) {
		var date = $("#cupcake_calendar_date").val();
		var dateParts = date.split("/");
		day = dateParts[1];
		month = dateParts[0];
		year = dateParts[2];
	}

	closeSubwindow("date_chooser");
	$.post("ajax/load_available_cupcakes.php", { day: day, month: month, year: year }, function (response) {
		$("#cupcake_calendar_chooser").hide();
		$("#cupcake_calendar_available").html(response);
	});
}

/*********************/
/*  Todays Cupcakes  */
/*********************/
function toggleTodaysCupcakes () {
	if ($("#cupcake_links").css("display") == "none") {
		showTodaysCupcakes();
	}
	else hideTodaysCupcakes();
}

function showTodaysCupcakes () {
	showFooterSubmenu("cupcake_links", "todays_cupcakes_border");
}

function showFooterSubmenu (contentContainerID, borderID) {
	$("#" + contentContainerID).slideDown("slow");
	$("#" + borderID).css("border-color", "#90D9E2");
	// $("#" + borderID).animate({ borderTopColor: "#90D9E2",  borderLeftColor: "#90D9E2", borderRightColor: "#90D9E2", borderBottomColor: "#90D9E2" }, "slow"); 
}

function hideTodaysCupcakes () {
	hideFooterSubmenu("cupcake_links", "todays_cupcakes_border");
}

function hideFooterSubmenu (contentContainerID, borderID) {
	// $("#todays_cupcakes_border").animate({ borderTopColor: "#582918", borderRightColor: "#582918", borderBottomColor: "#582918", borderLeftColor: "#582918" }, "slow");
	$("#" + contentContainerID).slideUp("slow", function () {
		$("#" + borderID).css("border-color", "#582918");
	});
}

/*******************/
/*  Location Info  */
/*******************/

function toggleLocations () {
	if ($("#locations").css("display") == "none") {
		showLocations();
	}
	else hideLocations();
}
	

function hideLocations () {
	$("#locations").slideUp("slow");
}

function showLocations () {
	$("#locations").slideDown("slow");
}

/********************/
/*  Product Images  */
/********************/
function loadProductImage (productImageID, imageNum) {
	$("#" + productImageID + " img").addClass("hidden");
	$("#" + productImageID + "_" + imageNum).removeClass("hidden");
	$("#" + productImageID + " .product_image_control_button").removeClass("product_image_control_button_current");
	$("#" + productImageID + "_" + imageNum + "_control").addClass("product_image_control_button_current");
}

/************************/
/*  Baking-a-Difference  */
/************************/

var organizationInfo = new Array();

function showOrganizationInfo (event, organizationID) {
	cancelHideOrganizationInfo(organizationID);

	positionOrganizationInfo(event, organizationID);
	$("#organization_" + organizationID + "_info").fadeIn("fast");
	$("body").bind("mousemove", function (event) { positionOrganizationInfo(event, organizationID); });
}

function positionOrganizationInfo (event, organizationID) {
	var top = event.clientY + 10;
	var left = event.clientX + 10;
// console.log("(" + left + ", " + top + ")");
	$("#organization_" + organizationID + "_info").css("top", top).css("left", left);
}

function cueHideOrganizationInfo (organizationID) {
	organizationInfo[organizationID] = setTimeout(function () { hideOrganizationInfo(organizationID); }, 250);
}

function cancelHideOrganizationInfo (organizationID) {
	clearTimeout(organizationInfo[organizationID]);
}

function hideOrganizationInfo (organizationID) {
	$("#organization_" + organizationID + "_info").fadeOut("fast");
	$("body").unbind("mousemove");
}

