var overlay_wrapper_id = "overlayWrap"; // This variable contains the ID of overlay container on HTML page that will contain html markup for overlays.
var overlay_close_btn = "closeOverlayBtn"; // This variable contains the class of close button in overlay markup for hiding or closing overlays.
// This variable contains an associative array of keys and values of elements in the HCP disclaimer overlay.
var overlay_hcp = {
	    "hdr": "hcpOverlayHdr", // The hdr key binds with the value of the class of HCP image header.
		"hdr_message": "HCP Disclaimer", // The hdr_message key binds with the value of the alternative text for the HCP image header.
		"message": "The section you selected contains information intended for healthcare professionals only. Please certify that you are a healthcare professional by clicking &quot;OK&quot; to continue; otherwise click &quot;Cancel&quot; to exit.", // The message key binds with the value of the message on the HCP disclaimer overlay.
		"yesBtn": "okBtn", // The yesBtn key binds with the value of the class of "Yes" or "OK" button in the HCP disclaimer overlay HTML markup.
		"yesBtn_msg": "OK", // The yesBtn_msg key binds with the value of the alternative text for "Yes" or "OK" button.
		"noBtn": "cancelBtn", // The noBtn key binds with the value of the class of "No" or "Cancel" button in the HCP disclaimer overlay HTML markup.
		"noBtn_msg": "Cancel" // The noBtn_msg key binds with the value of the alternative text for "No" or "Cancel" button.
}
// This variable contains an associative array of keys and values of elements in the Leaving the Site disclaimer overlay.
var overlay_leaving = {
		"hdr": "leavingOverlayHdr", // The hdr key binds with the value of the class of Leaving the Site image header.
		"hdr_message": "Leaving ALVESCO.US", // The hdr_message key binds with the value of the alternative text for the Leaving the Site image header.
		"message": "You are about to leave the ALVESCO.US website. Sepracor is not responsible for the information contained in the independent website you are about to visit. Click &quot;OK&quot; to continue or &quot;Cancel&quot; to exit.", // The message key binds with the value of the message on the Leaving the Site disclaimer overlay.
		"yesBtn": "okBtn", // The yesBtn key binds with the value of the class of "Yes" or "OK" button in the Leaving the Site HTML markup.
		"yesBtn_msg": "OK", // The yesBtn_msg key binds with the value of the alternative text for "Yes" or "OK" button.
		"noBtn": "cancelBtn", // The noBtn key binds with the value of the class of "No" or "Cancel" button in the Leaving the Site disclaimer overlay HTML markup.
		"noBtn_msg": "Cancel" // The noBtn_msg key binds with the value of the alternative text for "No" or "Cancel" button.
}

var no_hcp_overlay = new Array("hcp", "healthcare", "healthcare_professionals", "healthcare-professionals"); // This variable contains an array of keywords of file names and folder names that DO need the HCP disclaimer overlay message to inform when users click on these links.
var no_leaving_overlay = new Array("alvesco.us", "sepracor.com", "reliefishere.com", "asthmareliefforless.com","secure.omnaris.com"); // This variable contains an array of sites that DO NOT need the Leaving the Site overlay message to inform users. e.g ("alvesco.us", "thirdpartysite1.com", "thirdpartysite2.com")
var verticalOffset = 150; // This variable contains the number that indicates the distance from the top of browser to the top of overlay.

var popup_wrapper_id = "popupWrap"; // This variable contains the ID of popup window container block element on HTML page that contains html markup for popup window content.
var max_popup_height = 900; // maximum height of popup window. 

// don't change anything below
var cur_overlay;
var cur_location = window.location.href;
var third_party_boolean;
var overlay_type;
var check_cur_location;
var check_hcp_link;



