// City of Middletown
// Facebox
(function($) {
 	$.facebox = function(data, klass) {
    $.facebox.loading();    
    if (data.ajax) {
		fillFaceboxFromAjax(data.ajax);
	} else {
		if (data.image) {
			fillFaceboxFromImage(data.image);
		} else {
			if (data.div) {
				fillFaceboxFromHref(data.div);
			} else {
				if ($.isFunction(data)) {
					data.call($);
				} else {
					$.facebox.reveal(data, klass);
				}
			}
		}
	}
	$(window).resize(function(){
    	$("#facebox").css("left", getPageWidth() / 2 - ($("#faceboxtable").width() / 2));
    }); 
}
$.extend($.facebox, {
	settings: {
    	opacity      : 0,
	    overlay      : true,
    	loadingImage : '/assets/images/common/facebox/loading.gif',
	    closeImage   : '/assets/images/common/facebox/closelabel.gif',
    	imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],
	    faceboxHtml  : '\
    		<div id="facebox" style="display:none;"> \
	    	  <div class="popup"> \
    	    	<table> \
	        	  <tbody> \
    	        	<tr> \
	    	          <td class="tl"/><td class="b"/><td class="tr"/> \
    	    	    </tr> \
        	    	<tr> \
	            	  <td class="b"/> \
		              <td class="body"> \
    		            <div class="content"> \
        		        </div> \
            		    <div class="footer"> \
	                	  <a href="#" class="close"> \
    	                	<img src="/assets/images/common/facebox/closelabel.gif" title="close" class="close_image" /> \
	        	          </a> \
    	        	    </div> \
	    	          </td> \
    	    	      <td class="b"/> \
        	    	</tr> \
	            	<tr> \
		              <td class="bl"/><td class="b"/><td class="br"/> \
    		        </tr> \
        		  </tbody> \
		        </table> \
    		  </div> \
	    	</div>'
	},
    loading: function() {
		init();
      	if ($('#facebox .loading').length == 1) {
	  		return true;
	  	}
      	showOverlay();
      	$('#facebox .content').empty();
      	$('#facebox .body').children().hide().end().append('<div class="loading"><img src="'+$.facebox.settings.loadingImage+'"/></div>');
      	$('#facebox').css({
        	/*top:	getPageScroll()[1] + (getPageHeight() / 10),
        	left:	385.5*/
			top: getPageScroll()[1] + (getPageHeight()/10),
			left: ($(window).width() - $('#facebox').width()) /2
      	}).show();

      	$(document).bind('keydown.facebox', function(e) {
        	if (e.keyCode == 27) {
				$.facebox.close();
			}
        	return true;
      	});
      	$(document).trigger('loading.facebox');
    },
    reveal: function(data, klass) {
		$(document).trigger('beforeReveal.facebox');
      	if (klass) {
	  		$('#facebox .content').addClass(klass);
	  	}
      	$('#facebox .content').append(data);
      	$('#facebox .loading').remove();
      	$('#facebox .body').children().fadeIn('normal');
      	$('#facebox').css('left', $(window).width() / 2 - ($('#facebox table').width() / 2));
      	$(document).trigger('reveal.facebox').trigger('afterReveal.facebox');
    },
    close: function() {
      	$(document).trigger('close.facebox');
      	return false;
    }
});


$.fn.facebox = function(settings) {
	init(settings);

    function clickHandler() {
    	$.facebox.loading(true);
      	// support for rel="facebox.inline_popup" syntax, to add a class
      	// also supports deprecated "facebox[.inline_popup]" syntax
      	var klass = this.rel.match(/facebox\[?\.(\w+)\]?/);
      	if (klass) {
	  		klass = klass[1];
	  	}
      	fillFaceboxFromHref(this.href, klass);
      	return false;
    }
    
	return this.click(clickHandler);
}

function init(settings) {
	if ($.facebox.settings.inited) {
		return true;
	} else {
		$.facebox.settings.inited = true;
	}
    $(document).trigger('init.facebox');
    makeCompatible();

    var imageTypes = $.facebox.settings.imageTypes.join('|');
    $.facebox.settings.imageTypesRegexp = new RegExp('\.' + imageTypes + '$', 'i');

    if (settings) {
		$.extend($.facebox.settings, settings);
	}
    $('body').append($.facebox.settings.faceboxHtml);

    var preload = [ new Image(), new Image() ];
    preload[0].src = $.facebox.settings.closeImage;
    preload[1].src = $.facebox.settings.loadingImage;

    $('#facebox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image());
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1');
    });

    $('#facebox .close').click($.facebox.close);
    $('#facebox .close_image').attr('src', $.facebox.settings.closeImage);
  }
  
  // getPageScroll() by quirksmode.com
  	function getPageScroll() {
    	var xScroll, yScroll;
    	if (self.pageYOffset) {
      		yScroll = self.pageYOffset;
      		xScroll = self.pageXOffset;
	    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
    	  yScroll = document.documentElement.scrollTop;
	      xScroll = document.documentElement.scrollLeft;
    	} else if (document.body) {// all other Explorers
	      yScroll = document.body.scrollTop;
    	  xScroll = document.body.scrollLeft;	
	    }
    	return new Array(xScroll,yScroll) ;
  	}

  // Adapted from getPageSize() by quirksmode.com
  	function getPageHeight() {
    	var windowHeight;
    	if (self.innerHeight) {	// all except Explorer
      		windowHeight = self.innerHeight;
    	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      		windowHeight = document.documentElement.clientHeight;
    	} else if (document.body) { // other Explorers
      		windowHeight = document.body.clientHeight;
    	}	
   	 	return windowHeight;
  	}

  // Backwards compatibility
  	function makeCompatible() {
    	var $s = $.facebox.settings;
    	$s.loadingImage = $s.loading_image || $s.loadingImage;
    	$s.closeImage = $s.close_image || $s.closeImage;
   	 	$s.imageTypes = $s.image_types || $s.imageTypes;
   		$s.faceboxHtml = $s.facebox_html || $s.faceboxHtml;
  	}

  // Figures out what you want to display and displays it
  // formats are:
  //     div: #id
  //   image: blah.extension
  //    ajax: anything else
  	function fillFaceboxFromHref(href, klass) {
    	if (href.match(/#/)) {
      		var url = window.location.href.split('#')[0];
      		var target = href.replace(url,'');
	      	$.facebox.reveal($(target).clone().show(), klass);
    	} else if (href.match($.facebox.settings.imageTypesRegexp)) {
      		fillFaceboxFromImage(href, klass);
    	} else {
      		fillFaceboxFromAjax(href, klass);
    	}
  	}

  	function fillFaceboxFromImage(href, klass) {
    	var image = new Image();
    	image.onload = function() {
      		$.facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass);
    	};
    	image.src = href;
  	}

  	function fillFaceboxFromAjax(href, klass) {
    	$.get(href, function(data) { $.facebox.reveal(data, klass) });
  	}

  	function skipOverlay() {
    	return $.facebox.settings.overlay == false || $.facebox.settings.opacity === null ;
  	}

  	function showOverlay() {
    	if (skipOverlay()) {
			return;
		}
    	if ($('facebox_overlay').length == 0) {
			$("body").append('<div id="facebox_overlay" class="facebox_hide"></div>');
		}
    	$('#facebox_overlay').hide().addClass("facebox_overlayBG").css('opacity', $.facebox.settings.opacity).click(function() { $(document).trigger('close.facebox') }).fadeIn(200);
   	 	return false;
  	}

  	function hideOverlay() {
    	if (skipOverlay()) {
			return;
		}
    	$('#facebox_overlay').fadeOut(200, function(){
      		$("#facebox_overlay").removeClass("facebox_overlayBG");
      		$("#facebox_overlay").addClass("facebox_hide") ;
      		$("#facebox_overlay").remove();
    	});    
    	return false;
  	}
	
	function getPageWidth() {
  		var windowWidth;
  		if( typeof( window.innerWidth ) == 'number' ) {
    		windowWidth = window.innerWidth; //Non-IE
  		} else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    		windowWidth = document.documentElement.clientWidth; //IE 6+ in'standards compliant mode'
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    		windowWidth = document.body.clientWidth; //IE 4 compatible
  		}
  		return windowWidth;
	} 

	$(document).bind('close.facebox', function() {
    	$(document).unbind('keydown.facebox');
    	$('#facebox').fadeOut(function() {
      		$('#facebox .content').removeClass().addClass('content');
      		hideOverlay();
      		$('#facebox .loading').remove();
    	});
  	});
})(jQuery);
// Mega HoverMenu
function megaHoverOver(){
  $(this).find(".sub").stop().fadeTo('fast', 1).show();
	  
  //Calculate width of all ul's
  (function($) { 
	  jQuery.fn.calcSubWidth = function() {
		  rowWidth = 0;
		  //Calculate row
		  $(this).find("ul").each(function() {					
			  rowWidth += $(this).width(); 
		  });	
	  };
  })(jQuery); 
		  
  if ( $(this).find(".row").length > 0 ) { //If row exists...
	  var biggestRow = 0;	
	  //Calculate each row
	  $(this).find(".row").each(function() {							   
		  $(this).calcSubWidth();
		  //Find biggest row
		  if(rowWidth > biggestRow) {
			  biggestRow = rowWidth;
		  }
	  });
	  //Set width
	  $(this).find(".sub").css({'width' :biggestRow});
	  $(this).find(".row:last").css({'margin':'0'});

  } else { //If row does not exist...
	  
	  $(this).calcSubWidth();
	  //Set Width
	  $(this).find(".sub").css({'width' : rowWidth});

}
try {

    var totOffset = $(this).find(".sub").offset().left - $("#page").offset().left;
    var subDiff = 921 - (totOffset + $(this).find(".sub").width());
    if (subDiff < 0) {										  
		  $(this).find(".sub").css({'left': subDiff});
	  }
} catch (Err) { }
   

	  
}

