
/* Cufon */
Cufon.replace('#nav li a,h2,h3,.tabs a',{
 fontFamily:'helvetica'
 //,color:'-linear-gradient(#bbb,#333)'
});
/**/

$(function(){


 $('a[href$=top]').click(function(){
  scroll(this,0);
  return false;
 });
 
 $('#header-images *').round({corners:[['top','left'],['top','right']]});
 $('#header-images>*').hide().first().show();
 $('#header-controls a').click(function(){
  if($(this).hasClass('on'))
   return false;
 
  $('#header-images>*:visible').fadeOut();
  $($(this).attr('rel'),'#header-images').fadeIn();
  
  $('#header-controls a.on').removeClass('on').find('img').css('opacity',0.4);
  $(this).addClass('on').find('img').css('opacity',1);
  return false;
 }).first().addClass('on').end().not(':first').find('img').css('opacity',0.4);
 
 var $img;
 $('.showcase-images').wrapInner('<div class="showcase-runner"></div>').find('img').each(function(n){
  $img=$(this);
  
  $('.showcase-controls').css({marginLeft:-n*10-31}).find('a').last().before($('<a />',{
   href:'#',
   'class':'dimple'+(!n?' on':''),
   click:function(){
    $('.showcase-runner').animate({left:-n*$img.width()},500);
    $(this).siblings().removeClass('on').end().addClass('on');
    return false;
   }
  }));
 });
 
 $('.showcase-controls a:not(.dimple)').click(function(){
  var dir=$(this).is('.showcase-nextsmall')?1:-1,$d=$(this).siblings('.dimple'),n=$d.filter('.on').index()-1+dir;

  if(n<0) n=$d.length-1;
  if(n>$d.length-1) n=0;
  
  $('.showcase-runner').animate({left:-n*$img.width()},500);
  $d.removeClass('on').eq(n).addClass('on');
  return false;
 });

 $('.home-block').each(function(){
  $(this)
   .css({cursor:'pointer'})
   .hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');})
   .click(function(){location.href=$(this).find('a').first().attr('href');});
 });

 
 $('#share-open').click(function(){
  var o=$(this).offset();
  $('#share').css({left:o.left,top:o.top+27}).slideToggle();
  $(this).toggleClass('open');
  return false;
 });
 $('#toolbox-open').click(function(){
  var o=$(this).offset();
  $('#toolbox').css({left:o.left-$('#toolbox').width()+41,top:o.top+30}).slideToggle();
  return false;
 });
 
 $('.tab').hide();
 $('.tabs').each(function(){
  var $a=$('a',this);
  
  $a.click(function(){
   $a.each(function(){$($(this).attr('href')).hide();});
   $($(this).attr('href')).show();
   
   $a.removeClass('on');
   $(this).addClass('on');
   return false;
  });
  
  $a.first().addClass('on');
  $($a.first().attr('href')).show();
 });
 
 $('p.social a')
  .css({opacity:0.5})
  .hover(function(){$(this).css({opacity:1});},function(){$(this).css({opacity:0.5});});
 
 $('#map-open').click(function(){cmap.open();return false;});
 $('#map-close').click(function(){cmap.close();return false;});
 $('form.contact').length && cmap.open();
 
 $('[type=text],textarea','#add-comment').each(function(){
  var title=$(this).attr('title');
  if($(this).val()=='' && title!=''){
   $(this)
    .focus(function(){if($(this).val()==title) $(this).val('');})
    .blur(function(){if($(this).val()=='') $(this).val(title);})
    .val(title);
  }
 });
 
 $('.subnav a').click(function(){
  if(this.hash=='#all')
   $('.'+$(this).attr('rel')).show();
  else
   $('.'+$(this).attr('rel')).hide().filter('.'+this.hash.split('#')[1]).show();

  $(this).siblings('a').removeClass('selected');
  $(this).addClass('selected');
  return false;
 });
 
 $('#toolbox a').click(function(){
  var newbg=$('img',this).attr('src').split('bg/bg')[1].split('-thumb')[0];
  $(document.body).css('backgroundImage','url('+_theme_root+'images/bg/bg'+newbg+'.jpg)');
 
  $(this).siblings('a').removeClass('on');
  $(this).addClass('on');

  (new Image()).src=$(this).attr('href');
  return false;
 });

 $('div.comment')
  .hover(function(){$('span',this).stop().dequeue().animate({opacity:1},350);},
         function(){$('span',this).animate({opacity:0},350);})
  .find('p.author').each(function(){$('span a',this).html()!='' && $('span',this).html(' &rarr; '+$('span',this).html()).css({opacity:0});});
 
 $('form').ketchup();
});