$(document).ready(function(){				   
	  if ($(this).find("#"+popup_wrapper_id).length > 0){
		resize_this_element = "#" + popup_wrapper_id;
	  	resize_popup_window(this, resize_this_element);
	  }
	  
	  $("a").click(function(){							
		var cur_elem_href = this.href;
		check_location(cur_location);
		check_hcp(cur_elem_href);
		check_third_party(cur_elem_href);
		
		if ((!check_cur_location) && (check_hcp_link)) {
			// href = hcp site
			overlay_type = "hcp";
			cur_overlay = overlay_hcp;		
		}else if (third_party_boolean){
		  	for (i=0;i<no_leaving_overlay.length;i++){
				if (cur_elem_href.indexOf(no_leaving_overlay[i]) == -1){
					// href = other websites
					overlay_type = "leaving";
					cur_overlay = overlay_leaving;
				}else{
					return true;
				}
			}
			overlay_type = "leaving";
			cur_overlay = overlay_leaving;
		}else{
			return true;
		}
		
		if (overlay_type){	
			prepare_overlay_wrapper();
			create_overlay_block();
			show_overlay(cur_elem_href);
		}
		
		return false;
	});
	
	$("a[rel*='popup']").click(function(){
		var cur_elem_href = this.href;
		window.open(cur_elem_href, 'popup_window', 'location=no,status=no,scrollbars=yes,width=608,height=500');
		return false;
	});
	
});

function prepare_overlay_wrapper(){
	remove_overlay_wrapper();
	create_overlay_wrapper();
}

function create_overlay_wrapper(){
	$("body").append('<div id="' + overlay_wrapper_id + '"></div>');
}

function remove_overlay_wrapper(){
	$("#"+overlay_wrapper_id).remove();
}

function create_overlay_block(){	
	$("#"+overlay_wrapper_id).append(
		'<div class="overlayBox">' +
			'<a href="#" class="closeOverlayBtn"><span class="headerMessage">close</span></a>"' +
			'<h2 class="' + cur_overlay["hdr"] + '"><span class="headerMessage">' + cur_overlay["hdr_message"] + '</span></h2>' + 

			'<p>' + cur_overlay["message"] + '</p>' +
			'<a href="#" class="' + cur_overlay["yesBtn"] + '"><span class="headerMessage">' + cur_overlay["yesBtn_msg"] + '</span></a>' +
        	'<a href="#" class="' + cur_overlay["noBtn"] + '"><span class="headerMessage">' + cur_overlay["noBtn_msg"] + '</span></a>' +
			'<div class="spacer height25"></div>' +
			'</div>');
}

function show_overlay(this_elem_href){	
			margin_top = $(window).scrollTop() + verticalOffset;
			margin_left = Math.round(($(window).width()/2) - ($("#"+overlay_wrapper_id).width()/2));
			
			$("#"+overlay_wrapper_id).css({
				top: margin_top + 'px',
				left: margin_left + 'px'
			});
			
			//close button
			$("."+overlay_close_btn).click(function(){
				hide_overlay();					
				return false;
			});
			
			// Yes or OK button
			$("."+cur_overlay["yesBtn"]).click(function(){
				if (overlay_type == "hcp"){
					window.location = this_elem_href;
				}else{
					window.open(this_elem_href);
				}
				hide_overlay();
				return false;
			});
			
			// No or Cancel button
			$("."+cur_overlay["noBtn"]).click(function(){
				hide_overlay();					
				return false;
			});
			$("#"+overlay_wrapper_id).fadeIn("slow");
}

function hide_overlay(){
	$("#"+overlay_wrapper_id).fadeOut("slow");
	third_party_boolean = false;
}

function check_hcp(target_href){
	for (i=0;i<no_hcp_overlay.length;i++){
		if (target_href.indexOf(no_hcp_overlay[i]) == -1){
			check_hcp_link = false;
		}else{
			check_hcp_link = true;
		}
	}
	return check_hcp_link;	
}

function check_location(target_href){
	for (i=0;i<no_hcp_overlay.length;i++){
		if (target_href.indexOf(no_hcp_overlay[i]) == -1){
			check_cur_location = false;
		}else{
			check_cur_location = true;
		}
	}
	return check_cur_location;	
}

function check_third_party(target_href){
	var target_domain = target_href.split("//")[1].split("/")[0];
	if(cur_location.indexOf(target_domain) == -1) {
		third_party_boolean = true;
	}
	return third_party_boolean;
}

function resize_popup_window(this_window, this_elem){
	var window_width = $(this_elem).width() + 70;
	var window_height = $(this_elem).height() + 150;
	if (window_height > max_popup_height ) { window_height = max_popup_height; }
	window.resizeTo(window_width,window_height);
}