/*-------------------------------------------------------------------------------------------------------
	This plugin will create a new modal box on the page where it is called,
	and then ajax in the content targeted in the contentURL parameter.
	
	Below is an example of how you could use this plugin:
		jQuery(#signup_link).modalbox({
			newBoxClass: 'registration_box',
			contentURL: TV_BASE_URL+'/static/registration/index.html',
			contentID: '#signup_form',
			callback: 'validateSignup()'
		});
	
	Here are the potential arguments it will take (* is required):
	- *contentURL (what page is the ajaxable content is on?)
	- contentID (what is the id of the parent of the content you want to grab?)
	- newBoxClass (what class do you want to give the new modal?)
	- newBoxID (what id do you want to give the new modal?)
	- callback (what callback function do you want to initialize after the modal is created?)
-------------------------------------------------------------------------------------------------------*/

(function(jQuery){
	jQuery.fn.modalbox = function(options){
		var settings = jQuery.extend({}, jQuery.fn.modalbox.defaults, options);
		
		return this.each(function(){
			var modalBoxIsDisplayed = jQuery('.MODAL_BOX:visible').length;//anything greater than 0 detects its presence
			var hiddenModals = jQuery('.MODAL_BOX:hidden').length;
			
			//kill any pre-existing, hidden modals in the codeÑthese will cause weird conflicts
			if (hiddenModals > 0){
				jQuery('#blackout,.blackout').remove();
			}
			
			//re-class or re-id any existing modal boxes
			if (modalBoxIsDisplayed > 0){
				//both id & class defined
		  	if (settings.newBoxID!=null && settings.newBoxClass!=null){
					jQuery('.MODAL_BOX').attr('id, '+settings.newBoxID).attr('class','MODAL_BOX '+settings.newBoxClass).html('<div class="loading"/></div>');

				//only id defined
				} else if (settings.newBoxID!=null){
					jQuery('.MODAL_BOX').attr('id, '+settings.newBoxID).attr('class','MODAL_BOX').html('<div class="loading"/></div>');
				
				//only class defined
				} else if (settings.newBoxClass!=null){
					jQuery('.MODAL_BOX').removeAttr('id').attr('class','MODAL_BOX '+settings.newBoxClass).html('<div class="loading"/></div>');
					
				//neither class or id defined
				} else {
					jQuery('.MODAL_BOX').removeAttr('id').attr('class','MODAL_BOX').html('<div class="loading"/></div>');
				}
				
			//create the MODAL_BOX div according to what has been specified
			} else {
		  	//both id & class defined
		  	if (settings.newBoxID!=null && settings.newBoxClass!=null){
					jQuery('body').append('<div id="blackout"><div id="'+settings.newBoxID+'" class="MODAL_BOX '+settings.newBoxClass+'"><div class="loading"/></div></div>');
					
				//only id defined
				} else if (settings.newBoxID!=null){
					jQuery('body').append('<div id="blackout"><div id="'+settings.newBoxID+'" class="MODAL_BOX"><div class="loading"/></div></div>');
				
				//only class defined
				} else if (settings.newBoxClass!=null){
					jQuery('body').append('<div id="blackout"><div class="MODAL_BOX '+settings.newBoxClass+'"><div class="loading"/></div></div>');
				
				//neither class or id defined
				} else {
					jQuery('body').append('<div id="blackout"><div class="MODAL_BOX"><div class="loading"/></div></div>');
				}
			}

			
			//on success, load the specified content into the new modal box
			jQuery.ajax({
			  type: 'GET',
			  url: settings.contentURL,
			  success: function(html){
			  	//hide the modal box while pulling only the right content into it
			  	jQuery('.MODAL_BOX').html(html);

					if(settings.contentID!=null){
						jQuery('.MODAL_BOX').children(':not(' +settings.contentID+ ',a.close)').hide();
			  	}
			  	
			  	//now show the modal box
			  	jQuery('.MODAL_BOX').fadeIn(350);
			  	
			  	//this must be a timeout because the content loaded above would otherwise overwrite it
			  	setTimeout(makeCloseLink,300);
			  	
			  	//prevents MPU's z-index from rising above blackout and obstructing the modal
			  	demoteMPU();
			  	
			  	//here's a callback function if it's needed
			  	if (settings.callback){
			  		setTimeout(settings.callback,300);
			  	}
			  }
			});			
			
			jQuery('div.MODAL_BOX .close, div.MODAL_BOX .close_window.button, #blackout').live('click',function(){
				killTheModalAndBlackout();				
			});
		});
		
		//don't really need this right now, as this plugin won't work unless some of these arguments are explicitly specified
		/*jQuery.fn.modalbox.defaults = {
			contentURL:'',
			contentID: '',
			newBoxClass: '',
			newBoxID: '',
			callback:''
		}*/
	}
})(jQuery);

//Custom functions
function makeCloseLink(){ 
	jQuery('div.MODAL_BOX').append('<a class="close" href="#">Close</a>'); 
}

function killTheModalAndBlackout(){ 
	jQuery('div.MODAL_BOX, #blackout').fadeOut(400,function(){
		jQuery(this).remove();
	});
}

function demoteMPU(){
	jQuery('#mpu div').each(function(){
    var potentialSuspect = parseInt( jQuery(this).css('z-index') );
    
    if (potentialSuspect > 999){
        jQuery(this).css('z-index','998');
    }
	});
}
