/*
** Universe.js
** Copyright Ronningen Design, LLC (http://www.websitesthatdostuff.com)
** Subcontracted to Substratal Code LLC (http://www.substratalcode.com)
** Tampered with by Way Short of Heaven (http://www.wayshortofheaven.com)
**
*/

$(document).ready(function() {
	// Add the class to the first of the dynamically generated authors:
	$("#subAuthor li:first").addClass("first");
	$("#subDate li:first").addClass("first");
	$("#subCategory li:first").addClass("first");
	$("#subTag li:first").addClass("first");

	// Add the click listeners:
	$('ul.universeNav li a[id!=all]').click(function(e){
		showSection(e.target.id);
		return false;
	});

	// Show the current subnav if needed:
	showSubnavByUrl();

	$("#modal").dialog({
		autoOpen: false,
		modal: true,
		height: $(window).height() - 30,
		width: $(window).width() - 30,
		position: [15, 15],
		dialogClass: 'expandingModal',
		close: function(event, ui) { closeDialog() }
   });

    $("#constrainedModal").dialog({
		autoOpen: false,
		modal: true,
		height: 575,
		width: 1006,
		position: [0, 0]
   });

	$("#personModal").dialog({
		autoOpen: false,
		modal: true,
		height: $(window).height() - 30,
		width: $(window).width() - 30,
		position: [15, 15],
		dialogClass: 'peopleModal expandingModal'
	});

   // Launch correct modal based on the item clicked
	$("#content .postComment .modal").click(function(){
		// Get the ID of the enclosing div:
		var id = $(this).closest("div").attr("id");
		var type = $(this).closest("div").find(".pills a.slideshow, .pills a.video, .pills a.photo, .pills a.news").attr("title");

		if (type == "Photo" ) {
			type = "News";
		}

		return showDialog($(this).attr('href'), id, type);
	});

	$("#content .universePostItem .modal").click(function(){
		// Get the ID of the enclosing div:
		var id = $(this).closest("div").attr("id");
		var type = $(this).closest("div").find(".pills a.slideshow, .pills a.video, .pills a.photo, .pills a.news").attr("title");

		if (type == "Photo" ) {
			type = "News";
		}

		return showDialog($(this).attr('href'), id, type);
	});

	$("#content .mediaComment .modal").click(function(){
		// Get the ID of the enclosing div:
		var id = $(this).closest("div").attr("id");
		var type = $(this).closest("div").find(".pills a.slideshow, .pills a.video, .pills a.photo, .pills a.news").attr("title");

		if (type == "Photo" ) {
			type = "News";
		}

		return showDialog($(this).attr('href'), id, type);
	});

	$("#content .universeProfile .modal").click(function(){
		// Get the ID of the clicked link:
		var id = $(this).closest("div").attr("id");
		var link = $(this).attr("href");
		return showPersonDialog(link, id);
	});

	$("#content .devotion .modal").click(function(){
		// Get the ID of the clicked link:
		var id = $(this).closest("div").attr("id");
		var link = $(this).attr("href");
		var title = "Devotion";
		$(".ui-dialog[aria-labelledby='ui-dialog-title-constrainedModal']").addClass("constrainedModal");
		return showConstrainedDialog(link, id, title);
	});

	/*
	$("#content .universeProfile .modal").click(function(){
		// Get the ID of the clicked link:
		var id = $(this).closest("div").attr("id");
		var link = $(this).attr("href");
		var title = "People Profile";
		$(".ui-dialog[aria-labelledby='ui-dialog-title-constrainedModal']").addClass("constrainedModal");
		return showConstrainedDialog(link, id, title);
	});
	*/


	var uni_heading = {
		typeface: "Helvetica Bold",
		typeColor: "#ffffff",
		typeSize: "24",
		allCaps: "true",
		content: $("h1#replaced").text()
	};

	var params = {
		menu: "false",
		salign: "LEFT",
		wmode: "transparent"
	};
	var attributes = {};

	// Embed the heading:
	swfobject.embedSWF("/swf/customType.swf", "replaced", "100%", "100%", "10","/swf/expressInstall.swf", uni_heading, params, attributes);

	$("#modal").append('<a class="closeModal" href="#">close</a>');

	/* If no cookie (is first time here) */
	/*************************************/
	if(!$.cookie('visited')) {
		$("#content").after('<div id="bubbleIntro"></div>');
		$("#content").after('<div class="item universeNext introButton"><a title="Dive in!" href="#" id="removeIntro">GO!</a></div>');
		$("#content").hide();

		// Set a visited cookie:
		$.cookie('visited', 'true', { expires: 700} );
	}

	$("#removeIntro").click(function(){
		removeIntro();
	});

	$("li #byDate").click(function(){
		removeIntro();
	});

	$("li #byAuthor").click(function(){
		removeIntro();
	});

	$("li #byCategory").click(function(){
		removeIntro();
	});

	function removeIntro() {
		$("#content").show();
		$("#bubbleIntro").remove();
		$(".introButton").remove();
	}

	/*************************************/
	/* end if no cookies are set *********/


});


