/* IFRAME popup

 * Last Update: December 08, 2008(Alex)

 * Description:
   Extends the window object with a method for creating IFRAME popups.
   The popups are created dynamically as IFRAMES and appended to a wrapper given as parameter(default: the body node).
   
  The window object is extended so the popups can be called from other popups/IFRAMES/FRAMES
	
 * Dependencies:
   - Requires: jquery.js, jquery.dimensions.js, popup_utils.js, css/iframe_popups.css, images/dc_BtnClosePopup.gif
	
 * 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]"
	- popupIframeClass
		the CSS class associated to the popup, defined in external stylesheet
	- popupDefaultSrc
		the default source of the popup
	- popupWidth
		the width of the popup IFRAME
	- popupHeight
		the height of the popup IFRAME
	- popupLeft: viewportCenter/parentNodeCenter/precise value
		the left position of the popup
	- popupTop: viewportCenter/parentNodeCenter/precise value
		the top position of the popup
		
	- popupCloseButton: true/false
		flag for showing close button
	- popupCloseButtonID
		the ID of the popup close button. The button is dynamically created
	- popupCloseButtonClass
		the CSS class associated to the popup close button, defined in external stylesheet
		
	- popupWaitMessage
		flag for showing wait message 
	- popupWaitID
		the ID of the div displayed as "wait message". The div is dynamically created
	- popupWaitClass
		the CSS class associated to the popup wait message div, defined in external stylesheet
	- popupWaitMessageText
		the text within the wait box
		
	- popupMask
		flag for showing popup mask
	- popupMaskID
		the ID of the div blocking other content interaction outside the popup. Dynamically created.
	- popupMaskClass
		the CSS class associated to the popup wait mask, defined in external stylesheet
	- popupMaskOpacity
		the opacity of the mask
		
	- 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 node
		
	- 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 dynamically
 */
var dc_Popup = function(triggers, settings)
				{
					this.settings = jQuery.extend({
													popupIframeName: 'dc_PopupIframe',
													popupIframeClass: 'dc_PopupIframe',
													popupDefaultSrc: '',
													popupWidth: 400,
													popupHeight: 300,
													popupLeft: 'viewportCenter',
													popupTop: 'viewportCenter',
													
													popupCloseButton: true,
													popupCloseButtonID: 'dc_PopupCloseButton',
													popupCloseButtonClass: 'dc_PopupCloseButton',
													
													popupWaitMessage: true,
													popupWaitID: 'dc_PopupWaitMessage',
													popupWaitClass: 'dc_PopupWaitMessage',
													popupWaitMessageText: 'Please wait...',
													
													popupMask: true,
													popupMaskID: 'dc_ViewportMask',
													popupMaskClass: 'dc_ViewportMask',
													popupMaskOpacity: 0.3,
																			
													popupParentNode: '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);
														
														dcp.off(); // hide any previously opened popups
														
														// add "selected" state to the trigger clicked on
														dc_Trigger
															.addClass(dcp.settings.triggerSelectionClass)
															.addClass(dcp.settings.triggerDynamicClass);
														
														dcp.create(); // create popup and popup dependencies
														
														dcp.standBy(); // display 'please wait' message
														
														dcp.dc_PopupContent.load(function()
																					{ // show popup when loaded
																						dcp.on();
																					});
														
														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
										if (this.settings.popupCloseButton == true)
											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
										if (this.settings.popupMask == true)
											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_PopupWaitMessage(dcp.settings);
									}
					
					this.standBy = function()
									{
										// show viewport mask
										if (this.settings.popupMask == true)
											dcp.dc_PopupMask.on();
										
										// show wait popup
										if(dcp.settings.popupWaitMessage == true)
											dcp.dc_PopupWaitMessage.on();
									}
									
					this.on = function()
								{	
									// off wait popup
									if(dcp.settings.popupWaitMessage == true)
										dcp.dc_PopupWaitMessage.off();
									
									 // show popup
									dcp.dc_PopupContent.on();
									
									 // show popup close button
									 if (dcp.settings.popupCloseButton == true)
											dcp.dc_PopupCloseButton.on();
									
									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, {customPopups:{}});
