$(document).ready(function(){
// Make all external links open in a new window
$("a[href^='http://']").attr("target","_blank");
// Allow class="target_blank" as a way to workaround
// invalid target attribute.
$("a.target_blank").click(function(){
                            // this.target = "_blank";
                            window.open(this.href);
                            return false;
      });
var chop = {
  debug: null
}
if ( $.browser.msie && $.browser.version < 8 )
   chop.menu_bug = true;
else
   chop.menu_bug = false;
$("ul.sf-menu").supersubs({ 
  minWidth:    12,   // minimum width of sub-menus in em units 
  maxWidth:    35,   // maximum width of sub-menus in em units 
  extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                     // due to slight rounding differences and font-family 
                     // call supersubs first, then superfish, so that subs are 
                     // not display:none when measuring. Call before initialising 
                     // containing tabs for same reason. 
    }).superfish({
      delay:       1000,
//      animation:   {opacity:'show',height:'show'},
      speed:       'fast',
      autoArrows:  true,
      dropShadows: true
      });

// Pure popup on mouseover
$('a.popup[href]').click(function(event){ event.preventDefault(); });
$('a.popup').each(function()
  {
    var _o = parseUri($(this).attr('href'));
    var _width = _o.queryKey['width'];
    $(this).qtip({
        content: {
            text: $('<img>', {
                src:  $(this).attr('href')
                ,alt: $(this).attr('title')
                })
            ,title: $(this).attr('title')
            ,button: true
        }
        ,position: {
            my: 'bottom right'
            ,at: 'center'
            ,target: $(this).children()
            ,adjust: {
                screen: true
            }
        }
        ,show: {
            solo: true
        }
//         ,hide: 'unfocus'
        ,hide: {
            event: false
            ,inactive: 5000     // 5 seconds
        }
        ,style: {
            classes: 'ui-tooltip-chop'
            ,tip: true
        }
    });
  });

// popup on mouseover but allow click on thumbnail to go to child
// $('a.project[href]').click(function(event){ event.preventDefault(); });
$('a.project').each(function()
  {
    var _href = $(this).attr('href');
    var _tg = $(this).parent().parent().find('td.display_none a').attr('href');
    var _cnt = 0;
    $(this).qtip({
        content: {
            text: $('<img>', {
                src:  $(this).attr('href')
                ,alt: $(this).attr('title')
                })
            ,title: $(this).attr('title')
            ,button: true
        }
        ,position: {
            my: 'bottom right'
            ,at: 'center'
            ,target: $(this).children()
            ,adjust: {
                screen: true
            }
        }
        ,show: {
            solo: true
        }
//         ,hide: 'unfocus'
        ,hide: {
            event: false
            ,inactive: 5000     // 5 seconds
        }
        ,style: {
            classes: 'ui-tooltip-chop'
            ,tip: true
        }
    })
// Set mouse click to the real target
      .click(function(event){
               location.replace(_tg);
               event.preventDefault();
      });
  });

if ( ! chop.menu_bug ) {
// tooltip for the navigation menu
$("#nav a").each(function()
  {
    var _at_pos;
    if ( $(this).parent().parent().is('ul#top-menu.sf-menu') == true )
      _at_pos = 'bottom center';
    else
      _at_pos = 'bottom right';
    $(this).qtip({
        style: {
            classes: 'ui-tooltip-chop'
            ,tip: {
                corner: true
            }
        }
        ,position: {
            at: _at_pos
            ,my: 'top left'
            ,adjust: { screen: 'flip' }
        }
   });
});
}
$("a[title],img[title]").has('img').qtip({
    position: { 
        my: 'center center'
        ,at: 'center center'
    }
    ,hide: { fixed: true }
    ,style: {
        classes: 'ui-tooltip-chop'
        ,tip: {
            corner: true
        }
    }
  });

var fb_active=false;  // Global state of fancybox popup
  $('a.modal').each(function() {
    var title = $(this).data('oldtitle') || $(this).title;
    $(this).fancybox({
      showCloseButton:  true
      ,cyclic:          true
      ,title:           title
      ,centerOnScroll:  true
      ,titlePosition:   'outside'
      ,transitionIn:    'none'
      ,transitionOut:   'none'
      ,overlayOpacity:   0.95
      ,onStart:   function() {
          fb_active=true;
      }
      ,onClosed:   function() {
          fb_active=false;
      }
      ,onComplete: function() {
          $("#fancybox-title").css({'top':'-3em', 'bottom':'auto'});
      }
  });
 });

  $("a.map_frame").fancybox({
      width: '95%'
      ,height: '95%'
      ,autoScale: false
      ,transitionIn: 'none'
      ,transitionOut: 'none'
      ,type: 'iframe'
  });

// Disable all mousewheel events
  $('body').mousewheel(function(event, delta, deltaX, deltaY) {
                         return ! fb_active;
                       }
                 );
//   var _x=0;
  $('a[href*="youtube."]').click(function(event){ event.preventDefault(); });
  $('a[href*="youtube."]').each(function() {
  // Use each method to gain access to all youtube links
      var _d,_w,_h;
      _d = $(this).metadata();
      _w = _d.width  ? _d.width  : 680;
      _h = _d.height ? _d.height : 495;
      var title = $(this).data('oldtitle') || $(this).title;
//       _x++;
//       console.log(_x + ': w(' + _w + ') h(' + _h + ')');
      $(this).fancybox({
          showCloseButton: true
          ,cyclic:         true
          ,title:          title
          ,centerOnScroll: true
          ,titlePosition:  'outside'
          ,autoScale:      false
          ,transitionIn:   'none'
          ,transitionOut:  'none'
          ,overlayOpacity: 0.95
          ,width:          _w
          ,height:         _h
          ,href:           this.href.replace(new RegExp("watch\\?v=", "i"), 'v/')
          ,type:           'swf'
          ,swf: {
              wmode:            'transparent'
              ,allowfullscreen: 'true'
              ,flashvars: 'color1=0xe1600f&color2=0xfebd01&border=1'
          }
          ,onStart:   function() {
              fb_active=true;
          }
          ,onClosed:   function() {
              fb_active=false;
          }
          ,onComplete: function() {
              $("#fancybox-title").css({'top':'-3em', 'bottom':'auto'});
          }
      });
   });

});

