// ---------- Popup ----------

RHG.Classes.Popup = new RHG.Class();
RHG.Classes.Popup.selectors = {
	"pageContainer": "body",
	"closeLinks": "a.popup-close-link, a[rel='close-popup']"
};

RHG.Classes.Popup.frameJq = jQuery("<div id='popup' class='popup-underlay hidden'></div>");

RHG.Classes.Popup.classFunction = function(configObject) {
	var classFunctionPub = {};
	var thisClass = RHG.Classes.Popup;
	var selectors = thisClass.selectors;	
	
	var frameJq = RHG.Classes.Popup.frameJq;
	var frameScrollJq = jQuery("<div class='popup-scroll'></div>");
	var frameBoxJq = jQuery("<div class='popup-box'></div>");
	var closeButton = (configObject.closeButton !== false) ? true : false;
	
	var closeJq = jQuery("<a href='#' class='close'>Close</a>");
	var openEventFunction = (typeof configObject.openEventFunction != 'function') ? function(popup){} : configObject.openEventFunction
	var closeEventFunction = (typeof configObject.closeEventFunction != 'function') ? function(popup){} : configObject.closeEventFunction
	var jqHashBack = jQuery("<span id='popup-back' />");
	classFunctionPub.triggerJq;
	classFunctionPub.contentJq;
			
	var customCloseLinks;
	var hashBackInAction = false;
	
	frameJq.addClass("hidden");
	classFunctionPub.isOpen = false;

	classFunctionPub.close = function(evt) {
		if (classFunctionPub.isOpen) {
			classFunctionPub.isOpen = false;
			RHG.Utilities.PIEDetach(frameBoxJq[0]);			
			frameJq.addClass("hidden");
			frameJq.after(frameBoxJq);

							
			jQuery("html").removeClass("lightboxed");

			if (hashBackInAction) {
				// Focus keyboard control
				window.location.hash = "#popup-back";
				// Remove temporary hash back element
				jqHashBack.remove();
			}
			
			if (evt) {
				// Add reference to this popup to event
				if (!evt.RHG) { evt.RHG = {}; }
				evt.RHG.popup = classFunctionPub;
			}

			jQuery(window).unbind('resize', position);

			closeEventFunction(evt);
			return false;
		}
	}
	
	var position = function() {
		frameBoxJq.css({
			height: classFunctionPub.contentJq.outerHeight()
		});			
		// max-height: 90%
		if (frameBoxJq.outerHeight() / frameJq.outerHeight() > 0.8) {
			frameBoxJq.css({
				height: "90%"
			});				
		}
				
		frameBoxJq.css({
			marginLeft: -frameBoxJq.outerWidth() / 2,
			marginTop: -frameBoxJq.outerHeight() / 2
		});
	}

	classFunctionPub.open = function(evt) {	
							
		if (!classFunctionPub.isOpen) {
			classFunctionPub.isOpen = true;
			
			if (evt) {
				// Add element to hash back to on close
				var sourceElement = evt.target;
				jQuery(sourceElement).after(jqHashBack);
				jqHashBack.css("position", "fixed");
				jqHashBack.css("top", "0");
				jqHashBack.css("left", "0");						
				hashBackInAction = true;
				
				// Add reference to this popup to event
				if (!evt.RHG) { evt.RHG = {}; }
				evt.RHG.popup = classFunctionPub;
			} else {
				hashBackInAction = false;
			}		
			
			frameJq.append(frameBoxJq);
			frameJq.removeClass("hidden");	
			RHG.Utilities.PIEAttach(frameBoxJq[0]);
			
			frameBoxJq.css({
				left: 0,
				top: 0
			});
			
			position();
			
			frameBoxJq.css({
				left: "50%",
				top: "50%"
			});
				
			
			// Focus keyboard control
			window.location.hash = "#popup";
			
			jQuery(window).bind('resize', position);
			
			if (configObject.imageURL) {
				
				classFunctionPub.contentJq.empty();
				classFunctionPub.contentJq.append(RHG.Variables.loadSpinnerHtml)
				
				var jqImage = jQuery("<img/>");
				jqImage.one("load", function() {
					jqImage.removeClass("hidden");
					classFunctionPub.contentJq.find(".load-spinner").remove();
					position();
				});
				jqImage.addClass("hidden");
				classFunctionPub.contentJq.append(jqImage)
				jqImage.attr("src", configObject.imageURL);
			}
			
			openEventFunction(evt);
		}

	}

	classFunctionPub.init = function() {
		if (configObject.uniqueTargetNodeOrSelector) {
			classFunctionPub.contentJq = jQuery(configObject.uniqueTargetNodeOrSelector);
		} else {
			classFunctionPub.contentJq = jQuery("<div></div>");
			classFunctionPub.contentJq.append(RHG.Variables.loadSpinnerHtml)
		}
		classFunctionPub.contentJq.addClass("popup-content")			
		
		if (configObject.triggerNodeOrSelector) {
			classFunctionPub.triggerJq = jQuery(configObject.triggerNodeOrSelector);
			classFunctionPub.triggerJq.bind("click", function(evt){
				classFunctionPub.open(evt);
				return false;
			});
		}

		if (closeButton) {
			frameBoxJq.append(closeJq)
			closeJq.bind("click", function(evt){
				classFunctionPub.close(evt);
				return false;
			});			
		}

		customCloseLinks = classFunctionPub.contentJq.find(selectors.closeLinks);
		customCloseLinks.bind("click", function(evt){
			classFunctionPub.close(evt);
			return false
		})

		frameJq.appendTo(selectors.pageContainer);
		frameJq.after(frameBoxJq);
		frameBoxJq.prepend(frameScrollJq);
		classFunctionPub.contentJq.appendTo(frameScrollJq);
	}

	return classFunctionPub;
}
