/**
 * Albertsons Homepage
 * Version 1.0.0 - 11/23/2008
 * @author Benjamin Truyman
 **/

var BEEF = BEEF || {};

// Albertsons Homepage
BEEF.Homepage = {
	initialize: function () {
		if ($('#tabbed-body').get(0)) { BEEF.Homepage.TabbedContent.initialize(); }

		$('a[rel="external"]').click(function () {
			window.open($(this).attr('href'));
			return false;
		});
	}
};

// Tabbed Content
BEEF.Homepage.TabbedContent = {
	initialIndex: 0,
	containers: {},
	navigation: [],
	sections: [],
	
	initialize: function () {
		// Define Containers
		this.containers.main = $('#tabbed-body').get(0);
		this.containers.viewport = $('#tabbed-body-viewport').get(0);
		this.containers.navigation = $('#tabbed-nav').get(0);
		
		// Build Sections
		this.buildSections();
		
		// Build Navigation
		this.buildNavigation();
		
		// Force the tabbed body to move back to the left
		this.containers.main.scrollLeft = 0;
		
	},
	
	getSections: function () {
		var that = this;
		var items = [];
		
		// Find all navigation elements in navigation element
		$('li.section', this.containers.viewport).each(function () {
			items.push(new BEEF.Homepage.TabbedContent.Section(this));
		});
		
		return items;
	},
	
	getNavigation: function () {
		// Find all navigation elements in navigation container
		return $('li a', this.containers.navigation).get();
	},
	
	buildSections: function () {
		var that = this;
		
		this.sections = this.getSections();
		
		$(this.sections).each(function (index) {
			this.build();
			if (index !== that.initialIndex) {
				$(this.dom).hide();
			}
		});
	},
	
	buildNavigation: function () {
		var that = this;
		
		this.navigation = this.getNavigation();
		
		$(this.navigation).each(function (index) {
			if (index === that.initialIndex) {
				$(this).addClass('active');
			}
			$(this).mousedown(that.handleNavigation).click(ABS.Utils.dummyEventHandler);
		});
	},
	
	handleNavigation: function (e) {
		var that = BEEF.Homepage.TabbedContent;
		
		e.preventDefault();
		
		// Deactivate all navigation items
		$(that.navigation).each(function () {
			$(this).removeClass('active');
		});
		
		// Make navigation item active
		$(this).addClass('active');
		
		// Hide all sections
		$(that.sections).each(function () {
			$(this.dom).hide();
		});
		
		// Hide popups contained in tabbed body
		BEEF.Homepage.TabbedContent.PersonalizedCoupons.hidePopup();
		
		// Show new section
		$(this.hash).show();
	}
};

// Tabbed Content Section class
BEEF.Homepage.TabbedContent.Section = function (el) {
	if (arguments.length !== 1) {
		throw new Error('Expected an argument of one but recieved ' + arguments.length);
	}
	
	this.dom = el;
	
	this.information = {
		currentlyOpen: null
	};
	this.options = {
		closedHeight: 61,
		openedHeight: 200,
		duration: 700
	};
};

BEEF.Homepage.TabbedContent.Section.prototype = {
	build: function () {
		// Retrieve children (subsections)
		this.children = this.getChildren();
		
		// Build header links
		this.buildHeaders();
		
		// Close all but first subsection
		for (var i=0; i < this.children.length; i++) {
			/*if (i !== 0) {*/
				this.information.currentlyOpen = 20;
				this.closeChild(i, false);
			/*}*/
		}
		
	},
	
	getChildren: function () {
		return $('div.subsection', this.dom).get();
	},
	
	getChildIndexById: function (id) {
		$(this.children).each(function(index) {
			if(this.id == id) {}
		});
	},
	
	buildHeaders: function () {
		var that = this;
		
		for (var i=0; i < this.children.length; i++) {
			/*$('h3', this.children[i]).each(function () {
				this.innerHTML = '<a href="#">' +  this.innerHTML + '</a>';
			});*/
			$('.clickArea', this.children[i]).each(function () { /*3 > a*/
				var x = i;
				$(this).bind('mousedown', function (e) {
					that.handleHeaderLink(e, x);
				}).bind('click', ABS.Utils.dummyEventHandler);
			});
		}
	},
	
	closeChild: function (index, animate) {
		if (typeof animate === undefined || animate === true) {
			$(this.children[index]).animate({
				height: this.options.closedHeight
			}, this.options.duration, function () {
				// Hides this child nodes children
				// This fixes an issue when the user attempts to tab through to hit form inputs
				$(':input', this).hide();
			}).addClass('closed');
			
			// TODO: Abstract this out
			/*ABS.Homepage.TabbedContent.PersonalizedCoupons.hidePopup();
			ABS.Homepage.TabbedContent.PreferredSavings.hidePopup();
			ABS.Homepage.TabbedContent.RecipeSearch.hidePopup();*/
			
		} else {
			$(this.children[index]).css('height', this.options.closedHeight).addClass('closed');
			// Hides this child nodes children
			// This fixes an issue when the user attempts to tab through to hit form inputs
			$(':input', this.children[index]).hide();
		}
	},
	
	openChild: function (index, animate) {
		$(this.children[index]).animate({
			height: this.options.openedHeight
		}, this.options.duration).removeClass('closed');
		
		// Show a section's children to prevent issues with tabbing
		$(':input', this.children[index]).show();
	},
	
	handleHeaderLink: function (e, openIndex) {
		var that = this;

		e.preventDefault();
		
		// Don't do anything if user clicks currently open section
		if (openIndex === this.information.currentlyOpen) return;
		
		if (this.information.currentlyOpen < 20) 
			this.closeChild(this.information.currentlyOpen, true);
		this.information.currentlyOpen = openIndex;
		this.openChild(openIndex);
	}
};

$(document).ready(function () {
	BEEF.Homepage.initialize();
});
