/* 	SPLASH PAGE SCRIPT ADDED 10/27/2011 
	REPLACED THE POPUP PREVIOUSLY USED FOR THE SURVEY PROMPT */

if (typeof CDC == "undefined") var CDC = new Object();
if (typeof CDC.Survey == "undefined") CDC.Survey = new Object();
if (typeof CDC.Survey.HomePage == "undefined") CDC.Survey.HomePage = new Object();
CDC.Survey.HomePage = function() {
}
CDC.Survey.HomePage.prototype = {

	TESTING : false,
	SURVEY_URL : 'http://www.surveymonkey.com/s/F625SBJ',
	
	popupHomepageSurvey : null,
	
	showSurveyWindow : function () {
		$.ajax({
			dataType: 'html',
				url: '/nchs/JS/homePageSurvey.html',										  
			cache: false,
			context: document.body,
			success: function(data){
				$('<div class="wrapper" />').html(data).appendTo($('<div id="CSS-survey" class="survey-splash" />').appendTo($('div#content')));
			}
		});
		$('#CSS-survey').css('visibility', 'visible').fadeIn('slow');
	},

	hideSurveyWindow : function () {
		$('#CSS-survey').fadeOut('slow').css('visibility', 'hidden');
	},

	handleSurveyClick : function () {
		this.hideSurveyWindow();
		this.popupHomepageSurvey = window.open(this.SURVEY_URL, '', 'resizable=yes,menubar=no,scrollbars=yes,toolbar=no,height=700,width=800,left=' + (screen.availWidth / 2 - 375) + ',top=' + (screen.availHeight / 2 - 225));
		if (!this.popupHomepageSurvey.opener) this.popupHomepageSurvey.opener = window;
		if (this.popupHomepageSurvey) this.popupHomepageSurvey.focus();
	}
		
}


/*Logic for popup 
 *•	On some # (_ConsecutiveHitsCount)  consecutive hits to web page “that contains popup script” of the same user, 
 *  set a flag. Where (_ConsecutiveHitsCount) is set by web page desinger/analyst;
 *
 *•	On some # (_VisiterCounter) of visits by any user to web page “that contains popup script” set a flag.
 *    Where (_VisiterCounter) is set by web page designer/analyst;
 *
 *•	All flags expire (deleted) after n (_WaiteForConsecutiveHits) minutes of no activity on page
 * 
 *When popup is displayed;
 *•	Popup dialog is displayed when consecutive hits by same user is more than or equal to _ConsecutiveHitsCount and when the visitor counter equals # of visitors
 */    
    
    /*
     * Anne, you can modify the following varables with out breaking the script;
     * _ConsecutiveHitsCount, _VisiterCounter, _TestMode,_PopupWidth, _PopupHieght
     */

    /* 
     * Number of consecutive hits this browser has had inside nchs domain, where this script is present.  Count 
     * starts at 0.  So for 3 consective hits set 
     * _ConsecutiveHitsCount to 2
     */
    var _ConsecutiveHitsCount = 2;

    /*
     * Number of visiters to hit this site before
     * taking action.  This number specifies the cieling of a random number generator.
     * VisiterCount is simulated.
     */
    var _VisiterCounter = 3;

    /*
     *Popup Width in Pixels
     */
    var _PopupWidth=400;

    /*
     *Popup Hieght in Pixels
     */
    var _PopupHieght=300;
    
    /*
     *How long to waite for consecutive hits in minutes
     */
     var _WaiteForConsecutiveHits=10;
     
      
    /*
    * If you want to test the 2 parameters to see that they are working (_VisiterCounter,_ConsecutiveHitsCount)
    * set the following value to true
    */
    var _TestMode=false;
    
