/**
 * uwmfmAccordion - jQuery Plugin
 *
 * @author  Dominik Lukasik
 * @version 0.1
 *
 * Dual licensed under the MIT and GPL licenses:
 * 		http://www.opensource.org/licenses/mit-license.php
 * 		http://www.gnu.org/licenses/gpl.html
 *
 *
 */
(function($){
	$.fn.uwmfmAccordion = function(options){

		var defaults = {
			compressDuration : 100,
         neutralDuration  : 100
		};

		var options = $.extend(defaults, options);
		
		return this.each(function(){
			var accordion = this;
         var accordionWidth = $(accordion).width();
         var compressWidth = 100;
         var normalWidth = 150;
         var showWidth = 250;
         var borderWidth = 4;
         var panels = $(accordion).children('div.panel');
         var selected = null;
         var openedPanel = null;
         var timesboxs = $('.uwmfm-times');

			_init();

			function _init()
			{
            $(panels).mouseenter(function(){
               $(this).addClass('selected');
               leftPos = 0;

               if(openedPanel) $(openedPanel).removeClass('opened');
               
               $(panels).each(function(i){
                  selected = $(this).hasClass('selected');
                  ti = '.time'+i;
                  
                  if(selected)
						{
                     $(this).stop()
                            .animate({
                                   left:leftPos,
                                   width:(showWidth-(borderWidth*2))
                                },
                                {  queue:false,
                                   duration: options.compressDuration
                                }
                             );
                     $(timesboxs).find(ti).stop().animate({
                                   left:leftPos,
                                   width:(showWidth)
                                },
                                {  queue:false,
                                   duration: options.compressDuration
                                }
                            );
                     $(this).find('.panel-content')
                            .stop()
                            .animate({'opacity': 1}, {queue: false, duration: 350 });
                     
							leftPos+=showWidth;
							lastPanel = $(this);
						}
						else
						{
                     $(this).stop()
                            .animate({
                                   left:leftPos,
                                   width:(compressWidth-(borderWidth*2))
                                },
                                {  queue:false,
                                   duration: options.compressDuration
                                }
                            );
                     $(timesboxs).find(ti).stop().animate({
                                   left:leftPos,
                                   width:(compressWidth)
                                },
                                {  queue:false,
                                   duration: options.compressDuration
                                }
                            );
                     $(this).find('.panel-content').stop().animate({opacity: 0.0}, {queue : false, duration: 100});
							leftPos += compressWidth;
						}
                  $(this).removeClass('selected').addClass('opened');
                  openedPanel = $(this);
               });
               
            }); // end panels mouseenter

            $(accordion).mouseleave(function(){
               $(openedPanel).removeClass('opened');
						openedPanel=null;

                  _neutralPanel(true);
            });
			}

         function _neutralPanel(t)
			{
				isOpened = $(accordion).has('.opened').length;
				leftPos = 0;
            $('.panel-content').stop().animate({opacity: 0.0}, {queue : false, duration: 100});
				$(panels).each(function(i){
					if(t==true){
                  ti = '.time'+i;
						$(this).stop()
                         .animate({
                                left:leftPos,
                                width:(normalWidth-(borderWidth*2))
                             },
                             {  queue:false,
                                duration: options.neutralDuration
                             }
                          );
               $(timesboxs).find(ti).stop().animate({
                                   left:leftPos,
                                   width:(normalWidth)
                                },
                                {  queue:false,
                                   duration: options.compressDuration
                                }
                            );
					}
					else
					{
						$(this).css({left: leftPos});
					}
					leftPos += normalWidth;
				});
			}
         
		}); //end this.each
	};
})(jQuery);
