///////////////////////////////////////////////////////////////////
// widgetBlogPhotoOverlayMgr was written to handle photo uploads for the Question Blog Widget on the Profile page.
///////////////////////////////////////////////////////////////////

// make wbpoMgr variable global so it's accessible from its child iframes
var wbpoMgr;

function WidgetBlogPhotoOverlayMgr() {
	
	this.form = document.forms[0];
	this.overlay = YAHOO.util.Dom.get('lightbox');
	this.overlayCloseBtn = YAHOO.util.Dom.get('lightbox_close');
	this.overlayIframe = YAHOO.util.Dom.get('widgetblog_iframe');
	this.overlayIframeStartSrc = this.overlayIframe.src;
	this.momentIframe = YAHOO.util.Dom.get('moment_iframe');
	
	this.init();
	
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.init()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.init = function() {
	
	// use the form on the parent page to create functions the iframes can access
	// the child iframes can access the parent's form and its properties & methods
	// we're using the form instead of the global itmMgr object b/c at times itmMgr is inaccessible or doesn't exist yet - the form is a workaround
	var tempObj = this;
	var formObj = this.form;
	
	formObj.resizeWBPhotoIframe = function(newHeight) {
		tempObj.resizeOverlayIframe(newHeight);
	}
	
	formObj.positionWBPhotoOverlay = function() {
		tempObj.positionOverlay();
	}
	
	formObj.closeWBPhotoOverlay = function() {
		tempObj.closeOverlay();
	}
	
	formObj.showAttachedWBPhotoMssg = function() {
		tempObj.showAttachedMssg();
	}
	
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.getWidgetBlogIframes()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.initFormForWidget = function(widgetId) {
	
	this.widgetId = widgetId;
	this.widgetIframe = YAHOO.util.Dom.get(widgetId + '_iframe');
	this.getWidgetFormElements();
	this.getOverlayIframeFormElements();

}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.getWidgetFormElements()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.getWidgetFormElements = function() {
		
	// access iframe's form, get form elements
	if (this.widgetIframe.contentDocument) { //FF
		this.widgetIframeForm = this.widgetIframe.contentDocument.getElementById('widgetblog_form');
		this.widgetIframeTextbox = this.widgetIframe.contentDocument.getElementById('txtAnswer');
		this.addPhotoBtn = this.widgetIframe.contentDocument.getElementById('btn_widgetblog_addphoto');
		this.photoAttachedMssg = this.widgetIframe.contentDocument.getElementById('btn_widgetblog_attached_icon');
		if(this.widgetIframe.contentDocument.getElementById('photo_added_flag')) {
			this.photoAddedFlag = this.widgetIframe.contentDocument.getElementById('photo_added_flag');
			this.formErrorPhoto = this.widgetIframe.contentDocument.getElementById('form_error_photo');
		}
	} else if (this.widgetIframe.document) { //IE
		// the only way that IE seems to be able to access the iframe is by iterating through and matching by name
		// accessing the frame directly by name or id does not work in IE6 or 7, inexplicably
		for (var i=0; i<top.frames.length; i++) {
			// place in a try/catch so that one bad iframe doesn't spoil the bunch
			try {
				if (top.frames[i].name == this.widgetIframe.id) {
					this.widgetIframeForm = top.frames[i].document.getElementById('widgetblog_form');
					this.widgetIframeTextbox = top.frames[i].document.getElementById('txtAnswer');
					this.addPhotoBtn = top.frames[i].document.getElementById('btn_widgetblog_addphoto');
					this.photoAttachedMssg = top.frames[i].document.getElementById('btn_widgetblog_attached_icon');
					if(this.photoAddedFlag = top.frames[i].document.getElementById('photo_added_flag')){
						this.photoAddedFlag = top.frames[i].document.getElementById('photo_added_flag');
						this.formErrorPhoto = top.frames[i].document.getElementById('form_error_photo');					}
				}
			} catch(e) {}
		}
	}

}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.getOverlayIframeFormElements()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.getOverlayIframeFormElements = function() {
		
	// access iframe's form, get form elements
	if (this.overlayIframe.contentDocument) { //FF
		this.overlayIframeForm = this.overlayIframe.contentDocument.getElementById('blogWidgetUploadPhoto_form');
		this.overlayIframeBlurb = this.overlayIframe.contentDocument.getElementById('txt_blurb');
		this.overlayIframeWidgetId = this.overlayIframe.contentDocument.getElementById('txt_widgetId');
	} else if (this.overlayIframe.document) { //IE
		// the only way that IE seems to be able to access the iframe is by iterating through and matching by name
		// accessing the frame directly by name or id does not work in IE6 or 7, inexplicably
		for (var i=0; i<top.frames.length; i++) {
			// place in a try/catch so that one bad iframe doesn't spoil the bunch
			try {
				if (top.frames[i].name == 'widgetblog_iframe') {
					this.overlayIframeForm = top.frames[i].document.getElementById('blogWidgetUploadPhoto_form');
					this.overlayIframeBlurb = top.frames[i].document.getElementById('txt_blurb');
					this.overlayIframeWidgetId = top.frames[i].document.getElementById('txt_widgetId');
				}
			} catch(e) {}
		}
	}

}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.showOverlay()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.showOverlay = function(widgetId) {
	
	// reset iframe source to clear previous files from file upload tag if the lightbox has been used before
	this.resetIframeSrc();
	
	// this preps the iframe for use by this specific widget
	this.initFormForWidget(widgetId);
	
	YAHOO.util.Dom.setStyle(this.overlayIframe, 'display', 'block');
	YAHOO.util.Dom.setStyle(this.momentIframe, 'display', 'none');
	
	//don't do any of this until overlay content is loaded
		// iframeHeight is attached to the form in the iframe document once its DOM is ready
		// it's set there b/c the parent doesn't know when the iframe's DOM is ready
		this.overlayIframeHeight = document.forms[0].overlayIframeHeight;
		
		/* scrolls page */
		//var currentScroll = YAHOO.util.Dom.getDocumentScrollTop();
		//var colAScroll = YAHOO.util.Dom.getY('main_container_wrapper');
		//window.scrollBy(0, colAScroll - currentScroll);

		/* creates element with transparent dark background and sets element to full size of window*/
		if (!document.getElementById('lightboxDarkBg')) {
		this.darkness = document.createElement('img');
		this.darkness.src = "http://family.go.com/images/lightbox_bg.png";
		this.darkness.className = 'transparency';
		this.darkness.id = 'lightboxDarkBg';
		this.darkness.style.position = 'absolute';
		this.darkness.style.zIndex = 14;
		this.darkness.style.top = '0px';
		this.darkness.style.left = '0px';
		this.darkness.style.width = YAHOO.util.Dom.getDocumentWidth() + 'px';
		this.darkness.style.height = YAHOO.util.Dom.getDocumentHeight() + 'px';
		
		document.body.appendChild(this.darkness);
		}
		
		this.resizeOverlayIframe(this.overlayIframeHeight);
		this.positionOverlay();
		
	// add these event listeners only when overlay is shown b/c the lightbox is shared by other widgets
	
	YAHOO.util.Event.removeListener(this.overlayCloseBtn, 'click');
	YAHOO.util.Event.addListener(this.overlayCloseBtn, 'click', function() {
			this.closeOverlay();
		}, this, true);
	
	YAHOO.util.Event.addListener(window, 'resize', function() {
			if(this.darkness && (this.overlay.style.display == 'block')) {
				this.setLightboxBackground();
				this.positionOverlay();
			}
		}, this, true);
	
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.positionOverlay()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.positionOverlay = function() {
	
	// iframe has to be displayed before it can be positioned correctly
	//this.overlay.style.display = 'block';
	
	var overlayHeight = parseInt(this.overlay.offsetHeight);
	//var overlayWidth = parseInt(this.overlay.offsetWidth);
	
	var viewportHeight = YAHOO.util.Dom.getViewportHeight();
	//var viewportWidth = YAHOO.util.Dom.getViewportWidth();
	var scrollTop = YAHOO.util.Dom.getDocumentScrollTop();
	var topCorrection = scrollTop - 0 + ((viewportHeight - overlayHeight) / 2);
	//var leftCorrection = ((viewportWidth - overlayWidth) / 2);
	
	/* positions overlays longer than page viewable height
	if (overlayHeight >= viewportHeight) {
		topCorrection = YAHOO.util.Dom.getY('main_container_wrapper');
		window.scrollBy(0, topCorrection - YAHOO.util.Dom.getDocumentScrollTop());
	} */
	
	this.overlay.style.position = 'absolute';
	this.overlay.style.zIndex = 15;
	this.overlay.style.top = topCorrection + 'px';
	//this.overlay.style.left = leftCorrection + 'px';
	
}
	
///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.setLightboxBackground()
// repositions darkness and overlay when window is resized
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.setLightboxBackground = function(){
	this.darkness.style.width = YAHOO.util.Dom.getDocumentWidth() + 'px';
	this.darkness.style.height = YAHOO.util.Dom.getDocumentHeight() + 'px';
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.closeOverlay()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.closeOverlay = function() {
	document.body.removeChild(this.darkness);
	YAHOO.util.Dom.setStyle(this.overlay, 'top', '-5000px');
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.showAttachedMssg()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.showAttachedMssg = function() {
	YAHOO.util.Dom.setStyle(this.addPhotoBtn, 'display', 'none');
	YAHOO.util.Dom.setStyle(this.photoAttachedMssg, 'display', 'block');
	this.photoAddedFlag.value = 1;
	this.formErrorPhoto.style.display = 'none';
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.copyPostToOverlayIframe()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.copyPostToOverlayIframe = function(widgetId) {
	
	// if a user submits a text-only post w/o engaging the photo overlay, the form was never initialized
	this.initFormForWidget(widgetId);
	
	this.overlayIframeBlurb.value = this.widgetIframeTextbox.value;
	this.overlayIframeWidgetId.value = this.widgetId;
	this.overlayIframeForm.submit();
	
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.renderWidgetBlogPost()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.renderWidgetBlogPost = function(post) {
	
	// this sends the post down to the widget iframe for rendering
	this.widgetIframeForm.renderWidgetBlogPost(post);
	
	// resize Iframe to fit content
	this.widgetIframeForm.resizeWidgetIframe();
	
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.resizeOverlayIframe()
// resizes the iframe window to the pixel height of the actual iframe document
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.resizeOverlayIframe = function(newHeight) {
	// the iframe sends in its height when its DOM is ready
	this.overlayIframe.height = newHeight;
}

///////////////////////////////////////////////////////////////////
// WidgetBlogPhotoOverlayMgr.resetIframeSrc()
///////////////////////////////////////////////////////////////////
WidgetBlogPhotoOverlayMgr.prototype.resetIframeSrc = function() {
	// reset the iframe source
	this.overlayIframe.src = this.overlayIframeStartSrc;
}

///////////////////////////////////////////////////////////////////
// this kicks everything off
///////////////////////////////////////////////////////////////////
/*
YAHOO.util.Event.onDOMReady(function() {
		// added a timeout on this b/c onDomReady wasn't doing the trick in IE6
		setTimeout(function(){
				wbpoMgr = new WidgetBlogPhotoOverlayMgr();
			}, 1000);
	});
*/

YAHOO.util.Event.onContentReady('lightbox', function() {
		// added a timeout on this b/c onDomReady wasn't doing the trick in IE6
		setTimeout(function(){
				wbpoMgr = new WidgetBlogPhotoOverlayMgr();
			}, 1000);
	});

