//http://code.drewwilson.com/entry/tiptip-jquery-plugin

(function($){
	$(function () {
		
		/*$('#nav a').click(function () {
			$('#nav div.active').removeClass('active');
			$(this).closest('div').addClass('active');
		});*/
		
		$('#photos .photo div.enlarge').click(function () {
			//popup big view
			showPhoto($(this));
			
			//stop redirect
			return false;
		});
		
		$('#popup-background, #popup-close').click(function() {
			hidePhoto()
		});
		$('#popup-container').click(function () { return false; });
		
		$('#popup-nav span').hover(function () {
			$(this).addClass('hover');
		}, function () {
			$(this).removeClass('hover');
		});
		
		$('#popup-previous').click(function () {
			var curr = $('#popup-position').val();
			
			if($('#' + curr).prev().size() > 0) {
				showPhoto($('#' + curr).prev().children('a.enlarge'));
			}
			
			return false;
		});
		
		$('#popup-next').click(function () {
			var curr = $('#popup-position').val();
			
			if($('#' + curr).next().size() > 0) {
				showPhoto($('#' + curr).next().children('a.enlarge'));
			}
			
			return false;
		});
		
		$('#mask').click(function () {
			$('input.password').each(function () {
				if($(this)[0].type == 'text') {
					$(this)[0].type = 'password';
				}
				else {
					$(this)[0].type = 'text';
				}
			});
		});
		
		var showPhoto = function($item) {
			$('#popup-container').css({
				'margin-top': window.pageYOffset + 20
			});
			
			$('#popup-position').val($item.closest('.photo').attr('id'));
			
			if($('#popup-background:visible').size() == 0) {
				$('#popup-image').html($item.closest('.photo').children('.popup').html());
				
				$('#popup-background').height($(document).height()).fadeIn(250);
			}
			else {
				$('#popup-image').fadeOut(250, function () {
					$(this).html($item.closest('.photo').children('.popup').html());
				}).fadeIn(250);
			}
			
		}
		
		var hidePhoto = function() {
			$('#popup-background').fadeOut(500);
		}
	});
})(jQuery);

// Fix FOUT for font replacement (http://paulirish.com/2009/fighting-the-font-face-fout/)
(function(){
	// if firefox 3.5+, hide content till load (or 3 seconds) to prevent FOUT
	var d = document, e = d.documentElement, s = d.createElement('style');
	if(e.style.MozTransform === '') { // gecko 1.9.1 inference
		s.textContent = 'body{visibility:hidden}';
		e.firstChild.appendChild(s);
		var f = function(){s.parentNode && s.parentNode.removeChild(s);};
		addEventListener('load',f,false);
		setTimeout(f,3000);
	}
})();
