
/* mmh/modal.js */

 /**
 * modals.js
 *
 * This file contains all of the code to generate the modal objects
 * note: (getElementsByClassName) or something....we should set up a way to do this automatically for scalability.
 */

if (typeof modal == 'undefined') {
  var modal = {};
}

Object.extend(modal, {

  __waiter: null,

  choice: function(url, options, choicef) {
    var m;
    Object.extend(options, {
      overlayCloseOnClick: false,
      afterClose: function() {
        var c = m.__choice;
        delete m.__choice;
        choicef(c);
      }
    });
    m = new Control.Modal(false, options);
   	m.mode = 'ajax';
    m.href = url;
    m.open();
  },
  
  select: function(c) {
    Control.Modal.current.__choice = c;
    Control.Modal.close();
  },
 
  close: function() {
    modal.select(null);
  },
 
  wait: function(choicef) {
  	modal.__waiter = choicef;
  },
  
  notify: function(c) {
    modal.__waiter(c);
    delete modal.__waiter;
  }
 
});

function addModalListeners(){}

/*
if( $('pageOverlays') ){
	// now lets check to grab all of the children
	// in the pageOverlays element
		var  children = Element.immediateDescendants('pageOverlays');
		for(var i =0; i< children.length;i++){
			//alert(children[i].id);
			//Event.observe($(children[i].id), 'click', function(){ alert("clicked")} );
			// set listeners
			//alert(children[i].id);
				//Event.observe(children[i].id +"Close", 'click', closeModal);
			// we can set the display:none property here...
				//$(children[i]).hide();
			// then we can create the objects here...but we have to return a public interface!!!
			//alert(children[i].getDimensions().height);	
		}
}
}

/**
  * set modal close listener

  
 function setModalCloseListener(e){
	var modal = Event.element(e).id;
	if( Event.element(e).tagName == 'IMG') modal = Event.element(e).up().id;
	var closeModalEl = modal.replace('Link', 'Close');
	Event.observe($(closeModalEl), 'click', closeModal);
 }
 */
 
 /**
  * close the modal
  */
 function closeModal(e){
	//alert("element");
	var id = Event.element(e);
	//eval(e +".close();");
	
 }
	 
	 

//Event.observe(window, 'load', addModalListeners);

// end testing....do not touch above!
	// all of the variables for the modal window objects.
		var whatsThisModalObj, addRoomModalObj, addSpaceModalObj, addItemModalObj, addItemWarningModalObj, addPhotoModalObj, editPhotoModalObj, assignRoomModalObj, warningModalObj, learnMoreModalObj;
		
		
		var whatsHomeProfileModalObj; //jpg
		var loginModalObj;
		var assignRoom2ModalObj;
		var contactCustomerSupportModalObj;
		var contactSupportSuccessModalObj;
		var whatsThis2ModalObj;
		var whatsHomeProfile2ModalObj;
		var assignRoom3ModalObj;
		var removeAreaModalObj;
		var homeProfileDemoObj;
		var assignChangeItemModalObj;
		var planEligibleModalObj;
		var challengeOneModalObj;
		
		
	// browser detection
    	var browser=navigator.appName
	// modal Array (not sure where this gets set)
		var modalArray = [];
		var itemCount;
		var addRoomModalSummaryObj,addSpaceModalSummaryObj;
		
