﻿var regModal = {
		defaults: {
			buttonsrc: null,
			buttonAltText: null,
			subheader: null
		},
		options: {
			parent: "body",
			modalID: null,
			subheaderID: null,
			position: null,
			buttonsrc: null,
			buttonAltText: null,
			subheader: null,
			width: null,
			height: null,
			buttonID: null,
			top: '10%',
			left: '10%',
			backgroundcolor: "#555555",
			backgroundOpacity: 75,
			speed: {
				close: 1000,
				open: 1000
			}
		},
		close: function () {
			//Remove the modal registration form
			$psuedoRegForm.fadeOut(regModal.options.speed.close);
			$psuedoRegForm.remove();
			$('#'+regModal.options.modalID).css({ 'position': 'static' });
			if(regModal.defaults.subheader != null){
				$('#'+regModal.options.modalID).find('#'+regModal.options.subheaderID).replaceWith('<p id="'+regModal.options.subheaderID+'" >'+regModal.defaults.subheader+'</p>');
			}
			$('#'+regModal.options.modalID).find('#'+regModal.options.buttonID).attr('src', regModal.defaults.buttonsrc);
			$('#'+regModal.options.modalID).find('#'+regModal.options.buttonID).attr('alt', regModal.defaults.buttonAltText);
			if(regModal.options.width != null){
				$('#'+regModal.options.modalID).css({ 'width': '' });
			}
			if(typeof addRoundedCorners == 'function'){
				addRoundedCorners();
			}
			//Remove the overlay from behind the modal registration
			$(".modal-overlay").remove();

			//Remove the close button of the modal registration
			$(".close-window").remove();

		},
		open: function () {
			
			var modal = "<div class=\"modal-overlay\"></div>";

			//Write the overlay to the page
			$(regModal.options.parent).append(modal);

			//Assign the filter to the element as opposed to using CSS to get the opacity and the "fadein" to work in IE
			$('.modal-overlay').css('filter', 'alpha(opacity=75)');
			$('.modal-overlay').fadeIn(1000);

			//Create a copy of the registration module to hold it's place on page
			$psuedoRegForm = $('#'+regModal.options.modalID).clone();
			$('#modal-placeholder').before($psuedoRegForm);
			$psuedoRegForm.css({ 'position': 'static', 'display': 'block' });

			//Write the close window button to the modal
			$('#'+regModal.options.modalID).append("<a class=\"close-window\"></a>");

			//Position the registration module
			$('#'+regModal.options.modalID).css({ 'position': 'fixed', 'top': regModal.options.top, 'left': regModal.options.left, 'z-index': '102', 'display': 'none' });

			if(regModal.options.subheader != null && regModal.options.subheaderID != null){
				$('#'+regModal.options.modalID).find('#'+regModal.options.subheaderID).replaceWith('<p id="'+regModal.options.subheaderID+'" >'+regModal.options.subheader+'</p>');
			}
			if(regModal.options.buttonID != null){
				if(regModal.options.buttonsrc != null){
					$('#'+regModal.options.modalID).find('#'+regModal.options.buttonID).attr('src', regModal.options.buttonsrc);
				}
				if(regModal.options.buttonAltText != null){
					$('#'+regModal.options.modalID).find('#'+regModal.options.buttonID).attr('alt', regModal.options.buttonAltText);
				}
			}
			if(regModal.options.width != null){
				$('#'+regModal.options.modalID).css({ 'width': regModal.options.width });
			}
			if(typeof removeRoundedCorners == 'function'){
				removeRoundedCorners();
			}
			$('#'+regModal.options.modalID).fadeIn(1000);

			//Assign functionality to to close the modal to the close window button and to the ref form button.
			$('.close-window').click(function () { regModal.close(); });
			$('#'+regModal.options.buttonID).click(function () {
				$psuedoRegForm.remove();
			});
		},
		showModal: function showModal(contentFirstLine, contentSecondLine, button, altText){
			if(contentFirstLine.length > 0){
				regModal.options.subheader = contentFirstLine;
				if(contentSecondLine.length > 0){
					regModal.options.subheader = contentFirstLine + '<br />' + contentSecondLine;
				}
			}
			if(button.length > 0){
				regModal.options.buttonsrc = button;
			}
			if(altText.length > 0){
				regModal.options.buttonAltText = altText;
			}
			regModal.open();
			
		}
	};