/*
** Function to open a modal window displaying an iframe holding news content.
**
** param slug: the URL of the news content to display.
** param id: the ID of the originating request to pull related data from.
*/
function showDialog(slug, id, title) {

	// Set the date and user based on the passed id:
	var date = $("#"+id+" .timestamp").html();
	var userLink = $("#"+id+" .user").attr('href');
	var username = $("#"+id+" .user").html();

	$("#ui-dialog-title-modal").before("<span id='modalTitle'></span>");

	// Strip the 's out of the name:
	username = username.replace("'s", "");

	// Set the meta info on the modal:
	var postInfo = {
		typeface: "Silkscreen",
		typeColor: "#ffffff",
		typeSize: "8",
		allCaps: "false",
		content: "posted by "+ username +" "+date
	};
	var params = {
		menu: "false",
		salign: "LEFT",
		wmode: "transparent"
	};
	var attributes = {};

	swfobject.embedSWF("/swf/customType.swf", "ui-dialog-title-modal", "250", "11", "10","/swf/expressInstall.swf", postInfo, params, attributes);

	// Set the 'type' text on the modal:
	var modalTitle = {
		typeface: "Helvetica Bold",
		typeColor: "#ffffff",
		typeSize: "16",
		allCaps: "false",
		content: title
	};
	var params = {
		menu: "false",
		salign: "LEFT",
		wmode: "transparent"
	};
	var attributes = {};

	swfobject.embedSWF("/swf/customType.swf", "modalTitle", "250", "20", "10","/swf/expressInstall.swf", modalTitle, params, attributes);

	$("#modal").prepend('<iframe id="modalIframeId" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" class="ieModalIframe" />').dialog("open");

	var pos = slug.indexOf("?");
	if(pos != -1) {
		slug = slug.substr(0, pos)+"modal/"+slug.substr(pos, slug.length);
	} else {
		slug = slug+"/modal/";
	}

	$("#modalIframeId").attr("src", slug);

	$(".universe").addClass("childModal");
	// $(".universe").parent().addClass("childModal");
	$(".universe").attr("scroll","no");

	modalSize();

	$(window).resize(function(){
		modalSize();
	});

	// Add close modal listener
	$(".closeModal").click(function(){
		// Close the dialog:
		$('#modal').dialog('close');
		$(".universe").removeAttr("scroll");
		$(".universe").removeClass("childModal");
		// $(".universe").parent().removeClass("childModal");

		return false;
	});

	return false;
}

function showConstrainedDialog(slug, id, title) {

	// Set the date and user based on the passed id:
	var date = $("#"+id+" .timestamp").html();

	$("#constrainedModal").html('<iframe id="modalIframeId" width="959" height="453" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" />').dialog("open");

	var pos = slug.indexOf("?");
	if(pos != -1) {
		slug = slug.substr(0, pos)+"modal/"+slug.substr(pos, slug.length);
	} else {
		slug = slug+"/modal/";
	}

	$("#modalIframeId").attr("src", slug);
	$("#constrainedModal").append('<a class="closeModal" href="#">close</a>');
	$("#ui-dialog-title-constrainedModal").before("<span id='modalTitle'></span>");

	// Set the stuff on the modal:
	var timestamp = {
		typeface: "Silkscreen",
		typeColor: "#ffffff",
		typeSize: "8",
		allCaps: "false",
		content: "posted "+date
	};
	var params = {
		menu: "false",
		salign: "LEFT",
		wmode: "transparent"
	};
	var attributes = {};

	swfobject.embedSWF("/swf/customType.swf", "ui-dialog-title-constrainedModal", "250", "20", "10","/swf/expressInstall.swf", timestamp, params, attributes);


	// Set the modal title:
	var modalTitle = {
		typeface: "Helvetica Bold",
		typeColor: "#ffffff",
		typeSize: "16",
		allCaps: "false",
		content: title
	};
	var params = {
		menu: "false",
		salign: "LEFT",
		wmode: "transparent"
	};
	var attributes = {};

	swfobject.embedSWF("/swf/customType.swf", "modalTitle", "250", "21", "10","/swf/expressInstall.swf", modalTitle, params, attributes);

	// Add close modal listener
	$(".closeModal").click(function(){
		// Close the dialog:
		$('#constrainedModal').dialog('close');
		$("#people").removeAttr("scroll");
		$("#people").removeClass("childModal");
		$("#people").parent().removeClass("childModal");
	});

	return false;
}

