function init_modal() {

	  if (!$('a.modal').length) {
		    return;
	  }

    var $IE6 = typeof document.addEventListener !== 'function' && !window.XMLHttpRequest;

    // Insert modal at end of </body>.
    $('body').append('<div id="modal_wrapper"><!--[if IE 6]><iframe id="modal_iframe" frameborder="0"></iframe><![endif]--><div id="modal_overlay"></div><div id="modal_window"><div id="modal_bar"><strong>Modal window</strong><a href="#" id="modal_close">Close</a></div><div id="modal_content"></div></div>');


    // Do some math.
    function sizeModal() {

        // Modal dimensions.
        var $modal          = $('#modal_window');
        var $modal_width    = $modal.outerWidth();
        var $modal_height   = $modal.outerHeight();
        var $modal_top      = '-' + Math.floor($modal_height / 2) + 'px';
        var $modal_left     = '-' + Math.floor($modal_width / 2) + 'px';

        // Set modal.
        $('#modal_window').css('margin-top', $modal_top).css('margin-left', $modal_left);
    }


    /* For IE6. */
    function positionModal() {
        // Force modal into place.
        $('#modal_wrapper').css('top', $(document).scrollTop() + 'px');
    }

    // Reveal the modal.
    function showModal() {

        // Bleh.
        if ($IE6) {
            positionModal();
        }

        // Unveil the wrapper.
        $('#modal_wrapper').show();

        // Size it.
        sizeModal();

        // Reveal modal window.
        $('#modal_window').css('visibility', 'visible').show();

        // Resize as images load.
        $('#modal_content img').each(function() {
            $(this).load(function() {
                $(this).removeClass('modal_placeholder').show();
                sizeModal();
            });
        });
    }



    // Look for modal links.
    $('a.modal').click(function() {

        // Check the href="..."
        var $the_link = $(this).attr('href');
        
        // Determine link target.
        if ($the_link.match(/^#./)) {

            // Assume #anchor content.
            $('#modal_content').html($($(this).attr('href')).html());
            showModal();

        } else if ($the_link.match(/.jpg$/) || $the_link.match(/.png$/) || $the_link.match(/.gif$/)) {

            // Assume image content.
            $('#modal_content').html('<p id="modal_image_wrapper"><img src="' + $the_link + '" class="modal_placeholder" /></p>');
            showModal();

        } else {
        
            

            // Assume external Ajax content.
            $('#modal_content').load($(this).attr('href').replace('#', ' #'), '', showModal);
        }

        // Determine modal title.
        if ($(this).attr('title')) {

            // Insert title.
            $('#modal_bar strong').html($(this).attr('title'));

        } else if ($(this).html() !== '') {

            // Insert link text.
            $('#modal_bar strong').html($(this).html());
        }

        // Nofollow.
        this.blur();
        return false;
    });



    // Hide modal elements.
    $('#modal_overlay, #modal_close').click(function() {

        // Hide the modal.
        $('#modal_wrapper').hide();

        // Hide, because images might load later.
        $('#modal_window').css('visibility', 'hidden');

        // Unbind image listeners.
        $('#modal_content img').each(function() {
            $(this).unbind();
        });

        // Destroy modal content.
        $('#modal_content').html('');

        // Reset modal title.
        $('#modal_bar strong').html('Modal window');

        // Nofollow.
        this.blur();
        return false;
    });



    if ($IE6) {
        $(window).scroll(function() {
            if ($('#modal_wrapper').is(':visible')) {
                positionModal();
            }
        });
    }

}



function init_dropdown() {
  
    if (!$('ul.dropdown').length) {
        return;
    }

    $('ul.dropdown li.dropdown_trigger').hover(function() {
        $(this).find('ul').fadeIn(1);
    },
    function() {
        $(this).find('ul').hide();
    });
}

function init_comment_toggle() 
{
    $('a#commenttoggle').click(function(e) {

        e.preventDefault();
        $('div#commentcontent').toggle('slow');

    });
}

function init_comment_form()
{

        $('form#commentform').ajaxForm({

            target:  'div#commentsubmit', 
            success: function() {
                // alert('Comment Sent!');
            }
    
        });

}

function init_tabbed_navigation() 
{

    //  tabbed content (static)
    //  TODO - AJAX content
  
    $('div.tab-anchor ul li a').click(function(e) {
  
        e.preventDefault();

        var css_active    = "on";
        var css_inactive  = "off";
        
        //  manage tab appearance

        $(this.parentNode).removeClass(css_inactive);
        $(this.parentNode).addClass(css_active);

        $(this.parentNode).siblings().each(function() {
            $(this).removeClass(css_active);
            $(this).addClass(css_inactive);
        });

        //  turn on/off content
        
        $('div#' + this.className).show();

        $('div#' + this.className).siblings().each(function() {            
            $(this).hide();
        });

    });


}


function init_row_expander() 
{
    //  expanding row
    //  TODO - AJAX content
    
    $('div.rowexpander-widget td.toggler a').click(function(e) {
    
      e.preventDefault();

      var css_active    = "on";
      var css_inactive  = "off";
      
      var obj_li        = $(this).parent().parent().parent().parent().parent().parent(); 
      var obj_content   = $(obj_li).find('> div.fullcontent > div');
      
      if (this.className == css_active)
      {
          $(this).removeClass(css_active);
          $(this).addClass(css_inactive);
          
          $(obj_li).removeClass(css_active);
          $(obj_li).addClass(css_inactive);
          
          $(obj_content).removeClass(css_active);
          $(obj_content).addClass(css_inactive);
      }
      else
      {
          $(this).removeClass(css_inactive);
          $(this).addClass(css_active);

          $(obj_li).removeClass(css_inactive);
          $(obj_li).addClass(css_active);
          
          $(obj_content).removeClass(css_inactive);
          $(obj_content).addClass(css_active);
          
          
          if (this.href != "") 
          {
              $(obj_content).load(this.href);
          }
          
      }

    
    });
}



$(document).ready(function() {
    init_modal();
    init_dropdown();
    init_comment_toggle();
    init_comment_form();
    init_tabbed_navigation();
    init_row_expander();

    //  open active sites in a new window

    $('a.twitter').click(function(){
        window.open($(this).attr('href'));
        return false; 
    });

});

