BB.App.FindAPlan = function (elements, data) {
	
	this.model = new BB.App.FindAPlan.model();
	this.view = new BB.App.FindAPlan.view();
	this.controller = new BB.App.FindAPlan.controller();
	
	this.model.init(this.view, this.controller, data);
	this.view.init(this.model, this.controller, elements);
	this.controller.init(this.model, this.view);
	
};


/**
 * Data Model for FindAPlan
 * @version 1.0
 * @author Scott Byrns
 * @returns void
 */
BB.App.FindAPlan.model = function () {
	
	var _view;
	var _controller;
	var _step;
	this.data;
	
	
};

BB.App.FindAPlan.model.prototype = {
	/**
	 * Initilize the data model
	 * @param {object} view Reference to the applications view
	 * @param {object} controller Reference to the applications controller
	 * @param {object} data application data
	 */
	init: function (view, controller, data) {
		_view = view;
		_controller = controller;
		_step = 1;
		_path = [];
		this.data = data;
	},
	
	/**
	 * Return current step
	 * @returns interger representation of step
	 */
	getStep: function () {
		return _step;
	},
	
	/**
	 * Jump to a previous step tab by interger representation of step
	 * @param {Number} tabNumber Interger representation of step
	 */
	jumpToTab: function (tabNumber) {

		while (tabNumber-1 < _path.length) {
			_path.pop();
		}

		_step = tabNumber;
		_view.jumpToTab(_step);
		var data = this.data[_step-1][0];
		
		for (var i = 0, len = _path.length; i < len; i+=1) {
			data = data[_path[i]];
		}

		if (data[0].data.type == 'landing') {
			_view.renderJump(data[0], _step);
		}
		else {
			_view.renderStep(data, _step);
		}
	},
	
	/**
	 * Move to the next tab
	 */
	nextTab: function () {

		_step += 1;
		_view.jumpToTab(_step);
		
		var data = this.data[_step-1][0];
		
		for (var i = 0, len = _path.length; i < len; i+=1) {
			data = data[_path[i]];
		}
		
		if (data[0].data.type == 'landing') {
			_view.renderJump(data[0], _step);
		}
		else {
			_view.renderStep(data, _step);
		}

	},

	/**
	 * Store selected ID to preserve a record of the path taken through the application
	 * @param {String} id Data ID name
	 */
	store: function (id) {
		_path.push(id);
	}
};
/**
 * Controller layer of the Find A Plan application
 * @version 2.0
 * @author Scott Byrns
 */
BB.App.FindAPlan.controller = function () {
	
	var _model, _view;

};

BB.App.FindAPlan.controller.prototype = {
	/**
	 * Initilize the controller
	 * @param {Ojbect} model Reference to the applications data model
	 * @param {object} view Reference to the applications view layer
	 */
	init: function (model, view) {
		_model = model;
		_view = view;
		
		_model.jumpToTab(1);
	},
	
	/**
	 * Notify the data model that the user has chosen to jump to a previous tab
	 * @param {Number} id Index of selected tab
	 */
	jumpToTab: function (id) {
		if (_model.getStep() > id) {
			_model.jumpToTab(id);
		}
	},
	
	/**
	 * Notify the data model to proceed to the next tab
	 */
	nextTab: function () {
		_model.store(this.id);
		_model.nextTab();
	}
	
};
/**
 * View layer of the Find A Plan application
 * @version 2.0
 * @author Scott Byrns
 */
BB.App.FindAPlan.view = function () {
	var _model;
	var _controller;
	
	this._elements;

};

