// variables
var clicked = "";
var backgroundId = "popup_bg";
var popupId = "popup";
var popupVidID = "popup_video";
var sendPopup = "send_popup";
var extPopup = "link-ext_popup";
var popupEnabled = false;
var flash = '';

var backgroundId = "link-ext_bg";
var extURL;

var currentPageTitle;
var videoOpen = false;

// functions
function loadPopup(){

    if (!popupEnabled) {
			$("#"+backgroundId).fadeIn("slow");
			$("#"+clicked).fadeIn("slow");
			if($.browser.msie == true && $.browser.version == "6.0"){
                $('#stateDropDown').hide();
            }
			popupEnabled = true;

	}
}

function disablePopup(){
	if (popupEnabled) {
		
		    //resets title after tracking
		    if(videoOpen){
		        videoOpen = false;
		        document.title = currentPageTitle;
		    }
    		
		
		
			$("#"+backgroundId).fadeOut("slow");
			$("#"+clicked).fadeOut("slow");
			$('#stateDropDown').show();
			popupEnabled = false;
			if($('#popup_video video').size() > 0 && clicked == popupVidID){
			    try {
			        vid = $('#popup_video video').get(0);
			        if(vid.pause()){
			            $('#popup_video video').get(0).pause()
			            $('#popup_video video').get(0).currentTime = 0;
			        }
			    }catch(e){
			        //using flash
			        /*alert("TEST");
			        player = document.getElementById('flashPlayerPopup');
			        player.pause();*/
			        flash = $('#popup_video').html();
			        $('#popup_video').empty();
			    }
			}
		}
	popupEnabled = false;
}

function centerPopup(){
	
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#"+clicked).height();
	var popupWidth = $("#"+clicked).width();
	$("#"+clicked).css({
		"position": "absolute",
		"top": (windowHeight / 2) - (popupHeight / 2) + $(window).scrollTop() + "px",
		"left": (windowWidth / 2) - (popupWidth / 2) + $(window).scrollLeft() + "px"
	});
}


function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}


