
var pw,ph;
$(function(){
 var de=document.documentElement||null;
 pw=(window.innerWidth||self.innerWidth||(de&&de.clientWidth)||document.body.clientWidth)-($.browser.msie?0:17);
 ph=window.innerHeight||self.innerHeight||(de&&de.clientHeight)||document.body.clientHeight;

 $('a').focus(function(){this.blur();});

 var bopen=false,bobj=null;
 var bdhtml='<div id="bkm-d">%links%</div>';
 var blhtml='<a href="%link%" id="bkm-l-%n%">&nbsp;</a>';
 var burl=[
  'http://www.stumbleupon.com/submit?url=%url%&title=%title%&newcomment=&tagnames=',
  'http://del.icio.us/post/?url=%url%&title=%title%&notes=&tags=,',
  'http://www.furl.net/savedialog.jsp?p=1&t=%title%&u=%url%&r=&v=1&c=&topics=&description=&keywords=,',
  'http://digg.com/submit?phase=2&url=%url%&title=%title%&bodytext=&topic=',
  'http://reddit.com/submit?url=%url%&title=%title%',
  'http://www.facebook.com/share.php?src=bm&u=%url%&t=%title%&sharer_popup_message=&v=3',
  'http://technorati.com/faves/?add=%url%'
 ];
 $('p.bookmark a').click(function(){
  if(bopen) 
   bclose();
  if(bobj && bobj==this){
   bobj=null;
   return false;
  }
  bopen=true;
  bobj=this;
  $(this).addClass('on');
  tmp=$(this).attr('rel').split('|');
  var url=tmp[0],title=tmp[1],html='';

  for(var n=0;n<burl.length;n++)
  html+=blhtml.replace(/%link%/g,burl[n].replace(/%url%/g,url).replace(/%title%/g,escape(title))).replace(/%n%/g,n);
  html=bdhtml.replace(/%links%/g,html);

  $(document.body).append(html);
  var _o=$(this).offset();
  var _l=parseInt(_o.left)+1,_t=_o.top-182;
  $('#bkm-d').css({left:_l,top:_t});
 
  $(document).click(function(e){
   var _obj=e.target,_id=[],close=true;
   while(_obj.parentNode){
    _id[_id.length]=_obj.id;
    _obj=_obj.parentNode;
   }
   for(var q=0;q<_id.length;q++)
    if(_id[q].indexOf('bkm')>-1)
     close=false;
   if(close){
    bclose();
    bobj=null;
   }
  });
 });
 function bclose(){
  if($(bobj).length)
   $(bobj).removeClass('on');
  $('#bkm-d').remove();
  bopen=false;
 }
 
 gallery.init();
});

var cmap={
 is_open:false,
 gmap:false,
 map_id:'mapc',
 time:750,
 toggle:function(obj){
  $(obj).toggleClass('open');
  
  if(cmap.is_open)
   cmap.close();
  else
   cmap.open();
 },
 open:function(){
  if(cmap.is_open) return false;
  $('#'+cmap.map_id).slideDown(cmap.time,function(){
   if(GBrowserIsCompatible() && !cmap.gmap){
    cmap.gmap=true;
    var map=new GMap2(document.getElementById('gmap'));
    var ll=new GLatLng(51.465772,-0.298347);
    map.setCenter(ll,13);
    map.addOverlay(new GMarker(ll));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
   }
  });
  cmap.is_open=true;
 },
 close:function(){
  if(!cmap.is_open) return false;
  $('#'+cmap.map_id).slideUp(cmap.time);
  cmap.is_open=false;
 }
};

var gallery={
 time:450,
 load:'<img id="gallery-load" src="'+_siteRoot+'images/loader.gif" alt="Loading..." style="position:absolute;" />',
 html:'<div id="gallery-black"></div><div id="gallery-c"><img id="gallery-img" src="%src%" alt="%alt%" /><a href="#" class="l"></a><a href="#" class="r"></a></div>',
 images:[],
 image_index:-1,
 showing:false,
 init:function(){
  $('div.image a').click(function(){
   if(gallery.showing) 
    return false;
   gallery.showing=true;

   gallery.image_index=$(this).attr('rel')-1;

   var $img=$(this).parent().find('img'),src=this.href.replace(/&amp;/,'&'),alt=this.alt,height=0,width=0;
   var _top=$img.offset().top,_left=$img.offset().left,tmb_width=$img.width(),tmb_height=$img.height();
   
   $(document.body).append(gallery.load);
   $('#gallery-load').show().css({top:_top+(tmb_height-32)/2,left:_left+(tmb_width-32)/2});

   var _loader=new Image();
   _loader.onload=function(){
    $('#gallery-load').remove();

    width=this.width;
    height=this.height;

    var html=gallery.html.replace(/%src%/gi,src).replace(/%alt%/gi,alt);

    $(document.body).append(html);

    _otop=($(window).height()-688)/2;
    _top=$(window).scrollTop()+(_otop>0?_otop:20);
    _left=$(window).scrollLeft()+($(window).width()-993)/2;

    $('#gallery-c').show().css({top:_top,left:_left,width:993,height:688});
    $('#gallery-img').css({width:width,height:height});
    $('#gallery-black').css({height:ph,width:pw,opacity:0.75}).show();

    $('a.l','#gallery-c').click(function(){change(-1);return false;}).focus(function(){this.blur();});
    $('a.r','#gallery-c').click(function(){change(+1);return false;}).focus(function(){this.blur();});
    var change=function(d){
     var new_index=gallery.image_index+d;
     if(new_index<0) new_index=gallery.images.length-1;
     if(new_index>=gallery.images.length) new_index=0;

     var new_src=unescape(gallery.images[new_index]).replace(/&amp;/,'&');
     gallery.image_index=new_index;

     $('#gallery-img').animate({opacity:0},gallery.time,function(){
      var _nloader=new Image();
      _nloader.onload=function(){
       $('#gallery-img').attr('src',new_src).animate({opacity:1},gallery.time);
      }
      _nloader.src=new_src;
     });
    } 

    $(window).scroll(function(){
     _otop=($(window).height()-688)/2;
     _top=$(window).scrollTop()+(_otop>0?_otop:20);
     _left=$(window).scrollLeft()+($(window).width()-993)/2;
     $('#gallery-c').dequeue().animate({top:_top,left:_left},gallery.time);
    });
    $(window).resize(function(){
     _otop=($(window).height()-688)/2;
     _top=$(window).scrollTop()+(_otop>0?_otop:20);
     _left=$(window).scrollLeft()+($(window).width()-width)/2;
     $('#gallery-c').dequeue().animate({top:_top,left:_left},gallery.time);
    });
    $(document).click(function(e){
     var _obj=e.target,_id=[],close=true;
     while(_obj.parentNode){
      _id[_id.length]=_obj.id;
      _obj=_obj.parentNode;
     }
     for(var q=0;q<_id.length;q++)
      if(_id[q].indexOf('gallery')>-1 && _id[q]!='gallery-black')
       close=false;
     if(close)
      gallery.close();
    });
    $(document).keypress(function(e){
     var key=e.which;
     if(!key) gallery.close();
    });
   };
   _loader.src=src;
   return false;
  });
 },
 close:function(){
  $('#gallery-black').animate({opacity:0},{duration:gallery.time});
  $('#gallery-c').animate({opacity:0,top:'+=50px'},gallery.time,function(){
   $('#gallery-c,#gallery-black').remove();
   gallery.showing=false;
   $(document).click(function(){});
  });
  return false;
 }
}