

(function($) {

    $.widget("ui.accordiont1", {

        _init: function() {
            var _this = this;

            var $current = null;            
            var locked = false, next = null;
            
            this.element.children('li').each(function() {
                
                var $li = $(this);

                if( $li.is('.active') ) {                    
                    _this.options.complete($current = $li);                    
                }

                // Bind accordion events.
                $li.children('a').click(function(event) {                    
                    if( locked ) {
                        // The user clicked the link while the animation was running. Expand the link when the animation is done.
                        next = $(this);
                    } else if( $li != $current  ) {
                        locked = true;

                        var $old = $current, $oldBody = $old.children(_this.options.sectionSelector);

                        $current = $li;
                        var $currentBody = $current.children(_this.options.sectionSelector);

                        //Ajax-loader.
                        var $loader = $('<div class="ajax-loader"></div>').appendTo($current);                        
                        var loaderHeight = $loader.height(); //Full loader height
                        $loader.hide(); //Hide the ajax loader until it's needed

                        var animate = function() {
                            var attrs1 = {
                                height: 'show',
                                paddingTop: 'show',
                                paddingBottom: 'show'
                            };
                            
                            if( $loader.is(":visible") ) {
                                // The expanding section must start from the ajax loader's posistion
                                $currentBody.show();
                                attrs1.height = $currentBody.height();
                                $currentBody.css("height", $loader.height()+"px");
                                $currentBody.hide();
                            }

                            // Remove loader and stop animation
                            $loader.stop().remove();
                            $oldBody.stop();


                            $.CO.sync($currentBody, $oldBody,
                            {
                                attrs1: attrs1,
                                duration: _this.options.mainDuration,
                                easing: _this.options.easing,
                                complete: function() {

                                    $old.removeClass('active');
                                    $oldBody.css("height", "");                                    
                                    $currentBody.css("height", "");

                                    $current.addClass('active');

                                    locked = false;

                                    _this.options.complete($current);

                                    //The user wanted to expand this section during the animation. Expand it now.
                                    if( next ) {
                                        next.trigger("click");
                                        next = null;
                                    }
                                }
                            });
                        }
                        var url = _this.options.getUrl($(this));
                        
                        if( url ) {
                            tusind1.get(url, function(data) {

                                //Hide current section and show the new one with the data returned from the server
                                //Set the body of the section to the html returned from the server
                                var $target = $('.ajax-target', $currentBody);
                                if( !$target.length ) {
                                    $target = $currentBody;
                                }
                                $target.html(data);
                                animate();
                            }, {
                                load: function() {
                                    // Slide in the ajax loader and make room for it by reducing the size of the current section

                                    $.CO.sync($loader, $oldBody, {
                                        attrs2: {
                                            height: Math.max(0, $oldBody.height() - loaderHeight)
                                        },
                                        duration: _this.options.ajaxDuration,
                                        easing: 'swing'
                                    })
                                }
                            });
                        } else {
                            animate();
                        }
                    }
                    event.preventDefault();
                });
            });
        }
    });

    $.extend($.ui.accordiont1, {
        defaults: {
            mainDuration: 300,
            ajaxDuration: 300,
            sectionSelector: '.feed-in',
            easing: "swing",

            complete: function() {},
            getUrl: function($a) {}
        }
    });

    
})(jQuery);



(function($) {
    function CO() {

        var fxs = [], totals = {};

        function store(i, fx) {
            if( !fxs[i] ) {
                fxs[i] = {};
            }
            fxs[i][fx.prop] = fx;
        }

        function get(i, fx_ref) {
            var other = fxs[i == 1 ? 0 : 1];
            if( other ) {
                return other[fx_ref.prop];
            }
            
            return null;
        }

        this.lock = function(i,  fx) {
           

            store(i, fx);
            
            if( fx.unit == "px" ) {
                if( i == 0 ) {
                     // Browsers floor the fractional pixel values, so we round the number for them.
                    fx.now = Math.round(fx.now);
                } else if( i == 1 ) {
                    var other = get(i, fx);
                    if( other ) {
                        if( !totals[fx.prop] ) {
                            totals[fx.prop] = {
                                start: Math.floor(fx.start) + Math.floor(other.start),
                                end: Math.floor(fx.end) + Math.floor(other.end)
                            };
                        }
                        if( Math.abs(totals[fx.prop].end - totals[fx.prop].start) <= 1 ) {
                            totals[fx.prop].start = totals[fx.prop].end;
                        }                       

                        // What there's missing when we animate the total height and subtract the current value of the first.
                        var res = (totals[fx.prop].start + fx.pos*( totals[fx.prop].end- totals[fx.prop].start)) - other.now;
                        fx.now = Math.round(Math.max(0, res));
                    }
                } else {
                    fx.now = Math.floor(fx.now);
                }
            }
        }
    }

    var co = null;

    $.extend({
        CO: {
            f1: function() {
                co = new CO();
                return function(now, fx) {
                    co.lock(0, fx);
                }
            },
            f2: function() {
                return function(now, fx) {
                    co.lock(1, fx);
                }
            },

            sync: function(e1, e2, options) {
                e1.animate(options.attrs1 || {
                    height: 'show',
                    paddingTop: 'show',
                    paddingBottom: 'show'
                }, {
                    duration: options.duration,
                    easing: options.easing,
                    step: $.CO.f1()
                });
                e2.animate(options.attrs2 || {
                    height: 'hide',
                    paddingTop: 'hide',
                    paddingBottom: 'hide'
                }, {
                    duration: options.duration,
                    easing: options.easing,
                    step: $.CO.f2(),
                    complete: options.complete
                });
            }
        }
    });
})(jQuery);
