/************************************************************************
/* main.js
/*
/* Primary JavaScript file for "DipNote," the website at blogs.state.gov
/* Copyright (c) 2011 U.S. Department of State Bureau of Public Affairs
/************************************************************************
/* Created: 2011-05-26 
/* Author: Tom Ogden, Interactive Web Developer, PA/DCC
/*
/************************************************************************
*/

	$(document).ready(function() {
		
		$('a').click(function() {
			var newurl = $(this).attr('href');
			if(!matchurl(newurl)) {
				var $exit = $('<div></div>')
					.html('Are you sure you want to proceed to the website at <em>'+newurl+'</em>?')
					.dialog({
						autoOpen: false,// Dialog must be reusable, since users might choose not to leave the site. 
						title: '<h3>Now leaving DipNote\'s website.</h3>',
						modal: true,
						closeOnEscape: true,
						width: 400,
						height: 200,
						buttons: {
							"Ok": function() { window.location.href=newurl; },
							"Cancel": function() { $(this).dialog("close"); }
						}
					});
		
		
			//function confirmExit() {
				$exit.dialog('open');
				// prevent the default action, e.g., following a link
				return false;
			}
		});
	});


	/********************************************************************
	/* FUNCTION: getdomain
	/* DESCRIPTION: Parses a given URL to extract and return the domain. 
	/********************************************************************
	/*   ARGUMENTS: url
	/*     RETURNS: domain
	/*      AUTHOR: Tom Ogden
	/*     CREATED: 2011-05-24
	/*    MODIFIED: 2011-05-26, to add to library. 
	/*    MODIFIED: 2011-05-27, parse URL down to 2nd-level domain. 
	/********************************************************************
	*/
	function getdomain (url) {
		var partsArray1 = url.split(':');
		var urlstr = partsArray1.pop();
		if (urlstr.charAt(0) == '/') {urlstr=urlstr.substring(1);}
		if (urlstr.charAt(0) == '/') {urlstr=urlstr.substring(1);}
		var partsArray2 = urlstr.split ( "/" );
		var domain = partsArray2[0];
		var domainPartsArray = domain.split ( "." );
		var tld = domainPartsArray.pop();
		var domainL2 = domainPartsArray.pop() + "." + tld;	

		return (domainL2);	
	}
	
	/********************************************************************
	/* FUNCTION: matchurl
	/* DESCRIPTION: Determines if a given URL is within the website. 
	/********************************************************************
	/*   ARGUMENTS: newurl
	/*     RETURNS: boolean
	/*      AUTHOR: Tom Ogden
	/*     CREATED: 2011-05-24
	/*    MODIFIED: 2011-05-26
	/********************************************************************
	*/
	function matchurl (newurl) {
		var olddomain = getdomain(window.location.href);
		var newdomain = getdomain(newurl);
//alert ('D0:'+olddomain+' D1:'+newdomain);

		return (olddomain==newdomain);
	}
	