function megaHoverOut(){ 
$(this).find(".sub").stop().fadeTo('fast', 0, function() {
	$(this).hide(); 
});
}
// OpenBrWindow
function OpenBrWindow(theURL,winName,features, myWidth, myHeight, isCenter) {
	if(window.screen)if(isCenter)if(isCenter=="true"){
		var myLeft = (screen.width-myWidth)/2;
		var myTop = (screen.height-myHeight)/2;
		features+=(features!='')?',':'';
		features+=',left='+myLeft+',top='+myTop;
	}
	window.open(theURL,winName,features+((features!='')?',':'')+'width='+myWidth+',height='+myHeight);
}
//highlight v3

//Highlights arbitrary terms.

//<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

//MIT license.

//Johann Burkard
//<http://johannburkard.de>
//<mailto:jb@eaio.com>

jQuery.fn.highlight = function(pat) {
	function innerHighlight(node, pat) {
  		var skip = 0;
  		if (node.nodeType == 3) {
   			var pos = node.data.toUpperCase().indexOf(pat);
   			if (pos >= 0) {
    			var spannode = document.createElement('span');
    			spannode.className = 'highlight';
    			var middlebit = node.splitText(pos);
    			var endbit = middlebit.splitText(pat.length);
    			var middleclone = middlebit.cloneNode(true);
    			spannode.appendChild(middleclone);
    			middlebit.parentNode.replaceChild(spannode, middlebit);
    			skip = 1;
   			}	
  		}  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   			for (var i = 0; i < node.childNodes.length; ++i) {
    			i += innerHighlight(node.childNodes[i], pat);
   			}
  		}
  		return skip;
 	}
 
 	return this.each(function() {
  		innerHighlight(this, pat.toUpperCase());
 	});
};