function showPersonDialog(slug, id) {

	var date = $("#"+id+" p.timestamp").html();
	if(date == "") {
		date = "a while ago";
	}

	$("#personModal").html('<iframe id="modalIframeId" width="959" height="453" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" class="ieModalIframe" />').dialog("open");
	$("#modalIframeId").attr("src", slug+"/modal/");
	$("#personModal").append('<a class="closeModal" href="#">close</a>');
	$("#ui-dialog-title-personModal").before("<span id='modalTitle'></span>");

	// Set up the sharing stuff: (Disabled for now)
	/*
	var url = "http://new.campgreystone.com/profiles/view/"+ encodeURIComponent(id) +"&t="+encodeURIComponent('Greystone People');
	$("#modalTitle").before("<span id='shareOn'></span>");
	$("#modalTitle").before("<a id='personFacebook' href='http://www.facebook.com/sharer.php?u="+url+"' target='FacebookShare' onclick='window.open('http://www.facebook.com/sharer.php?u=http://www.campgreystone.com/profiles/view/"+id+"&t=Greystone People','FacebookShare','height=400, width=500,toolbar=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no'); return false;'></a>");
	$("#modalTitle").before("<a id='personMyspace' href='#'></a>");
	$("#modalTitle").before("<a id='personEmail' href='#'></a>");
	*/

	// Set the stuff on the modal:
	var timestamp = {
		typeface: "Silkscreen",
		typeColor: "#222222",
		typeSize: "8",
		allCaps: "false",
		content: "posted "+date
	};
	var params = {
		menu: "false",
		salign: "LEFT",
		wmode: "transparent"
	};
	var attributes = {};

	swfobject.embedSWF("/swf/customType.swf", "ui-dialog-title-personModal", "250", "20", "10","/swf/expressInstall.swf", timestamp, params, attributes);


	// Set the modal title:
	var modalTitle = {
		typeface: "Helvetica Bold",
		typeColor: "#222222",
		typeSize: "16",
		allCaps: "false",
		content: "Greystone People"
	};
	var params = {
		menu: "false",
		salign: "LEFT",
		wmode: "transparent"
	};
	var attributes = {};

	swfobject.embedSWF("/swf/customType.swf", "modalTitle", "250", "21", "10","/swf/expressInstall.swf", modalTitle, params, attributes);

	$(".universe").addClass("childModal");
	$(".universe").parent().addClass("childModal");

	$(".expandingModal").css({'position':'fixed'});

	modalSize();

	$(window).resize(function(){
		modalSize();
	});

	// Add close modal listener
	$(".closeModal").click(function(){
		// Close the dialog:
		$('#personModal').dialog('close');
		$(".universe").removeClass("childModal");
		$(".universe").parent().removeClass("childModal");
		return false;
	});

	return false;
}

/*
** Function to monitor window width and resize the modal accordingly.
*/
function modalSize() {

	$(".expandingModal").css({'position':'fixed'});

	// calc the new size for modals and the iframe.
	viewPortWidth = $(window).width();
	viewPortHeight = $(window).height();

	// set the modal dimensions
	$(".expandingModal").width(viewPortWidth - 30);
	$(".expandingModal .ui-dialog-content").height(viewPortHeight - 88);

	// set the iframe dimensions
	$(".expandingModal .ui-dialog-content iframe").width(viewPortWidth - 50);
	$(".expandingModal .ui-dialog-content iframe").height(viewPortHeight - 130);

	$(".ui-widget-overlay").width(viewPortWidth + 30);

	// Possibly try and move the page (scroll it somehow) to (0,0). Sometimes in IE7 the modal is ofset off the page
	// If you've made the window smaller between launching modals. but you can scroll it back in to proper view.
	// Grrr. gr. grrrrrr.
}


/*
** Function to clean up stuff from the dialog.
*/
function closeDialog() {
	$("#modal").html('&nbsp;');
	$("#modal").append('<a class="closeModal" href="#">close</a>');
}


/*
** Function to display a given section of the subnavigation for the universe.
**
** param id: id of the calling link.
*/
function showSubnavByUrl() {
	var url = window.location.pathname;
	// Find the current location:
	if(url.indexOf('date') != -1) {
		showSection("byDate");
	} else if (url.indexOf('author') != -1) {
		showSection("byAuthor");
	} else if (url.indexOf('category') != -1) {
		showSection("byCategory");
	} else if (url.indexOf('tag') != -1) {
		showSection("byTag");
	} else if (url.indexOf('media') != -1) {
		showSection("byMedia");
	}
}

/*
** Function to display a given section of the subnavigation for the universe.
**
** param id: id of the calling link.
*/
function showSection(id) {
	hideAll();
	switch(id) {
		case 'byDate':
			$("#subDate").fadeIn("fast");
			break;
		case 'byAuthor':
			$("#subAuthor").fadeIn("fast");
			break;
		case 'byCategory':
			$("#subCategory").fadeIn("fast");
			break;
		case 'byTag':
			$("#subTag").fadeIn("fast");
			break;
		case 'byMedia':
			$("#subMedia").fadeIn("fast");
			break;
	}
}

/*
** Function to hide all of the subnav sections in the top navigation.
*/
function hideAll() {
	$("#subDate").hide();
	$("#subAuthor").hide();
	$("#subCategory").hide();
	$("#subTag").hide();
	$("#subMedia").hide();
}
