﻿//Josh Bennett, Whittmanhart, 2008
//This script will automaticly pick up links to external sites and add the promt to leave window/functionality

window.onload = function(){
	var preloadPop = new Image();
	preloadPop.src = "/images/popUp.png"
}
document.navigateTo = "xsdf";
document.currentOpenWindow = null;

window.addEvent('domready', function(e){

    $$('a').each(function(el){
        var text = new RegExp("^(http)[s]*(://)(www.)*.*\.{0,1}(" + document.domain + ").*", "g");

        if( el.href.indexOf('javascript') == -1 && !el.href.match(text) ){
            el.link = el.href;
            el.href = "javascript:void(0)";
            
            $(el).addEvent('mousedown', function(e2){
                if(document.currentOpenWindow)
                    closeWindow(document.currentOpenWindow)

                document.currentOpenWindow = document.warningPopup;

                document.navigateTo = this.link;

                if($(document.warningPopup).getStyle('display') != 'block')
                {
                    if (!window.ie6) {
						$(document.warningPopup).setStyles({
							'display': 'block',
							'top': ((window.getHeight() / 2) - parseInt($(document.warningPopup).getStyle('height')) / 2),
							'left': (window.getWidth() / 2) - parseInt($(document.warningPopup).getStyle('width')) / 2
						});
					}
					else {
						$(document.warningPopup).setStyles({
							'display': 'block',
							'top' : ((window.getHeight()/2) - parseInt( $(document.warningPopup).getStyle('height') )/2) + window.getScrollTop(),
							'left': (window.getWidth() / 2) - parseInt($(document.warningPopup).getStyle('width')) / 2
						});						
					}
                }
            });
/*____________prescribingInfo PDF*/ 
        }else if( el.href.indexOf('.pdf') != -1 || el.href.indexOf('prescribingInfo') != -1 ){
            el.link = el.href;
            el.href = "javascript:void(0)";

            $(el).addEvent('mousedown', function(e2){
                if(document.currentOpenWindow)
                    closeWindow(document.currentOpenWindow)
                
                document.currentOpenWindow = document.pdfPopup;
                
                document.navigateTo = this.link;

                if($(document.pdfPopup).getStyle('display') != 'block')
                {
					if (!window.ie6) {
						$(document.pdfPopup).setStyles({
							'display': 'block',
							'top': ((window.getHeight() / 2) - parseInt($(document.pdfPopup).getStyle('height')) / 2),
							'left': (window.getWidth() / 2) - parseInt($(document.pdfPopup).getStyle('width')) / 2
						});
					}else{
						$(document.pdfPopup).setStyles({
							'display': 'block',
							'top' : ((window.getHeight()/2) - parseInt( $(document.warningPopup).getStyle('height') )/2) + window.getScrollTop(),
							'left': (window.getWidth() / 2) - parseInt($(document.pdfPopup).getStyle('width')) / 2
						});
					}
                }
            });
            
        }
    });

    /*PopUP Elements_________________*/
    document.warningPopup = createPopUp(
        {
            'title' : "You Are Now Leaving BioThrax.com", 
            'bodyText' : "Clicking OK below will take you to a website to which our site's <a href=\"/privacyPolicy.aspx\" id=\"privacyPolicy\">Privacy Policy</a> does not apply. You are solely responsible for your interactions with that website." ,
            'btn1' : "OK",
            'btn2' : "BACK"
        },
        {
            'btn1OnClick' : handleBtn1Click,
            'btn2OnClick' : handleBtn2Click
        }
    );
    
    document.pdfPopup = createPopUp(
        {
            'title' : "You Are About To View A PDF", 
            'bodyText' : "The material requested requires Adobe<sup>&reg;</sup> Reader<sup>&reg;</sup>. If you do not have Adobe Reader, <a href=\"http://www.adobe.com/products/acrobat/readstep2.html\" target=\"_blank\">download it now.</a>" ,
            'btn1' : "OK",
            'btn2' : "BACK"
        },
        {
            'btn1OnClick' : handleBtn1ClickPDF,
            'btn2OnClick' : handleBtn2ClickPDF
        }
    );
    /*_________________*/
    
    $(document.warningPopup).injectAfter( $('containerWrapper') );
    $(document.pdfPopup).injectAfter( $('containerWrapper') );
    
    $$('.popUpWindow').each(function(el){
        el.makeDraggable( {'handle' : el.getElement('#popUpTitle') } );
    })
    /* ____________IE6 FIX___________*/
    if(window.ie6){

        $$('.popUpWindow').each(function(el){
			el.setStyles( {'position': 'absolute'} );
			el.setStyles( {'top' : ((window.getHeight()/2) - parseInt( $(el).getStyle('height') )/2) + window.getScrollTop() } );
        });

        window.addEvent('scroll', function(e){
             $$('.popUpWindow').each(function(el){
                el.setStyles( {'top' : ((window.getHeight()/2) - parseInt( $(el).getStyle('height') )/2) + window.getScrollTop() } );
             });
        });
    }
    
});

