jQuery.noConflict();

jQuery(document).ready(function () {
  jQuery('.maximized').hide();
  jQuery('.bubbleWrap').hide();

  document.getElementById('frame').style.height = jQuery('body').innerHeight() + 'px';
  // Prepare bubble
  jQuery('#linkDescriptions li').hide();

  // Check for hash tag (bookmarks)
  if (window.location.hash) {
    if (window.location.hash.indexOf('--') != -1) {
      parts = window.location.hash.substr(1).split('--');
      element = jQuery('#desc-' + parts[0]);
      list = '#sl' + parts[0];
      name = parts[0];
      url = parts[1];
    }
    else {
      element = jQuery('#desc-' + window.location.hash.substr(1));
      list = '#sl-' + window.location.hash.substr(1);
      name = window.location.hash.substr(1);
      url = false;
    }

    if (url) {
      jQuery('#linkDescriptions > li:first-child').show().addClass('active');
      jQuery('#links > li:first-child').addClass('active');
      document.getElementById('frame').src = unescape(url);
    }
    else if (jQuery(list).length != 0) {
      jQuery(list).parent().parent().addClass('active');
      element.show().addClass('active');
      document.getElementById('frame').src = jQuery(list).children('a').attr('href');
    }
    else if (jQuery(element).parent().attr('id') == 'linkDescriptions') {
      element.show().addClass('active');
      document.getElementById('frame').src = jQuery('.' + name).attr('href');
      jQuery('#links a.' + name).parent('li').addClass('active');
    }
    else {
      var first = jQuery('#links > li:first-child').show().addClass('active');
      document.getElementById('frame').src = jQuery('#links > li:first-child a').attr('href');
    }
  } else {
    jQuery('#linkDescriptions > li:first-child').show().addClass('active');
    document.getElementById('frame').src = jQuery('#links li:first-child a').attr('href');
    jQuery('#links > li:first-child').addClass('active');
  }

  // Link clicked
  jQuery('#links a').click(function () {
    var theclass = jQuery(this).attr('class');
    var theID = 'desc-' + theclass;
    jQuery('#linkDescriptions .active').hide();
    jQuery('#linkDescriptions').children('li').removeClass('active');
    jQuery('#links .active').removeClass('active');
    jQuery('#' + theID).show().addClass('active');
    jQuery('#links a.' + theclass).parent('li').addClass('active');
    document.getElementById('frame').src = jQuery(this).attr('href');

    window.location.hash = theclass;
    return false;
  });

  // Toggle toolbar
  jQuery('.minimized').click(function () {
    jQuery('#toolbar').animate(
      { width: '248px', height: '100px' },
      1000
    );
    jQuery('#toolbar').children('.minimized')
      .hide().delay(1000);
      jQuery('.minimized').fadeOut(
        400,
        function () {
          jQuery('.maximized').fadeIn(200);

          // Slide out "bubble"
          jQuery('.bubbleWrap').fadeIn(300);
          minimized = false;
        }
      );
  });

  // Minimize
  jQuery('.maximized, .minimize').click(function () {
    jQuery('.bubbleWrap').fadeOut();
    jQuery('.maximized').fadeOut(
      400,
      function () {
        jQuery('#toolbar').animate(
          { height: '41px', width: '41px' },
          400,
          function () {
            jQuery('.minimized').css({marginTop: 0}).fadeIn();
          }
        );
      }
    );

    return false;
  });


  // Show submenu
  jQuery('#links > li a').mouseenter(
    function () {
      jQuery(this).parent().children('.sublinks').stop(true, true).slideDown();
    }
  );

  jQuery('#links > li').mouseleave(function () {
    jQuery('.sublinks').slideUp();
  });

  // Open
  jQuery('.minimized').click();
});

function receiveURL(e){
  if (e.origin === "http://movingimages.local" || e.origin === "http://blog.movingimagesmalmo.se" || e.origin === "http://movingimagesmalmo.se") {
    if (window.location.hash.indexOf('--') != -1) {
      parts = window.location.hash.substr(1).split('--');
      hash = parts[0];
    } else {
      hash = window.location.hash.substr(1);
    }
    window.location.hash = hash + '--' + escape(e.data);
  }
}
if (window.addEventListener) {
  window.addEventListener("message", receiveURL, false);
}
else if (window.attachEvent) {
  window.attachEvent("message", receiveURL);
}