$(document).ready(function() {

	/*
		create website.getGoingDetail menu
	*/

	website.dashboardActivityItem.poster =(function() {

		pub = {};

		// setup empty flashData object (this will be send to the update() flash callbackmethod
		var flashData = {
			label : $("#eventName").val(),
			imagepreview : 'http://'+window.location.host+'/dummy/dashboardActivityItem/poster_image_small.jpg',
			image : 'http://'+window.location.host+'/dummy/dashboardActivityItem/poster_image_large.jpg',
			copy : $("#posterText").val()
		}

		init = function() {

			// set eventHandlers to update the flashData
			$("#eventName").blur(function() {
				 flashData.label = $("#eventName").val();
				 updateFlashData();
			});

			// TODO: id meegeven in de embedcode
			var flashvars = false;

			var params = {
				scale : "noscale",
				menu  : "false",
				wmode : "transparent",
				allowscriptaccess : "samedomain"
			};

			// add class on successful embed to check if flash is present
			attributes  = {
				styleclass : "swfobject_present"
			};

			// trigger the swf embed
			$(document).ready(function() {
				window.swfobject.embedSWF("/swf/customposter.swf", "flashPoster", "100%", "100%", "10", "false", flashvars, params, attributes);
			});

			// trigger the embed-check
			setTimeout(function() { embedCheck(); }, 1000);

			/*
				Event handlers input fields & textareas
			*/

			// set eventHandlers to update the flashData
			$("#eventName").blur(function() {
				 flashData.label = $("#eventName").val();
				 updateFlashData();
			});

			$("#posterText").blur(function() {
				 flashData.copy = $("#posterText").val();
				 updateFlashData();
			});

			if ($("#posterText").length == 1) {
				$("#posterText").limit(463);
			}


			/*
				Event Handlers buttons
			*/

			$("#dashboardActivityItem-rowPoster .rowContent .poster .create .options .text .generate").click(function() {
				var theCopy = flashData.copy == '...' ? '' : flashData.copy;
				getFlashMovie("flashPoster").createPoster(theCopy);
			});


			/*
				Event Handlers forms
			*/

			// dropdown to switch the flash embed
			$("#posterImageDropdown div.jqTransformSelectWrapper ul li a").click(function() {

				var currentOption = $(this).attr('index');

				switch (currentOption) {

					case "0" :

						// TODO: insert method to update the flashData's imagepreview & image objects

						resetFlashData();
						updateFlashData();

					break;

					case "1" :

						getFlashMovie("flashPoster").uploadImage();

					break;
				}
			});
		};

		var getFlashMovie = function(flashId) {

			if (navigator.appName.indexOf("Microsoft") != -1) {
				return window[flashId];
			} else {
				return document[flashId];
			}
		};

		var updateFlashData = function() {

			getFlashMovie("flashPoster").updateFlashData(JSON.stringify(flashData));

		};

		var resetFlashData = function() {

			flashData['imagepreview'] = 'http://'+window.location.host+'/dummy/dashboardActivityItem/poster_image_small.jpg';
			flashData['image'] = 'http://'+window.location.host+'/dummy/dashboardActivityItem/poster_image_large.jpg';

		};

		var embedCheck = function() {

			// if no flash hide the entire poster segment
			if(!$("#flashPoster.swfobject_present").length) {
				$('#dashboardActivityItem-rowPoster').remove();
			};
		};

		/*
			public methods
		*/

		// gets called once from Flash (to see if it's there)
		pub.registerFlashObject = function() {

			updateFlashData();
		};

		init();

		return pub;

	})();
});

