var App = {
    bgImageFadeInAnimationTime : 2000,
    tooltipDelay : 200
};

var lightbox = {

    lbBg : null,
    lbFg : null,
    backContainer : false,
    closeCb : null,
    config :
    {
        closeOnBgClick : true,
        removeBgInsteadOfHide : false,
        bgColor : '#000000',
        bgOpacity : 0.5
    },

    _create : function()
    {
        if(this.lbBg) this.lbBg.remove();
        if(this.lbFg) this.lbFg.remove();
        this.lbBg = $('<div>').css({
            zIndex: 85,
            width : $(window).width(),
            height : $(window).height(),
            backgroundColor : lightbox.config.bgColor,
            position : 'fixed',
            top : 0,
            left : 0,
            opacity : lightbox.config.bgOpacity
        }).click(function(){
            if(lightbox.config.closeOnBgClick)
            {
                lightbox.close();
            }
        }).appendTo('body');

        this.lbFg = $("<div>").css({
            zIndex: 85,
            position : 'fixed',
            top : 40,
            left : parseInt($(window).width() / 2)
        }).appendTo('body');
    },

    load : function(selector, bc)
    {
        this._create();
        var html = $(selector);
        html.find('script').remove();
        this.lbFg.html(html).css({
            marginLeft : (parseInt(this.lbFg.width() / 2) * -1)
        });
        this.backContainer = bc;
    },

    close : function()
    {
        this.lbBg.hide();
        if(this.backContainer) this.lbFg.children().appendTo(this.backContainer);
        this.lbFg.remove();

        if(typeof this.closeCb == 'function')
        {
            this.closeCb();
        }
    },

    lbClick : function(selector, lbcontent, callback, backContainer, closecb)
    {
        $(selector).click(function(e){
            e.preventDefault();
            lightbox.load(lbcontent, backContainer);
            if(typeof callback == 'function') callback();
            if(typeof closecb == 'function') this.closeCb = closecb;
        });
    },

    lbOpen : function(node)
    {
      this._create();
      this.lbFg.html(node).css({
        marginLeft : (parseInt(this.lbFg.width() / 2) * -1)
      });
    }
};

/**
 * Seitenelemente verstecken
 */
function hideSiteElements() {
  var browserWidth = ($(window).width()-960)/2;
  var browserHeight = $(window).height();

  /* nach oben */
  $('#header_bg, #name_tag').animate({
    top: $('#header_bg').height() * -1
  }, 2000);
  $('#name_tag').animate({
    top: ($('#name_tag').height()+10) * -1
  }, 2000);
  /* nach rechts */
  $('#wrapper').animate({
    right: ($('#wrapper').width()+browserWidth) * -1
  }, 2000);
  /* nach unten */
  $('#footer').animate({
    bottom: $('#footer').height() * -1
  }, 2000);
  /* nach links */
  $('#main_nav').animate({
    left: ($('#main_nav').width()+browserWidth) * -1
  }, 2000);
  $('#logo').animate({
    left: ($('#logo').width()+browserWidth+210) * -1
  }, 2000, function(){
      $('#ico_maximize').fadeToggle(200);
  });
}

function showSiteElements() {
  var browserWidth = $(window).width();
  var browserHeight = $(window).height();
  
  /* nach oben */
  $('#header_bg, #name_tag').animate({
    top: 0
  }, 2000);
  /* nach rechts */
  $('#wrapper').animate({
    right: 0
  }, 2000);
  /* nach unten */
  $('#footer').animate({
    bottom: 0
  }, 2000);
  /* nach links */
  $('#main_nav').animate({
    left: 0
  }, 2000);
  $('#logo').animate({
    left: 0
  }, 2000);

  $('#ico_maximize').fadeToggle(200);
}

/*
 *
 */
function centerPanorama()
{
    function doCenter(hard)
    {
        var width = $('#panorama img').width();
        var height = $('#panorama img').height();
        if(width < 960) width = 960;
        $('#panorama').css({
            left : '50%',
            top : '50%',
            marginTop : (parseInt(height / 2) * -1),
            marginLeft : (parseInt(width / 2) * -1),
            display : 'none',
            visibility : 'visible'
        });
        if(hard) $('#panorama').show();
        else $('#panorama').fadeIn(App.bgImageFadeInAnimationTime);
    }
    
    var img = $('#panorama img');
    if(img.width() > 0)
    {
        doCenter(true);
        return;
    }
    else
    {
      var interval = window.setInterval(function(){
          if(img.width() > 0)
          {
              doCenter(false);
              window.clearInterval(interval);
              return;
          }
      }, 50);
    }
}