/*-------------do not modify after this point-----------------*/
     /*
      *Time to waite before dropping the session (aka time between window popups
      *when all other criteria for popup has been met
      */
      var MinutesTillNextPopupThisSession=20;
      
      var TempNCHSCheckPopupBlocked=false;

    
    /*
     * if popup comes up, this is how long to waite befor using popup again
     */
    var PopupBlocked_CookieExpiration;
    var IsPopupBlocked;
    {
        var NCHSCheckPopupBlocked=getCookie("NCHSCheckPopupBlocked");
        IsPopupBlocked=Boolean(NCHSCheckPopupBlocked);
        if(NCHSCheckPopupBlocked==null){
            PopupBlocked_CookieExpiration=new Date();
            PopupBlocked_CookieExpiration.setTime(PopupBlocked_CookieExpiration.getTime() + (1000*60*MinutesTillNextPopupThisSession) ); 
        }
    }
    function setCookie (name, value, expires) { 
        if (!expires) expires = new Date(); 
        document.cookie = name + "=" + escape (value) + "; expires=" + 
        expires.toString() +  "; path=/"; 
    } 
    function getCookie (name) { 
        var dcookie = document.cookie; 
        var cname = name + "="; 
        var clen = dcookie.length; 
        var cbegin = 0; 
        while (cbegin < clen) { 
            var vbegin = cbegin + cname.length; 
            if (dcookie.substring(cbegin, vbegin) == cname){ 
                var vend = dcookie.indexOf (";", vbegin); 
                if (vend == -1) vend = clen; 
                    return unescape(dcookie.substring(vbegin, vend)); 
            }
            cbegin = dcookie.indexOf(" ", cbegin) + 1; 
            if (cbegin == 0) break; 
        } 
        return null; 
    } 

    function delCookie (name) { 
        var expireNow = new Date(); 
        document.cookie = name + "=" + 
        "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/"; 
    } 
    
    function ClearPopupCookies(){
        delCookie("NCHSCurrentHistoryCount");
        delCookie("NCHSHitCount");
        delCookie("NCHSCheckPopupBlocked");
    }
    
    /*
    * If user has hit this site at least N (see _ConsecutiveHitsCount in header) times then return true
    * else return false.
    */
    function IsNConsecutiveHits(){
        var ReturnValue=false;
        var CurrentCount=Number(getCookie("NCHSCurrentHistoryCount"));
        var ConsecutiveHits=Number(getCookie("NCHSHitCount"));
        var CookieExpiration=new Date();
        var CookieExpireDelay = 1000*60*_WaiteForConsecutiveHits; //_WaiteForConsecutiveHits is number of minutes
        CookieExpiration.setTime(CookieExpiration.getTime() + CookieExpireDelay);
        
        if(CurrentCount == 0){
            setCookie ("NCHSCurrentHistoryCount", history.length , CookieExpiration);
            CurrentCount=Number(getCookie("NCHSCurrentHistoryCount"));        

            setCookie ("NCHSHitCount", 1 , CookieExpiration);
            ConsecutiveHits=Number(getCookie("NCHSHitCount"));        
        }
        else{
            if(CurrentCount>history.length + 1){
                delCookie("NCHSCurrentHistoryCount");
                delCookie("NCHSHitCount");
            }
            else{
                ConsecutiveHits++;
                setCookie("NCHSHitCount", ConsecutiveHits , CookieExpiration)
            } 
            setCookie ("NCHSCurrentHistoryCount" , history.length , CookieExpiration);
            CurrentCount=getCookie("NCHSCurrentHistoryCount");        
        }
        
        /*
         * Case for _ConsecutiveHitsCount set to 0
         */
         if(Number(_ConsecutiveHitsCount)==Number(0)){
            ConsecutiveHits=_ConsecutiveHitsCount;
         }
         
        if(ConsecutiveHits>=_ConsecutiveHitsCount){
            ReturnValue=true;
        }
        return ReturnValue;
    }

    function randomNumberInRange (high,low){ 
        return Math.floor((high - low + 1) * Math.random() + low) 
    } 

    function IsNthVisiter(){
        var ReturnValue=false;
        /*
         * If VisiterCount is 0 then every visiter gets survey
         */
        if(_VisiterCounter==0){
            ReturnValue=true;
        }
        else{
            var MyCount=randomNumberInRange(_VisiterCounter,0);
            if(MyCount==_VisiterCounter-1){
                ReturnValue=true;
            }
        }
        return ReturnValue;
    }
	
	/* 	OLD NewWindow FUNCTION USED WHEN POPUP PROMPT WAS IN EFFECT
		REPLACED 10/27/2011 BY AJAX SPLASH PAGE */
	
    /* function NewWindow(mypage, myname, w, h, scroll) { 

        var winl = (screen.width - w) / 2; 
        var wint = (screen.height - h) / 2; 
        var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'; 
        var win = window.open(mypage, myname, winprops);
        TempNCHSCheckPopupBlocked=true;
        if (parseInt(navigator.appVersion) >= 4){ 
            if(win!=null){
                win.window.focus(); 
            }
        } 
	}
	*/
	
	/* NEW NewWindow FUNCTION FOR SPLASH PAGE - ADDED 10/27/2011 */
	
    function NewWindow() { 
	
		var hps = new CDC.Survey.HomePage(); 
			
        TempNCHSCheckPopupBlocked=true;
        if (parseInt(navigator.appVersion) >= 4){ 
			hps.showSurveyWindow();
        } 	
		
    } 
	
    var IsPopupSurveyNow = IsNConsecutiveHits();
    if(IsPopupSurveyNow==true){
        if(_TestMode==true){
            alert("met consecutive Hits Criterea");
        }
    }
    var IsNthVisiter = IsNthVisiter();
    if(IsNthVisiter==true){
        if(_TestMode==true){
            alert("met nth visiter criterea");
        }
    }
    /*
     * If user is Nth visiter, if popusurvey now (met consecutive hit criteria) and if
     * popup is not blocked (aka has not occurred in this session) then do popup
     */
    IsPopupSurveyNow =  (IsNthVisiter && IsPopupSurveyNow && !IsPopupBlocked);      
    if (IsPopupSurveyNow == true){
        
		/* REMOVED 10/27/2011 - OLD CALL TO NewWindow FUNCTION USING POPUPS */
		/* NewWindow("/nchs/JS/PopUp2.htm", "", _PopupWidth, _PopupHieght, "no"); */
		
		/* ADDED 10/27/2011 - NEW CALL TO NewWindow FUNCTION USING SPLASH PAGE */
		 NewWindow();
				

        /*
         * Because launching a new window (popup) expires cookies on this page
         * set relevant cookie after page has been spawned.
         */
        if(TempNCHSCheckPopupBlocked==true){
            setTimeout('SetCheckPopupBlockedCookie();',2000);
        }
    }
    function SetCheckPopupBlockedCookie(MilliSeconds){
            setCookie ("NCHSCheckPopupBlocked", true , PopupBlocked_CookieExpiration);   
    }




