$(document).ready(function(){
	defaultTitle = $('div#txt h1:first').text();
	defaultNav = $('ul.subnav:visible');
	defaultNavRel = defaultNav.attr('rel');
	thumbsHover();
	listingHover();
	iconHover();
	// subNav();
	lightbox('a[rel=lightbox]');
	window.makeMailLinks()?makeMailLinks():null;
	dropdownList();
});

function subNav(){
	$('ul#nav>li>a').hover(function(){
		newNavRel = $(this).attr('rel');
		newNav = $('ul#'+newNavRel);
		currentNav = $('ul.subnav:visible');
		currentNavId = currentNav.attr('id');
		if(currentNav.size()>0){
			if(newNavRel!=currentNavId){
				currentNav.fadeOut(50, function(){
					newNav.fadeIn(100);
				});
			}
		}
		else{
			newNav.fadeIn(100);
		}
	});
	
//	$('div#top').hover(function(){
		
//	}, function(){
//		if(defaultNavRel!=currentNavId){
//			currentNav.fadeOut(50, function(){
//				defaultNav.fadeIn(100);
//			});
//		}
//	});

}

function thumbsHover(){

	thumbListLink = $('ul.thumblist li a');
	
	thumbListLink.hover(function(){
	
		hoverNumber = thumbListLink.index(this);
		$('ul.listing.effect li a').removeClass('active');
		$('ul.listing.effect li a:eq('+hoverNumber+')').addClass('active');
		
		replaceTitle($(this).html());

	}, function(){
		$('ul.listing.effect li a').removeClass('active');
		replaceTitle(defaultTitle);
	});
	
}

function listingHover(){
	listingLink = $('ul.listing.effect li a');
	listingLink.hover(function(){
		hoverNumber = listingLink.index(this);
		hoverNumberThumb = $('ul.thumblist li a:eq('+hoverNumber+')');
		$('ul.thumblist li a:eq('+hoverNumber+')').addClass('active');
		replaceTitle(hoverNumberThumb.html());
	}, function(){
		$('ul.thumblist li a').removeClass('active');
		replaceTitle(defaultTitle);
	});
}

function iconHover(){
	$('span.icon').hover(function(){
		positionTooltip($(this));
	}, function(){
		removeTooltip($(this));
	});
}

function positionTooltip(element){
	element.children().fadeIn(200);
	$(element).mousemove(function(e){
		$(this).children().css({top: e.pageY+17, left: e.pageX});
	});
	
}

function removeTooltip(element){
	element.children().hide();
}

function replaceTitle(string){
	titleToReplace = $('div#txt>h1:first');
	titleToReplace.html(string);
}
function lightbox(e){
	$(e).click(function(){
		$('body').append('<div id="overlay" style="display:none;" title="click anywhere to close"></div><div id="lightboxcontainer" style="display:none"><div id="close" style="display: none;" title="click anywhere to close"></div><div id="lightboxcontent" style="display:none;cursor:pointer;" title="click anywhere to close"></div></div>');
		$('#overlay').height($(document).height()).show();
		$('#close,#overlay,#lightboxcontent').click(function(){
			$('#lightboxcontainer,#overlay').remove();
			});
		var img = new Image();
		img.onload = function(){
				$('#lightboxcontainer').css({
					"margin-left": "-"+parseInt(this.width)/2+'px',
					"margin-top": "-"+parseInt(this.height)/2+'px'
				});
				$('#lightboxcontainer').show();
				$('#lightboxcontent').append('<img src="'+$(this).attr('src')+'" />').fadeIn();
				$('#close').fadeIn();
				
			};
		img.src=$(this).attr('href');
		return false;
		});
	}
	
/* <span class="email">x (at) x.com</span> becomes <a href="x@x.com">x@x.com</a> 
* this is an anti-spam measure
*/
function makeMailLinks(){
	$('span.email').each(function(){
		var theAddress = $(this).text().replace(/ \(at\) /, "@");
		$(this).replaceWith($('<a href="mailto:'+theAddress+'">'+theAddress+'</a>'));
		});
}


/* BELOW IS ALL JS CODE ADDED WITH THE UPDATE OF 25 FEBRUARY 2011 */

function dropdownList(){
	var trigger = $('ul.dropdownlist>li>a');
	trigger.click(function(){
		var clicked = $(this);
		$('ul.dropdownlist>li>ul').hide();
		clicked.next('ul').show();
		return false;
	});
}
