/* modal window class
 * normal modal and popup windows
 * author Samuil Gospodinov
 */



function NormalModalWindow() {
	
	var normalModalWindow = this;
	
	// Setup variables
	var normalModalWindowCaller 	= 'a.window_normal_call';		//class that is used to call the modal window
	var normalModalCurtainElement	= 'div.normal_modal_window_curtain';	//the transperant modal curtain
	var normalModalWindowHolder	= 'div.modal_window';			//the modal window holder element
	var normalModalWindowCloseBtn	= 'a.close_modal';			//the element that closes the modal window
	var pageWrapper			= '#global';				//page wrapper on which we will prepend the modal curtain
	
	
	//init user interface functions
	this.initNormalModalWindow = function(showOnLoad, targetWindow){
		
		
		//attach click event on modal caller element
		$(normalModalWindowCaller).bind('click', function(){
			target = $(this).attr('rel');
			noFade	= $(this).attr('rev');
			if(noFade == 'false'){
				closeModalTarget = '.' + target + ' ' + normalModalWindowCloseBtn;
				$(closeModalTarget).attr('rev','false');
			}
			normalModalWindow.showModalWindow(target, noFade);
		});
		
		//attach close event on close button
		$(normalModalWindowCloseBtn).bind('click', function(){
			noFade	= $(this).attr('rev');
			normalModalWindow.hideModalWindow(noFade);
		});
		
		if(showOnLoad && targetWindow){
			normalModalWindow.showModalWindow(targetWindow);
		}
		
	};
	
	/* Shows modal window
	 * returns void
	 * @param target (String) - the modal window unique class without .
	 * @param noFade (String) - if present the modal window will not have modal curtain element
	 *  */
	this.showModalWindow = function(target, noFade){
       		
       		normalModalWindow.centerModalWindow();
		
		//search for modal windows that are popUp's and close them before showing the target moda window
		$(normalModalWindowHolder + ':visible').each(function(){
			if($(this).attr('rev') == 'popup'){
				$(this).fadeOut();
			}
		})
		
		modalWindowToShow = '.' + target;
		if(!noFade){
			$(normalModalCurtainElement).fadeIn('fast');
			$(modalWindowToShow).fadeIn('fast', function(){
				
				//bind close on curtain click
				$(normalModalCurtainElement).one('click', function(){
				normalModalWindow.hideModalWindow();
				});
			});
		}else{		
			$(modalWindowToShow).attr('rev', 'popup');
			$(modalWindowToShow).fadeIn('fast', function(){
				//bind out of the modal box close event
				$('body').one('click', function(e){
					
					if(!$(e.target).is('a.modal_window_normal_call')){
						
						if((!$(e.target).is(".modal_window") && $(e.target).parents("div.modal_window").size() == 0)){
					        	normalModalWindow.hideModalWindow();
						}
					}
					
				});
			});
		}
		
		//add modal curtain if not present
		if(!noFade){
			if(!$(normalModalCurtainElement).html()){
				$(pageWrapper).after('<div class="normal_modal_window_curtain"> <!--  --> </div>');			
			}
		}
	
		//bind escape key
		$(document).one('keyup', function(e) {
	        if(e.keyCode==27){
	        	normalModalWindow.hideModalWindow();   
	        }
		});
		
		$(window).bind('resize', function(){
			normalModalWindow.centerModalWindow();
		});
		
	};
	
	/* Hides modal window
	 * returns void
	 *  */
	this.hideModalWindow = function(noFade){
		
		
		$(normalModalWindowHolder).fadeOut('fast');
		if(!noFade){
			$(normalModalCurtainElement).fadeOut('fast');
			//unbind click curtain
			$(normalModalCurtainElement).unbind('click');			
		}
		//unbind escape key
		$(document).unbind('keyup');
		$(window).unbind('resize');
		
	};
	
	/* Centers the modal window
	 * returns void
	 *  */
	this.centerModalWindow = function(){
		
		centeredX = 0;
		centeredY = 0;
		
		windowHeight = $(document).height();
		activeHeight = $(window).height();
		windowWidth = $(document).width();
		
		modalHeight = $(normalModalWindowHolder).outerHeight();
		modalWidth = $(normalModalWindowHolder).outerWidth();
		
		if(windowHeight > modalHeight){
			//centeredY = windowHeight - activeHeight + ((activeHeight - modalHeight) /2);
			centeredY = (activeHeight - modalHeight) /2;
		}else{
			centeredY = (modalHeight - windowHeight) /2;			
		}
		if(windowWidth > modalWidth){
			centeredX = (windowWidth - modalWidth) /2;
		}else{
			centeredX = (modalWidth - windowWidth) /2;			
		}

		$(normalModalWindowHolder).css('left', centeredX);
		$(normalModalWindowHolder).css('top', centeredY);
		
	}
}

var ModalWindowObject = new NormalModalWindow();

$(document).ready(function() {	
	ModalWindowObject.initNormalModalWindow();
});
