
var ReusableLBConstants = {
    containerId: "main_container_wrapper",
    darkImg: "http://family.go.com/images/lightbox_bg_sm.png",
    darkClass: "transparency",
    darkId: "lightboxDarkBg",
    lastScrollAt: 0,
    overlayId: "lightbox",
    fileUploadId: "file_photo",
    fileUploadContainerId: "file_upload_container",
    photoAttachedId: "photo_attached",
    attachPhotoId: "attach_photo",
    hideOnSuccessId: "id-reusableStartIcon-",
    showOnSuccessId: "id-reusableCompleteIcon-",
    answerId: "answer",
    windowResizeAttached: false};

var lbPrevParent = null;
var acceptExt = "png gif jpg"; // Acceptable extensions

function reusableLB_Start(idSuffix) {
    if ( ! ReusableLBConstants.windowResizeAttached ) {
        YAHOO.util.Event.addListener(window, 'resize', function() { reusableLB_ResetDarkness() }, this, true);
        ReusableLBConstants.windowResizeAttached = true;
    }
    reusableLB_Show(idSuffix);
	 YAHOO.util.Event.onAvailable('uploadedImageFormatError' , function() {
	document.getElementById('uploadedImageFormatError').style.display = "none";
	});

}
function reusableLB_AttachPhoto(idSuffix) {
    if ( reusableLB_Validate(idSuffix) ) {
        reusableLB_FlipFlop(idSuffix,"success");
        reusableLB_Position(idSuffix);
    }
}
function reusableLB_FlipFlop(idSuffix,what){
    var overlay = YAHOO.util.Dom.get(ReusableLBConstants.overlayId + idSuffix);
    var start = YAHOO.util.Dom.get(ReusableLBConstants.attachPhotoId + idSuffix);
    var success =   YAHOO.util.Dom.get(ReusableLBConstants.photoAttachedId + idSuffix);
    var hideOnSuccess =   YAHOO.util.Dom.get(ReusableLBConstants.hideOnSuccessId + idSuffix);
    var showOnSuccess =   YAHOO.util.Dom.get(ReusableLBConstants.showOnSuccessId + idSuffix);

    switch(what) {
    case "start":
        overlay.style.display ="block";
        start.style.display   ="block";
        success.style.display ="none";
		lbPrevParent = overlay.parentNode;
		document.body.appendChild(overlay);
        break;
    case "success":
        overlay.style.display ="block";
        start.style.display   ="none";
        success.style.display ="block";
		if ( hideOnSuccess ) {
            hideOnSuccess.style.display ="none";
        }
        if ( showOnSuccess ) {
            showOnSuccess.style.display ="block";
        }
        break;
    case "close":
        overlay.style.display ="none";
        start.style.display   ="block";
        success.style.display ="none";
        break;
    }
}
function reusableLB_Cancel(idSuffix) {
	var errorMessages = YAHOO.util.Dom.getElementsByClassName('error');
	for(var i = 0; i < errorMessages.length; i++) {
		errorMessages[i].style.display = 'none';
	}
	
    reusableLB_DisposeFile(idSuffix);
	reusableLB_Close(idSuffix);
}
function reusableLB_Show(idSuffix) {
    var overlay = YAHOO.util.Dom.get(ReusableLBConstants.overlayId + idSuffix);
    var darkness = YAHOO.util.Dom.get(ReusableLBConstants.darkId);

    if ( !overlay ) {
        alert("overlay ("+ReusableLBConstants.overlayId + idSuffix+") not found");
        return;
    }

    // scroll page
    var currentScroll = ReusableLBConstants.scrollLastAt = YAHOO.util.Dom.getDocumentScrollTop();
    var colAScroll = YAHOO.util.Dom.getY(ReusableLBConstants.containerId);
    window.scrollBy(0, colAScroll - currentScroll);

    reusableLB_FlipFlop(idSuffix,"start");

    //Creates (or reuses) element with transparent dark background and sets element to full size of window
    if ( !darkness ) {
        darkness = document.createElement('img');
        darkness.src =         ReusableLBConstants.darkImg;
        darkness.className =   ReusableLBConstants.darkClass;
        darkness.id =          ReusableLBConstants.darkId;
        darkness.style.position = 'absolute';
        darkness.style.zIndex = 4;
        darkness.style.top = '0px';
        darkness.style.left = '0px';
        document.body.appendChild(darkness);
    }
    darkness.style.width = YAHOO.util.Dom.getDocumentWidth() + 'px';
    darkness.style.height = YAHOO.util.Dom.getDocumentHeight() + 'px';

    reusableLB_Position(idSuffix);
}


function reusableLB_Position(idSuffix) {
    var overlay = YAHOO.util.Dom.get(ReusableLBConstants.overlayId + idSuffix);
    
    var overlayHeight = parseInt(overlay.offsetHeight);
    //var overlayWidth = parseInt(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(ReusableLBConstants.containerId);
        window.scrollBy(0, topCorrection - YAHOO.util.Dom.getDocumentScrollTop());
    }
    
    overlay.style.position = 'absolute';
    overlay.style.zIndex = 5;
    overlay.style.top = topCorrection + 'px';
    //overlay.style.left = leftCorrection + 'px';
}
function reusableLB_ResetDarkness(){
    var darkness = YAHOO.util.Dom.get(ReusableLBConstants.darkId);
    // repositions darkness and overlay when window is resized
    if ( darkness ) {
        darkness.style.width = YAHOO.util.Dom.getDocumentWidth() + 'px';
        darkness.style.height = YAHOO.util.Dom.getDocumentHeight() + 'px';
        reusableLB_Position();
    }
}

