var init = function() {
  jQuery('.tips').cluetip({
    splitTitle: '|', // use the invoking element's title attribute to populate the clueTip...
                     // ...and split the contents into separate divs where there is a "|"
    positionBy: 'fixed',
    leftOffset: 20,
    sticky: false,
    fx: {
      open: 'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'
      openSpeed: 'fast'
    }
  });
  jQuery('.zoomartikel').cluetip({
    positionBy: 'auto',
    topOffset: 0,
    leftOffset: 0,
    width: 520,
    height: 'auto',
    sticky: false,
    showTitle: false, // hide the clueTip's heading
    fx: {
      open: 'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'
      openSpeed: 'fast'
    }
  });
  jQuery('.zoombasket').cluetip({
    positionBy: 'fixed',
    topOffset: -100,
    leftOffset: -650,
    width: 550,
    height: 650,
    zIndex: 5,
    showTitle: false, // hide the clueTip's heading
    fx: {
      open: 'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'
      openSpeed: 'fast'
    }
  });
  initZoombasket();
  initAGB();
  // FIXME disable close on ESC and close-button, replace by custom behaviour, as we are in the middle of a workflow here...
  jQuery('#dialog-order').dialog({
    title: 'Bestellvorgang',
    resizable: true,
    width: 785,
    height:500,
    modal: true,
    autoOpen: false,
    zIndex: 3,
    dialogClass: 'extracolordialog',
    open:  function(event, ui) { jQuery( '#b2cBasketWrapper' ).css( 'visibility', 'hidden' ); },
    close: function(event, ui) { jQuery( '#b2cBasketWrapper' ).css( 'visibility', 'visible'); jQuery( 'body' ).css( 'overflow', 'auto' ) } // show basket on the right again, dont suppress scrolling of page anymore
  });
  var maxHeight = 580;
  jQuery('#article-details').dialog({
    title: 'KONPLOTT - Miranda Konstantinidou',
    resizable: true,
    width: 760,
    height:470,
    modal: true,
    show: {
      effect: 'fade',
      complete: function() {
        $(this).focus(); // cf. http://stackoverflow.com/questions/3603854/why-does-my-jquery-ui-dialog-lose-the-focus-when-it-finishes-the-open-effect
        $( '.ui-dialog-content', this ).animate( { height: Math.min( Math.max( $('.artikelbild', this ).height(), $('.artikeltext', this ).height() ), maxHeight ) + 25 }, 'fast' );
        //$( 'body' ).css( 'overflow-y', 'scroll' );
      }
    },
    hide: 'fade',
    autoOpen: false,
    zIndex: 3,
    dialogClass: 'extracolordialog',
    close: function(event, ui) { jQuery( 'body' ).css( 'overflow', 'auto' ) } // dont suppress scrolling of page anymore
  });
  // FIXME! dynamic yearRange
  jQuery('#youtube-dialog').dialog({
    resizable: true,
    width: 550,
    height:470,
    modal: true,
    autoOpen: false,
    zIndex: 3,
    dialogClass: 'extracolordialog',
    close: function(event, ui) { jQuery( '#youtube-dialog' ).css( 'visibility', 'hidden'); } // hide again for IE
  });
  jQuery('#miranda-dialog').dialog({
    resizable: true,
    width: 700,
    height:500,
    modal: true,
    autoOpen: false,
    zIndex: 3,
    dialogClass: 'extracolordialog',
    close: function(event, ui) { jQuery( '#miranda-dialog' ).css( 'visibility', 'hidden'); } // hide again for IE
  });
  /* FIXME clean up redundant definition on various pages... */
  jQuery('#dialogRequestPasswordChange').dialog({
    title: 'Passwort vergessen',
    resizable: false,
    width: 700,
    height:300,
    modal: true,
    autoOpen: false,
    zIndex: 3,
    dialogClass: 'extracolordialog',
    close: function(event, ui) { jQuery( 'body' ).css( 'overflow', 'auto' ) } // dont suppress scrolling of page anymore
  });
  // limit height of fixed positioned b2cBasketContent-container
  if( jQuery( '#b2cBasketItems' ).height() > jQuery( window ).height() - 240 )
    jQuery( '#b2cBasketItems' ).height( jQuery( window ).height() - 240 );
  /*
  jQuery( document ).click( function () {
    jQuery( '.price:hidden' ).show( "slow" );
  });
  */
};
initZoombasket = function() {
  jQuery('.zoomorderbasket').cluetip({
    positionBy: 'fixed',
    topOffset: -100,
    leftOffset: 40,
    width: 400,
    height: 550,
    zIndex: 5,
    showTitle: false, // hide the clueTip's heading
    fx: {
      open: 'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'
      openSpeed: 'fast'
    }
  });
};
var initZoom = function() {
  jQuery("#wrap").anythingZoomer({
    expansionSize: 30,
    speedMultiplier: 2.2,
    zoomPort: '#artikeltext'
  });
};
var initAGB = function() {
  jQuery('#agb-popup').dialog({
    title: 'Allgemeine Gesch&auml;ftsbedingungen',
    resizable: false,
    width: 700,
    height:400,
    modal: true,
    autoOpen: false,
    zIndex: 3,
    dialogClass: 'extracolordialog',
    open: function(event, ui) { jQuery( 'body' ).css( 'overflow', 'hidden' ) }, // suppress scrolling of page
    close: function(event, ui) { jQuery( 'body' ).css( 'overflow', 'auto' ) } // dont suppress scrolling of page anymore
  });
};
$(document).ready(init);

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
    var $next =  $active.next().length ? $active.next()
  : $('#slideshow IMG:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
  .addClass('active')
  .animate({opacity: 1.0}, 500, function() {
      $active.removeClass('active last-active');
  });
}
$(function() {
    setInterval( "slideSwitch()", 1000 );
});

$.fn.animateHighlight = function( highlightColor, duration, delay ) {
    var highlightBg = highlightColor || "#FFFF9C";
    var animateMs = duration || 1500;
    var delayMs = delay || 1000;
    var originalBg = this.css( "backgroundColor" );
    this.stop().css( "background-color", highlightBg ).delay( delayMs ).animate( { backgroundColor: originalBg }, animateMs );
};


FB.init({
  status : true, // check login status
  cookie : true, // enable cookies to allow the server to access the session
  xfbml  : true  // parse XFBML
});

