var popupEnabled = false;
var extURL;
function loadPopup(){
    
if (!popupEnabled) {
        $("#link-ext_bg").css({
            "opacity": "0.7"
        });
        $("#link-ext_bg").fadeIn("slow");
        $("#link-ext_popup").fadeIn("slow");
        popupEnabled = true;
    }
}

function disablePopup(){
    
if (popupEnabled) {
        $("#link-ext_bg").fadeOut("slow");
        $("#link-ext_popup").fadeOut("slow");
        popupEnabled = false;
    }
}

function centerPopup(){
    var scrollXoffset = 0;
    var scrollYoffset = 0;
    
if (typeof(window.pageYOffset) == 'number') {
        //Netscape compliant
        scrollYoffset = window.pageYOffset;
        scrollXoffset = window.pageXOffset;
    }
    else 
        
if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            //DOM compliant
            scrollYoffset = document.body.scrollTop;
            scrollXoffset = document.body.scrollLeft;
        }
        else 
            
if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
                //IE6 standards compliant mode
                scrollYoffset = document.documentElement.scrollTop;
                scrollXoffset = document.documentElement.scrollLeft;
            }
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#link-ext_popup").height();
    var popupWidth = $("#link-ext_popup").width();
    $("#link-ext_popup").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2 + scrollYoffset,
        "left": windowWidth / 2 - popupWidth / 2 + scrollXoffset
    });
    //fix for IE6
    $("#link-ext_bg").css({
        // this gives it a height in IE6 when height: 100% in CSS doesn't work, but only as much as the initial viewport, so no bg when scrolling down; used a CSS fix instead in components.css
        //"height": windowHeight,
        "width": windowWidth
    });
}

