var mm = {
	
	init: function () {
		
		$(".object").each(function(){
			$(this).bind("mouseout", function(){
				mm.tooltip.hide(this);
			});
		});
		
		$(".handle").each(function(){
			$(this).bind("click", function(){
				mm.tooltip.show(this);
			});
		});

		this.sp = $(".scrollpane");
		if (this.sp.length) {

			this.scrollpane = $(".scrollpane").jScrollPane({
				showArrows: true,
				horizontalGutter: 39,
				verticalGutter: 39,
				horizontalArrowPositions: "after",
				hideFocus: true
			});
			
			$(window).bind("resize", function(){
				mm.resize();
			}).trigger("resize");
			
		}
		
		$(window).load(function(){
			$("#loading").hide();
			$(".news-item").each(function(){
				var item = $(this);
				var image = item.find("> .static-image").height();
				var text = item.find("> .static-text").height();
				var height = image > text ? image : text;
				height += 50;
				item.css({
					"height": height,
					"visibility": "visible",
					"background-position": "0 "+(height-22)+"px"
				});
			});
		});
		
	},
	
	tooltip: {
		show: function (handle) {
			var handle = $(handle);
			handle.parent().addClass("hover");
		},
		hide: function (obj) {
			var obj = $(obj);
			obj.removeClass("hover");
		}
	},
	
	resize: function () {
		
		var windowHeight = $(window).height();
		var contentHeight = windowHeight - 178 - 35;
		if (contentHeight > 515) contentHeight = 515;
		if (contentHeight < 395) contentHeight = 395;
		var objectHeightLarge = contentHeight - 35;
		var objectHeightSmall = Math.floor(objectHeightLarge / 3 * 2);
		$(".scrollpane").css("height", contentHeight).find("li").css("height", objectHeightLarge);
		
		// var containerWidth = 284 + 39;
		var containerWidth = 0;
		if ($(".description").length) containerWidth += 284;
		
		$(".object").each(function(){
			
			var obj = $(this);
			var ratio = obj.data("ratio");
			var width = Math.floor(obj.hasClass("small") ? ratio * objectHeightSmall : ratio * objectHeightLarge);
			var height = Math.floor(obj.hasClass("small") ? objectHeightSmall : objectHeightLarge);
			obj.css({
				"width": width,
				"height": height
			});
			containerWidth += width;
		
		});

		$(".portfolio").css({
			"width": containerWidth,
			"height": objectHeightLarge
		});
		
		this.scrollpane.data("jsp").reinitialise();
		
	}
	
};

$(function(){mm.init();});
