$(document).ready(function() {
	Construct.init();
});

var Construct = new function ConstructObject() {
	this.init = function() {
		Event.bindGeneralEvents();
		Event.bindFundEvents();
		Event.bindPopoverEvents();
		Event.bindWorkEvents();
		Event.bindLetterEvents();
		Funds.init();
	};
};

var Funds = new function FundsObject() {
	this.active;
	
	this.init = function() {
		Funds.active = $("#content .funds .fund .copy > div:first-child").css({ display: "block" });
		$("#content .funds .fund .nav ul li:first-child a").click();

		Funds.active.each(function() {
			var has_image = $(this).find(".leftAlign, .rightAlign").length;
			$(this).parent().css({ height: Funds.active.outerHeight({ margin: true }) - (has_image ? 19 : 3) });
		});
	};
};

var Popover = new function PopoverObject() {
	this.toggle = function(id) {
		var active = ($("#dither").css("display") != "none");
		
		if(active) {
			$("#dither").fadeOut(300);
			$("#popover").fadeOut(300);
		} else {
			$("#p_" + id).css({ display: "block" }).siblings().css({ display: "none" });
			$("#dither").css({ height: ($(document).height() > $(window).height() ? $(document).height() : $(window).height()), display: "block", opacity: 0.0 }).fadeTo(300, 0.9);
			Popover.fixPosition(id);
		}
	};
	
	this.fixPosition = function(id) {
		var width;
		$("#popover").css({ marginTop: -($("#popover").outerHeight() / 2) }).fadeIn(300);
		if(!!id) {
			_width = id != "letter" ? 500 : 590
			$("#popover").css({ width: _width, marginLeft: -(_width / 2) });
		
			width = 0;
			$("#p_" + id).children("." + id).each(function() {
				if($(this).css("display") == "block") {
					width = $(this).outerWidth() > width ? $(this).outerWidth() : width;
				}
			});
			$("#popover").css({ width: (width > 0 ? width : 500), marginLeft: -(width / 2) });
		}
		
	};
};

var Event = new function EventObject() {
	this.bindGeneralEvents = function() {
		$('.enablePopUp').unbind('click').click(function() {
			var dimensions = !!$(this).attr('id') ? $(this).attr('id').replace(/p\d_/, '').split('x') : [675, 340];
			window.open($(this).attr('href'), $(this).attr('id'), 'menubar=no,width=' + dimensions[0] + ',height=' + dimensions[1] + ',toolbar=no');
			return false;
		});
	};
	
	this.bindFundEvents = function() {	
		$("#content .funds .fund .nav ul li a").click(function() {
			var $pointer	= $(this).parents(".nav").siblings(".pointer");
			var section		= $(this).attr("id").replace(/n_/, "");
			
			Funds.active	= $("#" + section);
			
			$pointer.stop().animate({ marginLeft: $(this).position().left + ($(this).outerWidth({ margin: true }) / 2) - 4 }, 500, "easeOutQuint");
			
			Funds.active.siblings().fadeOut(300, function() {
				var has_image = Funds.active.find(".leftAlign, .rightAlign").length;
				Funds.active.parent().stop().animate({ height: Funds.active.outerHeight({ margin: true }) - (has_image ? 19 : 3) }, 500, "easeOutQuint");
				Funds.active.fadeIn(500);
			});
			
			return false;
		});
	};
	
	this.bindPopoverEvents = function() {
		$(".enablePopOver").unbind("click").click(function() {
			Popover.toggle($(this).attr("id"));
			return false;
		});
		$("#dither, #popover .close").click(function() {
			Popover.toggle();
			return false;
		});
	};
	
	this.bindWorkEvents = function() {
		Work.active = $("#popover .work .thumbs ul li:first a");
		Work.active.parent().siblings().fadeTo(1, 0.4);
	
		$("#popover .work .thumbs ul li a").unbind("click").click(function() {
			var src = $(this).children("img").attr("src").replace(/small/, "large");
			Work.active	= $(this);
			Work.active.parent().siblings().fadeTo(500, 0.4);
			Work.active.parent().fadeTo(500, 1.0);
			Work.viewer	= $(this).parents(".work").find(".viewer");
			Work.swapImage(src);		
			return false;	
		});
		
		$("#popover .work .viewer img").unbind("click").click(function() {
			if(Work.active.parent().next().not(".clear").length) {
				Work.active.parent().next().children("a").click();
			} else {
				Work.active.parent().siblings(":first-child").children("a").click();
			}
			
			return false;
		});
	};
	
	this.bindLetterEvents = function() {
		$("#p_letter .viewWork").unbind("click").click(function() {
			$("#p_letter").css({ display: "none" });
			$("#p_work").css({ display: "block" });
			Popover.fixPosition("work");
			return false;
		});
	};
};

var Work = new function WorkObject() {
	this.active;
	
	this.swapImage = function(src) {
		Work.viewer.children("img").fadeTo(300, 0.0, function() {
			$(this).attr("src", src).load(function() {
				Work.showImage();
			});
		});
	};
	
	this.showImage = function() {
		Work.viewer.children("img").fadeTo(300, 1.0);
		Popover.fixPosition();
	};
};