$(document).ready(function(){
	initAccordion('#menu', '.opener', '.slide', 300);
	initAccordion('.accordion', '.opener', '.slide', 300);
	initPopup();
});
function initAccordion(_hold, _btn, _box, _duration){
     $(_hold).each(function(){
          var _list = $(this).children();
          var _a = _list.index(_list.filter('.selected:eq(0)'));
          _list.each(function(_i){
               var _el = $(this);
               _el.removeClass('selected');
               this._btn = _el.find(_btn);
               this._box = _el.find(_box).hide();
               if(_i == _a){
                    this._box.show();
                    _el.addClass('selected');
               }
               this._btn.click(function(){
                    if(_a != -1){
                         _list.eq(_a).removeClass('selected');
                         _list.get(_a)._box.stop().animate({height: 0}, _duration, function(){
                              $(this).css({display: 'none', height: 'auto'});
                         });
                    }
                    if(_a != _i){
                         _list.eq(_i).addClass('selected');
                         if(!_list.get(_i)._box.is(':animated')){
                              _list.get(_i)._h = _list.get(_i)._box.height();
                              _list.get(_i)._box.css({height: 0, display:'block'});
                         }
                         _list.get(_i)._box.stop().animate({ height: _list.get(_i)._h}, _duration, function(){
                              $(this).height('auto');
                         });
                         _a = _i;
                    }
                    else{
                         _a = -1;
                    }
                    return false;
               });
          });
     });
}
/*--- popup function ---*/
function initPopup(){
    if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
    var _fader = $('#fader');
    _fader.hide();
    var _popup = -1;
    
    $('a.with-popup').each(function(){
        var _el = this;
        if(_el.hash && _el.hash.length > 1){
            _el._popup = $(_el.hash);
            if(_el._popup.length){
                _el.onclick = function(){
                    if(_el._popup){
						_el._popup.hide();
                        _popup = _el._popup;
                        showPopup();
                    }
                    return false;
                }
                _el._popup.find('.btn-close').click(function(){
                    if($.browser.msie){
                        _popup.hide();
                        _fader.fadeOut(200);
                        _popup = -1;
                        if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
                    }
                    else{
                        _popup.fadeOut(400, function(){
                            _fader.fadeOut(200);
                            _popup = -1;
                        });
                    }
                    return false;
                });
            }
        }
    });
    _fader.click(function(){
        if(_popup != -1){
            if($.browser.msie){
                _popup.hide();
                _fader.fadeOut(200);
                _popup = -1;
                if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
            }
            else{
                _popup.fadeOut(400, function(){
                    _fader.fadeOut(200);
                    _popup = -1;
                });
            }
        }
        return false;
    });
    
    $(document).keydown(function(e){
        if(!e)evt = window.event;
        if(e.keyCode == 27 && _popup != -1){
            if($.browser.msie){
                _popup.hide();
                _fader.fadeOut(200);
                _popup = -1;
                if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'visible');
            }
            else{
                _popup.fadeOut(400, function(){
                    _fader.fadeOut(200);
                    _popup = -1;
                });
            }
        }
    });
    function showPopup(){
        _fader.css({
            opacity: 0,
            height: initH(),
            display:'block'
        });
        _popup.css({top: $(window).scrollTop()});
        if($.browser.msie && $.browser.version < 7) if($.browser.version < 7) $('#wrapper select:not(".outtaHere")').css('visibility', 'hidden');
        _fader.fadeTo(400, 0.5, function(){
            if(_popup != -1){
                if($.browser.msie) _popup.show();
                else _popup.fadeIn(200, function(){
                    if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
                });
                _popup.css({top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2});
                if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
            }
        });
    }
    function initH(){
        var _h = $('#wrapper').outerHeight();
        if(_h < $(window).height()) _h = $(window).height();
        if(_h < $('body').height()) _h = $('body').height();
        return _h;
    }
}

