/**
 * JavaScript - main functions
 *
 * This Sciptfile provides general javascript functionalities.
 * The most functions requires jQuery
 *
 * @copyright 2002-2007 by papaya Software GmbH - All rights reserved.
 * @link http://www.papaya-cms.com/
 * @licence   GNU General Public Licence (GPL) 2 http://www.gnu.org/copyleft/gpl.html
 *
 * You can redistribute and/or modify this script under the terms of the GNU General Public
 * License (GPL) version 2, provided that the copyright and license notes, including these
 * lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 *
 * @package display_customization
 * @author  Michael van Engelshoven <engelshoven@papaya-cms.com>
 * @version $Id$
 */

/**
 
* This will create the dom-object for papayaDynamicData for Javascripts. This
 * Object should be fille in xsl-templates and can be uses in javascripts
 */


if(typeof papayaDynamicData == "undefined") {
  papayaDynamicData = {};
}

/**
 
* This will create the dom-object for all functions and methodes we need in aglobal
 * context.
 */


if(typeof HobnoxMainFunctions == "undefined") {
  HobnoxMainFunctions = {};
}

/**
 * This script provides the collapse functionality. It toggle the collapse state
 * of each Element match the Query.
 * How to Use:
 * <a rel="div.anyClass" href="#" class="collapser somelink">Collapse it!</a>
 * <a rel="div#anyId" href="#" class="collapser otherlink">Collapse it!</a>
 *
 * write your Query like css into the "rel"-attribute i.e. rel="div.anyClass".
 * The script identifies yor link by tagname "<a" and classname "collapser".
 * Remember: you can set more than one class ;-)
 */
$(document).ready( function() {
  // Set "click" event to all anchors with class "collapser"
  $('a.collapser').click( function() {
    // use string in anchors attribute "rel" as object and slide it up/down
    $($(this).attr('rel')).slideToggle('slow');
    // Return false to deactive loading of target in href attribute
    return false;
  });
});


/**
 

* Accordion Function for FAQ and some others. This will be expanded for use in
 * other Cases...
 */
$(document).ready( function () {
  // Close all objects at initialization
  $('.cntFaqAnswer').hide();
  // The first object should be opened
  $('.cntFaqQuestion').css('cursor', 'pointer');
  $('.cntFaqAnswer').eq(0).show().toggleClass('cntFaqAnswerOpen');
  // Is an anchor clicked ...
  $('.cntFaqQuestion').click( function () {
    var faqQuestion = $(this);
    // ... get the Clicked object ...
    //.. slide all others opened up ...
    $('.cntFaqAnswerOpen').not( $(this).next('.cntFaqAnswer') ).slideUp('fast', function() {
    }).toggleClass('cntFaqAnswerOpen');
      // ... and open the following object!
      $(this).next('.cntFaqAnswer').slideToggle('slow').toggleClass('cntFaqAnswerOpen');
    return false;
  });
});


/*
 * @author Tobias Stechbarth <stechbarth@papaya-cms.com>
 * @param string $id
 * @return bool TRUE
 */
function stopPlayer(id) {
  var flashObject = document.getElementById(id);
  if (flashObject) {
    var innerObjects = flashObject.getElementsByTagName('object');
    if (innerObjects.length > 0) {
      flashObject = innerObjects[0];
    }
    flashObject.closeConnection();
  }
  return true;
}

HobnoxMainFunctions.initSpeechbubbles = function() {
  // bind evnent handler to display the speechbutton tooltips
  var currentSpeechBubble;
  var currentSpeechBubbleColorPool = ['speechBubbleBronze', 'speechBubblePurple', 'speechBubbleBlue', 'speechBubbleGreen', 'speechBubbleYellow', 'speechBubbleOrange'];
  var currentSpeechBubbleColor = currentSpeechBubbleColorPool[Math.ceil(Math.random() * currentSpeechBubbleColorPool.length - 1)];

  $('.speechBubbleParent img').bind('mouseover', function(e) {
    // remove an existing speechbubble if there is one
    if(currentSpeechBubble) {
      $(currentSpeechBubble).remove();
    }
    // clone the speechbubble
    currentSpeechBubble = $(this).parents('.speechBubbleParent').children('.speechBubble').clone();
    // and append it to the body
    $('body').append(currentSpeechBubble);
    // change the speech bubbles color
    $(currentSpeechBubble).addClass(currentSpeechBubbleColor);
    // to make it easily moveable wwith pageX and pageY
    $(currentSpeechBubble).css({display: 'block', zIndex: '500', position: 'absolute', left: e.pageX + 5, top: e.pageY - 5});
  });

  $('.speechBubbleParent img').bind('mousemove', function(e) {
    // to make it easily moveable wwith pageX and pageY
    $(currentSpeechBubble).css({display: 'block', zIndex: '500', position: 'absolute', left: e.pageX + 5, top: e.pageY - 5});
  });

  $('.speechBubbleParent img').bind('mouseout', function() {
    // remove the node and set the reference to null.
    if (currentSpeechBubble) {
      $(currentSpeechBubble).remove();
    }
    // currentSpeechBubbleColor = currentSpeechBubbleColorPool[Math.ceil(Math.random() * currentSpeechBubbleColorPool.length - 1)];
    currentSpeechBubble = null;
    test = Math.ceil(Math.random() * currentSpeechBubbleColorPool.length - 1);
    currentSpeechBubbleColor = currentSpeechBubbleColorPool[test];
  });
  $('.speechBubbleParent .expandCollapse').bind('mousedown', function(e) {
    if($(this).attr('rel') == 'expand') {
      $(this).attr('rel', 'collapse');
      $('.speechBubbleParent .collapsed').fadeOut('5000', function() {$('.speechBubbleParent .expanded').fadeIn('5000');});
    } else {
      $(this).attr('rel', 'expand');
      $('.speechBubbleParent .expanded').fadeOut('5000', function() {$('.speechBubbleParent .collapsed').fadeIn('5000');});
    }
      // change link icon
    $('.speechBubbleParent .expandCollapse span').toggleClass('iconProjectMemberExpanded');
    $('.speechBubbleParent .expandCollapse span').toggleClass('iconProjectMemberCollapsed');
  });
}

// Speech Bubbles
$(document).ready(function(){
  HobnoxMainFunctions.initSpeechbubbles();
});

/*
 * End of file
 */
