var pandg = window.pandg || {};

pandg.main = (function($) {
	
	var initFonts = function() {
		Cufon.replace('.Knockout', { fontFamily: 'Knockout', textShadow: '#000 0 1px' });
		Cufon.replace('.Knockout67', { fontFamily: 'Knockout67', textShadow: '#000 0 1px' });
		Cufon.replace('.Ziggurat', { fontFamily: 'Ziggurat', textShadow: '#000 0 1px' });
		Cufon.replace('.sentinel-18', { fontFamily: 'Sentinel Book', textShadow: '#000 0 1px' });
		Cufon.replace('.sentinel-italic-18', { fontFamily: 'Sentinel Semibold', textShadow: '#000 0 1px' });
		Cufon.replace('.ziggurat-34', { fontFamily: 'Ziggurat', textShadow: '#000 0 1px' });
		Cufon.replace('.knockout-18', { fontFamily: 'Knockout', textShadow: '#000 0 1px' });
		Cufon.replace('.knockout-15', { fontFamily: 'Knockout', textShadow: '#000 0 1px' });
		Cufon.replace('#mainmenu a', { fontFamily: 'Sentinel Semibold', textShadow: 'rgba(0, 0, 0, 0.6) 0 1px' });
		Cufon.replace('#sidebar h3', { fontFamily: 'Ziggurat', textShadow: '#000 0 1px' });
		Cufon.replace('.Security #content h2', { fontFamily: 'Ziggurat', textShadow: '#000 0 1px' });
	}
	
	var initTwitterScroller = function() {
		$jScroller.add("#twitter-feed", "#twitter-data", "left", 1, 1);
		$jScroller.start();
	}
	
	var initLightbox = function() {
		if (!$.fn.fancybox) return;

		$('a.lightbox').live('click', function() {
			$(this)
				.parents('ul:eq(0)')
					.find('li.cloned a')
						.attr('rel', '')
					.end()
				.end()
			.fancybox({
				titleShow: false,
				title: false
			}).trigger("click");
			return false;
		});
	}
	
	var watermark = {
		init: function() {
			$('form').addClass('watermark-enabled');
			watermark.bindEvents();
		},
		bindEvents: function() {
			$("input:text, input:password, textarea")
				.blur(function() {
					if (!$(this).val()) {
						watermark.getLabel(this).fadeIn("fast");
					}
				})
				.keydown(function() {
					watermark.getLabel(this).hide();
				})
				.focus(function() {
					watermark.getLabel(this).hide();
				})
				.each(function() {
					if ($(this).val()) {
						watermark.getLabel(this).hide();
					}
				});
		},
		getLabel: function(input) {
			return $(input).parents('.field:eq(0)').find('label');
		}
	}
	
	var work = {
		baseSegment: '/work/',
		
		init: function() {
			work.bindEvents();
			
			var hash = window.location.hash;
			var urlSegment = hash.replace(/^#/, '');
			
			if (urlSegment) {
				work.loadProject(work.baseSegment + urlSegment);		
			}
		},
		bindEvents: function() {
			$('#projects #listing a').click(function() {
				var url = $(this).attr('href');
				if (url) work.loadProject(url);
				return false;
			});
			$('#project-close').live('click', function() {
				var detail = $('#projects #detail');
				work.closeAnimation(detail);
				return false;
			});
			$('#previous-project, #next-project').live('click', function() {
				var url = $(this).attr('href');
				if (url) work.loadProject(url);
				return false;
			});
			$('#projects .slider')
				.live('mouseenter', function() {
					$(this).addClass('over');
				})
				.live('mouseleave', function() {
					$(this).removeClass('over');
				});
		},
		loadProject: function(url) {
			$.ajax({
				url: url,
				success: function(data) {
					var detail = $('#projects #detail');
					
					if (detail.css('display') == 'none') {
						work.openAnimation(detail, data);
					} else {
						work.transitionAnimation(detail, data);
					}
				}
			});
		},
		bindProjectEvents: function() {
			var slider = $('#project-wrap .slider');
			$.removeData(slider);
			
			slider.anythingSlider({
				autoPlay: false,
				buildNavigation: false,
				forwardText: '&raquo;',
				backText: '&laquo;'
			});
		},
		openAnimation: function(el, content) {
			el
				.css({
					height: 0,
					display: 'block',
					opacity: 0
				})
				.html(content)
				.wrapInner('<div/>')
				.animate({
					opacity: 1,
					height: $('div:first', el).height()
				}, function() {
					$('#listing .title').css('position', 'relative'); //IE7 bug
				});
				Cufon.refresh();
				work.bindProjectEvents();
		},
		closeAnimation: function(el) {
			el
				.animate({
					opacity: 0,
					height: 0
				}, function() {
					$(this).css('display', 'none');
					$('#listing .title').css('position', 'static'); //IE7 bug
				});
		},
		transitionAnimation: function(el, content) {
			el
				.css('height', el.height())
				.animate({opacity:0}, function() {
					$(this)
						.html(content)
						.wrapInner('<div/>')
						.animate({
							opacity: 1,
							height: $('div:first', el).height()
						});
						Cufon.refresh();
						work.bindProjectEvents();
				});
		}
	}
	
	return {
		init: function() {
			initFonts();
			
			$(function() {
				initTwitterScroller();
				initLightbox();
				watermark.init();
				work.init();	
			});
		}
	}
	
})(jQuery).init();

