/**
 * jQuery-Toolbox
 *
 * author: dirk loose | dloose@prolounge.de
 *
 * date: 30.11.2011
 */


/*******************************************************************
 * init namespaces after dom is ready
 ******************************************************************/
jQuery(document).ready(function() {
  var runToolbox = true;     // run this Namespace
  if (runToolbox) TOOLBOX.init();
});


/*******************************************************************
 * init functions after window is loaded
 ******************************************************************/
jQuery(window).load(function() {
  TOOLBOX.setDimensions();
});


var TOOLBOX = {

  // --- configuration-settings ---
  debug : false,

  // --- initialize ---
  init: function(event) {

    /**
     * bind event to resize
     */
    $(window).bind('resize', function() {
      TOOLBOX.setDimensions();
    });

    // --- setDimensions on domready (needed for internet-explorer)
    // TOOLBOX.setDimensions();
    // end block

  },

  /**
   * set dimensions for picture
   */
  setDimensions : function() {

    var myOffsetHeight = 70;
    var myOffsetWidth = 200;

    // --- define needed objects
    var myDocumentHeight = $(window).height();
    var myDocumentWidth = $(window).width();

    // --- calculate height of elements
    var myElementsHeight = 0;
    $('.calculateHeight').each(function() {
      myElementsHeight = myElementsHeight + $(this).height();
    });


    var myAspectRatio = 1400 / 625;
    var myHolderHeight = myDocumentHeight - myElementsHeight - myOffsetHeight;
    var myImgWidth = Math.floor((myHolderHeight * myAspectRatio));

    // portrait & landscape calculations
    if(myImgWidth > myDocumentWidth ) {
      myImgWidth = myDocumentWidth;
      myHolderHeight = Math.floor((myDocumentWidth / myAspectRatio));
    }

    var myHolder = $('#holder');
    myHolder.css({
      'height' : myHolderHeight,
      'width'  : myImgWidth
    });

    // --- set width and height for all desired elements
    $('.setWidth').css('width', myImgWidth);
    $('.setHeight').css('height', myHolderHeight);


    var myImage = $('#holder img');
    myImage.css('visibility', 'visible');
    // myImage.fadeIn(1000, 'swing', true);
    myImage.css({
      'height' : myHolderHeight,
      'width'  : myImgWidth
    });

    /*
     // --- set viewPort of images
     var myImages = $('#holder img');
     var viewPort = 'landscape';
     myImages.each(function(index, domElement) {

     var theWindow        = $('#holder'), // $(window) | $('#holder')
     myAspectRatio = 980 / 438;

     viewPort = ( (theWindow.width() / theWindow.height()) < myAspectRatio ) ? 'portrait' : 'landscape';
     (viewPort == 'portrait') ? $(domElement).css('min-height', myHolderHeight) : $(domElement).css('min-height', '');

     $(domElement).removeClass().addClass(viewPort);
     });
     */

//    console.log('height: ' + myDocumentHeight);
//    console.log('width: ' + myDocumentWidth);
//    console.log('calculated Offset: ' + myElementsHeight);
//    console.log('holder height: ' + myHolderHeight);
//    console.log('img height: ' + myHolderHeight);
//    console.log('img width : ' + Math.floor(myHolderHeight * myAspectRatio));

//    console.log('-----------------------------------------------')

  }

};


/**
 * output to console, if console.log exists
 *
 * usage: jQuery.fn.log(message);
 */
jQuery.fn.log = function(message) {
  if (typeof console.log != "undefined" || typeof jQuery.fn.console.log == 'function') {
    console.log(message);
  }
};
