/*
	general.js
	general
*/


$(document).ready(function(){

	/**
	 * toggle set
	 */
	$('.toggle-set')
		// show visible items
		.find('.toggle-visible').show().end()
		// hide non-visible items
		.find('.toggle-hidden').hide().end()
		// show tiggers
		.find('.toggle-trigger').show()
			.find('a').click(function() {
				// slide
				$(this).closest('.toggle-item').slideEm(300);
				// one or less trigger items left? -> hide global trigger
				var toggleset = $(this).closest('.toggle-set');
				var triggers = toggleset.find('.toggle-trigger:visible');
				if (triggers.length <= 1) {
					toggleset.find('.toggle-trigger-all').hide();
				}
				// return
				return false;
			})
			.end()
		.end()
		// show global trigger
		.find('.toggle-trigger-all').show()
			.find('a').click(function() {

				var toggleset = $(this).closest('.toggle-set');
				// get current height
				var fixheight = toggleset.outerHeight();				
				toggleset
					.css('min-height', fixheight+'px')
					// slide
					.find('.toggle-item').andSelf().slideEm(1000);
				// hide trigger
				$(this).blur().hide();
				// return
				return false;
			});

	$.fn.slideEm = function(speed) {  
  		return this.each(function() {
			// get current height
			var fixheight = $(this).outerHeight();
			$(this)
				// .css('min-height', fixheight+'px')
				// slide
				.find('.toggle-hidden').slideDown(speed, function() {
					// reset display style on list items
					if ($(this).is('li')) {
						$(this).css('display', 'list-item');
					}
				})
				.end()
				// hide trigger
				.find('.toggle-trigger').blur().hide();
  		});
	}

	/**
	 * :first-child + :last-child (IE)
	 */
	var li = $('li');
	li.find(':first-child').addClass('first-child');
	li.find(':last-child').addClass('last-child');

	/**
	 * search field
	 * clear on focus, replace with default on blur
	 */
	$.fn.search = function() {
		return this.focus(function() {
			if( this.value == this.alt ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$("#searchfield").search();


	/*
	 * external links
	 * based on: http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links
	*/
	$('a').filter(function() {
		return this.hostname && (this.hostname).split(":")[0] !== (location.hostname).split(":")[0];
	})
	.not(':has(img, div, mailto)')
	.addClass('external')
	.click(function() {
		window.open(this.href);
		return false;
	});




/**
 * Add clicks to screenshots
 **/
$('dl#screenshots a').click( function() { 
  $('#screenshot img').attr('src', this.href); return false; 
} );
    
    
    
$('a[rel=screencast]').click( function() {
  var videoStage = $('#video-stage')[0]
  if ( !videoStage ) {
    return;
  }
        
  var body = $('body')[0];
  if ( videoStage.parentNode != body ) {
    body.appendChild( videoStage )
  }
  videoStage.style.display = 'block'

  var video = $('video', videoStage)[0]
  var container = $('.video-container', videoStage)[0];
  var width = height = 0
  if ( video && video.width ) {
     width = parseInt(video.width)
     height = parseInt(video.height)
  } else {
    var object = $('object', videoStage)[0]
    if ( object && object.width ) {
      width = parseInt(object.width)
      height = parseInt(object.height)
    } else {
      width = parseInt(container.width)
      height = parseInt(container.height)
    }
  }

  container.style.marginTop = ( height / 2 * -1 ) + 'px'
  container.style.marginLeft = ( width / 2 * -1 ) + 'px'
        
  var closeButton = $('.close', container)[0];
  if ( !closeButton ) {
    closeButton = document.createElement( 'button' )
    closeButton.innerHTML = 'close'
    closeButton.className = 'close'
    container.appendChild(closeButton)
    $(closeButton).click( function( element ) {
      videoStage.style.display = 'none'
      if ( video ) {
        video.pause()
      }
    })
  }
        
  if ( video ) {
    video.play()
  }

  return false;
});
    
$('#video-stage').each( function( index, element ) {
  element.style.display = 'none'
})

$('html')[0].className += ' script-enabled'
});
