/* IFRAME popup

 * Last Update: September 08, 2008(Alex)

 * Description:
   Extends the window object with a method for creating IFRAME popups.
   The popups are created dynamically as IFRAMES and appended and positioned relatively to a wrapper given as parameter(default: body).
   The window object is extended so the popups can be called from other popups/IFRAMES/FRAMES
	
 * Dependencies:
   - Requires: jquery.js, popup_utils.js
	
 * Usage: called on document or window ready. 
 
 * Settings:
	- popupIframeName
		the name of the IFRAME; used for the "target" attribute of the trigger links, and also for the popup dependencies, identified using rel="[popupIframeName]"
	- popupWidth
		the width of the popup IFRAME
	- popupHeight
		the height of the popup IFRAME
	- popupLeft
		the left position of the popup, relatively to the wrapper
	- popupTop
		the top position of the popup, relatively to the wrapper
	- popupBackground
		background of the IFRAME
	- popupDefaultSrc
		the default source of the popup
	- popupCloseButtonID
		the ID of the popup close button. The button is dynamically created
	- waitPopupID
		the ID of the div displayed as "wait message". The div is dynamically created
	- popupMaskID
		the ID of the div blocking other content interaction outside the popup. Dynamically created.
	- filterAndFormatContent
		the function called when the popup IFRAME is loaded, for formatting the content
	- popupParentNode
		the popup parent node, given as jQuery object. All the elements dynamically created(popup IFRAME, popup close button, wait message, popup mask) will be appended to this node. Default: body element
	- triggerDynamicClass
		the name of the CSS class associated to the trigger when clicked on. Used for manipulating the "selected" state
	- triggerSelectionClass
		the name of the CSS class associated with the trigger when the popup is opened. Default: "selected".
	- disabledElementsClass
		the name of the CSS class associated with the elements disabled when the popup is open. Example: select elements. The class is used to hide/show elements.
 */
var dc_Popup = function(triggers, settings)
				{
					this.settings = jQuery.extend({
												  popupIframeName: 'dc_PopupIframe',
												  popupWidth: 400,
												  popupHeight: 300,
												  popupLeft: 'viewportCenter',
												  popupTop: 'viewportCenter',
												  popupBackground: 'transparent', 
  												  popupDefaultSrc: '',
												  
												  popupCloseButtonID: 'dc_PopupCloseButton',
												  
												  waitPopupID: 'dc_PopupWaitMessage',
												  popupMaskID: 'dc_ViewportMask',
												  
												  filterAndFormatContent: function(popupIframeName){return false}, // function for filtering and formatting content
												  							
												  popupParentNode: jQuery('body'),
												  
												  triggerDynamicClass: 'dc_PopupTrigger',
												  triggerSelectionClass: 'selected',
												  disabledElementsClass: 'disabledByPopup'
												 
											 },
											 settings);
					
					this.dc_PopupContent = null;
					this.dc_Triggers = triggers;
					this.dc_PopupWaitMessage = null;
					this.dc_PopupMask = null;
					this.dc_PopupCloseButton = null;
					this.displayed = false;
												
					this.init = function()
									{
										this.dc_Triggers
												.css('visibility', 'hidden')  // hide triggers
												.attr('target', dcp.settings.popupIframeName) // target triggers to open inside IFRAME
												.click(function()
													{
														var dc_Trigger = jQuery(this);
														
														// add "selected" state to the trigger clicked on
														dc_Trigger.addClass(dcp.settings.triggerSelectionClass);
														
														dcp.off(); // hide any previously opened popups
														dcp.create(); // create popup and popup dependencies
														
														dcp.standBy(); // display 'please wait' message
														dcp.dc_PopupContent.load(function()
																					{ // show popup when loaded
																						dcp.on();
																						
																						// show popup close button
																						dcp.dc_PopupCloseButton.show();
																					});
														
														dcp.dc_PopupContent.attr('src', dc_Trigger.attr('href'));
														return false;
													})
												.css('visibility', 'visible') // show triggers
									}
					this.create = function()
									{	
										 // create the IFRAME popup
										dcp.dc_PopupContent = jQuery.dc_PopupIframe(dcp.settings);
										
										
										 // create the close button
										dcp.dc_PopupCloseButton = jQuery.dc_PopupCloseBtn(jQuery.extend(
																								dcp.settings,
																								{whenClicked: function()
																												{
																													try{window.customPopups[dcp.settings.popupIframeName].off()}
																													catch(err){}}
																												})
																							 );
										
										 // create the viewport mask												
										dcp.dc_PopupMask = jQuery.dc_ViewportMask(jQuery.extend(
																						dcp.settings,
																						{whenClicked: function()
																										{
																											try{window.customPopups[dcp.settings.popupIframeName].off()}
																											catch(err){}}
																										})
																					 );

										 // create the wait popup
										dcp.dc_PopupWaitMessage = jQuery.dc_WaitPopup(dcp.settings);
									}
					
					this.standBy = function()
									{
										// show viewport mask
										dcp.dc_PopupMask.show();
										
										// show wait popup
										dcp.dc_PopupWaitMessage.show();
									}
									
					this.on = function()
								{	
									// hide wait popup		
									dcp.dc_PopupWaitMessage.hide();
									
									 // show popup
									dcp.dc_PopupContent.show();
									
									dcp.displayed = true;
								}
								
					this.off = function()
								{
									// remove selection class from the trigger
									jQuery('.' + dcp.settings.triggerDynamicClass)
									 	.removeClass(dcp.settings.triggerSelectionClass);
									
									// remove popup and popup dependencies
									 jQuery('iframe[name=' + dcp.settings.popupIframeName + ']')
										.add('[rel=' + dcp.settings.popupIframeName + ']')
										.remove();
									
									// make elements disabled by popup visible
									jQuery('.'+ dcp.settings.disabledElementsClass)
										.removeClass(dcp.settings.disabledElementsClass)
										.css('visibility', 'visible');
									
									dcp.displayed = false;
								}
					
					var dcp = this; // dcp: dynamic content popup
					dcp.init();
					
					return dcp;
				}
				
jQuery.extend(window, dc_Popup);