function reusableLB_Close(idSuffix) {
    var darkness = YAHOO.util.Dom.get(ReusableLBConstants.darkId);
    var overlay = YAHOO.util.Dom.get(ReusableLBConstants.overlayId + idSuffix);

    //overlay
    reusableLB_FlipFlop(idSuffix,"close");
    //darkness
    if ( darkness ) {
        document.body.removeChild(darkness);
        YAHOO.util.Dom.setStyle(overlay, 'top', '-5000px');

        //possible problem here...
        //  this expects the overlay to move around, 
        //  but it is now a simple popup.
        //overlayContainer.appendChild(overlay);
    }
    //scroll
    if ( ReusableLBConstants.scrollLastAt ) {
        window.scrollBy(0, ReusableLBConstants.scrollLastAt);
    }
	lbPrevParent.appendChild(overlay);
}

function reusableLB_DisposeFile(idSuffix) {
    var fileUpload = YAHOO.util.Dom.get(ReusableLBConstants.fileUploadId + idSuffix);
    var fileUploadContainer = YAHOO.util.Dom.get(ReusableLBConstants.fileUploadContainerId + idSuffix);
    var newEl = document.createElement('input');

    newEl.type = 'file';
    newEl.name = fileUpload.name;
    newEl.size = fileUpload.size;
    newEl.id = fileUpload.id;
    fileUploadContainer.innerHTML = '';
    fileUploadContainer.appendChild(newEl);
}

function reusableLB_Validate(idSuffix) {
    var fileUpload = YAHOO.util.Dom.get(ReusableLBConstants.fileUploadId + idSuffix);
	
	var errorMessages = YAHOO.util.Dom.getElementsByClassName('error');
	for(var i = 0; i < errorMessages.length; i++) {
		errorMessages[i].style.display = 'none';
	}
   
    var overlay = YAHOO.util.Dom.get(ReusableLBConstants.overlayId + idSuffix);

    var photo = fileUpload.value;
	
	var validForm = true;
	if (photo == '') {
		document.getElementById('imageURIRequiredError' + idSuffix).style.display = "block";
		//alert('Please select a photo.');
		fileUpload.focus();
		validForm = false;
	} else {
		/*
		 * We use lastIndexOf here instead of indexOf
		 * to account for dots that may be in the filename.
		 * We also convert to lower case to make it easier to
		 * compare to the extensions we are looking for
		 */ 
		var ext = photo.substring(photo.lastIndexOf('.')+1).toLowerCase();
		if(ext.length == 3 || ext.length == 4) // Image extensions will be either 3 or 4 chars
		{
			/*
			 * for this kind of check where there are numerous possibilities,
			 * it's better to put them in a list (see variable acceptExt at  top).
			 */
			if (acceptExt.indexOf(ext)==-1)
			{
				document.getElementById('imageURIFormatError' + idSuffix).style.display = "block";
				//alert('Invalid file type.');
				fileUpload.focus();
				validForm = false;
			}
		} else {
			/*
			 * This will run if the extension was larger than 4 or less than 3 characters
			 * or is empty which should be an optimistic catch for our Mac-loving users
			 * who can upload files without extensions
			 */
			validForm = true;
		}
	}
	
	var certifyCheckbox = document.getElementById('certify_check' + idSuffix);
	if (certifyCheckbox.checked == false) {
		document.getElementById('certify_checkRequiredError' + idSuffix).style.display = "block";
		validForm = false;
	}
			
	if(validForm == true) {
		return true;
	} else {
		return false;
	}
	
	/*
    if (photo == '') {
        alert('Please select a photo.');
        fileUpload.focus();
        return false;
    } else {
        var ext = photo.substring(photo.indexOf('.') + 1);
        if (ext != 'gif' && ext != 'GIF' && ext != 'jpg' && ext != 'JPG' && ext != 'png' && ext != 'PNG') {
            //alert('Invalid file type.');
			document.getElementById('uploadedImageFormatError').style.display = "block";
            fileUpload.focus();
            return false;
        }   
    }
    return true;
	
	*/
}

//I don't think these were for the lightbox, they seem to be for the rest of the form.
//    var answer = YAHOO.util.Dom.getY(ReusableLBConstants.answerId + idSuffix);
//    if( answer ) {
//        ReusableLBConstants.defaultText = answer.value;
//    }
//    defaultText: false,
//    showBeforeAdd: null,
//    showAfterAdd: null,
//function reusableLB_Start(idSuffix,showBeforeAdd,showAfterAdd) {
//    reusableLB_Show(idSuffix,showBeforeAdd,showAfterAdd);
//function reusableLB_Show(idSuffix,showBeforeAdd,showAfterAdd) {
//    reusableLB_ShowAll(ReusableLBConstants.showBeforeAdd = showBeforeAdd);
//    reusableLB_HideAll(ReusableLBConstants.showAfterAdd = showAfterAdd);
//function reusableLB_ShowAttachedMsg(idSuffix) {
//    // close upload panel and display confirmation panel
//    reusableLB_ShowAll(ReusableLBConstants.showAfterAdd);
//    reusableLB_HideAll(ReusableLBConstants.showBeforeAdd);
//
//    reusableLB_Position(idSuffix);
//}
//function reusableLB_ResetTextbox(idSuffix) {
//    var answer = YAHOO.util.Dom.getY(ReusableLBConstants.answerId + idSuffix);
//
//    // reset default text to textbox & change text color to grey
//    if( answer ) {
//        answer.value = ReusableLBConstants.defaultText;
//        answer.style.color = '#999';
//    }
//}
//function reusableLB_ReadyTextbox(idSuffix) {
//    var answer = YAHOO.util.Dom.getY(ReusableLBConstants.answerId + idSuffix);
//
//    // clear default text onfocus & change text color to black
//    if( answer ) {
//        answer.value = '';
//        answer.style.color = '#000';
//    }
//}