function closeWindow(puw){
    puw.setStyle('display', 'none');
    document.currentOpenWindow = null
}

/*_____________Event Handlers_______________*/

function handleBtn1Click (e){
        window.open( document.navigateTo );
        handleBtn2Click(e);
}

function handleBtn2Click (e){
        $(document.warningPopup).setStyles( { 'display' : 'none' } );
}

function handleBtn1ClickPDF (e){
        window.open( document.navigateTo );
        handleBtn2ClickPDF(e);
}

function handleBtn2ClickPDF (e){
        $(document.pdfPopup).setStyles( { 'display' : 'none' } );
}

/*______________________________________________________________________________________________________
                                              popUp.js

//Josh Bennett, Whittmanhart, 2008

createPopUp(contentOptions, eventHandlers); returns [HTML OBJECT]

*************************
contentOptions
*****************************
---------------------------------------------------
Param    | Accept Type |         Description       |
---------------------------------------------------
title     (HTML)           Sets the title to appear at the top
bodyText  (HTML)           sets the text which will apear in the message body
btn1      (PlaneText)      the text to appear on button #1
btn2      (PlaneText)      the text to appear on button #2


*************************
eventHandlers
*****************************
----------------------------------------------------
Param         | Accept Type |         Description   |
----------------------------------------------------
btn1OnClick     function      onClick event handler for btn1
btn2OnClick     function      onClick event handler for btn2

*/

function createPopUp(contentObj, eventsObj){

    titleText = contentObj["title"] ? contentObj["title"] : "";
    bodyText  = contentObj["bodyText"] ? contentObj["bodyText"] : "";
    btn1Text  = contentObj["btn1"] ? contentObj["btn1"] : "";
    btn2Text  = contentObj["btn2"] ? contentObj["btn2"] : "";
        
    //____Main Div Container____
    var popUpDiv = new Element('div', {
        'class' : 'popUpWindow'
    });
    
    //_____Window Title__________
    var popUpTitle = new Element('h1', {
        'id' : 'popUpTitle'
    });
    popUpTitle.setHTML(titleText);
    
    //_____Message Body Text_____
    var pupUpBodyText = new Element('p', {
        'id' : 'pupUpBodyText'
    });
    pupUpBodyText.setHTML(bodyText);
    
    //___Btns____
    var btnContainer = new Element('div', {
        'id' : 'btnContainer'
    });
    
    //___btn1
    var btn1 = new Element('a', {
        'id' : 'btn1',
        'class' : 'btn',
        'href' : 'javascript:void(0)'
    });
    btn1.addEvent('click', eventsObj["btn1OnClick"] ? eventsObj["btn1OnClick"] : function(){} )
    btn1.setText(btn1Text);
    
    //__btn2
    var btn2 = new Element('a', {
        'id' : 'btn2',
        'class' : 'btn',
        'href' : 'javascript:void(0)'
    });
    btn2.addEvent('click', eventsObj["btn2OnClick"] ? eventsObj["btn2OnClick"] : function(){} )
    btn2.setText(btn2Text);
    
    //_____Add These Elments to the popUpDiv (main container)___
    $(popUpTitle).injectInside(popUpDiv);
    pupUpBodyText.injectAfter(popUpTitle);
    btnContainer.injectAfter(pupUpBodyText);
    btn1.injectInside(btnContainer);
    btn2.injectAfter(btn1);
    
    return popUpDiv;
}