/**
 * Hintergrundbild skallieren
 */
//groesse des Hintergrundbildes ermitteln
var maxH;

function bg_resize() {
  //minimale groesse festlegen
  var minH = 633;

  //resize bei Bild groesser Browserfenster (Initialzustand)
  //Browser < Bild
  if ($(window).height() < maxH) {
    //Browser <= 960px W
    if ($(window).height() <= minH)
    {
      $('#panorama img').height(minH);
    }
    else
    {
      $('#panorama img').height($(window).height());
    }
  }
  else
  {
    $('#panorama img').height(maxH);
  }
  centerPanorama();
}

/**
 * Diese Funktion wird ausgeführt, sobald das Dokument vollständig geladen wurde.
 */
$(document).ready(function(){
    centerPanorama();
    //Hintergrundbild skallieren
    maxH = $('#panorama img').height();
    $('#panorama img').removeAttr('width').removeAttr('height');
    bg_resize();
    $(window).resize(bg_resize);
     //resize bei Browserfenster Groessenaenderung

    /* Elemente ein-/ausblenden */
    $('#ico_minimize').click(function(e){
        hideSiteElements();
    });
    $('#ico_maximize').click(function(e){
        showSiteElements();
    });

    /* Tooltip */
    $('.tooltip').jstooltip({
               showURL: false,
               delay: App.tooltipDelay,
               top: -3,
               left:20}
           );

    /* Imagescroller */
    if($('#img_scr_team_container').length > 0)
    {
        $('#img_scr_team_container').ImageScroller({
            hideInactiveBoxes : false,
            cntrls : {
                left : $('#left_ctrl'),
                right : $('#right_ctrl'),
                buttonContainer : $('#bottom_ctrl'),
                buttonClass : 'bg_sprite inact',
                buttonActiveClass : 'act',
                buttonShowNum : false
            },
            viewPortSize : 5,
            oneLine : true,
            defaultAnimation : 'slideFirstOutAndAppend',
            boxSelector : '.img_scr_team_item_container'
        });
    }
    if($('#img_scr_gallery_container').length > 0)
    {
        $('#img_scr_gallery_container').ImageScroller({
            cntrls : {
                left : $('#left_ctrl'),
                right : $('#right_ctrl'),
                buttonContainer : $('#bottom_ctrl'),
                buttonClass : 'bg_sprite inact',
                buttonActiveClass : 'act',
                buttonShowNum : false
            },
            viewPortSize : 1,
            stackBoxes: true,
            hideInactiveBoxes: true,
            defaultAnimation : 'slide',
            boxSelector : '.img_scr_gallery_item_container'
        });
    }

  // Ligthbox
  lightbox.lbClick('#ico_weather', '#lb_weather', function(){
    if($.browser.msie)
    {
      $('.weatheriframe').attr('src', $('.weatheriframe').attr('src'));
    }
  }, '#hidden_area');
  //- Close button
  $('.lb_btn_close').live('click', function(){
    lightbox.close();
  });

  //Externeseiten iFrame
  $('a[target=_iframe]').externalLink({
      urlTarget : '/index.php?id=95'
  });

  if($('#lb_hotnews .lb_middle_content').children('div').length <= 0)
  {
    $('#ico_hotnews').css({
      backgroundPosition: '-200px -300px',
      cursor : 'default'
    });
  }
  else
  {
    lightbox.lbClick('#ico_hotnews', '#lb_hotnews', function(){
    }, '#hidden_area');
  }

  $('a.lightbox').live('click', function(e){
    var imgTag = $('<img>').attr('src', $(this).attr('href'));
    var lbc = $('#lb_image').clone();
    lbc.attr('id', 'lb_image_active');
    lbc.find('.lb_middle_content').append(imgTag);
    lightbox.lbOpen(lbc);
    var width = lbc.find('.lb_top').width();
    lbc.find('.lb_top').children('.middle').width((width - 12));
    return false;
  });

  lightbox.lbClick('a[target=_lightbox]', '#lb_tournament_schedule', function(){
  }, '#hidden_area');
});

