// Copyright (c) 20011 REDF (jhulbert AT redf DOT com || http://www.redf.com)
// Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
// and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
(function($) {
	var opts;
	var scaler = $(".scaler"),
		wrap = $("#content-wrap"),
		cell = $(".i-cell"),
		cellContent = $(".cell-content"),
		cellOverlay = $(".cell-overlay"),
		fullContent = $(".full-content"),
		menu = $("#menu"),
		menuToggle = $("#menu-toggle"),
		scalerW = scaler.outerWidth(true),
		scalerH = 0,
		wrapW = wrap.outerWidth(true),
		wrapH = 0,
		minW = 686,
		minH = minH * .75,
		numCols = 4,
		scalerRatio = minH / minW,
		currScale = scalerW / minW,
		defaultCSS = new Array(),
		cellW = 512,
		cellH = 384,
		cellRatio = cellH / cellW;

	var methodObj = {
		init: function(options) {
			opts = $.extend({
			}, options);
			
			methodObj.captureDefaultCSS();
			
			// Add menu toggle event
			menuToggle.click(function(evt) {
				evt.preventDefault();
				methodObj.toggleMenu();
			});
			// Tie the menu links to their associated cells
			$(".index-link").mouseenter(
				function() {
					aC = $(this).attr("rel");
					$(".cell-overlay#" + aC).addClass("active");
				}).mouseleave(
				function() {
					$(".cell-overlay#" + aC).removeClass("active");
				})
			
			// Bind resize event
			$(window).bind("resize load", function() {
				methodObj.resizeScaler();
			}, false);
			
			methodObj.resizeScaler();
		},

		resizeScaler: function() {
			// Capture dimensions of wrapper
			wrapW = wrap.outerWidth(true) - scaler.offset().left;
			wrapH = wrap.outerHeight(true) - scaler.offset().top;
			scalerW = scaler.outerWidth();
			scalerH = scaler.outerHeight(true);
			// Maintain aspect ratio when resizing
			if (wrapW / wrapH > scalerW / scalerH && wrapW > minW) {
				scalerRatio = minH / minW;
				scaler.width(wrapW);
				scaler.height(wrapW * scalerRatio);
			}
			else if (wrapH > minH) {
				scalerRatio = minW / minH;
				scaler.width(wrapH * scalerRatio);
				scaler.height(wrapH);
			}
			else {
				scaler.width(minW);
				scaler.height(minH);
			}
			
			currScale = scalerW / minW;
			methodObj.resizeGrid();
		},

		resizeGrid: function() {
			var cCount = 1;
			var rCount = 1;
			
			cell.each(function(i, el) {
				if (cCount == numCols) {
					$(el).css({
						"margin-right":0,
						"border-right":"none"
					});
					cCount = 1;
					rCount += 1;
				} else {
					$(el).css({
						"margin-right":defaultCSS["margin-right"],
						"border-right-width":defaultCSS["border-right-width"],
						"border-right-style":defaultCSS["border-right-style"],
						"border-right-color":defaultCSS["border-right-color"]
					});
					cCount += 1;
				}
			})
			cell.width((scaler.width() / numCols) - (cell.outerWidth(true) - cell.width()));
			cell.height(cell.width() * cellRatio);
			// Resize cell content
			cellContent.width(cell.width() - (cellContent.outerWidth(true) - cellContent.width()));
			cellContent.height(cell.height() - (cellContent.outerHeight(true) - cellContent.height()));
			
			fullContent.css({"width":$(window).width()});
		},
		
		captureDefaultCSS:function() {
			// Create an object containing the default css properties and values
			var props = ["width", "height", "margin-top", "margin-right", "margin-bottom", "margin-left", "padding-top", "padding-right", "padding-bottom", "padding-left", "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", "border-top-color", "border-right-color", "border-bottom-color", "border-left-color","border-top-style", "border-right-style", "border-bottom-style", "border-left-style"];
			$(props).each(function(i) {
				defaultCSS[props[i]] = cell.css(props[i]);
			});
		},

		toggleMenu: function() {
			if (menu.hasClass("open")) {
				menu.animate({
					left: -menu.width()
				}, 500, "easeInOutCirc")
				scaler.animate({
					left: "80px"
				}, 500, "easeInOutCirc", methodObj.resizeScaler);
				$("#toggle-left").stop().fadeOut(300);
				$("#toggle-right").stop().fadeIn(300);
			}
			else {
				menu.animate({
					left: "0px"
				}, 500, "easeInOutCirc");
				scaler.animate({
					left: menu.width() + 80
				}, 500, "easeInOutCirc", methodObj.resizeScaler);
				$("#toggle-right").stop().fadeOut(300);
				$("#toggle-left").stop().fadeIn(300);
			}
			menu.toggleClass("open");
		}
	}

	$.fn.indexGrid = function(_method) {
			var method = methodObj[_method];
			if (method) {
				return method.apply(this, Array.prototype.slice.call(arguments, 1));
			}
			else if (typeof method === 'object' || !method) {
				return methodObj.init.apply(this, arguments);
			}
			else {
				$.error("The method " + _method + " does not exist in jQuery.indexGrid");
			}
		}

	$.fn.indexGrid('init');

})(jQuery);

