$(document).ready(function() {

	/*
		create website.getGoingDetail menu
	*/

	website.dashboardActivityItem.banner =(function() {

		pub = {};

		// setup empty flashData object (this will be send to the update() flash callbackmethod
		var flashData = {
			label : $("#eventName").val(),
			url : 'http://'+window.location.host+'/activiteit/'+$("#eventId").attr('value')
		}

		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/custombanner_300x250.swf", "flashBannerStaand", "100%", "100%", "10", "false", flashvars, params, attributes);
			});

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

			/*
				Event Handlers forms
			*/
			
			// dropdown to switch the flash embed
			$("#bannerFormatDropdown div.jqTransformSelectWrapper ul li a").click(function() {
				var currentOption = $(this).attr('index');

				switch (currentOption) {

					case "0" :
						$('#dashboardActivityItem-rowBanner .rowContent .container .inner .flashEmbed .preview .flashWrapper').animate({
							'opacity' : 0
						},{'queue': true, 'duration': 250, 'easing': 'linear', 'complete' : function() {

								// update dom
								$("#flashBannerLiggend").remove();
								$('#dashboardActivityItem-rowBanner .rowContent .container .inner .flashEmbed .preview .flashWrapper').append('<div id="flashBannerStaand"></div>');
								$('#dashboardActivityItem-rowBanner .rowContent .container .inner .flashEmbed .preview .banner-background').attr('src', '/img/dashboardActivityItem/banner-staand_bg.gif');
								
								// switch flash embed
								window.swfobject.embedSWF("/swf/custombanner_300x250.swf", "flashBannerStaand", "100%", "100%", "10", "false", flashvars, params, attributes);

								// switch emebed code
								$('#embedCode').val('<object><embed src="http://'+window.location.host+'/swf/custombanner_300x250.swf" FlashVars="id=' + $('#dashboardActivityItem-rowBanner .embedCode').attr('eventid') + '&apiPath=http://'+window.location.host+'/api/banner/get_banner_data.php" type="application/x-shockwave-flash" width="300" height="250" allowscriptaccess="always"></embed></object>');
							}
						}).animate({
							'opacity' : 1
						},{'queue': true, 'duration': 250, 'easing': 'linear'});

						break;

					case "1" :
						$('#dashboardActivityItem-rowBanner .rowContent .container .inner .flashEmbed .preview .flashWrapper').animate({
							'opacity' : 0
						},{'queue': true, 'duration': 250, 'easing': 'linear', 'complete' : function() {

								// update dom
								$("#flashBannerStaand").remove();
								$('#dashboardActivityItem-rowBanner .rowContent .container .inner .flashEmbed .preview .flashWrapper').append('<div id="flashBannerLiggend"></div>');
								$('#dashboardActivityItem-rowBanner .rowContent .container .inner .flashEmbed .preview .banner-background').attr('src', '/img/dashboardActivityItem/banner-liggend_bg.gif');
													
								// switch flash embed
								window.swfobject.embedSWF("/swf/custombanner_468x60.swf", "flashBannerLiggend", "100%", "100%", "10", "false", flashvars, params, attributes);

								// switch emebed code
								$('#embedCode').val('<object><embed src="http://'+window.location.host+'/swf/custombanner_468x60.swf" FlashVars="id=' + $('#dashboardActivityItem-rowBanner .embedCode').attr('eventid') + '&apiPath=http://'+window.location.host+'/api/banner/get_banner_data.php" type="application/x-shockwave-flash" width="468" height="60" allowscriptaccess="always"></embed></object>');
							}
						}).animate({
							'opacity' : 1
						},{'queue': true, 'duration': 250, 'easing': 'linear'});

						break;
				}
			});
			
			/*
				Event Handlers textarea
			*/
			
			$('#embedCode').click(function() {
				// on click select all text
				$(this).select();			
			});
			

		};

		var getFlashMovie = function(flashId) {

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

		var updateFlashData = function() {
			
			if($("#flashBannerStaand").length == 1) {

				getFlashMovie("flashBannerStaand").updateFlashData(JSON.stringify(flashData));
				
			} else if ($("#flashBannerLiggend").length == 1) {
				
				getFlashMovie("flashBannerLiggend").updateFlashData(JSON.stringify(flashData));
			}
		};
		
		var embedCheck = function() {

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

		/*
			public methods
		*/

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

			updateFlashData();
		};

		init();

		return pub;

	})();
});
