var AO = {
  setUpNavigation: function () {
    jQuery("#nav").megaFlyout();
    // jQuery('#nav').superfish();

    jQuery('#nav div.search input').click(function () {
      if (this.value == 'Search...') {
        this.value = '';
      }
    });

    jQuery('#nav div.search input').focusout(function () {
      if (this.value == '') {
        this.value = 'Search...';
      }
    });

    jQuery('#nav li.drop').mouseleave(function () {
      jQuery(this).find('.dropdown').hide();

    });

    jQuery('#nav li.drop').mouseenter(function () {
      jQuery(this).find('.dropdown').show();
      if (!jQuery(this).data('dropdown')) {
        jQuery(this).find('.dropdown').mouseleave(function () {
          jQuery(this).hide();
        });
        jQuery(this).data('dropdown', true);
      }
    });
  },
  initTeaserMega: function () {
    var fadeActive = false;
    var layers = jQuery('div.teaser_mega ul li a');
    var timer = window.setInterval(function () {
      jQuery(layers).each(function (e) {
        if (jQuery(this).parent().hasClass("active")) {
          if (e + 1 === layers.length) {
            fadeNext(layers[0]);
          } else {
            fadeNext(layers[e + 1]);
          }
        }
      });
      }, 8000);

      var fadeEnd = function () {
        fadeActive = false;
      };
      var fadeNext = function (e) {
        if (!fadeActive) {
          fadeActive = true;
          var me = jQuery(e);

          var liClass = me.attr('class');
          var effectTime = 400;

          me.parents('div.teaser_mega').find('div.layers div.layer').not('.' + liClass).stop().animate({ opacity: 0 }, effectTime, function () { jQuery(this).css("display","none") });
          me.parents('div.teaser_mega').find('div.layers div.layer').not('.' + liClass).find('div.text').stop().animate({ opacity: 0 }, effectTime, function () {  });
          me.parents('div.teaser_mega').find('div.' + liClass).stop().css("display","block").animate({ opacity: 1 }, effectTime, function () {  });
          me.parents('div.teaser_mega').find('div.' + liClass).find('div.text').stop().animate({ opacity: 1 }, effectTime, fadeEnd);
          me.parents('ul').find('.active').removeClass('active');
          me.parent().addClass('active');
        }
      };

      jQuery('div.teaser_mega ul li a').click(function () {
        clearInterval(timer);
        fadeNext(this);
      });
    },


    /*

initTeaserMega: function(){
jQuery('div.teaser_mega ul li a').click( function(){
var liClass = jQuery(this).attr('class');
var effectTime = 400;

jQuery(this).parents('div.teaser_mega').find('div.layers div.layer').not('.' + liClass).stop().animate({opacity: 0}, effectTime, function(){jQuery(this).css('display', 'none');});
jQuery(this).parents('div.teaser_mega').find('div.layers div.layer').not('.' + liClass).find('div.text').stop().animate({opacity: 0}, effectTime, function(){});
jQuery(this).parents('div.teaser_mega').find('div.' + liClass).css('display', 'block');
jQuery(this).parents('div.teaser_mega').find('div.' + liClass).stop().animate({opacity: 1}, effectTime, function(){});
jQuery(this).parents('div.teaser_mega').find('div.' + liClass).find('div.text').stop().animate({opacity: 1}, effectTime, function(){});
jQuery(this).parents('ul').find('.active').removeClass('active');
jQuery(this).parent().addClass('active');

});
},
*/

initMailbox: function () {
  // setup the resiable table
  jQuery('div.message_list .resizable').resizable({
    handle: 's',
    maxHeight: jQuery(this).find('.messages table').height(),
    minHeight: 100,
    resize: function () {
      var difference = jQuery(this).height() - jQuery(this).find('.messages').height();
      jQuery(this).find('.messages').height(jQuery(this).height());
      jQuery(this).find('.messages .tablescroll_wrapper').height(jQuery(this).find('.messages .tablescroll_wrapper').height() + difference);
    }
  });
  jQuery('.messages table').tableScroll({ height: 200, width: jQuery('div.message_list table').width() });

  // setup new message
  jQuery('.new_message').add('.actions input').click(function () {
    loadSnippetModal(
      '../../baseobject/snpt_dialog_new_message.html',
      function () {
        jQuery(this).find('#choose').click(function () {
          loadSnippetModal(
            '../../baseobject/snpt_dialog_choose_persons.html',
            function () {
              jQuery(this).find('table').each(function () {
                jQuery(this).tableScroll({ height: 240, width: jQuery(this).width() });
              });
            }
          );
        });
      }
    );
  });
},
initFlagToggle: function () {
  jQuery('.icon_flag_on').live('click', function () {
    jQuery(this).removeClass('icon_flag_on');
    jQuery(this).addClass('icon_flag_off');
  });
  jQuery('.icon_flag_off').live('click', function () {
    jQuery(this).removeClass('icon_flag_off');
    jQuery(this).addClass('icon_flag_on');
  });
},

initSortTable: function () {
  jQuery('th.sort').css('cursor', 'pointer')
  jQuery('th.sort').click(function () {
    var now = "";
    if (jQuery('span', this).hasClass('icon_asc')) {
      now = 'asc';
    }
    else if (jQuery('span', this).hasClass('icon_desc')) {
      now = 'desc';
    }
    else {
      now = 'sort';
    }
    var tds = jQuery(this).parent('tr');
    jQuery('th', tds).each(function () {
      jQuery('span', this).removeClass('icon_asc');
      jQuery('span', this).removeClass('icon_desc');
      jQuery('span', this).addClass('icon_sort');
    });
    switch (now) {
      case 'asc':
      jQuery('span', this).removeClass('icon_sort');
      jQuery('span', this).addClass('icon_desc');
      break;
      case 'desc':
      jQuery('span', this).removeClass('icon_sort');
      jQuery('span', this).addClass('icon_asc');
      break;
      default:
      jQuery('span', this).removeClass('icon_sort');
      jQuery('span', this).addClass('icon_asc');
      break;
    }
  });
},

initMemberLogin: function () {
  jQuery('#demo_switch_member_login a').click(function () {
    jQuery('#demo_switch_member_login div.loggedin').css('display', 'block');
    jQuery('#demo_switch_member_login div.login').css('display', 'none');
  });
},

initProfileOverlays: function () {
  try {























    var mouseOverLink = false;


    jQuery('a.person').mouseleave(function (event) {
      mouseOverLink = false;
    });
    jQuery('a.person').mouseenter(function (event) {

      mouseOverLink = true;
      var personlink = this;
      var pathname = window.location;
      var hideOnTimeout = true;
      if ($('#person_box_layer').length > 0) {
        $('#person_box_layer').remove();
      }
      if ($('#person_box_layer').length <= 0) {
        var personlink = this;
        var offset = jQuery(personlink).offset();
        var body = jQuery('body');
        jQuery('<div/>', {
          //class: 'person_box',
          id: 'person_box_layer'
          }).appendTo(body);
          $('#person_box_layer').addClass("person_box");
        }

        var personbox = $('#person_box_layer');
        if ($(this).attr('loginname') !== undefined) {
          var offset = jQuery(personlink).offset();
          jQuery(personbox).hide();

          if (jQuery(this).hasClass('noimage')) {
            personbox.addClass('noimage');
          } else {
            personbox.removeClass('noimage');
          }

          $.ajax({
            type: "GET",
            url: "/_layouts/AOFoundation.ECM.MemberDialog/CompressedInfo.aspx?loginname=" + $(this).attr('loginname') + "&locationurl=" + escape(pathname),
            dataType: 'text',
            success: function (msg) {
              personbox.html(msg)
            },
            error: function () {
              alert('Error with the Ajax-Request.');
            }
          });

          setTimeout(function () {
            if (mouseOverLink) {

              var top = offset.top + jQuery(personlink).height();
              personbox.find('div.person_box_content').removeClass('top');
              personbox.find('div.person_box_content').addClass('bottom');

              if (top + personbox.height() > jQuery(window).height() + window.scrollY) {
                top = offset.top - personbox.height() - 20;
                personbox.find('div.person_box_content').addClass('top');
                personbox.find('div.person_box_content').removeClass('bottom');
              }

              personbox.css('position', 'absolute');
              personbox.css('top', (top) + 'px');
              personbox.css('left', (offset.left) + 'px');
              personbox.css('z-index', '600');
              personbox.stop().animate({ opacity: 0 }, 0, function () { });
              personbox.stop().show();

              personbox.stop().animate({ opacity: 1 }, 300, function () { });

              hideOnTimeout = false;

              jQuery(personlink).mouseleave(function () {
                hideOnTimeout = true;

                setTimeout(function () {
                  if (hideOnTimeout) {
                    jQuery(personbox).hide();
                  }
                },
                400);
              });

              personbox.mouseenter(function () {
                hideOnTimeout = false;
                personbox.mouseleave(function () {
                  hideOnTimeout = true;
                  setTimeout(function () {
                    if (hideOnTimeout) {
                      jQuery(personbox).hide();

                    }
                  },
                  400);
                });
              });
            }
            }, 600);


          }
        });

      } catch (ex) {
        return null;
      }
    },
    /*
    * Initializes the general information overlays.
    */
    initInfoOverlays: function () {
      try {
        jQuery("#col3_content").snippet({
          url: '../../assembledobject/snpt_info_box.html',
          callback: function () {

            // link
            var mouseOverLink = false;

            // info
            jQuery('.icon_info').mouseleave(function (event) {
              mouseOverLink = false;
            });
            jQuery('.icon_info').mouseenter(function (event) {

              mouseOverLink = true;
              var infolink = this;
              var hideOnTimeout = true;
              var infobox = jQuery('div.info_box');
              var body = jQuery('body');
              infobox.appendTo(body);
              var offset = jQuery(infolink).offset();
              jQuery(infobox).hide();


              setTimeout(function () {
                if (mouseOverLink) {

                  var top = offset.top + jQuery(infolink).height();
                  infobox.find('div.info_box_content').removeClass('top');
                  infobox.find('div.info_box_content').addClass('bottom');

                  if (top + infobox.height() > jQuery(window).height() + window.scrollY) {
                    top = offset.top - infobox.height() - 20;
                    infobox.find('div.info_box_content').addClass('top');
                    infobox.find('div.info_box_content').removeClass('bottom');
                  }

                  infobox.css('position', 'absolute');
                  infobox.css('top', (top) + 'px');
                  infobox.css('left', (offset.left) - 28 + 'px');
                  infobox.css('z-index', '600');
                  infobox.stop().animate({ opacity: 0 }, 0, function () { });
                  infobox.stop().show();
                  infobox.stop().animate({ opacity: 1 }, 300, function () { });

                  hideOnTimeout = false;

                  jQuery(infolink).mouseleave(function () {
                    hideOnTimeout = true;

                    setTimeout(function () {
                      if (hideOnTimeout) {
                        jQuery(infobox).hide();
                      }
                    },
                    400);
                  });

                  infobox.mouseenter(function () {
                    hideOnTimeout = false;
                    infobox.mouseleave(function () {
                      hideOnTimeout = true;
                      setTimeout(function () {
                        if (hideOnTimeout) {
                          jQuery(infobox).hide();

                        }
                      },
                      400);
                    });
                  });
                }
              },
              600);
            });
          }
        });
      } catch (ex) {
        return null;
      }
    },
    /*
    * Initializes the image overlay.
    */
    initImageOverlays: function () {

      // load modal window
      jQuery("body").find('a.image_dialog').click(function () {
        loadSnippetModal(
          '../../baseobject/snpt_dialog_image.html'
        );
        return false;
      });
    },
    initRatingOverlays: function () {
      try {
        jQuery("#col3_content").snippet({
          url: '../../assembledobject/snpt_rating_box.html'
        });
        var mouseOverLink = false;

        jQuery('DIV.rating').live('mouseleave', function (event) {
          mouseOverLink = false;
        });

        jQuery('DIV.rating').live('mouseenter', function (event) {
          mouseOverLink = true;
          var ratinglink = this;
          var hideOnTimeout = true;
          var ratingbox = jQuery('div.rating_box');
          var body = jQuery('body');
          ratingbox.appendTo(body);
          var offset = jQuery(ratinglink).offset();
          jQuery(ratingbox).hide();

          if (jQuery(this).hasClass('noimage')) {
            ratingbox.addClass('noimage');
          } else {
            ratingbox.removeClass('noimage');
          }

          setTimeout(function () {
            if (mouseOverLink) {
              var top = offset.top + jQuery(ratinglink).height() + 7;
              ratingbox.find('div.rating_box_content').removeClass('top');
              ratingbox.find('div.rating_box_content').addClass('bottom');

              if (top + ratingbox.height() > jQuery(window).height() + window.scrollY) {
                top = offset.top - ratingbox.height() - 10;
                ratingbox.find('div.rating_box_content').addClass('top');
                ratingbox.find('div.rating_box_content').removeClass('bottom');
              }

              ratingbox.css('position', 'absolute');
              ratingbox.css('top', (top) + 'px');
              ratingbox.css('left', (offset.left) + 'px');
              ratingbox.css('z-index', '600');

              ratingbox.stop().animate({ opacity: 0 }, 0, function () { });
              ratingbox.stop().show();
              ratingbox.stop().animate({ opacity: 1 }, 300, function () { });

              hideOnTimeout = false;

              jQuery(ratinglink).mouseleave(function () {

                hideOnTimeout = true;

                setTimeout(function () {
                  if (hideOnTimeout) {
                    jQuery(ratingbox).hide();

                  }
                },
                400);
              });

              ratingbox.mouseenter(function () {
                hideOnTimeout = false;
                ratingbox.mouseleave(function () {
                  hideOnTimeout = true;
                  setTimeout(function () {
                    if (hideOnTimeout) {
                      jQuery(ratingbox).hide();

                    }
                  },
                  400);
                });
              });
            }
          },
          600);
        });

      } catch (ex) {
        return null;
      }
    },
    /*
    * Initializes the jQuery UI Datepicker.
    */
    initDatePicker: function () {

      // init datepicker		
      jQuery(".datepicker").each(function (i, el) {

        jQuery(el).datepicker({
          'dateFormat': "dd.mm.yy",
          'dayNamesMin': ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
          'firstDay': 1,
          'monthNames': ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],          
          'nextText': "nÃ¤chster Monat",
          'prevText': "vorhergehender Monat",
          'showOtherMonths': true,
          beforeShow: function (input) {
            // position to the right
            var picker = jQuery('#ui-datepicker-div')
            var field = jQuery(input);
            var left = jQuery(field).offset().left + jQuery(field).width() + 10;
            var top = jQuery(field).offset().top - 2;
            setTimeout(function () {
              jQuery(picker).css({ 'left': left + 'px', top: top + "px" });
              }, 1);
            }
          });

        });
      },
      initImageZoom: function () {
        if (jQuery.fn.rtpImgZoom) {
          jQuery('DL.image_zoom').each(function () {
            // init rtp img zoom
            jQuery('IMG', this).rtpImgZoom({
              'src': jQuery('A', this).attr('href')
            });

            // attach zoom icon on to zoom wrappper
            jQuery('DIV.rtp-imgzoom-wrapper', this)
            // insert the zoom link / icon
            .prepend('<a href="" class="zoom"></a>')

          });
        }
      }
    };

    jQuery(document).ready(function () {

      AO.initRatingOverlays();
      AO.setUpNavigation();
      AO.initTeaserMega();
      AO.initImageZoom();
      AO.initFlagToggle();
      AO.initSortTable();
      AO.initProfileOverlays();
    });