BB.App.FindAPlan.view.prototype = {
	/**
	 * Initilize View connections to model and controller
	 * @param {Object} model Reference to an instance of a data model
	 * @param {Object} controller Reference to an instance of a controller
	 * @param {Object} elements Object literal with jQuery references to UI elements
	 */
	init: function (model, controller, elements) {
		
		_model = model;
		_controller = controller;
		
		this._elements = {
			
			fapContainer: elements.fapContainer,
			
			containers: [
				elements.stepContainerOne,
				elements.stepContainerTwo,
				elements.stepContainerThree,
				elements.stepContainerFour
			],
			
			wrappers: [
				elements.stepContainerOne.parent(),
				elements.stepContainerTwo.parent(),
				elements.stepContainerThree.parent(),
				elements.stepContainerFour.parent()
			],
			
			tabs: [
				elements.stepTabOne,
				elements.stepTabTwo,
				elements.stepTabThree,
				elements.stepTabFour
			]
			
		};
		
		this._elements.tabs[0].bind('click', function () {
			_controller.jumpToTab(1);
		});
		this._elements.tabs[1].bind('click', function () {
			_controller.jumpToTab(2);
		});
		this._elements.tabs[2].bind('click', function () {
			_controller.jumpToTab(3);
		});

		_controller.jumpToTab(1);
	},
	
	/**
	 * Render a selected tab's content and animate the hiding of the previously visible tab
	 * @param {Number} id Index of target tab
	 */
	jumpToTab: function (id) {
		id -= 1;
		for (var i = 0, len = this._elements.wrappers.length; i < len; i += 1) {
			if (i != id) {
				this._elements.wrappers[i].animate({
					width:35
				}, 400);
			}
		}
		this._elements.wrappers[id].animate({
			width:410
		}, 400);
		
		for (var i = 0, len = this._elements.tabs.length; i < len; i += 1) {
			if (i < id) {
				this._elements.tabs[i].find('img').attr('src', this._elements.tabs[i].find('img').attr('src').replace('_check.png', '.png'));
				this._elements.tabs[i].find('img').attr('src', this._elements.tabs[i].find('img').attr('src').replace('.png', '_check.png'));
			}
			else {
				this._elements.tabs[i].find('img').attr('src', this._elements.tabs[i].find('img').attr('src').replace('_check.png', '.png'));
			}
		}
		
	},

	/**
	 * Render the current steps data
	 * @param {object} data Data to be rendered on the current step
	 * @param {Number} step Index of the current step to reference target elements
	 */
	renderStep: function (data, step) {
		var html = '';

        var IE6 = navigator.userAgent.indexOf("MSIE 6") != -1;

		if (this._elements.containers[step-1].find('div.blurb').length) {
			html += '<div class="blurb">' + this._elements.containers[step-1].find('div.blurb').html() + '</div>';
		}

		for (var i = 0, len = data.length; i < len; i += 1) {
			html += '<div id="' + data[i].id + '">';
            if (IE6) {
                var size = data[i].data.largeImageSize.split("x");
                var dim = "width: " + size[0] + "px; height: " + size[1] + "px;";
                html += '<img src="blank.gif" class="plan-selector" style="' + dim + ' background:url(http://assets.bodybuilding.com/images/blank.gif); filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + data[i].data.largeImage + '\', sizingMethod=\'scale\');"/>';
            } else {
                html += '<img src="' + data[i].data.smallImage + '" large="' + data[i].data.largeImage + '" class="plan-selector" />';
            }
			html += '</div>';
		}
		
		this._elements.containers[step-1].html(html);
		
		for (var i = 0, len = data.length; i < len; i += 1) {
			
			$('div#' + data[i].id).bind('click', _controller.nextTab);
            if (!IE6)
            {
			$('div#' + data[i].id).find('img').bind('mouseover', function () {
				
				$(this).attr('small', $(this).attr('src'));
				$(this).attr('src', $(this).attr('large'));
				
				// $(this).css('zoom', 0.90);
				// $(this).animate({'zoom': 1.1}, 16, function () {
				// 	$(this).css('zoom', 0.95);
				// 	$(this).animate({'zoom': 1.1}, 16, function () {
				// 		$(this).css('zoom', 1);
				// 	});
				// });
			});
			$('div#' + data[i].id).find('img').bind('mouseout', function () {
				$(this).attr('src', $(this).attr('small'));
				
				// $(this).css('zoom', 1.1);
				// $(this).animate({'zoom': 1.1}, 16, function () {
				// 	$(this).css('zoom', 1.05);
				// 	$(this).animate({'zoom': 1.1}, 16, function () {
				// 		$(this).css('zoom', 1);
				// 	});
				// });
			});
            }
			
            $('div#getStarted').html('<p style="width:187px;color:#FFF;font-size:10pt;padding:5px; margin-top: 110px;">' + BB.Const.FAP_notes[step-1] + '</p>');
		}
	},

	/**
	 * Render the jump page after a user has chosen all appropriate options
	 * @param {Object} data Title, url, etc
	 * @param {Number} step Index of current step
	 */
	renderJump: function (data, step) {
		var html = '';
		html += '<div id="' + data.id + '">';
		html += '<h3>' + data.data.heading + '</h3>';
		html += '<span>' + data.data.description + '</span>';
		html += '</div>';
		
		this._elements.containers[step-1].html(html);
		
        if (navigator.userAgent.indexOf("MSIE 6") != -1) {
            $('div#' + data.id).css('background-image', 'url(http://assets.bodybuilding.com/images/blank.gif)');
            $('div#' + data.id).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + data.data.backgroundImage + '\', sizingMethod=\'crop\')');
        }
        else {
		$('div#' + data.id).css('background-image', 'url(' + data.data.backgroundImage + ')');
        }
		$('div#' + data.id).click(function () {
			_view._elements.tabs[step-1].find('img').attr('src', _view._elements.tabs[step-1].find('img').attr('src').replace('.png', '_check.png'));
			setTimeout(function () {
				window.location = data.data.path;
			}, 250);
		});
        $('div#getStarted').html('<p style="width:187px;color:#FFF;font-size:10pt;padding:5px; margin-top: 110px;">' + BB.Const.FAP_notes[3] + '</p>');
	}
};

