// ExitAlert Script
//
// (c) 2009 Iowa Interactive, LLC
// Written by Andrew Meyer
//
//
function initExitAlerts() {
	
	//global options
	//class of links that will launch external confirm boxes.
	var extClass="ext_link";
	
	//site name. to be used in alert that pops up.
	site_name="www.IowaTaxAndTags.gov";
	
	//style
	blockOutColor = '#fff';
	blockOutOpacity = 0.7;
	transBorderColor = '#0099FF';
	borderOpacity = 0.6;
	insideBoxBorderColor = transBorderColor;
	insideBoxColor = '#FFF';
	insideBoxTextColor = '#000';
	hilightColor='#f2f2f2';
	holderWidth = 550;
	topOffset = 150;
	boxMaxHeight = 180;
	
	//end of options. no more editing.
	startingZIndex=1000;
	
	$('a[target="_blank"], a.' + extClass).each(function(index){
		var ext_item_id='ext_link_'+index;
		var ext_box_id='ext_box_'+index;
		$(this).attr('id',ext_item_id);
		$(this).removeAttr('target');
		
		var ext_href=$(this).attr('href');
		if ($(this).html().toLowerCase().indexOf('<img ')<0) {
			var link_text=$(this).text();
		} else {
			if ($(this).find('img').attr('alt')) {
				var link_text=$(this).find('img').attr('alt');
			} else {
				var link_text=ext_href;
			}
		}
		$(this).attr('href','javascript://');
		
		$(this).click(function(){
			toggleBox(ext_box_id,ext_href,link_text);
		});
	});
}

function toggleBox(ext_box_id,ext_href,link_text) {
	if ($('#'+ext_box_id).length>0) {
		var actualTopOffset=topOffset+$(window).scrollTop();
		$('#'+ext_box_id).css('top',actualTopOffset+'px');
		$('#'+ext_box_id).animate({opacity: 'toggle'}, 'normal');
		toggleBlockOut();
	} else if ($('#'+ext_box_id).length==0) {
		//append first holder -- width:100%; top:150px; position:absolute; and second holder to which should be applied a margin and width.
		$('body').append('<div id="'+ext_box_id+'" class="external_confirm_holder"><div class="external_confirm"></div></div>');
		//apply styles to the first holder.
		var actualTopOffset=topOffset+$(window).scrollTop();
		$('#'+ext_box_id).css('width','100%').css('position','absolute').css('top',actualTopOffset+'px').css('left','0').css('z-index',startingZIndex);
		//apply styles to the second holder.
		$('#'+ext_box_id+' div.external_confirm').css('margin','0 auto').css('width',holderWidth+'px').css('position','relative');
		
		//append the two inside divs: transparent background and content holder
		$('#'+ext_box_id+' div.external_confirm').append('<div class="external_confirm_trans_border"></div><div class="external_confirm_content_area"></div>');
		//a little CSS transparent border div.
		$('#'+ext_box_id+' div.external_confirm_trans_border').css('position','absolute').width(holderWidth).css('top','0').css('left','0').css('background-color',transBorderColor).css('opacity', borderOpacity);
		//now a bit for the content div.
		var newWidth=(holderWidth-38)*1;
		$('#'+ext_box_id+' div.external_confirm_content_area').css('position','absolute').width(newWidth).css('top','8px').css('left','8px').css('background-color',insideBoxColor).css('color',insideBoxTextColor).css('text-align','center').css('border','1px solid '+insideBoxBorderColor).css('padding','10px');
		
		var confirm_html='<h3>You have requested to leave the site, ' + site_name + '</h3>'+
			'<p>Thank you for visiting.<br /> Click the link below to continue:</p>'+
			'<p style="background-color:'+hilightColor+'; padding:5px 0;"><a href="'+ext_href+'" style="font-size:1.2em;">'+link_text+'</a><br/>(Or you may <a href="'+ext_href+'" onclick="hideAll(); return true;" target="_blank">open the link in new window</a>)</p>'+
			'<p><em><a href="javascript:void(0)" onclick="hideAll(); return true;">Cancel and stay at '+site_name+'.</a></em></p>';
		
		$('#'+ext_box_id).hide()
		$('#'+ext_box_id+' div.external_confirm_content_area').html(confirm_html);
		$('#'+ext_box_id).css('visibility','hidden').show();
		var trans_height=$('#'+ext_box_id+' div.external_confirm_content_area').height();
		if (trans_height>boxMaxHeight) {
			$('#'+ext_box_id+' div.external_confirm_content_area').height(boxMaxHeight);
			trans_height=boxMaxHeight;
		}
		$('#'+ext_box_id).css('visibility','visible').hide();
		$('#'+ext_box_id+' div.external_confirm_trans_border').height(trans_height+38);
		$('#'+ext_box_id).fadeIn('slow');
		toggleBlockOut();
	}
}

function toggleBlockOut() {
	//checks if the blackout div has been created. if yes it shows it...
	if ($('div#external_confirm_blockout').length>0) {
		$('div#external_confirm_blockout').animate({opacity: 'toggle'}, 'normal');
	//if no it creates it then shows it.
	} else if ($('div#external_confirm_blockout').length==0) {
		$('body').append('<div id="external_confirm_blockout">&nbsp;</div>');
		$('div#external_confirm_blockout').hide().css('position','absolute').width('100%').height($('body').height()).css('top','0').css('left','0').css('background-color',blockOutColor).css('opacity',blockOutOpacity).css('z-index',0);
		$('div#external_confirm_blockout').fadeIn(function(){
			//applies event handler to hide the blackout div and external confirm when it is clicked.
			$('div#external_confirm_blockout').click(function(){
				$(this).fadeOut();
				$('div.external_confirm_holder').remove();
			});
			$('div#external_confirm_blockout').css('cursor','pointer');
		});
	}
}

function hideAll() {
	$('div.external_confirm_holder').fadeOut('normal', function(){$('div.external_confirm_holder').remove();});
	$('div#external_confirm_blockout').fadeOut();	
}

//this line makes the whole thing work.
$(function(){initExitAlerts();});