// JavaScript Document
/**
 * Class provides controller functionality for Hot and Service boxes on homepage.
 */
function HotandServicesController() {
	// Private Properties
	var _expandedBox;
	var _nextBox;
	var _isExpanding = false;
	var _isCollapsing = false;
	
	// _instance reference for callbacks
	var _instance = this;
	
// Constructor
	function initialize() {

		// Add expand events
		$(".box .toggle").click(function () { 
      		_instance.handleExpandRequest(this);
    	});
		
		// Add close events
		$(".box .close").click(function () { 
      		_instance.handleCollapseRequest(this);
    	});
	}

// User Event Handler Methods
	/**
	 * Method handles request from box to expand
	 * @param {Object} toggleLink
	 */
	this.handleExpandRequest = function(toggleLink) {
		var box = toggleLink.parentNode.parentNode;
		
		if(_isExpanding || _isCollapsing) return;
		
		// Ignore if box is already expanded
		if(box == _expandedBox) {
			this.handleCollapseRequest();
			return;
		};
		
		if(_expandedBox) {
			//Collapse first;
			_nextBox = box;
			this.collapse(_expandedBox);
		}
		else {
			this.expand(box);
			
		}
	}
	/**
	 * Method handles request from box to collapse
	 * @param {Object} box
	 */
	this.handleCollapseRequest = function() {
		if(_isExpanding || _isCollapsing) return;

		if(_expandedBox) {
			this.collapse(_expandedBox);			
		}
	
	}

// Methods with Logic
	/**
	 * Method expands the provided box element
	 * @param {Object} box
	 */
	this.expand = function(box) {
		// Get two items to animate
		var drawer = $(".drawer", box)[0];
		var badge = $(".badge", box)[0]

		// Mark is expanding
		_isExpanding = true;
		
		// Start animation to expand
		//$(badge).effect("size", { to: {width: 98,height: 68}, scale:"box"},500,
		$(badge).animate({ left:0, width:"98", top:0,height: 68},{duration: 500,complete: function(){
				$(drawer).show("slide", {
					duration:500
				}, 750, function(){
					_instance.handleExpandComplete(box);
				})
			}});
	}
	
	/**
	 * Method collapses the provided box element
	 * @param {Object} box
	 */
	this.collapse = function(box) {
		// Get two items to animate
		var drawer = $(".drawer", box)[0];
		var badge = $(".badge", box)[0]
		
		_isCollapsing = true;
		
		// Start animation to collapse
		$(drawer).hide("slide", { direction: "left" }, 500, 
			function() {$(badge).animate({ left:0, width:"98", top:0,height: 21},{
					duration: 500,
					complete: function(){
						_instance.handleCollapseComplete(box)
					}
				});
		});
		
	}
	
// Effect Complete Event Methods
	/**
	 * Method handles a the complete after a box has expanding
	 * @param {Object} box
	 */
	this.handleExpandComplete = function(box) {
		_expandedBox = box;
		_nextBox = null;
		// Mark is expanding
		_isExpanding = false;
	}
	
	/**
	 * Method handles the complete after a box has finished collapsing
	 * @param {Object} box
	 */
	this.handleCollapseComplete = function(box) {
		_expandedBox = null;
		_isCollapsing = false;
		
		if(_nextBox) {
			this.expand(_nextBox);
		}
	}
	// Invoke constructor
	initialize();
		
}
var controller;
$(document).ready(function () {
   controller = new HotandServicesController();
});
