var pss = new PSS();

function PSS() {
	this.collapsableAllow = true;
	this.allowScroller = true;
	this.allowProjectLoad = true;
	this.init = function() {
		this.domFixes();
		this.domEnhancements();
	}
	this.domFixes = function() {
		if($('#project-view, #people, #quicklinks, #projects, #project-list').size() > 0) {
			this.fauxColumns();
		}
	}
	this.domEnhancements = function() {
		if($('.collapsable').size() > 0) {
			$('.collapsable').each(function() {
				var content = $(this).find('.collapsable-content');
				$(this).find('.collapse-action').click(function() {
					if(pss.collapsableAllow) {
						pss.collapsableAllow = false;
						if(content.is(':hidden')) {
							content.slideDown('fast', function() {
								pss.collapsableAllow = true;
							});
							$(this).text('Hide');
							$(this).removeClass('hidden');
						}
						else {
							content.slideUp('fast', function() {
								pss.collapsableAllow = true;
							});
							$(this).text('Show All');
							$(this).addClass('hidden');
						}
					}
				});
			});
		}
		if($('#featured-projects').size() > 0) {
			var ml = 0;
			var speed = 300;
			$('#featured-prev a, #featured-next a').click(function() {
				ml = parseInt($('#projects').css('marginLeft').replace('px', ''));
				return false;
			});
			$('#featured-prev a').click(function() {
				if(ml != 0 && pss.allowScroller) {
					pss.allowScroller = false;
					$('#projects').animate({
						marginLeft: '+=285px'
					}, speed, function() {
						pss.allowScroller = true;
					});
				}
			});
			$('#featured-next a').click(function() {
				if(ml != -570 && pss.allowScroller) {
					pss.allowScroller = false;
					$('#projects').animate({
						marginLeft: '-=285px'
					}, speed, function() {
						pss.allowScroller = true;
					});
				}
			});
		}
		if($('#project-view').size() > 0) {
			if($('#project-view').hasClass('home')) {
				$('.arrow-button-next, .arrow-button-prev').click(function() {
					if(pss.allowProjectLoad) {
						pss.allowProjectLoad = false;
						$('#project-view').load($(this).attr('href').replace('view', 'request') + '?home', function() {
							pss.domFixes();
							pss.domEnhancements();
							pss.allowProjectLoad = true;
						});
					}
					return false;
				});
			}
			else {
				$('.arrow-button-next, .arrow-button-prev').click(function() {
					if(pss.allowProjectLoad) {
						pss.allowProjectLoad = false;
						$('#project').load($(this).attr('href').replace('view', 'request'), function() {
							pss.domFixes();
							pss.domEnhancements();
							pss.allowProjectLoad = true;
						});
					}
					return false;
				});
			}
		}
	}
	this.fauxColumns = function() {
		if($('#project-view').size() > 0) {
			var titleHeight = $('#project-view-title').height();
			var descriptionHeight = $('#project-view-description').height();
			if(titleHeight > descriptionHeight) {
				$('#project-view-description').height(titleHeight);
			} else {
				$('#project-view-title').height(descriptionHeight);
			}
		}
		if($('#quicklinks').size() > 0) {
			var heights = [];
			heights.push($('#quicklinks-info').height());
			$('#quicklinks li').each(function() {
				heights.push($(this).height());
			});
			heights.sort();
			heights.reverse();
			$('#quicklinks-info').height(heights[0] - 60);
			$('#quicklinks li').each(function() {
				$(this).height(heights[0]);
			});
		}
		if($('#projects').size() > 0) {
			var heights = [];
			$('#projects li').each(function() {
				heights.push($(this).height());
			});
			heights.sort();
			heights.reverse();
			$('#projects li').each(function() {
				$(this).height(heights[0]);
			});
		}
		if($('#other-projects').size() > 0) {
			var heights = [];
			$('#other-projects li').each(function() {
				heights.push($(this).height());
			});
			heights.sort();
			heights.reverse();
			$('#other-projects li').each(function() {
				$(this).height(heights[0]);
			});
		}
		if($('#people').size() > 0) {
			$('.collapsable').each(function() {
				var imagePadding = $(this).find('.content-left img').size() * 10;
				if($(this).find('.content-left').height() > $(this).find('.content-right').height()) {
					$(this).find('.content-right').height($(this).find('.content-left').height() + imagePadding);
				}
				else {
					$(this).find('.content-left').height($(this).find('.content-right').height() - imagePadding);
				}
			});
		}
		if($('.content').size() > 0) {
			$('.content').each(function() {
				var imagePadding = $(this).find('.content-left img').size() * 10;
				if($(this).find('.content-left').height() > $(this).find('.content-right').height()) {
					$(this).find('.content-right').height($(this).find('.content-left').height() + imagePadding);
				}
				else {
					$(this).find('.content-left').height($(this).find('.content-right').height() - imagePadding);
				}
			});
		}
	}
}

$(function() {
	pss.init();
});
