/* *
 * VerticalAccordion - jQuery Plugin
 *
 * LumenVox LLC
 * jQuery 1.4.x
 *
 * Date: April 11, 2011
 * 
 * });
 * */
(function($) {

	$.fn.VerticalAccordion = function(options) {
		/**
		 * @options
		 * @defaultSlide {int}The tab screen to display at runtime
		 * @displayWidth {int}The container width of the acordion menu
		 * @tabWidth {int}The tab width of an accordion menu
		 * @tabBuffer {int} Any space between the accordion menu tabs
		 * @callBack {obj} An event handler when a tab is chosen
		 */
		$.fn.VerticalAccordion.defaultOptions = {
			   defaultSlide:0,
			   displayWidth:990,
			   tabWidth:54,
			   tabBuffer:3,
			   callBack:null
		}

		// Loop through each Accordion Control (i.e. DIV element ) on screen and 
		// initialze control
		this.each(function () {

			 // A member variable used to store the Accordion container element. This could be a
			 // DIV element with the classname of 'v-accordion-control' assigned.
			var m_controlWrapper = $(this),
			
				// If an options object is sent at runtime merge default options with user specified
				// options into a single options object. If none specified use default options.
				m_options = (typeof(options) == 'object') ?
				$.extend({}, $.fn.VerticalAccordion.defaultOptions, options) : $.fn.VerticalAccordion.defaultOptions,
				
				m_activeSlide = m_options.defaultSlide,
				
				m_slideLength = $(this).children().length;
								
			$(this).children().each(function(i){
				// Calculate left and right side xPosition for menu element
				var leftPos = ((m_options.tabWidth + m_options.tabBuffer) * (i - 1)),
					rightPos = $(this).css('left');
					//rightPos = (m_options.displayWidth - ((m_options.tabWidth * (m_slideLength - (i - 1))) + (m_options.tabBuffer * ((m_slideLength - 1) - (i - 1)))));

				// Add config data to the DIV.slide element
				$(this).data('config', { index:i, left:leftPos, right:rightPos });
				
				// Disable the display to display:none		
				if(i > 0){
					$(this).children().each(function(i){
						if(i > 0){
							$(this).css('display', 'none');
						}
					});
				}
					
				// Assign onclick event
				$('span:first', this).click(function(){				
					openSlide($(this).parent());
				});
			});

			function openSlide(p_slide){

				var index = p_slide.data('config').index,
					xPos = parseInt(p_slide.css('left'));
										
				if(xPos > (m_options.displayWidth / 2)){

					// Shift Menu Items Left
					// **********************************************************
					if(index == 0){
						// Cannot shift first item any farther left!
						return;
					}else{
						// Loop through all elements and determine if any preceding elements need to 
						// be shifted as well.
						var elmLength = $(m_controlWrapper).children().length;
						
						$(m_controlWrapper).children().each(function(i){					
							if(i < index){
									var callBack = function(){
										$(this).children().each(function(i){
											if(i > 0){
												$(this).css('display', 'none');
											}
										});
									}
									
									$(this).animate({ left:$(this).data('config').left }, 'slow', callBack);
									
							}else if(i == index){
							
								$(this).children().each(function(i){
									if(i > 0){
										$(this).css('display', 'block');
									}
								});
								// Current Slide								
								$(this).animate({ left:$(this).data('config').left }, 'slow');
							}
						});
					}
				}else{
					// Shift Menu Items Right
					// **********************************************************					
					if((m_controlWrapper.children().length - 1) == index){
						// Cannot shift first right!
						return;
					}else{					
						// Incrament Index to Move neighbor Tab on Right
						index++;
											
						// Loop through all elements and determine if any preceding elements need to 
						// be shifted as well.
						$(m_controlWrapper).children().each(function(i){
							if(i >= index){
								
								var callBack = function(){
									$(this).children().each(function(i){
										if(i > 0){
											$(this).css('display', 'none');
										}
									});
								}
								
								$(this).animate({ left:$(this).data('config').right }, 'slow', callBack);
							}else if(i == (index -1)){
							
								// Current Slide
								$(this).children().each(function(i){
									if(i > 0){
										$(this).css('display', 'block');
									}
								});
							}
						});
						
						// Move Index Back For Cusor Assignment Below.
						index--;
					}
				}
				
				// Set cursor pointer only for available slides : Disable Active
				m_controlWrapper.children().each(function(i){
					$(this).css('cursor', (i == index) ? 'default' : 'pointer');
				});
			}
		
		});
	}
})(jQuery);