jQuery.fn.removeHighlight = function() {
 	return this.find("span.highlight").each(function() {
  		this.parentNode.firstChild.nodeName;
  		with (this.parentNode) {
   			replaceChild(this.firstChild, this);
   			normalize();
  		}
 	}).end();
};
// Zoom Search Hilite
function doHighlight(divObj) {
	var src = $.url.param("zoom_highlight");
	if (src != undefined) {
		var terms = src.split('+');
		if (terms.length > 0) {
			for (x = 0; x < terms.length;x++) {
				$(divObj).highlight(terms[x]);
			}
		}
	}
}
(function($) {  
	$.fn.stripHtml = function() {  
		var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;  
		this.each(function() {  
			$(this).html($(this).html().replace(regexp,""));  
		});  
		return $(this);  
	};  
})(jQuery);  
jQuery.fn.highlight = function(pat) {
	function innerHighlight(node, pat) {
  		var skip = 0;
  		if (node.nodeType == 3) {
   			var pos = node.data.toUpperCase().indexOf(pat);
   			if (pos >= 0) {
    			var spannode = document.createElement('span');
    			spannode.className = 'highlight';
    			var middlebit = node.splitText(pos);
    			var endbit = middlebit.splitText(pat.length);
    			var middleclone = middlebit.cloneNode(true);
    			spannode.appendChild(middleclone);
    			middlebit.parentNode.replaceChild(spannode, middlebit);
    			skip = 1;
   			}	
  		}  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
   			for (var i = 0; i < node.childNodes.length; ++i) {
    			i += innerHighlight(node.childNodes[i], pat);
   			}
  		}
  		return skip;
 	}
 
 	return this.each(function() {
  		innerHighlight(this, pat.toUpperCase());
 	});
};