$(document).ready(function(){
    
    // toolbar - text resize
    var toolbar_text_default = document.getElementById("toolbar_text_default");
    var toolbar_text_larger = document.getElementById("toolbar_text_larger");
    var toolbar_text_largest = document.getElementById("toolbar_text_largest");
    
if (toolbar_text_default != null && toolbar_text_larger != null && toolbar_text_largest != null) {
        toolbar_text_default.style.color = "#b5ae9c";
        $("#toolbar_text_default").click(function(event){
            event.preventDefault();
            toolbar_text_larger.style.color = "#002f41";
            toolbar_text_largest.style.color = "#002f41";
            this.style.color = "#b5ae9c";
            document.body.style.fontSize = "75%";
        });
        $("#toolbar_text_larger").click(function(event){
            event.preventDefault();
            toolbar_text_default.style.color = "#002f41";
            toolbar_text_largest.style.color = "#002f41";
            this.style.color = "#b5ae9c";
            document.body.style.fontSize = "100%";
        });
        $("#toolbar_text_largest").click(function(event){
            event.preventDefault();
            toolbar_text_default.style.color = "#002f41";
            toolbar_text_larger.style.color = "#002f41";
            this.style.color = "#b5ae9c";
            document.body.style.fontSize = "120%";
        });
    }

    // slideshow
    var imagesArray = [];
    var showIdentifier;
    var currentImage;
    var currentImageNum;
    var slideshowContainer = document.getElementById("js_slideshow");
    
if (slideshowContainer != null) {
        currentImage = slideshowContainer.getElementsByTagName("img")[0];
        showIdentifier = document.getElementById("js_slideshow_identifier").childNodes[0].nodeValue;
        currentImageNum = 0;
        try {
            var requester = new XMLHttpRequest();
        } 
        catch (error) {
            try {
                var requester = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (error) {
                var requester = null;
            }
        }
        
if (requester != null) {
            requester.open("GET", hv_parameters.slideshowPath, true);
            requester.onreadystatechange = function(){
                
if (requester.readyState == 4) {
                    
if (requester.status == 200 || requester.status == 304) {
                        //var showNode = requester.responseXML.getElementsByTagName("slideshow")[0];
                        var allShowNodes = requester.responseXML.getElementsByTagName("slideshow");
                        var identifierValue;
                        var showNode;
                        for (var i = 0; i < allShowNodes.length; i++) {
                            identifierValue = allShowNodes[i].getAttributeNode("identifier").nodeValue;
                            
if (identifierValue == showIdentifier) {
                                showNode = allShowNodes[i];
                                //return true;
                            }
                        }
                        var rawArray = showNode.getElementsByTagName("location");
                        for (var j = 0; j < rawArray.length; j++) {
                            var locationText = rawArray[j].childNodes[0].nodeValue;
                            imagesArray.push(locationText);
                            // preload images
                            var cacheImage = document.createElement('img');
                            cacheImage.src = locationText;
                        }
                    }
                    else {
                        alert("Requester encountered an error.");
                    }
                }
            }
            requester.send(null);
        }
        else {
            alert("No Ajax for you!");
        }
        $("#js_slideshow_next").click(function(){
            
if (currentImageNum >= imagesArray.length - 1) {
                currentImageNum = 0;
            }
            else {
                currentImageNum++;
            }
            currentImage.src = imagesArray[currentImageNum];
        });
        $("#js_slideshow_prev").click(function(){
            
if (currentImageNum <= 0) {
                currentImageNum = imagesArray.length - 1;
            }
            else {
                currentImageNum--;
            }
            currentImage.src = imagesArray[currentImageNum];
        });
    }
    
    
    $(".link_ext").click(function(event){
        event.preventDefault();
        extURL = this.href;
        centerPopup();
        loadPopup();
    });
    $("#link_ext_open").click(function(event){
        event.preventDefault();
        window.open(extURL, '', 'location=yes,menubar=yes,status=yes,titlebar=yes,toolbar=yes,scrollbars=yes,resizable=yes');
        // commenting out the above and uncommenting the below (and adding target="_blank" to the a tag with this ID) would open the window without using window.open, which might be better for getting around popup blockers... but the target attribute isn't supported for the XHTML Strict doctype
        //this.href = extURL;
        disablePopup();
    });
    $("#link_ext_close").click(function(event){
        event.preventDefault();
        disablePopup();
    });
    $("#link-ext_bg").click(function(){
        disablePopup();
    });
	
    $(document).keypress(function(e){
        
		if (e.keyCode == 27 && popupEnabled) {
				disablePopup();
			}
    });
   
   
   // X0602 - survey
   
   var x0602_activeQs = new Array();
   
   var x0602_userType = new String();
   
   var x0602_isValid = false;
   
   
   // showing / hiding
   
   if ($('input[name$="x0602_q01"]').length > 0) {
   
   		$(".x0602_q_patient_a, .x0602_q_patient_b, .x0602_q_caregiver_a, .x0602_q_caregiver_b").hide();
   
   }
   
   $('input[name$="x0602_q01"]').click(function(){
        
		$(".x0602_q_patient_a, .x0602_q_patient_b, .x0602_q_caregiver_a, .x0602_q_caregiver_b").hide();
		
		x0602_userType = $(this).val();
		
		$('fieldset.x0602_q_' + x0602_userType).show();
		
		x0602_activeQs = [];
		
		x0602_activeQs = $('fieldset.x0602_q_' + x0602_userType);
		
    });
   
   
   // just a manual assignment, for testing
   x0602_userType = "patient_b";
   
   
   if ($('input[name*="x0602_q11"]').length > 0) {
	   
   
   		$('input[name*="x0602_q12"]').parent().hide();
		
		$('input[name*="x0602_q11"]').click(function(){
        
			$('input[name*="x0602_q12"]').parent('fieldset.x0602_q_' + x0602_userType).show();
			
		});
   
   }
   
   if ($('input[name*="x0602_q16"]').length > 0) {
	   
	   $('input[name*="x0602_q17"]').parent().hide();
	   
	   $('input[id*="x0602_q16_opt02"]').click(function(){
			
			$('input[name*="x0602_q17"]').parent().show();
			
		});
   }
   
   if ($('input[name*="x0602_q18"]').length > 0) {
	   
	   $('input[name*="x0602_q19"]').parent().hide();
	   
	   $('input[id*="x0602_q18_opt01"]').click(function(){
			
			$('input[name*="x0602_q19"]').parent().show();
			
		});
   }
   
   if ($('input[name*="x0602_q20"]').length > 0) {
	   
	   $('input[name*="x0602_q21"], input[name*="x0602_q22"]').parent().hide();
	   
	   $('input[id*="x0602_q20_opt01"]').click(function(){
			
			$('input[name*="x0602_q21"], input[name*="x0602_q22"]').parent('fieldset.x0602_q_' + x0602_userType).show();
			
		});
   }
   
   if ($('input[name*="x0602_q23"]').length > 0) {
	   
	   
	   $('input[name*="x0602_q24"], input[name*="x0602_q25"]').parent().hide();
	   
	   
	   $('input[id*="x0602_q23_opt01"]').click(function(){
														
			$('input[name*="x0602_q24"], input[name*="x0602_q25"]').parent().hide();
			
			$('input[name*="x0602_q24"]').parent('fieldset.x0602_q_' + x0602_userType).show();
			
		});
	   
	   
	   $('input[id*="x0602_q23_opt02"]').click(function(){
														
			$('input[name*="x0602_q24"], input[name*="x0602_q25"]').parent().hide();
			
			$('input[name*="x0602_q25"]').parent().show();
			
		});
   }
   
   if ($('input[name*="x0602_q26"]').length > 0) {
	   
	   $('input[name*="x0602_q27"], input[name*="x0602_q28"], input[name*="x0602_q29"], input[name*="x0602_q30"], input[name*="x0602_q31"]').parent().hide();
	   
	   $('input[name*="x0602_q26"][id*="_opt01"]').click(function(){
			
			$('input[name*="x0602_q27"], input[name*="x0602_q28"], input[name*="x0602_q30"], input[name*="x0602_q31"]').parent('fieldset.x0602_q_' + x0602_userType).show();
			
		});
	   
	   $('input[name*="x0602_q28"]').filter('input[id*="_opt02"]').click(function(){
			
			$('input[name*="x0602_q29"]').parent('fieldset.x0602_q_' + x0602_userType).show();
			
		});
   }
   
   
   // validate that all active questions are filled out
   
//   $('input[id$="x0602_p01_next"]').click(function(event){
//									   
//		event.preventDefault();
//									   
//		var x0602_validityArray = new Array();
//		
//		var x0602_isThisQuestionValid = new Boolean();
//		
//		var x0602_isFormValid = true;
//		
//		x0602_activeQs.push($('input[name$="x0602_q01"]').parent());
//		
//		if (x0602_activeQs.length > 1) {
//        
//			$.each(x0602_activeQs, function(key, value) {
//										 
//			  if ($(this).children('input:checked').length > 0 || $(this).find('option:selected').val() != "") {
//			  
//				  x0602_isThisQuestionValid = true;
//				  x0602_validityArray.push(x0602_isThisQuestionValid);
//				  $(this).children('legend').removeClass('required');
//				  //alert("this question is filled out.");
//			  }
//			  
//			  else {
//				  
//				  x0602_isThisQuestionValid = false;
//				  x0602_validityArray.push(x0602_isThisQuestionValid);
//				  $(this).children('legend').addClass('required');
//				  //alert("this question is blank.");
//				  
//			  }
//			  
//			});
//			
//			$.each(x0602_validityArray, function(key, value) {
//									 
//			  if (value == false) {
//				
//				  x0602_isFormValid = false;
//				  alert("Please answer all the questions highlighted in red.");
//				  return;
//			  }
//		  
//	  		 });
//		
//		}
//		
//		else {
//			
//			x0602_isFormValid = false;
//		}
//		
//		
//		
//	  if (x0602_isFormValid) {
//		   
//		  alert("Congrats, all questions are filled out!");	
//	   }
//	   
//	   else {
//		   
//		    alert("First question not answered.");	
//	   }
//	   
//    });
   
    // preload all images attached to CSS documents
    $.preloadCssImages();
});

