// JavaScript Document

$( function () {
// on DOM ready:
			
	var contact_info = $('div#content table table table:not([border])').addClass('contact-info');
	var last_hover;
	
	
	
	$('div#content table table table[border]').each( function () {
		// each of these inner tables with a border is either a description or a "see also" link.
		var that = $(this);
		
		if ( that.find('i').text().match('See Also') ) {
			that.addClass('see-also');	
		} else {
			that.addClass('description');
		}													   
	});
	
	$('body').append('<div id="info-popup"><div class="contact-info"></div><div class="popup-footer"></div></div>');	
	$('#info-popup').css('opacity', 0).css('display','block');
	
	contact_info.each(function () {
		var that = $(this);
		var member = that.parents('table:first').addClass('member');
		
		member.prev().remove(); // get rid of that extraneous <BR>
		member.parent().addClass('no-padding');
		
		
		/*
		***  Ready the output for munging ***
		*/
		var output = $('<div class="contact-info"><div class="left-column"></div><div class="right-column"></div><div class="bottom-column"></div></div>');
		var left_column = output.children('.left-column');
		var right_column = output.children('.right-column');
		var bottom_column = output.children('.bottom-column');
		
		
		/*
		*** Begin munging ***
		*/
		$('<div class="name"></div>').text( that.prev().text() ).appendTo( left_column );
		$('<div class="address"></div>').html( that.find('tr:first td').html() ).appendTo( left_column );
		$('<div class="phone"></div>').text( that.find('tr:eq(1) ').text() ).appendTo( left_column ); // tr:eq(1) will get the second TR since the index is 0 based.
		
		
		var url = that.prev().find('a');
		if( url.length > 0 ) {
			// the member has a web address.
			$('<div class="website"></div>').append( url.clone().text('Website') ).appendTo( left_column );
		}
		
		var email = that.find('tr:last').prev().find('a');
		if( email.length > 0 ) {
			// the member has an email address.
			$('<div class="email"></div>').append( email.clone() ).appendTo( left_column );
		}
	
		$('<div class="map"></div>').append( that.find('tr:last a').clone().text('Map') ).appendTo( left_column );

		
		/*
		var bba_member = that.parent().find('p i b');
		if( bba_member.length > 0 ) {
			$('<div class="bba-member">BBA Member</div>').appendTo( left_column );
		}
		*/
		
		var logo = member.find('td:first img');
		if( logo.length > 0 ) {
			$('<div class="logo"></div>').append(logo).appendTo( right_column );
		} 
			
		var bba_member = that.parent().find('p i b');
		if( bba_member.length > 0 ) {
			$('<div class="bba-member">BBA Member</div>').appendTo( right_column );	
		}
		
		var description = that.siblings('.description');
		if( description.length > 0 ) {
			
			var everyscape = description.find('a.everyscape-link');
			if( everyscape.length > 0 ) {
				// the member has an Everyscape link
				$('<div class="everyscape"></div>').append(everyscape).appendTo( left_column );
			}
			
			var video = description.find('a.video-link');
			if( video.length > 0 ) {
				// the member has an Everyscape link
				$('<div class="video"></div>').append(video).appendTo( left_column );
			}
			
			
			$('<div class="description"></div>').html( description.html() ).appendTo( bottom_column );
		}
		
		var see_also = that.siblings('.see-also').find('td');
		if( see_also.length > 0 ) {
			// the member has a "see also" link
			$('<div class="see-also"></div>').html( see_also.html() ).appendTo( left_column );
		}
		
		/*
		*** End munging ***
		*/
		
		// Replace our previously displayed member info with a simple DIV, containing an A tag (which points to the member's website), and the member's name.  
		var new_member = $("<div class='member'><a href='" + url.attr('href') + "'  target='_blank'>" + output.find('.name').text() + "</a></div>");
		member.replaceWith(new_member);
		member = new_member; // Then re-assign the variable 'member' so the following code after this block still works.
		
				
		
		/*
		*** Attach behavior to show popup ***
		*/
		member.mouseover(
			function () {
				var offset, width, info_popup;
				
				info_popup = $('#info-popup');
				
				if (last_hover == this) {
					// do nothing
				} else {
					last_hover = this;
					
					info_popup.stop().fadeTo("fast", 0, function () { // stop current animation, and fade out the info box. run following code on completion:
						
						info_popup.find('.contact-info').remove();
						
						info_popup.prepend( output.clone().show() );// clone the hidden 'contact-info', make the clone visible and append to '#info-popup' (it won't appear yet, because '#info-popup' is transparent)
						
						offset = member.offset();
						width = member.width();
						width = width-100;
						
						/*
						if ( $(window).width() > 1300) {
							info_popup.css('top', offset.top + 20).css('left', offset.left + 400).css('right', 'auto');
						} else {
							info_popup.css('top', offset.top + 20).css('right', 0).css('left', 'auto');
						}
						*/
						
						info_popup.css('top', offset.top + 20).css('left', offset.left + 80).css('right', 'auto');
						
						info_popup.animate({opacity: 1.0, top: (offset.top + 10) + 'px'}, 300);
						
						tb_init('a.thickbox');
					});	
				}// close if...else
			}// close func()
		);
		
		member.hover(function () {
			$(this).addClass('hover');
		},
		function () {
			$(this).removeClass('hover');
		});





	});
	
	//Add zebra striping	
	$('.member:even').addClass('alt');
	
	
	$('img.ajax-loader').hide();
	
	$('#content-loading').slideDown('fast');
	
	
	/*
	//fix the hover's position on window resize:
	$(window).resize(function () {
		$('#info-popup').css('left', $(last_hover).offset().left + $(last_hover).width() - $('#info-popup .contact-info').width());
	});
	*/
	
	//select the search box.
	$('#BusinessName').focus().select();
	//$('#BusinessName').focus();
	

	
 	$("#BusinessName").click(function() {
		// or if ( $(this).val() == this.defaultValue") {$(this).val('');}
		if ( $(this).val() == "Search...") {$(this).val('');}
		}
	); 
		
}); // end of 'on DOM ready'

// other functions

// for popupwindow:
function openpopup(url, windowtarget) 
{
    var x=window.open(
        url, 
        windowtarget,
        'width=500,height=480,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0');
	x.focus();
	
}


