var RecaptchaOptions = {
	theme : 'white'
};

$(document).ready(function() {

	ImageRotator.init();
	BluPlugins.init();
	Forms.init();
	Colorbox.init($('.bluImage a[rel]'),'image');
	Colorbox.init($('.bluGallery.videos .bluImage a[rel]'),'video');
	
	$("#calendar a").each(function(i){
		var dChunks = $(this).attr("href").match(/calendar\/(listing|details)\/(?=[^\/#])([^\/#]+)/);
		if( dChunks ) {
			$(this).attr("href","#"+dChunks[dChunks.length-1]);
		}
	});
	
	$('.agendaDocsMore a').toggle(function() {
		$(this).parent().removeClass('agendaDocsMore').addClass('agendaDocsLess');
		$(this).parent().parent().find('.agendaShowHide').fadeIn(600);
	}, function() {
		$(this).parent().removeClass('agendaDocsLess').addClass('agendaDocsMore');
		$(this).parent().parent().find('.agendaShowHide').fadeOut(200);
	});
		
	
	
	/* FIX IMAGE HOVER FOR IE */
	$(function() {
		$('.bluImage > a[rel]').each(function(i,val) {
			var bluImageW = $(this).css('width').replace(/px$|em$|pt$|%$/,""); var bluImageH = $(this).css('height').replace(/px$|em$|pt$|%$/,"");
			if(isNumeric(bluImageW) && isNumeric(bluImageH)) { $(this).attr('style','width: '+(parseInt(bluImageW)+1)+'px !important; height: '+(parseInt(bluImageH)+1)+'px !important;'); }
			if( $(this).parent().parent().hasClass('tinyGallery') ) {
				$(this).hover(
					function() { $(this).parent().find('span:first').css("background", "url(/img/enlarge-photo_tiny.png) -1px 110% no-repeat"); },
					function() { $(this).parent().find('span:first').css("background", "none"); }
				);
			} else if( $(this).parent().parent().hasClass('video') ) {
				$(this).hover(
					function() { $(this).parent().find('span:first').css("background", "url(/img/play-video_mini.png) -1px 101% no-repeat"); },
					function() { $(this).parent().find('span:first').css("background", "none"); }
				);
			} else if( $(this).parent().parent().hasClass('bluGallery') ) {
				$(this).hover(
					function() { $(this).parent().find('span:first').css("background", "url(/img/enlarge-photo_mini.png) -1px 101% no-repeat"); },
					function() { $(this).parent().find('span:first').css("background", "none"); }
				);
			}
		});
	});
});

BluPlugins = {
	init: function() {
		$('#search form').BluForm({placeholder: headerSearchPlaceholder});
		
		var imageSizeConfig = {
			type: "square", 
			width: imageThumbW, 
			height: imageThumbH, 
			overlay: 'span:first'
		};
		$(".bluGallery .bluImage").ImageSize(imageSizeConfig);
		$(".bluVideoGallery .bluImage").ImageSize(imageSizeConfig);
		
		var imageSizeConfig = {
			type: "square", 
			width: 50, 
			height: 50, 
			overlay: 'span:first'
		};
		$(".tinyGallery .bluImage").ImageSize(imageSizeConfig);
	}
}

Colorbox = {
	init: function(target,mediaType) {
	
		var s = $('#cboxTitle');
		var find = /px$|em$|pt$|%$/; 
		var replace = "";
		var t = s.css('padding-top').replace(find,replace);
		var r = s.css('padding-right').replace(find,replace);
		var b = s.css('padding-bottom').replace(find,replace);
		var l = s.css('padding-left').replace(find,replace);
		var w = false;
		var h = s.css('height');
		var p_tb = parseInt(t)+parseInt(b); // calculate padding
		var p_lr = parseInt(l)+parseInt(r); // calculate padding
		var bo = 28;
		
		switch(mediaType) {
			case 'image':
				var options = {
					opacity: .25,
					initialWidth: 100,
					initialHeight: 100,
					current: "image {current} of {total}",
					onComplete:function(){
						var fullW;
						var fullH;
						if( !$(this).find('.dimensions').length ) {
							fullW = $("#cboxPhoto").width(); fullH = $("#cboxPhoto").height(); // get dimensions of fullsize photo
						} else {
							var fullDimensions = $(this).find('.dimensions').attr('title').split(':'); fullW = fullDimensions[0]; fullH = fullDimensions[1]; // get dimensions of fullsize photo
						}
						if( s.css('bottom').replace(find,replace) < bo || !isNumeric(bo) ) { s.css('bottom',bo); } // restore original css bottom position of caption container
						s.css('bottom',parseInt(s.css('bottom'))-(p_tb)); // set adjusted bottom position of caption container
						if(!w) { var w = fullW-(p_lr); } // calculate width of caption text
						if( s.width() < (w) || !isNumeric(w) ) { s.width(w); } // restore original css width of caption container
						//s.width( s.get(0).clientWidth-(parseInt((l*2))+parseInt((r*2))) ); // set adjusted width of caption container
						s.width( w-(parseInt((l*2))+parseInt((r*2))) ); // set adjusted width of caption container
						if( s.height() > (h) || !isNumeric(h) ) { s.height(h); } // restore original css height of caption container
						s.height(parseInt(s.get(0).clientHeight)); // set adjusted height of caption container
						var newFullHeight = parseInt(fullH) + parseInt( s.height() ); // calculate adjusted height of colorbox
						var newFullWidth = parseInt(fullW);
						if( newFullHeight > pageHeight() ) { // if height of colorbox is taller than the browser inner height
							newFullHeight -= (newFullHeight - pageHeight()) + parseInt(s.height()) + parseInt(bo) + p_tb; // recalculate height 
							$("#cboxPhoto").height( newFullHeight - bo ); $("#cboxPhoto").width( "auto" ); // set height of img tag; set width of img tag to auto
							newFullWidth = ((parseInt(fullW)/parseInt(fullH)) * newFullHeight); newFullWidth -= newFullWidth - $("#cboxPhoto").width(); // recalculate width proportionally based on height
							s.width(newFullWidth - p_lr); // set width of of caption/nav container
							$.colorbox.resize({innerHeight: newFullHeight,innerWidth: newFullWidth}); // resize the colorbox
						} else if( newFullWidth > pageWidth() ) { // if width of colorbox is wider than the browser inner width
							newFullWidth -= (newFullWidth - pageWidth() + 50); // recalculate width if width is larger than screen
							$("#cboxPhoto").width( newFullWidth ); $("#cboxPhoto").height( "auto" ); // set width of img tag; set height of img tag to auto
							newFullHeight = ((parseInt(fullH)/parseInt(fullW)) * newFullWidth); newFullHeight -= newFullHeight - $("#cboxPhoto").height() - bo; // recalculate height proportionally based on width
							s.width(newFullWidth - p_lr); // set width of of caption/nav container
							$.colorbox.resize({innerHeight: newFullHeight,innerWidth: newFullWidth}); // resize the colorbox
						} else {
							$.colorbox.resize({innerHeight: newFullHeight}); // resize the colorbox
						}
						
					}
				};
				
				target.colorbox(options);
				break;
				
			case 'video':
				
				var previous;
				
				target.each(function(i,val) {
					
					if($(this).find('.dimensions').attr('title').length == 1) {
						var splitChr = $(this).find('.dimensions').attr('title');
						$(this).find('.dimensions').attr('title', $(val.hash).find(':first').attr('width')+splitChr+$(val.hash).find(':first').attr('height') )
					}
					
					var options = {
						opacity: .25,
						inline: true,
						innerWidth: parseInt($(val.hash).find(':first').attr('width')),
						innerHeight: parseInt($(val.hash).find(':first').attr('height')),
						href: val.hash,
						initialWidth: 100,
						initialHeight: 100,
						current: "video {current} of {total}",
						onLoad: function() {
							if(previous) {
								$(previous).removeAttr('style');
								$(previous).hide();
							}
							$(val.hash).css('width',$(val.hash).find(':first').attr('width')+'px').show();
							$(val.hash).css('height',$(val.hash).find(':first').attr('height')+'px').show();
							$(val.hash).show();
						},
						onComplete: function(){
							var fullDimensions = $(this).find('.dimensions').attr('title').split(':'); var fullW = fullDimensions[0]; var fullH = fullDimensions[1];
							if( s.css('bottom').replace(find,replace) < bo || !isNumeric(bo) ) { s.css('bottom',bo); } // restore original css bottom position of caption container
							s.css('bottom',parseInt(s.css('bottom'))-(p_tb)); // set adjusted bottom position of caption container
							if(!w) { var w = fullW-(p_lr); } // calculate width of caption text
							if( s.width() < (w) || !isNumeric(w) ) { s.width(w); } // restore original css width of caption container
							//s.width( s.get(0).clientWidth-(parseInt((l*2))+parseInt((r*2))) ); // set adjusted width of caption container
							s.width( w-(parseInt((l*2))+parseInt((r*2))) ); // set adjusted width of caption container
							if( s.height() > (h) || !isNumeric(h) ) { s.height(h); } // restore original css height of caption container
							s.height(parseInt(s.get(0).clientHeight)); // set adjusted height of caption container				
							var newFullHeight = parseInt(fullH) + parseInt( s.height() ); // calculate adjusted height of colorbox
							$(val.hash).find(':first').nextAll().each(function(i,val) {
								newFullHeight += $(val).height();
							});
							$.colorbox.resize({innerHeight: newFullHeight}); // resize the colorbox
							previous = val.hash;
						},
						onCleanup: function() {
							$(val.hash).removeAttr('style');
							$(val.hash).hide();
						}
					};
				
					target.eq(i).colorbox(options);
				});
				
				break;
				
			default:
				break;
		}
		
	}
}

ImageRotator = {
	init: function() {
		if( ($('.imageRotator').length) != 0 && $('.imageRotator .bluGallery').children().length > 1 ) { // ie7 is finicky
			ImageRotator.configureImageRotator();
			ImageRotator.configureImageRotator_nav();
		}
	},
	
	configureImageRotator: function() {		
		
		$('.imageRotator .bluGallery').cycle({
			fx:     'fade', 
			speed:   600, 
			timeout: 6000,
			pager:  '.imageRotator .imageRotator_nav span',
			after: function() {  
				$("#featuredNav > h3").html( $(".bluGallery img:visible").attr("alt") );
				$("#featured .placeholderAnchor").attr('href',$(".bluGallery img:visible").parent().attr("href") );
				$("#featured .placeholderAnchor").css('width',$(".bluGallery img:visible").parent().css("width") );
				$("#featured .placeholderAnchor").css('height',$(".bluGallery img:visible").parent().css("height") );
			}
		});
	},
	
	configureImageRotator_nav: function() {
		$('.imageRotator_nav a').each(function(i) {
			var adjustedIndex = i+1;
			$('.imageRotator_nav a').eq(i).attr('href','rotatorImage_'+adjustedIndex);
		});
	}
}

$(function(){
	
 	$("input[type=checkbox]#rememberPortal").click(function() {
    	
    	var rememberPortalValue;     	
		
		if ($(this).attr('checked') == true)
		{
			var rememberPortalValue = $(this).val();
		}
		else
		{
			var rememberPortalValue = 'public';
		}
			
		//alert("assign "+"rememberPortalValue: "+rememberPortalValue+"\n");
		
		$.post( "/async/metroplan.php", 
			{ 
				asyncAction: 'rememberPortal',
				portal: rememberPortalValue
			},
			function(response){
			
			//alert("jQuery post returned message: "+response.resultMsg);
								
		}, "json");
    	
		return true;
	});
	
});

$(function(){
	$("#main .main .entry td").each(function( i, name ){
		var $td = $(this),
			cellpadding = $td.closest('table').filter(function(j) {
					return !($(this).hasClass('bluFiles'));
				}).attr('cellpadding');
			
		$td.css('padding',cellpadding+'px');
	});
});

