//_______________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//    Project: Pöppelmann Relaunch 2010
//
//    Copyright (C) 2009 by Knipp Medien und Kommunikation GmbH
//
//    initial autor:   Igor Lyubimov
//    version:         $Revision: 1641 $
//                     $Date: 2012-02-01 18:10:34 +0100 (Mi, 01 Feb 2012) $
//                     $Author: igor.lubimov $
//

// common code

/**
 * Function, which will be called on page load
 */
$(function () {
  // news stack initialization
  $('div.newsStack').each (initializeNewsStack);

  // frame dialog initialization
  $('<div id="frameDialogContainer" title="" />').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    height: 520,
    width: 500
  }).append($('<iframe id="modalIframeId" name="modalIframeId" width="100%" height="99%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="auto" />'));

  $('a.frameDialogLink').each(function () {
    var targetURI = $(this).attr('href');
    $(this).attr('href', "javascript:showFrameDialog('" + targetURI + "')");
  });
  $('form.frameDialogForm').each(function () {
    //var targetURI = $(this).attr('action');
    var params = '';
    var w = $(this).attr('w');
    var h = $(this).attr('h');
    var newSubmit = function () {
      showFrameDialog(0, '' + w + 'px', h);
    };
    if (w) {
      params += ', ' + w;
    }
    if (h) {
      params += ', ' + h;
    }
    $(this).attr('onsubmit1', "showFrameDialog(0" + params + ")");
    $(this).submit(newSubmit);
  });

  // make news teaser text linked to details page
  $('.newsArea .detailsLink').each(function () {
    var href = $(this).attr('href');
    $(this).siblings('.teaser').addClass('linked').click(function () {
      window.location.href = href;
      });
    });

//  activateCustomScrollbars ();
});

function activateCustomScrollbars ()
{
  var win = $(window);
  // Full body scroll
  var isResizing = false;
  win.bind(
    'resize',
    function()
    {
      if (!isResizing) {
        isResizing = true;
        var container = $('#container_page');
        // Temporarily make the container tiny so it doesn't influence the
        // calculation of the size of the document
        container.css(
        {
          'width': 1,
          'height': 1
        }
        );
        // Now make it the size of the window...
        container.css(
        {
          'width': 1,
          'height': 1
        }
        );
        // Now make it the size of the window...
        container.css(
        {
          'width': win.width(),
          'height': win.height()
        }
        );
        isResizing = false;
        container.jScrollPane(
        {
          'showArrows': true
        }
        );
      }
    }
    ).trigger('resize');

  // Workaround for known Opera issue which breaks demo (see
  // http://jscrollpane.kelvinluck.com/known_issues.html#opera-scrollbar )
  $('body').css('overflow', 'hidden');

  // IE calculates the width incorrectly first time round (it
  // doesn't count the space used by the native scrollbar) so
  // we re-trigger if necessary.
  if ($('#container_page').width() != win.width()) {
    win.trigger('resize');
  }

  // Internal scrollpanes
  $('.scroll-pane').jScrollPane({
    showArrows: true
  });
}

/**
* hides news container with current id and shows news container with new id
*
* @param curId       current news container id
* @param newId       news container id, which should be shown
*
* @return            0 to prevent problems, when called on href
*/
function rotateNewsStack (curId, newId) {
  jQuery('#' + curId).hide ();
  jQuery('#' + newId).show ();
  void (0);
}

/**
* news stack initialisation
*
* @param index       news stack index
* @param stack       news container object
*/
function initializeNewsStack (index, stack) {
  var $s = $(stack);
  var count = 0;
  var prevId = 0;
  var firstId;
  function setLink (aSelector, curId, newId) {
    $('a' + aSelector, '#' + curId).attr ('href',
      'javascript:rotateNewsStack("' + curId + '", "' + newId + '");');
  }
  $('div.news-list-item', $s).each (function (j, el) {
    count++;
    var $e = $(el);
    var curId = $e.attr ('id');
    if (j == 0) {
      $e.show ();
      firstId = curId;
    }
    $('div.nlCategory .number', $e).text (j+1);
    if (prevId != 0) {
      setLink ('.prev', curId, prevId);
      setLink ('.next', prevId, curId);
    }
    prevId = curId;
  });
  $('div.nlCategory .count', $s).text (count);
  setLink ('.prev', firstId, prevId);
  setLink ('.next', prevId, firstId);
}

/**
* show page with given url as modal dialog
*
* @param url      url of page to be shown (if not specified, then unchanged)
* @param width    new dialog width (if not specified, then unchanged)
* @param height   new dialog height (if not specified, then unchanged)
*/
function showFrameDialog(url, width, height) {
  if (url) {
    $("#modalIframeId").attr("src", url);
  }
  if (width) {
    $("#frameDialogContainer").dialog('option', 'width', width);
  }
  if (height) {
    $("#frameDialogContainer").dialog('option', 'height', height);
  }
  $("#frameDialogContainer").dialog("open");

  void (0);
}

/**
* submits specified form to specified url if specified precondition is met
* and shows dialog with submitted page
*
* @param formId       id of form to be submitted
* @param actionUrl    url of action form attribute, which need to be set before submit
*                     (if empty, then unchanged)
* @param precondition boolean function, whichwill be called before submission
*                     to determine if submission should be done
* @param w            dialog width (if not specified, then unchanged)
* @param h            dialog height (if not specified, then unchanged)
*/
function submitDialogForm (formId, actionUrl, precondition, w, h) {
  if (precondition) {
    if (actionUrl) {
      $('#' + formId).attr('action',actionUrl);
    }
    document.forms['contactForm'].submit();
    var width = w ? w : 770;
    var height = h ? h : 620;
    showFrameDialog(0, width, height);
  }
}

/**
* checks if some products in product table are checked
*
* @return     TRUE if product(s) selected, FALSE otherwise
*/
function checkCheckedProducts () {
  var hasChecked = ($("table.pd-table td.download input:checked").length > 0);

  if (!hasChecked) {
    $("#wishList").ibShowNotFoundDialog();
  }

  return hasChecked;
}

/**
* Shortcut for submitDialogForm with some default values
*
* @param actionUrl    url of action form attribute, which need to be set before submit
*/
function submitDialogContactForm (actionUrl) {
  submitDialogForm ('contactForm', actionUrl, checkCheckedProducts ());
}


