//-------------------------------
// Class
//-------------------------------

function FeaturedContentController() {
	
	var _self = this;
	
	//-------------------------------
	// Constants
	//-------------------------------
	
	//FeaturedContentController.prototype.BUTTON_CLICK = "button_click";
	
	var GRID_SIZE = 100;
	
	//-------------------------------
	// Properties
	//-------------------------------
	
	var _featuredContentView;
	var _featuredWrittenContentView;
	var _featuredVideoContentView;
	var _featuredGridView;
	
	var _data;
	var _pagination = new Object();
	
	//-------------------------------
	// Constructor
	//-------------------------------
	
	$j(function(){
		_featuredContentView = new FeaturedContentView();
		_featuredWrittenContentView = new FeaturedWrittenContentView();
		_featuredVideoContentView = new FeaturedVideoContentView();
		_featuredGridView = new FeaturedGridView();
		
		$j(_featuredContentView).bind(_featuredContentView.CONTENT_SELECT, onContentSelect);
		$j(_featuredContentView).bind(_featuredContentView.CONTROLS_CLICK, onControlsClick);
		$j(_featuredContentView).bind(_featuredContentView.GRID_APPENDED, onGridAppend);
		
		init();
	});
	
	//-------------------------------
	// Public Methods
	//-------------------------------
	
	_self.deploy = function(data) {
		
		switch(data.type) {
			case "content" :
				//alert("FeaturedContentController :: content")
				_featuredContentView.deploy(data.content);
				break;
			case "written" : 
				_featuredWrittenContentView.deploy(data);
				break;
			case "video" :
				_featuredVideoContentView.deploy(data);
				break;
			case "permalink" :
				//alert(data.permalink);
				window.location = data.permalink;
				break;
		}
	}

	//-------------------------------
	// Private Methods
	//-------------------------------
	
	function init() {
		_pagination.current = 0;
	}
	
	function _page(direction) {
		switch(direction) {
			case "previous" :
				_pagination.current++;
				break;
			case "next" :
				_pagination.current--;
		}
		
		if(_pagination.current < _pagination.min + 1) _pagination.current = 0;
		else if(_pagination.current > 0 ) _pagination.current = _pagination.min + 1;
		
		return _pagination.current;
	}

	//-------------------------------
	// Listeners
	//-------------------------------
	
	function onContentSelect(event, data) {
		//alert(data)
		//alert($j(data).find("description").text());
		_self.deploy(data);
	}
	
	function onControlsClick(event, target) {
		_pagination.min = -Math.ceil($j("#featuredButtons .buttons").width() / ($j("#featuredButtons").width() - GRID_SIZE));
		_pagination.width = $j("#featuredButtons .buttons").width() - GRID_SIZE;
		_pagination.visible = $j("body").width() - GRID_SIZE;
		
		switch($j(target).attr("class")) {
			case "previous" :
				var pos = _page("previous");
				if(_pagination.min < -1) _featuredContentView.animate(pos * _pagination.visible);
				if(_pagination.min < -1) _featuredGridView.animate(pos * _pagination.visible);
				break;
			case "next" :
				var pos = _page("next");
				if(_pagination.min < -1) _featuredContentView.animate(pos * _pagination.visible);
				if(_pagination.min < -1) _featuredGridView.animate(pos * _pagination.visible);
				break;
		}
		
		//alert(_pagination.min + " | " + ($j("#featuredButtons .buttons").width()) + "/" + ($j("#featuredButtons").width() - GRID_SIZE));
		
	}
	
	function onGridAppend() {
		_featuredGridView.appendVertical();
	}

}


