(function($){
    $.fn.AndrewsAccordion = function( options ){
        var defaults = {
            speed : 300,
            items : null ,
            image : null
        }

        var opts=$.extend(defaults,options);

        return this.each(function(){

            var positionInterval,working=false;

            var $wrap = $(this);
            $wrap.addClass("accordionContainer");
            var $loading=$("<div class='accordionLoading' ></div>");
            $wrap.append($loading);

            var $itemsContainer=$("<ul></ul>");
            var $mainImage=$("<img/>").attr("src",opts.image).addClass("accordionMainImage").appendTo($wrap);

            $wrap.append($itemsContainer);

            $.each(opts.items,function(i,item){

                var $item = $("<li></li>");
                $itemsContainer.append($item);

                $item.append("<div class='accordionTitle' >"+item.title+"<div class='accordianTagLine' ><div class='accordionTagLineInner' >"+item.tagLine+"</div></div><div class='accordionClosed' ></div></div>");
                if(item.tagLine.indexOf("<br/>")==-1){
                    $item.find("div.accordianTagLine").css("line-height",$item.find("div.accordianTagLine").height()+"px");
                }
                $item.append("<div class='accordionImage' ><img src='"+item.image+"' /></div>");
                $item.append("<div class='accordionText' >"+item.text+"<br clear='all'/><a href='"+item.moreInfo+"' class='accordianMoreInfo' >MORE INFO</a></div>");
                //$item.css("top",320+i*38);
                $item.data("interval",null);
                $item.find("div.accordionTitle").mouseenter(function(){
                    var $item=$(this);
                    clearInterval($item.data("interval"));
                    $item.data("interval",setInterval(function(){
                        $item.find("div.accordionClosed").toggle();
                    },50))
                    $item.stop().css("background","white").animate({"backgroundColor":"#1a5b98"},600);
                }).mouseout(function(){
                    var $item=$(this);
                    clearInterval($item.data("interval"));
                    $item.find("div.accordionClosed").show();
                });

                $item.find(".accordianMoreInfo").mouseenter(function(){
                    $(this).stop().css("background","white").animate({"backgroundColor":"#1a5b98"},600);
                })
            })


            var $items = $itemsContainer.find("div.accordionTitle");

            $wrap.height($mainImage.height() + ( $items.size()*$items.first().outerHeight()));

            function openAccordion($item){
                 var index=$($items).index($item);
                 var $tagLine=$item.find("div.accordianTagLine");
                 var $text=$item.next().next();

                 $tagLine.css("width","0px");
                 $text.css("display","none");

                 var $text=$item.next().next();
                 $items.filter(".accordionOpen").each(function(){
                     closeAccordion($(this),true);
                 });
                 $item.addClass("accordionOpen");
                 $item.parent().animate({"height":($item.outerHeight()+$item.next().height())},{duration:opts.speed,easing:"linear",queue:false,complete:function(){
                 }});

                 position(false);
                 $item.next().children().fadeIn(opts.speed,"linear",function(){
                     $tagLine.width(0).animate({"width":$tagLine.children().width()},opts.speed);
                     if($.browser.msie)
                        $text.width($wrap.width()-50).show();
                     else
                        $text.width($wrap.width()-50).fadeIn(opts.speed);
                    
                     working=false;
                 });
                 $mainImage.filter(":visible").fadeOut(opts.speed*2);
            }

            function closeAccordion($item,opening_new){
                 var index=($($items).index($item));
                 $item.removeClass("accordionOpen");
                 $item.parent().animate({"height":($item.next().height())},{duration:opts.speed/2,easing:"linear",queue:false});
                 $item.next().children().fadeOut(opts.speed/2);
                 $item.find("div.accordianTagLine").stop().width(0);
                 var $text=$item.next().next();
                 if($.browser.msie)
                    $text.hide();
                 else
                    $text.fadeOut(0);
                 $item.next().next().fadeOut(0);
                 if(opening_new==false){
                     position(true);
                 }
            }
        
            function initAccordion(){
                var top=$mainImage.height();
                var currentItem=0;
                
                positionInterval=setInterval(function(){
                    var $item=$items.eq(currentItem);
                    $item.parent().animate({"top":top},{duration:opts.speed,easing:"linear",queue:false,complete:function(){
                        if($items.index($(this).find("div.accordionTitle"))==$items.size()-1){
                            $mainImage.fadeIn(opts.speed);
                            working=false;
                            clearInterval(positionInterval);
                        }
                    }});
                    top+=$item.outerHeight();
                    if($item.hasClass("accordionOpen")) top+=$item.next().height();
                    currentItem+=1;
                },opts.speed);
            }

            function position(all_closed){
                var top=0;
                var currentItem=0;
                if(all_closed) {
                    top+=$mainImage.height()+($items.size()-1)*$items.outerHeight();
                    currentItem=$items.size();
                    
                    positionInterval=setInterval(function(){
                        currentItem-=1;
                        if(currentItem>=0){
                            var $item=$items.eq(currentItem);
                            $item.parent().animate({"top":top},{duration:opts.speed,easing:"linear",queue:false});
                            top-=$item.outerHeight();
                            if($item.hasClass("accordionOpen")) top+=$item.next().height();
                            if(currentItem<=0){
                                $mainImage.fadeIn(opts.speed);
                                working=false;
                                clearInterval(positionInterval);
                            }
                        }
                        else {
                            clearInterval(positionInterval);
                        }
                    },opts.speed);
                }
                else {
                    $items.each(function(){
                        var $item=$(this);
                        $item.parent().animate({"top":top},{duration:opts.speed,easing:"linear",queue:false});
                        top+=$item.outerHeight();
                        if($item.hasClass("accordionOpen")) top+=$item.next().height();
                    })
                }
            }

            $items.click(function(e){
                 e.preventDefault();
                 if(working) return false;
                 working=true;
                if($(this).hasClass("accordionOpen")){
                  closeAccordion($(this),false);
                }
                else {
                  openAccordion($(this));
                }
            })

            var images = $wrap.find("img").get();

            working=true;
            
            var loadingInterval = setInterval(function(){

                var images_loaded=0;

                $.each(images,function(i,image){

                   if(image.complete){
                       images_loaded+=1;
                   }

                });

                if(images_loaded==images.length){
                    $loading.hide();
                    clearInterval(loadingInterval);
                    initAccordion();
                }

            },100);
        });
    }
})(jQuery);
