$().ready(function() {  

  // *** insert id for the div where the tooltip should appear below, for var tooltip_div_id
  // currently set to look for "a" tags in the specified div, and add a tooltip
  // tooltip text is the text of the first "h1" tag on the page that is referenced by the "a" tag
  // jquery/javascript AJAX, "same-domain" restriction for pages referenced
  // next four lines of code necessary for the tooltip widget 
  var tooltip_div_id = 'tooltip';
  
  // here's where it's set to look for "a" tags in the specified div
  var tooltip_div_id_plus = '#'+tooltip_div_id+' a';
  
  // here's where the tooltip widget receives the id, and the widget is called
  TT.tooltip_div = tooltip_div_id_plus;
  TT.setTips();
  
});

var TT = {
  
  setTips: function(){
    $(TT.tooltip_div).hover(function(e){
    // Hover over code
        var _thisLink = $(this).attr('href');
      $('<div class="tooltip"></div>')
        .appendTo('body')
       .css('top', (e.pageY +20) + 'px')
        //.css('left', (e.pageX - 40) + 'px')
		.css('left', 300)
      .css('font-size', '1em')
      .css('width', '600px')
      .css('height', '700px')
      .css('color', '#006')
        .css('position', 'absolute')
        .css('border', '1px solid #333')
        .css('background-color', '#fffff0')
		.css('display', 'block')
       .css('padding', '2px 12px')
	   .css('margin-top', '0px')
      .css('border-radius', '8px')
      .css('-moz-border-radius', '8px')
        .css('-webkit-border-radius', '8px')
        .css('-opera-border-radius', '8px')
        .css('-khtml-border-radius', '8px')
		.css('overflow-y', 'hidden') // hide scrollbars! */
		.css('overflow-x', 'hidden') // hide scrollbars! */
        // .load(_thisLink + ' h2')
        .fadeIn('slow') 
		.click(function() { 
			window.open(_thisLink);
		});
      doAjax(_thisLink);
    }, function() {
		var timeFlys = setTimeout(function () { $('.tooltip').fadeOut('slow').remove(); }, 300);
		$('.tooltip').hover(function(){
							// do nothing while cursor hovers over the paragraph with id="tooltip"	
							clearTimeout(timeFlys);
					}, function() {		
			    			// Hover out code- remove paragraph with id="tooltip" when not hovering over paragraph or link
		     			   $('.tooltip').fadeOut('slow').remove();
					})
    }) 
  
     function doAjax(passUrl){
		  
		$.ajax({
  				url: passUrl,	
  				success: function(data) {
            			var data = filter_content(data);
           			    $('.tooltip').html(data);
  				}
		});
		
  }
  
  function get_image_only(data){
	  var new_data = data;
	  var start_image_tag = new_data.indexOf('<img');
	  var stop_image_tag = new_data.indexOf('/>', start_image_tag);
	  new_data = new_data.substring(start_image_tag,(stop_image_tag+2));
	  new_data = "<div style='width:500px;margin-left:auto;margin-right:auto;'>"+new_data+"</div>";
	  return new_data; 
  }
  
  function filter_content(data){
    data = data.replace(/<?\/body[^>]*>/g,'');
    data = data.replace(/[\r|\n]+/g,' ');
    data = data.replace(/<--[\S\s]*?-->/g,'');
    data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
    data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
    data = data.replace(/<script.*\/>/,'');
	// data.substring below controls amount of content (number of characters) shown in tooltip
    data = data.substring(0,1500);
	data = get_image_only(data);
    return data;
  }
  
    function lose_header(data){
	  var new_data = data;
	  var start_image_tag = new_data.indexOf('<div class="like_table">');
	  var stop_image_tag = new_data.indexOf('</div>', start_image_tag);
	  new_data = new_data.substring(start_image_tag,(stop_image_tag+2));
	  new_data = "<div style='width:600px;margin-left:auto;margin-right:auto;'>"+new_data+"</div>";
	  return new_data; 
  }
 
  }
}

