/*
** specialEvents.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() {

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

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

	// Set up the click listener for the next and previous buttons.

});

/*
** Function to launch the activity dialog.
**
** param @slug: the slug of the activity page to be shown.
*/
function showActivity(slug) {

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

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

	swfobject.embedSWF("/swf/customType.swf", "ui-dialog-title-specialEvents", "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: "Special Events at Greystone"
	};
	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);

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

	modalSize();

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

	// Add close modal listener
	$(".closeModal").click(function(){
		// Close the dialog:
		$('#specialEvents').dialog('close');
		$(".page").removeClass("childModal");
		$(".page").parent().removeClass("childModal");
		/*
		var flash = document.getElementById('replaced');
		flash.enableInteraction();
		*/
		return false;
	});

	$("#nextButton").click(function() {
		getSpecialEvent(true);
	});

	$("#prevButton").click(function() {
		getSpecialEvent(false);
	});

}

function getSpecialEvent(forward) {

	// Get the current id  from the iframe:
	var src = $("#modalIframeId").attr('src').replace('/', '');

	// Check to make sure the profiles array is available:
	if($.isArray(specialEventSlugs)) {
		// Go through the array of ids and find the next one:
		var idPos = $.inArray(src, specialEventSlugs);

		var nextID = 0;
		if(idPos == 0 &&  forward == false) {
			nextID = specialEventSlugs[specialEventSlugs.length-1];
		} else if (idPos == (specialEventSlugs.length-1) && forward == true) {
			nextID = specialEventSlugs[0];
		} else if (forward == false) {
			nextID = specialEventSlugs[idPos - 1];
		} else {
			nextID = specialEventSlugs[idPos + 1];
		}

		$("#modalIframeId").attr('src', '/' + nextID);
	}

	return false;
}

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

	$(".expandingModal").css('top', '15px');

	// 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.
}
