(function($){

	function newWindow(href) {
		window.open(href,'','directories=1,location=1,menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=1,width=800,height=480');
	}

	function cookiesEnabled(){
		var myResult = false;
		$.cookie('cookiesEnabled', null, {
			path: '/'
		});
		$.cookie('cookiesEnabled', 'true', {
			path: '/',
			expires: null
		});// set a cookie to remember the selected language
		if($.cookie('cookiesEnabled')=='true'){
			myResult = true;
		}
		$.cookie('cookiesEnabled', null, {
			path: '/'
		});
		return myResult;
	}
	
	var cookiesAreEnabled = cookiesEnabled();
	
	$.fn.clickNewWindow = function(){
		this.each(function(){
			$(this)
			.click(function(event){
				event.preventDefault();
				newWindow(this.href);
			});
		});
		return this;
	}	
	
	$.fn.leavingSite = function(){
		//if this is a link then
		this.each(function(){
			$(this).filter(function(){
				// Force disclaimer on following filter
				//if (this.hostname == 'sdplproxy.sandiego.gov') { return true; }
				
				// Skip disclaimer on following filters
				if (this.hostname == location.hostname) { return false; }
				if (this.hostname.substring(this.hostname.length - 10, this.hostname.length) == 'sannet.gov') { return false; }
				if (this.hostname.substring(this.hostname.length - 12, this.hostname.length) == 'sandiego.gov') { return false; }
				if (this.hostname.substring(this.hostname.length - 18, this.hostname.length) == 'governmentjobs.com') { return false; }
				if (this.hostname.substring(this.hostname.length - 19, this.hostname.length) == 'velocitypayment.com') { return false; }
				if (this.hostname == 'step1.caledoncard.com') { return false; }
				if (this.hostname == 'secure.coplogic.com') { return false; }
				if (this.hostname == 'secure3.i-doxs.net') { return false; }
				if (this.hostname == 'ssl.netfile.com') { return false; }
				if (this.hostname == 'www.paybill.com') { return false; }
				if (this.hostname == 'sddp.sirsi.net') { return false; }
				if (this.hostname == 'www.thinkblue.org') { return false; }
				if (this.hostname == 'citynet') { return false; }
				if (this.hostname == '') { return false; }
				
				return true;
			})
			.click(function(event){
				event.preventDefault();
				if (cookiesAreEnabled) {
					//window.alert("Cookies Enabled");
					if($.cookie('leavingSiteDisc')=='noshow'){
						newWindow(this.href);
					}
					else {
						$.cookie('urlredirect', null, {
							path: '/'
						});
						$.cookie('urlredirect', this.href, {
							path: '/',
							expires: null
						});
						newWindow('/leavingsite.shtml');
					}
				}
				else {
					window.alert("You are now leaving the City of San Diego's website. Links to websites outside of sandiego.gov do not constitute the City's endorsement of any content, product or service. The City's website policies may differ from those of linked websites.");
					newWindow(this.href);
				}
				return false;
			});
		});
		return this;
	}

	$.fn.openApps = function(){
		//if this is a link then
		this.each(function(){
			$(this).filter(function(){
				if (this.hostname == location.hostname) { return false; }
				if (this.href.match(/apps.sandiego.gov/) && this.href.match(/citizenservices|gallery|ereg|secalweb|landcalc|epermits|streetdiv|econtainer|verifycert|siteinfoweb/) ) {return false;}
				if (this.hostname == 'secure3idox.net') { return true; }
				if (this.hostname == 'activenet20.active.com') { return true; }
				if (this.hostname == 'activenet10.active.com') { return true; }
				if (this.hostname == 'pdpermits.sandiego.gov') { return true; }
				if (this.hostname == 'apps.sandiego.gov') { return true; }
				if (this.hostname == 'www.paybill.com') { return true; }
				if (this.hostname == 'sdplpayments.sandiego.gov') { return true; }
				if (this.hostname == 'step1.caledoncard.com') { return true; }
				if (this.hostname == 'customer.sandiego.gov') { return true; }
				if (this.hostname == 'citymaps.sandiego.gov') { return true; }
				if (this.hostname == 'sddp.sirsi.net') { return true; }
				if (this.hostname == 'secure.coplogic.com') { return true; }			
			
				return false;
			})
			.click(function(event){
				event.preventDefault();
				newWindow(this.href);
			});
		});
		return this;
	}

	$(document).ready(function(){
		$('a').leavingSite().openApps();
		$('a.newwindow').clickNewWindow();
	});

})(jQuery);
