var id;
var src;

function setParentFlag(_src)
{
	$(".modalFlag").attr("src", _src);
}

$(document).ready(function() {
    $('a[class=#dialog]').click(function(e) {

        //SHOW THE MODAL AND SET SRC and HEIGHT OF THE IFAME FOR THE MODAL WINDOW
        var attributes = $(this).attr("id").split(","); //grab id text and split it
        src = attributes[0];  //holds IFrame Location ie: src
        var sHeight = attributes[1]; //holds Modal Height
        var sFlagSrc = attributes[2];  //holds Left top image flag

        if (src != "" && src != null) {
            $("#modal-iframe-content").attr("src", src);
            $("#modal-iframe-content").attr("height", sHeight);
            $(".modalFlag").attr("src", sFlagSrc);
        }
       

        $('select').hide();
        //Cancel the link behavior
        e.preventDefault();
        //Set id for content div position
        id = $(this).attr('class');
        //call rezize to position div dynamically in center of window
        doAction(id);
        //fade mask and content
        fadeMask(id);

    });

	$('#mask').click(function(){
		if(window.location != "https://www.purerxo.com/default.asp"){							  
			$('a[class=close]').click();
		}
	});
    //HIDE THE MODAL AND CANCEL THE LINK BEHAVIOR
    $('a[class=close]').click(function(e) {
		
		if(window.location == "https://www.purerxo.com/default.asp") window.location.href = "http://www.purerxo.com/"
        e.preventDefault();
        $('#mask').hide();
        $('.window').hide();
        $('select').show();

        //RESET THE SRC OF THE IFRAME FOR THE MODAL TO NOTHING
        $("#modal-iframe-content").attr("src", "");
    });

    //on window re-size adjust div content position to middle of screen
    $(window).resize(function() { doAction(id); });

    function fadeMask(id) {
        //transition effect		
        $('#mask').show();
        $('#mask').fadeTo("fast", 0.8);
        //transition effect
        $(id).fadeIn(100);
    }

    //THIS FUNCTION WILL SIZE THE MASK LAYER FOR THE MODAL
    //IT WILL ALSO DETERMINE IF BROWSER IS IEXPLORE, AND RESIZE ACCORDINGLY
    function doAction(id) {
        var browserType = navigator.userAgent.toLowerCase();

        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        var winH = $(window).height();
        var winW = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

       // if ($('.window').innerHeight() >= (winH - 100)) { $(id).css('top', (winH / 2 - $(id).height() / 2 - 15)); }
       // else { $(id).css('top', winH / 2 - $(id).height() / 2); }
		
	   if ($('.window').innerHeight() >= (winH - 100)) { $(id).css('top', 50); }
        else { $(id).css('top', 100); }
		

        $(id).css('left', winW / 2 - $(id).width() / 2);

        if (winH < 700 && $('.window').innerHeight() >= winH) { $('.window').css({ position: 'absolute' }); $(id).css('top', 2); }
        else if (browserType.indexOf("msie") == -1) {
            $('.window').css({ position: 'fixed' });
        }
    }
});     //END DOCUMENT READY 

