$(document).ready(function() {
  setHeight();

  $(window).resize(function() {
    setHeight();
  });

  replacePinLeftSideBar();
  $('#jsddm > li').bind('mouseover', jsddm_open);
  $('#jsddm > li').bind('mouseout',  jsddm_timer);
});

function setHeight() {
  height = $(window).height() - 66;
  width = $(window).width();
  current_height = $("#body_content_right > #right_inner").height();
  $("#body_content_right > #right_inner").height(height);
  //$("html").height($(document).height() / 2);
  //window.resizeTo(width, height)
  //alert($(document).height());
  $('html').height($(window).height());
  return;
}

function replacePinLeftSideBar() {

  e = $("#body_content_right > .right_inner");
  e.find("p").each( function () {
    content = $(this);
    link = $(this).find("a");
    content.find("a").replaceWith('');
    //$(this).replaceWith(link);
    try {
      new_link = link.clone().wrap('<div></div>').parent().html();
      if (new_link == null) {
        new_link = '';
      }
      $(this).replaceWith('<table class="info_container"><tr class="info_top_row"><td class="info_top_left"><img src="/images/grey_arrow.png" height="26" width="27" /></td><td class="info_top_right">'+content.html()+'</td></tr><tr class="info_bottom_row"><td class="info_bottom_left"></td><td class="info_bottom_right">'+new_link+'</td></tr></table>');
      //$(this).replaceWith('<div class="info_container"><div class="info_top_row"><div class="info_top_left"><img src="/images/grey_arrow.png" height="26" width="27" /></div><div class="info_top_right">'+content.html()+'</div></div><div class="info_bottom_row"><div class="info_bottom_left"></div><div class="info_bottom_right">'+link[0].outerHTML+'</div></div></div>');
      //$(this).replaceWith('<div class="info_container"><div class="info_top_row"><div class="info_left_top"><img src="/images/grey_arrow.png" height="26" width="27" /></div><div class="info_left_bottom"></div></div><div class="info_right"><div class="info_right_top">'+content.html()+'</div><div class="info_right_bottom">'+link[0].outerHTML+'</div></div></div>');
    }
    catch (err) {
      $(this).replaceWith('<table class="info_container"><tr class="info_top_row"><td class="info_top_left"><img src="/images/grey_arrow.png" height="26" width="27" /></td><td class="info_top_right">'+content.html()+'</td></tr><tr class="info_bottom_row"><td class="info_bottom_left"></td><td class="info_bottom_right"></td></tr></table>');
      //$(this).replaceWith('<div class="info_container"><div class="info_top_row"><div class="info_top_left"><img src="/images/grey_arrow.png" height="26" width="27" /></div><div class="info_top_right">'+content.html()+'</div></div><div class="info_bottom_row"><div class="info_bottom_left"></div><div class="info_bottom_right"></div></div></div>');
      //$(this).replaceWith('<div class="info_container"><div class="info_left"><div class="info_left_top"><img src="/images/grey_arrow.png" height="26" width="27" /></div><div class="info_left_bottom"></div></div><div class="info_right"><div class="info_right_top">'+content.html()+'</div><div class="info_right_bottom"></div></div></div>');
    }
    //alert(link[0].outerHTML);
  });
}

var timeout    = 100;
var closetimer = 0;
var ddmenuitem = 0;
var dom_id     = "";

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').css('visibility', 'visible');
    url = "/images/"+$(this).attr('id')+"_hover.png";
    dom_id = $(this).attr('id');
    $(this).css('background-image','url('+url+')');
}

function jsddm_close() {
    if(ddmenuitem) {
        ddmenuitem.css('visibility', 'hidden');
        url = "/images/"+dom_id+".png";
        $('#'+dom_id+'').css('background-image','url('+url+')');
    }
}

function jsddm_timer() {
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
    if(closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = jsddm_close;
