//ModalPop.js 
//Author: Adaptations by NVisage

$(document).ready(function(){  

    var width = $(window).width();
    // Get the full page height including the scroll area
    var height = $(document).height();

    $ektron('body').prepend("<div id='mask'></div>");
    $ektron('#mask').css('height',height); // This will be overriden to incorporate the height of the modal winwo in modalPop()
    $ektron('#mask').css('width',width);
           
    $ektron("a[href$='?dialog=modal']").each(function(index) {
        var url = $ektron(this).attr('href');

        url = url.replace('#', '');        
        url = url.replace('?dialog=modal', ''); 

        $ektron(this).attr('href', "javascript:populateUrlIntoModalDialogDiv('" + url + "');"); // .Net uses onclick
    });
    $ektron("a[href$='&dialog=modal']").each(function(index) {
        var url = $ektron(this).attr('href');

        url = url.replace('#', '');        
        url = url.replace('&dialog=modal', ''); 
        $ektron(this).attr('href', "javascript:populateUrlIntoModalDialogDiv('" + url + "');"); // .Net uses onclick
    });
           
    $ektron('a[href*="?dialog=popup"]').each(function(index) {
        var url = $ektron(this).attr('href');
        var width = "1000";
        var height = "500";
        url = url.replace('#', '');
        url = url.replace('?dialog=popup', ''); 

        $ektron(this).attr('onClick', "javascript: return navigableWindow('" + url + "','" + width + "','" + height + "');"); // .Net uses onclick
        $ektron(this).attr('href', "#"); // .Net uses onclick
        $ektron(this).attr('target', "_blank"); // .Net uses onclick
    });  
    $ektron('a[href*="&dialog=popup"]').each(function(index) {
        var url = $ektron(this).attr('href');
        var width = "1000";
        var height = "500";
        url = url.replace('#', '');
        url = url.replace('&dialog=popup', ''); 

        $ektron(this).attr('onClick', "javascript: return navigableWindow('" + url + "','" + width + "','" + height + "');"); // .Net uses onclick
        $ektron(this).attr('href', "#"); // .Net uses onclick
        $ektron(this).attr('target', "_blank"); // .Net uses onclick
    });     
});


function navigableWindow(sURL, width, height){
	var newWin = window.open(sURL, 'navigableWindow', 'width=' + width + ',height=' + height + ',scrollbars,resizable');
	if (newWin.focus) newWin.focus();
	return false;
}

function populateUrlIntoModalDialogDiv(modalUrl) {

    var modalDiv = $ektron("#ajaxPageHtmlHere");
    var modalUrlViaProxy = "/utilitynavigation/proxy.aspx?url=" + modalUrl;
    var lastUrlLoadedValue = modalUrl;
    var isAjaxRequired = true;
       
    // Inspect whether the last modal link/url clicked is the same as the current modal url: This saves a redundant ajax call
    if (0 < modalDiv.html().length)
    {            
        //alert('val(): ' + $("#lastUrlLoaded").val().indexOf(lastUrlLoadedValue));
        if (-1 < $ektron("#lastUrlLoaded").val().indexOf(lastUrlLoadedValue))
        {
            isAjaxRequired = false;
        }
    }    
    
    if (isAjaxRequired)
    {
        // Inform the end user that the request has initiated
        modalDiv.html("<div class=\"modal_one_moment_please\"><p align=\"center\">One moment please.<br /><br /><br /><img src=\"/images/gavi-loader.gif\" alt=\"One moment please\" /></p></div>");
        $ektron().modalpop({ speed: 700 }, '#window1');       
        
        // RM: load an Iframe instead of pull content in from a script - used for forms etc
        if (modalUrl.substring(1,25) == "feeds/rss/subscribe.aspx") {
            // using css on the modalUrl script to hide irrelevant elements.
            
            modalDiv.html("<IFRAME id=\"frame1\" src=\"" + modalUrl + "\" style=\"width:100%;height:450px;overflow:auto;\" ></IFRAME>");
            
        }else{
        
            //$ektron.ajax({ url: "http://localhost/modaltest.htm", context: document.body, success: function(data){
            $ektron.ajax({ url: modalUrlViaProxy, context: document.body, success: function(data){
            
                //alert(data);
                //alert('ajax call success');   
                modalDiv.html(data);
                
                // Initiate any js that was not initated on document.ready : as it did not exist on the page yet.
                fontSizing();  // viewing option links that may have loaded: linkDecorate.js
                setRhsImmunisationInTheNews(5, ".inthenews .rhs_carousel"); // immunisation in the news : carousel_rhs.js

                $ektron("#lastUrlLoaded").val(lastUrlLoadedValue);

                $ektron().modalpop({ speed: 700 }, '#window1');

            } 
            });   
        }     
    }
    else
    {
        $ektron().modalpop({ speed: 700 }, '#window1'); 
    }  
    
    // Nothing can be run here as the modalpop returns false
}


