﻿(function($) {
	$.fn.outerHTML = function(s) {
		return (s) 
			? this.before(s).remove() 
			: $('<span>').append(this.eq(0).clone()).html();
	}
})(jQuery);

$.fn.nextUntil = function(expr) { 
    var match = []; 
    // We need to figure out which elements to push onto the array 
    this.each(function(){ 
        // Traverse through the sibling nodes 
        for( var i = this.nextSibling; i; i = i.nextSibling ) { 
            // Make sure that we're only dealing with elements 
            if ( i.nodeType != 1 ) continue; 
            // If we find a match then we need to stop 
            if ( jQuery.filter( expr, [i] ).r.length ) break; 
            // Otherwise, add it on to the stack 
            match.push( i ); 
        } 
    }); 
    return this.pushStack( match, arguments ); 
}; 

$().ready( function()
{
  // Menu
  $( 'ul.dropdown li' ).hover(
    function()
    {
      $(this).find( 'ul' ).show();
    },
    function()
    {
      $(this).find( 'ul' ).hide();
    } );

  // Balloons
  var balloonOptions =
    {
      cornerRadius: 10,
      padding: 15,
      width: 500,
      fill: '#f0f0f0',
      strokeWidth: 1,
      strokeStyle: '#363636',
      positions: ['bottom', 'right']
    }
    
  $( '.popup-link' ).each( function()
    {
      var html = [];
      $( this ).nextAll().each( function()
      {
         if( $( this ).filter( ':not(.popup-content)' ).length )
         {
           return false;
         }
         
         html.push( $( this ).show().remove().outerHTML( ) );
      } );

      $( this ).bt( html.join( '' ), balloonOptions );
    } );

} );