Event.onReady(function(){
    if (browser != 'Microsoft Internet Explorer') {
		  for (var i=0;i<modalArray.length;i++) {
				if (modalArray[i] == 'whatsThis') {
			    	whatsThisModalObj = new Control.Modal('whatsThisModalLink',{opacity: 0, position: 'relative', offsetTop: 15, offsetLeft: 65, width: 220});
				}
				if (modalArray[i] == 'whatsThis2') {
			    	whatsThis2ModalObj = new Control.Modal('whatsThis2ModalLink',{opacity: 0, position: 'relative', offsetTop: -170, offsetLeft: 5, width: 220});
				}
				if (modalArray[i] == 'challengeone') {
			    	purchaseHistoryChallenge1ModalObj = new Control.Modal('purchaseHistoryChallenge1ModalLink',{
			    	        opacity: 0.8, 
			    	        overlayCloseOnClick: false, 
			    	        fade: true, 
			    	        fadeDuration:0.5, 
			    	        afterOpen: function() {	 
							    //showChallengeOne();
						    }
			    	  }
			    	 );
				}				
				
				if (modalArray[i] == 'addRoom') {
			    	addRoomModalObj = new Control.Modal('addRoomModalLink',{
			    	   opacity: 0, 
			    	   position: 'relative', 
			    	   offsetTop: 0, 
			    	   offsetLeft: 100, 
			    	   width: 220, 
			    	   overlayCloseOnClick: false,
			    	   afterOpen: function() {				    		
				    		sendOmnitureToolUsage('Home Profile:Left Menu:Add Room clicked');				    	
				    	}	
			       });
				}
				
				if (modalArray[i] == 'addRoomSummary') {
			    	addRoomModalSummaryObj = new Control.Modal('addRoomModalLinkSummary',{opacity: 0, position: 'relative', offsetTop: 0, offsetLeft: 70, width: 220, overlayCloseOnClick: false});
				}

				if (modalArray[i] == 'addSpace') {
			    	addSpaceModalObj = new Control.Modal('addSpaceModalLink',{
			    	    opacity: 0, 
			    	    position: 'relative', 
			    	    offsetTop: 0, 
			    	    offsetLeft: 100, 
			    	    width: 220, 
			    	    overlayCloseOnClick: false,
			    	    afterOpen: function() {				    		
				    		sendOmnitureToolUsage('Home Profile:Left Menu:Add Space clicked');
				    	}	
			    	});
				}
				
				if (modalArray[i] == 'addSpaceSummary') {
			    	addSpaceModalSummaryObj = new Control.Modal('addSpaceModalLinkSummary',{opacity: 0, position: 'relative', offsetTop: 0, offsetLeft: 70, width: 220, overlayCloseOnClick: false});
				}
				if (modalArray[i] == 'homeProfileDemo') {
			    	homeProfileDemoObj = new Control.Modal('viewDemoModalLink',{
				    	opacity: 0.8,
				    	position: 'relative',
				    	offsetTop: -285,
				    	offsetLeft: -200,
				    	width: 220,
				    	overlayCloseOnClick: false,
				    	afterOpen: function() {
				    		runDemo();
				    	}
			    	});
				}
								
				if (modalArray[i] == 'addItem') {
					$('addItemModalLink').href = '#addItemModal';
				    addItemModalObj = new Control.Modal('addItemModalLink', {
						opacity: 0.8,
						overlayCloseOnClick: false, 
						fade: true,
						fadeDuration:0.5,
						position: 'relative',
						offsetTop: 100,
						offsetLeft: 220,
				        afterOpen: function() {
							if(document.documentElement.scrollTop >= 172){
							$('leftNavAreas').scrollTo();
							}	
							showAddItemView();
							//$('centerCol').scrollTo();
							
						}
					});
					addItemModalObj.element = $('main_content');
				}
				if (modalArray[i] == 'addPhoto') {
			    addPhotoModalObj = new Control.Modal('addPhotoModalLink',{opacity: 0, position: 'relative', offsetTop: 12, offsetLeft: 40, width: 265, overlayCloseOnClick: false});
				}
				if (modalArray[i] == 'editPhoto') {
			    editPhotoModalObj = new Control.Modal('editPhotoModalLink',{opacity: 0, position: 'relative', offsetTop: 12, offsetLeft: 40, width: 265, overlayCloseOnClick: false});
				}
				if (modalArray[i] == 'assignRoom') {
			    assignRoomModalObj = new Control.Modal('assignRoomModalLink',{opacity: 0, position: 'relative', offsetTop: 17, offsetLeft: 42, overlayCloseOnClick: false});
				}
				
				if (modalArray[i] == 'assignRoom2Modal') {
			    	assignRoom2ModalObj = new Control.Modal('assignRoom2ModalLink',{opacity: 0, position: 'relative', offsetTop: 10, offsetLeft: 90, overlayCloseOnClick: false});
				}
				
				if (modalArray[i] == 'assignRoom3') {
			   		 assignRoom3ModalObj = new Control.Modal('assignRoom3ModalLink',{opacity: 0, position: 'relative', offsetTop: -110, offsetLeft: -240, overlayCloseOnClick: false});
				}
				
				if (modalArray[i] == 'warning') {
					warningModalObj = new Control.Modal('warningModalLink',{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				if (modalArray[i] == 'warningItem') {
					warningItemModalObj = new Control.Modal('warningItemModalLink',{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				if (modalArray[i] == 'learnMore') {
			    	learnMoreModalObj = new Control.Modal('learnMoreModalLink',{opacity: 0, position: 'relative', offsetTop: -120, offsetLeft: 80});
				}			
				
				//<!-- whatsAHomeProfile -->
				if (modalArray[i] == 'whatsHomeProfile') {
			    	whatsHomeProfileModalObj = new Control.Modal('whatsHomeProfileModalLink',{opacity: 0, position: 'relative', offsetTop: -285, offsetLeft: 130,width:220});
				}
				
				//<!-- whats a home profile -->
				if (modalArray[i] == 'whatsHomeProfile2') {
			    	whatsHomeProfile2ModalObj = new Control.Modal('whatsHomeProfile2ModalLink',{opacity: 0, position: 'relative', offsetTop: -260, offsetLeft: -20, width: 220});
				}
				
				//<!-- login -->
				if (modalArray[i] == 'login') {
			    	loginModalObj = new Control.Modal('loginModalLink',{opacity: 0, position: 'relative', offsetTop: -260, offsetLeft: -270, width:230, overlayCloseOnClick: false});
				}
				
				//<!-- contact customer support -->
				if (modalArray[i] == 'contactCustomerSupport') {
			    	contactCustomerSupportModalObj = new Control.Modal('contactCustomerSupportModalLink',{opacity: 0, position: 'relative', offsetTop: -380, offsetLeft: -310, width:300, overlayCloseOnClick: false});
				}
				
				//<!-- contact customer support success-->
				if (modalArray[i] == 'contactSupportSuccess') {
			    	contactSupportSuccessModalObj = new Control.Modal('contactSupportSuccessModalLink',{opacity: 0, position: 'relative', offsetTop: -180, offsetLeft: -310, width:300, overlayCloseOnClick: false});
				}
				
				
				//<!-- remove warning modal -->
				if (modalArray[i] == 'removeWarning') {
					removeWarningModalObj = new Control.Modal('removeWarningModalLink',{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				
				if (modalArray[i] == 'removeArea') {
					removeAreaModalObj = new Control.Modal('removeAreaModalLink', {opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				
				if (modalArray[i] == 'assignChangeItemLoc') {
                    assignChangeItemModalObj = new Control.Modal('assignChangeItemModalLink', {opacity: 0, position: 'relative',offsetTop: 10, offsetLeft: 60, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
                }
				
				if (modalArray[i] == 'planEligibleModal') {
                   planEligibleModalObj = new Control.Modal('planEligibleModalLink', {opacity: 0, position: 'relative',offsetTop: 10, offsetLeft: 60, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
                }
				
				
				
			}
			applyGalleryModal();
		} else {
			for (var i=0;i<modalArray.length;i++) {
				if (modalArray[i] == 'whatsThis') {
			    whatsThisModalObj = new Control.Modal('whatsThisModalLink',{opacity: 0, position: 'relative', offsetTop: 15, offsetLeft: 65, width: 220});
				}
				
				if (modalArray[i] == 'whatsThis2') {
			    	whatsThis2ModalObj = new Control.Modal('whatsThis2ModalLink',{opacity: 0, position: 'relative', offsetTop: -150, offsetLeft: 60, width: 220});
				}
				
				if (modalArray[i] == 'challengeone') {
			    	purchaseHistoryChallenge1ModalObj = new Control.Modal('purchaseHistoryChallenge1ModalLink',{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}				
				
				if (modalArray[i] == 'addRoom') {
			    addRoomModalObj = new Control.Modal('addRoomModalLink',{
			            opacity: 0, 
			            position: 'relative', 
			            offsetTop: 12, 
			            offsetLeft: 100, 
			            width: 220, 
			            overlayCloseOnClick: false,
			            afterOpen: function() {				    		
				    		sendOmnitureToolUsage('Home Profile:Left Menu:Add Room clicked');				    	
				    	}	
			        });
				}

				if (modalArray[i] == 'addSpace') {
			    addSpaceModalObj = new Control.Modal('addSpaceModalLink',{
			            opacity: 0, 
			            position: 'relative', 
			            offsetTop: 12, 
			            offsetLeft: 100, 
			            width: 220, 
			            overlayCloseOnClick: false,
			            afterOpen: function() {				    		
				    		sendOmnitureToolUsage('Home Profile:Left Menu:Add Space clicked');
				    	}	
			        });
				}
				
				if (modalArray[i] == 'addRoomSummary') {
			    addRoomModalSummaryObj = new Control.Modal('addRoomModalLinkSummary',{opacity: 0, position: 'relative', offsetTop: 2, offsetLeft: 70, width: 220, overlayCloseOnClick: false});
				}
				
				if (modalArray[i] == 'addSpaceSummary') {
			    addSpaceModalSummaryObj = new Control.Modal('addSpaceModalLinkSummary',{opacity: 0, position: 'relative', offsetTop: 2, offsetLeft: 70, width: 220, overlayCloseOnClick: false});
				}
				
				if (modalArray[i] == 'homeProfileDemo') {
			    	homeProfileDemoObj = new Control.Modal('viewDemoModalLink',{
				    	opacity: 0.8,
				    	position: 'relative',
				    	offsetTop: -285,
				    	offsetLeft: -200,
				    	width: 220,
				    	overlayCloseOnClick: false,
				    	afterOpen: function() {
				    		runDemo();
				    	}
			    	});
				}
				
				if (modalArray[i] == 'addItem') {
					$('addItemModalLink').href = '#addItemModal';
			    	addItemModalObj = new Control.Modal('addItemModalLink', {
						opacity: 0.8, overlayCloseOnClick: false,
						position: 'relative',
						offsetTop: 100,
						offsetLeft: 220,
						afterOpen: function() {	
						if(document.body.scrollTop >= 172 || document.documentElement.scrollTop >= 172){
							$('leftNavAreas').scrollTo();
							}					    
							showAddItemView();				
						},
						fade: true, fadeDuration:0.5});
						addItemModalObj.element = $('main_content');
				}
				if (modalArray[i] == 'addPhoto') {
			    	addPhotoModalObj = new Control.Modal('addPhotoModalLink',{opacity: 0, position: 'relative', offsetTop: 25, offsetLeft: 40,  overlayCloseOnClick: false});
				}
				if (modalArray[i] == 'editPhoto') {
			    	editPhotoModalObj = new Control.Modal('editPhotoModalLink',{opacity: 0, position: 'relative', offsetTop: 25, offsetLeft: 40, overlayCloseOnClick: false});
				}	
				if (modalArray[i] == 'assignRoom') {
					assignRoomModalObj = new Control.Modal('assignRoomModalLink',{opacity: 0, position: 'relative', offsetTop: 17, offsetLeft: 42, overlayCloseOnClick: false});
				}
				
				if (modalArray[i] == 'assignRoom2Modal') {
			   		 assignRoom2ModalObj = new Control.Modal('assignRoom2ModalLink',{opacity: 0, position: 'relative', offsetTop: 20, offsetLeft: 90, overlayCloseOnClick: false});
				}
				
				if (modalArray[i] == 'assignRoom3') {
			   		 assignRoom3ModalObj = new Control.Modal('assignRoom3ModalLink',{opacity: 0, position: 'relative', offsetTop: -100, offsetLeft: -240, overlayCloseOnClick: false});
				}
				
				
				if (modalArray[i] == 'warning') {
					warningModalObj = new Control.Modal('warningModalLink',{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				if (modalArray[i] == 'warningItem') {
					warningItemModalObj = new Control.Modal('warningItemModalLink',{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				if (modalArray[i] == 'learnMore') {
			    	learnMoreModalObj = new Control.Modal('learnMoreModalLink',{opacity: 0, position: 'relative', offsetTop: -110, offsetLeft: 85});
				}	
				
				//<!-- whats a home profile -->
				if (modalArray[i] == 'whatsHomeProfile') {
			    	whatsHomeProfileModalObj = new Control.Modal('whatsHomeProfileModalLink',{opacity: 0, position: 'relative', offsetTop: -260, offsetLeft: 80, width: 220});
				}
				
				//<!-- whats a home profile -->
				if (modalArray[i] == 'whatsHomeProfile2') {
			    	whatsHomeProfile2ModalObj = new Control.Modal('whatsHomeProfile2ModalLink',{opacity: 0, position: 'relative', offsetTop: -250, offsetLeft: 130, width: 220});
				}
				
				//<!-- login -->
				if (modalArray[i] == 'login') {
			    	loginModalObj = new Control.Modal('loginModalLink',{opacity: 0, position: 'relative', offsetTop: -250, offsetLeft: -270, width:230, overlayCloseOnClick: false});
				}
				
				//<!-- contact customer support -->
				if (modalArray[i] == 'contactCustomerSupport') {
			    	contactCustomerSupportModalObj = new Control.Modal('contactCustomerSupportModalLink',{opacity: 0, position: 'relative', offsetTop: -390, offsetLeft: -310, width:300, overlayCloseOnClick: false});
				}
				
				//<!-- contact customer support success-->
				if (modalArray[i] == 'contactSupportSuccess') {
			    	contactSupportSuccessModalObj = new Control.Modal('contactSupportSuccessModalLink',{opacity: 0, position: 'relative', offsetTop: -195, offsetLeft: -262, width:300, overlayCloseOnClick: false});
				}
				
				//<!-- remove warning modal -->
				if (modalArray[i] == 'removeWarning') {
					removeWarningModalObj = new Control.Modal('removeWarningModalLink',{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				
				if (modalArray[i] == 'removeArea') {
					removeAreaModalObj = new Control.Modal('removeAreaModalLink', {opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
				
				if (modalArray[i] == 'planEligibleModal') {
					planEligibleModalObj = new Control.Modal('planEligibleModalLink', {opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
				}
          
	        
          
          if (modalArray[i] == 'assignChangeItemLoc') {
              assignChangeItemModalObj = new Control.Modal('assignChangeItemModalLink', {opacity: 0, position: 'relative', offsetTop: 10, offsetLeft: 60, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
          }

				
      		}
	      	applyGalleryModal();
   }
});	

function applyGalleryModal() {	
		for (var j=0;j<itemCount;j++) {
 			  new Control.Modal('item' + j + 'ModalLink',{hover:true, position: 'relative', offsetTop: -12, offsetLeft: 67});
		}
}

/* Added for Plum */
var contactSupportSuccess2ModalObj, contactSupportSuccess2ModalObj;

Event.onReady(function(){	
  for (var i=0;i<modalArray.length;i++) {
    //Positioned Elements (2 copies exist for each because of differences in relative positioning between IE and all other browsers)
    if (browser !== 'Microsoft Internet Explorer') {	  
			if (modalArray[i] == 'contactCustomerSupport2') {
				new Control.Modal('contactCustomerSupport2ModalLink',{opacity: 0, position: 'relative', offsetTop: -490, offsetLeft: 60, width:300, overlayCloseOnClick: false});
			}
			if (modalArray[i] == 'contactSupportSuccess2') {
				contactSupportSuccess2ModalObj = new Control.Modal('contactSupportSuccess2ModalLink',{opacity: 0, position: 'relative', offsetTop: -170, offsetLeft: 60, width:300, overlayCloseOnClick: false});
			}
		} else {
			if (modalArray[i] == 'contactCustomerSupport2') {
				new Control.Modal('contactCustomerSupport2ModalLink',{opacity: 0, position: 'relative', offsetTop: -490, offsetLeft: 60, width:300, overlayCloseOnClick: false});
			}
			if (modalArray[i] == 'contactSupportSuccess2') {
				contactSupportSuccess2ModalObj = new Control.Modal('contactSupportSuccess2ModalLink',{opacity: 0, position: 'relative', offsetTop: -170, offsetLeft: 120, width:300, overlayCloseOnClick: false});
			}
		}
		if (modalArray[i] == 'plumPreview') {
			plumPreviewModalObj = new Control.Modal('plumPreviewModalLink',{opacity: 0.8, overlayCloseOnClick: true, fade: true, fadeDuration:0.5});
		}		
  }
});


function initializeItemModal (itemNumber) {
  var itemModalObjLink;
	itemModalObjLink= 'item' + itemNumber + 'ModalLink';
	if ((browser == 'Microsoft Internet Explorer') && ((j <= 2) && (j >= 4))) {
		new Control.Modal(itemModalObjLink,{hover:true, position: 'relative', offsetTop: -37, offsetLeft: 67, beforeOpen: disableSelect('sortSelect'), afterClose: enableSelect('sortSelect') });
	} else {
		new Control.Modal(itemModalObjLink,{hover:true, position: 'relative', offsetTop: -37, offsetLeft: 67});
	}
}

function initializeCenteredModal (modalName) {
	var modalObjLink;
	modalObjLink= modalName + 'ModalLink';
	
	return new Control.Modal(modalObjLink,{opacity: 0.8, overlayCloseOnClick: false, fade: true, fadeDuration:0.5});
}