//ModalPop.js
//Author: Owain Lewis
//Author URL: www.Owainlewis.com
//Simple Modal Dialog for jQuery
(function() {

    jQuery.fn.modalpop = function(options, modalDialogId) {
        var defaults = {
            speed: 500,
            center: false,
            IsBackgroundClickable: true
        };
        var options = $ektron.extend(defaults, options);
        var width = $ektron(window).width();
        var height = $ektron(document).height(); // Get the full page height including the scroll area
        //alert(modalDialogId);
        var id = modalDialogId; // $this.attr('href');

        // Get the window height and width
        var winH = $ektron(window).height();
        var winW = width;
        //alert('id: ' + id + ',winH: ' + winH + ', winW: ' + winW);
        // Set the popup window to center if required
        if (defaults.center == true) {
            $ektron(id).css('top', winH / 2 - $ektron(id).height() / 2);
        } else {
            //$ektron(id).css('top', 200);
            // add by RM: bring wondow always to scroll top in staed of document top
            $ektron(id).css('top', ($ektron(this).scrollTop() + 200) + "px");
        }
        $ektron(id).css('left', winW / 2 - $ektron(id).width() / 2);

        //var revisedHeight = $(modalDialogId).height() + 250;
        var revisedHeight = $ektron(modalDialogId).height() + 20;
        //alert($ektron('#mask').css('height'));
        //alert(revisedHeight);
        //alert("combined:: " + (revisedHeight + ($(this).scrollTop() + 200)));
        //alert("mask height:: " + $ektron('#mask').css('height').replace('px',''));

        if ((revisedHeight + ($ektron(this).scrollTop() + 200)) > $ektron('#mask').css('height').replace('px', '')) {

            //alert("reseting height to :: " + (revisedHeight + ($(this).scrollTop() + 250)));

            $ektron('#mask').css('height', (revisedHeight + ($ektron(this).scrollTop() + 250)) + "px");

        } else {
            //alert("false");
        }
        $ektron('#mask').fadeIn(defaults.speed);
        $ektron(id).fadeIn(defaults.speed);

        $ektron('#mask').click(function() {
            if (!defaults.IsBackgroundClickable) {
                return false;
            }

            $ektron(this).fadeOut(defaults.speed);
            $ektron(id).fadeOut(defaults.speed);
        });

        $ektron('.close').click(function() {
            $ektron('#mask').fadeOut(defaults.speed);
            $ektron(id).fadeOut(defaults.speed);
        });
        // set the focus::
        $ektron(modalDialogId).focus();
        return false;
    };

    jQuery.fn.CloseModal = function(modalDialogId) {
        $ektron('#mask').fadeOut();
        $ektron(modalDialogId).fadeOut();
    }

})(jQuery);

// added by RM to correct background resize issue::

$(window).resize(function() {
    if ($('#mask').length > 0) {


        var height = $(document).height();
        var width = $(document).width();

        $('#mask').css('width', width)
        $('#mask').css('height', height)

        var winH = $(window).height();
        var winW = $(window).width();
        var id = "#window1"; // modal window id

        if ($(id).is(":visible")) {
            // Set the popup window to center if required
            //alert("reposition: " + id);
            //$(id).css('top', winH / 2 - $(id).height() / 2);
            $(id).css('top', ($(this).scrollTop() + 200) + "px");
            $(id).css('left', winW / 2 - $(id).width() / 2);
        }

    }
});
// code below needs amend as it expands the document height
/*$(window).scroll(function() {
    if ($('#mask').length > 0) {
        var id = "#window1";
        if (($(this).scrollTop() + $(id).height()) < ($(document).height())) {
            $(id).css('top', ($(this).scrollTop() + 200) + "px");
        }
    }
});*/


(function(){

jQuery.fn.modalpopOriginal = function(options){

	var defaults = {
		speed:500,
		center:false
	};
	
	var options = $.extend(defaults, options);
	var width = $(window).width();
	//Get the full page height including the scroll area
	var height = $(document).height();
	jQuery('body').prepend("<div id='mask'></div>");
	jQuery('#mask').css('height',height);
	jQuery('#mask').css('width',width);

    //alert('height: ' + height + ", width: " + width);

	return this.each(function(){
	
		jQuery(this).click(function(){
			$this = jQuery(this);
			var id = $this.attr('href');
			//Get the window height and width
			var winH = height;
			var winW = width;
			
			//Set the popup window to center if required
			if (defaults.center == true){
				$(id).css('top',  winH/2-$(id).height()/2);
			}else{
				$(id).css('top',  200);
			}
			$(id).css('left', winW/2-$(id).width()/2);
			jQuery('#mask').fadeIn(defaults.speed);
			jQuery(id).fadeIn(defaults.speed);
			return false;
		});
		
		jQuery('#mask').click(function(){
		    //alert('modalpopOriginal close');
			jQuery(this).fadeOut(defaults.speed);
			jQuery('.window').fadeOut(defaults.speed);
		});
		
		jQuery('.close').click(function(){
			jQuery(this).fadeOut(defaults.speed);
			jQuery('.window').fadeOut(defaults.speed);
		});
		
	});
};

})(jQuery);
