//<![CDATA[

var stories = new Array();
var mousex = 0;
var mousey = 0;
var story_shown;

jQuery(document).ready(function(){
   jQuery(document).mousemove(function(e){
      mousex = e.pageX
      mousey = e.pageY;
      
      if (story_shown) {
          var is_in = 0;
          err = [];
          //if there is a story shown, 
          if (mousex >= story_shown.x_low && mousex <= story_shown.x_high) {
              err.push('img x in bounds');
              is_in += 1;
          } else {
              err.push('img x out: ' + story_shown.x_low + ' < ' + mousex + ' > ' + story_shown.x_high);
          }
          if (mousey >= story_shown.y_low && mousey <= story_shown.y_high) {
              err.push('img y in bounds: ' + story_shown.y_low + ' < ' + mousey + ' > ' + story_shown.y_high);
              is_in += 2;
          } else {
              err.push('img y out: ' + story_shown.y_low + ' < ' + mousey + ' > ' + story_shown.y_high);
          }
          if (mousex >= story_shown.story_x_low && mousex <= story_shown.story_x_high) {
              err.push('story x in bounds');
              is_in += 4;
          } else {
              err.push('story x out: ' + story_shown.story_x_low + ' < ' + mousex + ' > ' + story_shown.story_x_high);
          }
          if (mousey >= story_shown.story_y_low && mousey <= story_shown.story_y_high) {
              err.push('story y in bounds');
              is_in += 8;
          } else {
              err.push('story y out: ' + story_shown.story_y_low + ' < ' + mousey + ' > ' + story_shown.story_y_high);
          }
          if (is_in == 3 || is_in == 11 || is_in == 14 || is_in == 15 || is_in == 12 || is_in == 13) {
            //console.debug('in',is_in,err);
          } else {
              //console.debug('out',is_in,err);
              storyMouseOut();
          }
          
          
          
      
      }
   }); 
})

function storyMouseOver(id,path)
{
  var story = jQuery('#story_title_' + id);
  var img = jQuery('#thumb_' + id);
  story.show();
  img.attr('src',path);
  img.css('cursor','pointer');
  var img_offset = img.offset();
  img_offset.top += img.height() - 20 - story.height();
  img_offset.left += 20 - story.width();
  story.offset(img_offset);
  return;
  /*var img = $('thumb_' + id);
  img.src = path;
  img.style.cursor = 'pointer'
  var text = $('story_title_' + id);
  var offset = img.positionedOffset();
  text.style.top = (offset[1] + img.getHeight() - text.getHeight() + 2) + "px";
  text.style.left = offset[0] + "px";
  //console.debug(offset,text.positionedOffset());
  //Element.show('text_' + id);
  Element.show('story_title_' + id);*/
}

function storyMouseOut()
{
    var id = story_shown.id;
    var path = story_shown.grey_path;
    var img = jQuery('#thumb_' + id);
  
    img.attr('src',path);
    //Element.hide('text_' + id);
    jQuery('#story_title_' + id).hide();
    story_shown = false;
}

function showStory(id)
{
  var story = stories[id];
  var box = jQuery('#story_box');
  box.find('img').attr('src',story.thumb);
  var scrollTop = f_scrollTop();
  if (scrollTop < 195) {
      scrollTop = 195;
  }
  box.css('top', scrollTop + "px");
  jQuery('#story_title').text(story.title);
  jQuery('#story_text').text(story.body);
  jQuery('#story_jobtitle').text(story.position);
  jQuery('#story_age').text(story.age);
  jQuery('#story_industry').text(story.industry);
  jQuery('#link_text').text(story.link);
  jQuery('#link_text').attr('href',story.link_href);
  
  Effect.Appear('story_box',{duration: .6});
  jQuery('.storyTitle').hide();
  return false;
}

jQuery(function() {
    jQuery('.storyThumbLink').hover(
        function(e) {
            
            //get the id and color path for the image story
            var id = jQuery(this).find('.storyId').text();
            if (story_shown) {
                storyMouseOut();
            }
            var path = jQuery(this).find('.thumbPath').text();
            
            var story = jQuery('#story_title_' + id);
            var img = jQuery('#thumb_' + id);
            story.show();
            
            //change the img to a color img
            img.attr('src',path);
            img.css('cursor','pointer');
            
            //move the story off of the image
            var story_offset = img.offset();
            story_offset.top += img.height() - 20 - story.height();
            story_offset.left += 20 - story.width();
            story.offset(story_offset);
            
            //now mark the coordinates of the image and the story text
            var img_offset = img.offset();
            story_shown = {};
            story_shown.id = id;
            story_shown.grey_path = jQuery(this).find('.greyThumbPath').text();
            story_shown.x_low = img_offset.left;
            story_shown.x_high = story_shown.x_low + img.width() + 1;
            story_shown.y_low = img_offset.top;
            story_shown.y_high = story_shown.y_low + img.height() + 1;
            
            //now mark the coordinates of the story itself
            story_shown.story_x_low = story_offset.left;
            story_shown.story_x_high = story_shown.story_x_low + story.width() + 1;
            story_shown.story_y_low = story_offset.top;
            story_shown.story_y_high = story_shown.story_y_low + story.height() + 1;
            
            return;
        },
        function(e) {
            //console.debug(jQuery(this).find('.storyId').text(),'out');
        }
    );
})


function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

//]]>