jQuery.fn.removeHighlight = function() {
 	return this.find("span.highlight").each(function() {
  		this.parentNode.firstChild.nodeName;
  		with (this.parentNode) {
   			replaceChild(this.firstChild, this);
   			normalize();
  		}
 	}).end();
};
jQuery.url = function()
{
	var segments = {};	
	var parsed = {};

  	var options = {	
		url : window.location, // default URI is the page in which the script is running		
		strictMode: false, // 'loose' parsing by default
		key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query 		
		q: {
			name: "queryKey",
			parser: /(?:^|&)([^&=]*)=?([^&]*)/g
		},		
		parser: {
			strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, // more intuitive, fails on relative paths and deviates from specs
			loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ //less intuitive, more accurate to the specs
		}		
	};
	
    /**
     * Deals with the parsing of the URI according to the regex above.
 	 * Written by Steven Levithan - see credits at top.
     */		
	var parseUri = function()
	{
		str = decodeURI( options.url );
		
		var m = options.parser[ options.strictMode ? "strict" : "loose" ].exec( str );
		var uri = {};
		var i = 14;

		while ( i-- ) {
			uri[ options.key[i] ] = m[i] || "";
		}

		uri[ options.q.name ] = {};
		uri[ options.key[12] ].replace( options.q.parser, function ( $0, $1, $2 ) {
			if ($1) {
				uri[options.q.name][$1] = $2;
			}
		});

		return uri;
	};

    /**
     * Returns the value of the passed in key from the parsed URI.
  	 * 
	 * @param string key The key whose value is required
     */		
	var key = function( key )
	{
		if ( ! parsed.length )
		{
			setUp(); // if the URI has not been parsed yet then do this first...	
		} 
		if ( key == "base" )
		{
			if ( parsed.port !== null && parsed.port !== "" )
			{
				return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/";	
			}
			else
			{
				return parsed.protocol+"://"+parsed.host+"/";
			}
		}
	
		return ( parsed[key] === "" ) ? null : parsed[key];
	};
	
	/**
     * Returns the value of the required query string parameter.
  	 * 
	 * @param string item The parameter whose value is required
     */		
	var param = function( item )
	{
		if ( ! parsed.length )
		{
			setUp(); // if the URI has not been parsed yet then do this first...	
		}
		return ( parsed.queryKey[item] === null ) ? null : parsed.queryKey[item];
	};

    /**
     * 'Constructor' (not really!) function.
     *  Called whenever the URI changes to kick off re-parsing of the URI and splitting it up into segments. 
     */	
	var setUp = function()
	{
		parsed = parseUri();
		
		getSegments();	
	};
	
    /**
     * Splits up the body of the URI into segments (i.e. sections delimited by '/')
     */
	var getSegments = function()
	{
		var p = parsed.path;
		segments = []; // clear out segments array
		segments = parsed.path.length == 1 ? {} : ( p.charAt( p.length - 1 ) == "/" ? p.substring( 1, p.length - 1 ) : path = p.substring( 1 ) ).split("/");
	};
	
	return {
		
	    /**
	     * Sets the parsing mode - either strict or loose. Set to loose by default.
	     *
	     * @param string mode The mode to set the parser to. Anything apart from a value of 'strict' will set it to loose!
	     */
		setMode : function( mode )
		{
			strictMode = mode == "strict" ? true : false;
			return this;
		},
		
		/**
	     * Sets URI to parse if you don't want to to parse the current page's URI.
		 * Calling the function with no value for newUri resets it to the current page's URI.
	     *
	     * @param string newUri The URI to parse.
	     */		
		setUrl : function( newUri )
		{
			options.url = newUri === undefined ? window.location : newUri;
			setUp();
			return this;
		},		
		
		/**
	     * Returns the value of the specified URI segment. Segments are numbered from 1 to the number of segments.
		 * For example the URI http://test.com/about/company/ segment(1) would return 'about'.
		 *
		 * If no integer is passed into the function it returns the number of segments in the URI.
	     *
	     * @param int pos The position of the segment to return. Can be empty.
	     */	
		segment : function( pos )
		{
			if ( ! parsed.length )
			{
				setUp(); // if the URI has not been parsed yet then do this first...	
			} 
			if ( pos === undefined )
			{
				return segments.length;
			}
			return ( segments[pos] === "" || segments[pos] === undefined ) ? null : segments[pos];
		},
		
		attr : key, // provides public access to private 'key' function - see above
		
		param : param // provides public access to private 'param' function - see above
		
	};
	
}();
jQuery.fn.extend({ /* Adds .disabled(true/false) to jquery */
	filterDisabled : function(){ return this.filter(function(){return (typeof(this.disabled)!=undefined)})},
	disabled: function(h) {
   		if (h!=undefined) return this.filterDisabled().each(function(){this.disabled=h});
   		this.filterDisabled().each(function() {h=((h||this.disabled)&&this.disabled)}); return h;
	},
	toggleDisabled: function() { return this.filterDisabled().each(function(){this.disabled=!this.disabled});}
});
function extlinks(target) {
	if (target == undefined) {
		// no target passed, use css style
		var base_url = "http://www.cityofmiddletown.org";
		$("a:not(':has(\"img\")')[href^=http]").each(function() {
    		if ( -1 == $(this).attr("href").indexOf(base_url) ) {
				$(this).addClass("external");
			/*	$(this).click(function () { 
 					var x=window.confirm('You are about to proceed to an offsite link. The City of Middletown has no control over the content of this site.  Click OK to proceed.');
					var val = false;
					if (x)
						val = true;
					else
						val = false;
					return val;
        		});*/
			}
		});
	} else {
/*		$(target).filter(function() {
			return this.hostname && this.hostname !== location.hostname;
		}).after(' <img src="/assets/images/common/external2.gif" alt="external link">').click(function () { 
 					var x=window.confirm('You are about to proceed to an offsite link.  The City of Middletown has no control over the content of this site.  Click OK to proceed.');
					var val = false;
					if (x)
						val = true;
					else
						val = false;
					return val;
        		});
	*/	
		
		$(target).filter(function() {
			return this.hostname && this.hostname !== location.hostname;
		}).after('&nbsp;<img src="/assets/images/common/external2.gif" alt="external link">');
	}
}
(function($) {
	
	var ajax = $.ajax;
	
	var pendingRequests = {};
	
	var synced = [];
	var syncedData = [];
	
	$.ajax = function(settings) {
		// create settings for compatibility with ajaxSetup
		settings = jQuery.extend(settings, jQuery.extend({}, jQuery.ajaxSettings, settings));
		
		var port = settings.port;
		
		switch(settings.mode) {
		case "abort": 
			if ( pendingRequests[port] ) {
				pendingRequests[port].abort();
			}
			return pendingRequests[port] = ajax.apply(this, arguments);
		case "queue": 
			var _old = settings.complete;
			settings.complete = function(){
				if ( _old )
					_old.apply( this, arguments );
				jQuery([ajax]).dequeue("ajax" + port );;
			};
		
			jQuery([ ajax ]).queue("ajax" + port, function(){
				ajax( settings );
			});
			return;
		case "sync":
			var pos = synced.length;
	
			synced[ pos ] = {
				error: settings.error,
				success: settings.success,
				complete: settings.complete,
				done: false
			};
		
			syncedData[ pos ] = {
				error: [],
				success: [],
				complete: []
			};
		
			settings.error = function(){ syncedData[ pos ].error = arguments; };
			settings.success = function(){ syncedData[ pos ].success = arguments; };
			settings.complete = function(){
				syncedData[ pos ].complete = arguments;
				synced[ pos ].done = true;
		
				if ( pos == 0 || !synced[ pos-1 ] )
					for ( var i = pos; i < synced.length && synced[i].done; i++ ) {
						if ( synced[i].error ) synced[i].error.apply( jQuery, syncedData[i].error );
						if ( synced[i].success ) synced[i].success.apply( jQuery, syncedData[i].success );
						if ( synced[i].complete ) synced[i].complete.apply( jQuery, syncedData[i].complete );
		
						synced[i] = null;
						syncedData[i] = null;
					}
			};
		}
		return ajax.apply(this, arguments);
	};
	
})(jQuery);
