(function(jq){
    jq.fn.angebote = function() { 
        var close_all_open_angebote = function() {
            jq('.plugin_angebot').each( function() {
                var cur_height = jq(this).height() ;
                if (cur_height > 138) {
                    jq(this).click() ; 
                }
            }) ; 
        }
        
        
        var list = this.find('.plugin_angebot') ;
        return list.each( function() {
            var locked = false;
            var _self = jq(this) ; 
            var _image = _self.find('.ang_image_wrap img') ; 
            var _form_wrap = _self.find('.angebots_form_wrap') ; 
            var _anmelden_button = _self.find('.ang_button_wrap') ; 
            var _angebot_id = parseInt(_self.find('.angebot_plugin_id').html()) ; 
            var _text_block = _self.find('.text') ; 
            _text_block.css({'visibility':'hidden'}) ; 
            
            var _open = false ; 
            var _form_open = false ; 
            
            // find the max height
            
            var get_open_height = function() {
                return Math.max(
                    parseInt(_self.find('.ang_image_wrap').height()), 
                    parseInt(_self.find('.ang_text_wrap').outerHeight()),
                    _closed_height
                ) ; 
            }
            
            var _closed_height = 138 ; 
            
            var can_be_opened = get_open_height() > _closed_height ; 
            if (can_be_opened) {
                _self.hover(
                    function () {
                        if (!_open) {
                            _image.css("opacity", 0.6) ; 
                            jq(this).css("cursor", "pointer") ; 
                        }
                    }, 
                    function () {
                        if (!_open) {
                            _image.css("opacity", 1.0) ; 
                        }
                    }
                ) ; 
                _self.unbind('click') ; 
                _self.click(
                    function () { 
                        if (_open) { close_angebot() ; }
                        else { open_angebot() ; }
                    }
                ) ;
            }
            else {
                // close the form even if the main item height is so small that it cannot be opened
                _self.unbind('click') ; 
                _self.click(
                    function() {
                        if (_form_open) {
                            _self.animate({
                                height: _closed_height
                            }, 500) ; 
                            _form_open = false ;  
                            set_anmelde_button_form_open() ; 
                        }
                    } 
                ) ; 
            }
            
            // prevent closing the dialog when clickin in the form container
            _form_wrap.click( function() { return false ;} ) ; 
            _form_wrap.hover( function() { jq(this).css("cursor", "default")} ) ; 
            
            jq(this).css("height", "138px") ; 
            jq(this).css("overflow", "hidden") ; 
            
            
            // F U N C T I O N S
            
            var set_anmelde_button_form_open = function() {
                _anmelden_button.unbind('click') ; 
                _anmelden_button.click(function(event) {
                    if (!_open) { open_angebot() ; }
                    show_form() ; 
                    event.stopPropagation() ; 
                }) ; 
            }
            
            set_anmelde_button_form_open() ;
            
            var set_anmelde_button_form_submit = function() {
                _anmelden_button.unbind('click') ; 
                _anmelden_button.click(function(event) {
                    event.stopPropagation() ; 
                    var form = _self.find('.the_angebots_form') ; 
                    var form_params = form.serialize() ; 
                    jq.getJSON("/submit_angebot_form/?"+form_params, 
                        { angebot_id:_angebot_id },        
                        function(data, textStatus) {
                            if (textStatus != "success") {
                                alert("Error while fetching Angebot form. " + textStatus) ; 
                                return ; 
                            }
                            if (data['success'] == 'false') {
                                _form_wrap.html(data['form']) ; 
                            }
                            else {
                               _form_wrap.html(data['html_message']) ;  
                               set_anmelde_button_form_open() ; 
                            }
                        }
                    ) ;
                }) ; 
            }
            
            var open_angebot = function() {
                if(locked==false) {
                    locked = true
                    _self.stop(true, true).animate({
                        height: get_open_height()
                    }, 500, function() {
                        jq.scrollTo(_self, 500, {offset:-100}) ; 
                        _text_block.css({'visibility':'visible'}) ; 
                        locked = false;
                    }) ; 
                    _open = true ;  
                    _image.css("opacity", 1.0) ; 
                    close_all_open_angebote() ; 
                }
            }
            
            var close_angebot = function() {
                if(locked==false) {
                    locked = true;
                    _self.stop(true, true).animate({
                        height: _closed_height
                    }, 500, function(){
                        _text_block.css({'visibility':'hidden'}) ; 
                        locked = false;
                    }) ; 
                    _open = false ; 
                    _form_open = false ; 
                    _image.css("opacity", 0.6) ; 
                    set_anmelde_button_form_open() ;
                }
            }
            
            var open_form = function() {
                if(locked==false) {
                    locked = true;
                    var form_height = _form_wrap.height() ; 
                    _self.animate({
                        height: get_open_height() + form_height 
                    }, 500, function() {
                        jq.scrollTo(_self, 500, {offset:-100}) ;
                        locked = false; 
                    }) ;
                    _form_open = true ; 
                    _form_wrap.find('input').each( function() {
                        jq(this).unbind('click') ; 
                        jq(this).click( function(event) { event.stopPropagation() ; }) ; 
                    }) ; 
                    set_anmelde_button_form_submit() ; 
                }
            }
            
            var show_form = function() {
                $.getJSON("/get_angebot_form/", 
                    {},        
                    function(data, textStatus) {
                        if (textStatus != "success") {
                            //alert("Error while fetching Angebot form. " + textStatus) ; 
                            return ; 
                        }
                        _form_loaded = true ; 
                        _form_wrap.html(data['form']) ; 
                        open_form() ; 
                    }
                ) ;
            }
            
        });
    }
})(jQuery);