var cmap={
 is_open:false,
 gmap:false,
 open:function(){
  if(cmap.is_open) return false;
  $('#map').slideDown(function(){
   if(GBrowserIsCompatible() && !cmap.gmap){
    cmap.gmap=true;
    var map=new GMap2(document.getElementById('google-map'));
    var ll=new GLatLng(51.51082,-0.2241);
    GEvent.addListener(map,'load',function(){$(window).trigger('resize');});
    map.setCenter(ll,13);
    map.addOverlay(new GMarker(ll));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    cmap.gdir=new GDirections(map,document.getElementById('google-dir'));
    GEvent.addListener(cmap.gdir,'error',cmap.handleErrors);
   }
  });
  cmap.is_open=true;
 },
 close:function(){
  if(!cmap.is_open) return false;
  $('#map').slideUp(350,function(){$(window).trigger('resize');});
  cmap.is_open=false;
 },
 directions:function(to){
  cmap.gdir.load('from: '+to+' to: W12 7SB, UK',{'locale':'en_UK'});
 },
 handleErrors:function(){
  if(cmap.gdir.getStatus().code==G_GEO_UNKNOWN_ADDRESS)
   alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + cmap.gdir.getStatus().code);
  else if(cmap.gdir.getStatus().code==G_GEO_SERVER_ERROR)
   alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + cmap.gdir.getStatus().code);
  else if (cmap.gdir.getStatus().code==G_GEO_MISSING_QUERY)
   alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + cmap.gdir.getStatus().code);
  else if(cmap.gdir.getStatus().code==G_GEO_BAD_KEY)
   alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + cmap.gdir.getStatus().code);
  else if(cmap.gdir.getStatus().code==G_GEO_BAD_REQUEST)
   alert("A directions request could not be successfully parsed.\n Error code: " + cmap.gdir.getStatus().code);
  else alert("An unknown error occurred.");
 }
};

(function($){
 $.fn.round=function(o){
  var o=$.extend({
   corners:[['top','left'],['top','right'],['bottom','right'],['bottom','left']],
   radius:5
  },o);
  
  o.radius=parseInt(o.radius,10)||5;
  
  return this.each(function(){ 
   var obj=this;
 
   $.each(o.corners,function(i,c){
    $(obj).css('border-'+c[0]+'-'+c[1]+'-radius',o.radius+'px');
    $(obj).css('-moz-border-radius-'+c[0]+c[1],o.radius+'px');
    $(obj).css('-webkit-border-'+c[0]+'-'+c[1]+'-radius',o.radius+'px');
   });
  });
 }
})(jQuery);

 
function scroll(obj,offset){
 if(obj.tagName!='A' || location.pathname.replace(/^\//,'')==obj.pathname.replace(/^\//,'') && location.hostname==obj.hostname){
  if(($t=(obj.tagName!='A'&&$(obj).length&&$(obj))||($(obj.hash).length&&$(obj.hash))||($('[name='+obj.hash.slice(1)+']').length&&$('[name='+obj.hash.slice(1)+']'))||null)){
   $('html,body').stop().animate({scrollTop:$t.offset().top+(offset||0)},500,'swing');
   return false;
  }
 }
};
function rand(l,u){return Math.floor((u-l+1)*Math.random())+l;}
String.prototype.shorten=function(n){l=this.length;return this.substring(0,n||l)+((n||l)<l?'&hellip;':'');};
String.prototype.toHex=function(){var str='';for(var i=0,l=this.length;i<l;i++)str+='\\x'+this.charCodeAt(i).toString(16);return str;};
(function(jQuery){jQuery.each(['backgroundColor','borderBottomColor','borderLeftColor','borderRightColor','borderTopColor','color','outlineColor'],function(i,attr){jQuery.fx.step[attr]=function(fx){if(fx.state==0){fx.start=getColor(fx.elem,attr);fx.end=getRGB(fx.end);}fx.elem.style[attr]="rgb("+[Math.max(Math.min(parseInt((fx.pos*(fx.end[0]-fx.start[0]))+fx.start[0]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[1]-fx.start[1]))+fx.start[1]),255),0),Math.max(Math.min(parseInt((fx.pos*(fx.end[2]-fx.start[2]))+fx.start[2]),255),0)].join(",")+")";}});function getRGB(color){var result;if(color&&color.constructor==Array&&color.length==3){return color;}if(result=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)){return [parseInt(result[1]),parseInt(result[2]),parseInt(result[3])];}if(result=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)){return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];}if(result=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)){return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];}if(result=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)){return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];}return null;};function getColor(elem,attr){var color;do{color=jQuery.curCSS(elem,attr);if(color!=''&&color!='transparent'||jQuery.nodeName(elem,"body")){break;}attr="backgroundColor";}while(elem=elem.parentNode);return getRGB(color);};})(jQuery);