$(document).ready(function(){

	$('#popup_video video').css('cursor','pointer');
	// startup actions
	$("#"+backgroundId).css({
							
		"opacity": "0.7"
	});
	
	
	
	//modules
	
	$("#videoFast").click(function(event){
    s.tl(true,"o","FAST Video");
    });
    
    $("#videoBleed").click(function(event){
        s.tl(true,"o","Bleed Video");
    });
    
    $("#ModuleSafetyRecord").click(function(event){
        s.tl(true,"o","Module Safety Record");
    });
    
    $("#ModuleBeingPrepared").click(function(event){
        s.tl(true,"o","Module Being Prepared");
    });

    $("#ModuleRoomTempStorage").click(function(event){
            s.tl(true,"o","Module Room Temp Storage");
    });

    $("#ModuleRealStories").click(function(event){
            s.tl(true,"o","Module Real Stories");
    });
        
    $("#ModuleSavingsCard").click(function(event){
            s.tl(true,"o","Module Savings Card");
    });
    
    $("#Module3000IU").click(function(event){
            s.tl(true,"o","Module 3000IU");
    });

    $("#ModuleChapters").click(function(event){
            s.tl(true,"o","Module Chapters and HTC");
    });

    $("#ModuleSave").click(function(event){
            s.tl(true,"o","Module Save on BeneFIX");
    });

    $("#ModuleVoice").click(function(event){
            s.tl(true,"o","Module Community Voice");
    });

    $("#ModuleMOM").click(function(event){
            s.tl(true,"o","Module MOM");
    });

    $("#ModuleHistory").click(function(event){
            s.tl(true,"o","Module History");
    });

    $("#ModuleHemoCommunity").click(function(event){
            s.tl(true,"o","Module Hemo Community");
    });

    $("#ModuleCommunityEvents").click(function(event){
            s.tl(true,"o","Module Community Events");
    });

    $("#ModuleR2Video").click(function(event){
            s.tl(true,"o","Module R2 Video");
    });

    $("#ModuleTheVillager").click(function(event){
            s.tl(true,"o","Module The Villager");
    });

    $("#ModuleHV").click(function(event){
            s.tl(true,"o","Module HV");
    });
    
    $("#ModuleHVSurvey").click(function(event){
            s.tl(true,"o","Module HV Survey");
    });

    $("#ModuleAmbassador").click(function(event){
            s.tl(true,"o","Module Ambassador Program");
    });

    $("#ModuleAskSteve").click(function(event){
            s.tl(true,"o","Module Ask Steve");
    });

    $("#ModuleInfusionality").click(function(event){
            s.tl(true,"o","Module Infusionality");
    });

    $("#ModuleCollege").click(function(event){
            s.tl(true,"o","Module College");
    });
    
    $("#ModuleHisTips").click(function(event){
            s.tl(true,"o","Module His Tips");
    });
    
    $("#HVRegistration").click(function(event){
            s.tl(true,"o","HV Registration");
    });

	
	
	// event handlers
   
    $(".link_popup").click(function(event){
		clicked = popupId;			  
        event.preventDefault();
        centerPopup();
        loadPopup();
    });

    $(".video_popup").click(function(event){
        
        event.preventDefault();
        
        currentPageTitle = document.title;
        videoOpen = true;
        //$(document).attr('title', $(this).attr('rel'));

        clicked = popupVidID;
        centerPopup();
        loadPopup();
        try {
            $('#popup_video video').get(0).play();
        }catch(e){}
        
        //if reopening
        if(flash != ''){
            $('#popup_video').html(flash);
            $(".popup_close").click(function(event){						
                event.preventDefault();
                disablePopup();
            });
        }
    });
    
     $(".send_popup").click(function(event){
        event.preventDefault();
        clicked = sendPopup;
        centerPopup();
        loadPopup();
    });
	$("#"+backgroundId).css({
		"opacity": "0.7"
	});
    $("#popup_close").click(function(event){
					
        event.preventDefault();
        disablePopup();
    });
    $(".popup_close").click(function(event){						
        event.preventDefault();
        disablePopup();
    });
	
    $("#"+backgroundId).click(function(){
									   
        disablePopup();
    });
    
    $(".link_ext").click(function(event){
        event.preventDefault();
        extURL = $(this).attr('href');
        clicked = extPopup;
        centerPopup();
        loadPopup();
    });
	
    $("#link_ext_open").click(function(event){
		disablePopup();
		// option 1 - will validate with a strict doctype since it does not require target="_blank"			   
        /*event.preventDefault();
        window.open(extURL, '', 'location=yes,menubar=yes,status=yes,titlebar=yes,toolbar=yes,scrollbars=yes,resizable=yes');*/
       
	    // option 2 - will not be blocked by popup blockers that block window.open; also requires target="_blank" on the link with this ID
        this.href = extURL;
    });
	
    $("#link_ext_close").click(function(event){
        event.preventDefault();
        disablePopup();
    });
	
    $("#"+backgroundId).click(function(){
        disablePopup();
    });
});

/* Poll Infusion Functions */
function initValidateAnswer(){   //triggered by hidden img onload event   
      
      //clear prior input
      $("input[type='radio']:visible:checked").attr("checked",false);
      $("#popup-age select").val('');
      
      //INIT close popup
      $(".popup-close-btn").click(function(){
            window.setTimeout(disablePopup,100);
      });
      
      //INIT Validate Answer Inputs
     $("a.popup-next").click(function (event) {
        //debugger;
        var $Questions = $("div.popup-question:visible");
        var vAnswer = $("input[type='radio']:visible:checked").val();
        
        if(!vAnswer){//flash Answer warnings
            event.preventDefault();
            var fntSize = $Questions.css("font-size");
            $Questions
                .css("color","yellow")
                .animate(
                    {
                        fontSize : '160%'
                    }
                    ,300
                    ,function() {$Questions.css("font-size",fntSize).css("color","white")}

                );

        }//if(!vAnsver)
    });
    
    
     /* validate Age input */
    $("a[name='LBtnQ01Next']").click(function (event) {
        var vAge = $("#popup-age select").val();
        if (!vAge){//flash age warnings
            event.preventDefault();
            var $popupAge = $("#popup-age");
            var fntSize = $popupAge.css("font-size");
            $popupAge
                .css("color","yellow")
                .animate(
                    {
                        fontSize : '160%'
                    }
                    ,300
                    ,function() {$popupAge.css("font-size",fntSize).css("color","white")}

                );
        }//if (!vAge)
    });
}
