window.onerror=function(){return true;};

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;

 game.ani=[
  // Left start, Top start, Width start, Height start, Left end, Top end, Width end, Height end 
  [[0,0,0,ph,0,0,pw/2,ph],[pw,0,0,ph,pw/2,0,pw/2,ph],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]],
  [[0,0,0,0,0,0,pw/2,ph/2],[pw,0,0,0,pw/2,0,pw/2,ph/2],[0,ph,0,0,0,ph/2,pw/2,ph/2],[pw,ph,0,0,pw/2,ph/2,pw/2,ph/2]],
  [[0,0,0,ph/4,0,0,pw,ph/4],[pw,ph/4,0,ph/4,0,ph/4,pw,ph/4],[0,ph/2,0,ph/4,0,ph/2,pw,ph/4],[pw,ph/4*3,0,ph/4,0,ph/4*3,pw,ph/4]],
  [[pw/4,ph/4,0,0,0,0,pw/2,ph/2],[pw/4*3,ph/4,0,0,pw/2,0,pw/2,ph/2],[pw/4,ph/4*3,0,0,0,ph/2,pw/2,ph/2],[pw/4*3,ph/4*3,0,0,pw/2,ph/2,pw/2,ph/2]],
  [[0,0,pw/4,0,0,0,pw/4,ph],[pw/4,ph,pw/4,0,pw/4,0,pw/4,ph],[pw/2,0,pw/4,0,pw/2,0,pw/4,ph],[pw/4*3,ph,pw/4,0,pw/4*3,0,pw/4,ph]],
  [[0,0,0,ph/2,0,0,pw/2,ph/2],[pw/2,0,pw/2,0,pw/2,0,pw/2,ph/2],[pw,ph/2,0,ph/2,pw/2,ph/2,pw/2,ph/2],[0,ph,pw/2,0,0,ph/2,pw/2,ph/2]],
  [[0,0,pw,0,0,0,pw,ph/2],[0,ph,pw,0,0,ph/2,pw,ph/2],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]
 ];
});

var game={
 url:_siteRoot+'flash/flash-game.swf',
 time:750,showing:false,anic:-1,
 openit:function(){
  game.showing=true;
  $('#game-c').show();
  $('#game-c div').css('opacity',0.75);
  
  game.anic=Math.floor(Math.random()*game.ani.length);
  var ani=game.ani[game.anic];
  
  for(var j=0;j<4;j++){
   $('#game-c-'+j).css({left:ani[j][0],top:ani[j][1],width:ani[j][2],height:ani[j][3]})
    .animate({left:ani[j][4],top:ani[j][5],width:ani[j][6],height:ani[j][7]},{duration:game.time,queue:false},'swing');
  }
	
  swfobject.embedSWF(game.url,'game-flash-r','500','420','9.0.0');
  $('#game-flash').css({left:(pw-500)/2,top:-420}).show().animate({top:(ph-500)/2},game.time,'swing');

  $(window).scroll(function(){
   if(game.showing){
    _top=$(window).scrollTop()+($(window).height()-420)/2;
    _left=$(window).scrollLeft()+($(window).width()-500)/2;
    $('#game-flash').dequeue().animate({top:_top,left:_left},game.time,'swing');
   }
  });
  return false;
 },
 closeit:function(){
  game.showing=false;
  $('#game-flash').animate({top:-500},game.time);
  var ani=game.ani[game.anic];
  for(var j=0;j<4;j++){
   $('#game-c-'+j).css({left:ani[j][4],top:ani[j][5],width:ani[j][6],height:ani[j][7]})
    .animate({left:ani[j][0],top:ani[j][1],width:ani[j][2],height:ani[j][3]},{duration:game.time,queue:false},'swing');
  }
  window.setTimeout(function(){
   $('#game-flash-r').remove();
   $('#game-flash').append('<div id="game-flash-r"></div>').hide();
  },game.time+150);
 }
}

var enlarge={
 time:450,
 load:'<img id="enlarge-load" src="'+_siteRoot+'images/loader.gif" alt="Loading..." style="position:absolute;" />',
 html:'<div id="enlarge-black"></div><div id="enlarge-c"><img id="enlarge-img" src="%src%" alt="%alt%" /><a href="#" onclick="enlarge.close();return false;" onfocus="this.blur();">Close</a></div>',
 showing:false,
 init:function(){
  $('div.entry p a:has(img)').click(function(){
   if(enlarge.showing) 
    return false;
   enlarge.showing=true;
   var $img=$(this).find('img'),src=this.href,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(enlarge.load);
   $('#enlarge-load').show().css({top:_top+(tmb_height-32)/2,left:_left+(tmb_width-32)/2});

   var _loader=new Image();
   _loader.onload=function(){
    $('#enlarge-load').remove();

    width=this.width;
    height=this.height;

    var html=enlarge.html.replace(/%src%/gi,src).replace(/%alt%/gi,alt);

    $(document.body).append(html);
    $('#enlarge-c').show().css({top:_top,left:_left,padding:'2px'});
    $('#enlarge-img').css({width:tmb_width,height:tmb_height});
		
    _otop=($(window).height()-height)/2;
    _top=$(window).scrollTop()+(_otop>0?_otop:20);
    _left=$(window).scrollLeft()+($(window).width()-width)/2;

    $('#enlarge-c').animate({top:_top,left:_left,width:width,height:height,padding:'10px'},{duration:enlarge.time,queue:false});
    $('#enlarge-img').animate({width:width,height:height},enlarge.time);
		
    $('#enlarge-black').css({height:ph,width:pw,opacity:0}).show().animate({opacity:0.75},{duration:enlarge.time});

    $(window).scroll(function(){
     _otop=($(window).height()-height)/2;
     _top=$(window).scrollTop()+(_otop>0?_otop:20);
     _left=$(window).scrollLeft()+($(window).width()-width)/2;
     $('#enlarge-c').dequeue().animate({top:_top,left:_left},enlarge.time);
    });
    $(window).resize(function(){
     _otop=($(window).height()-height)/2;
     _top=$(window).scrollTop()+(_otop>0?_otop:20);
     _left=$(window).scrollLeft()+($(window).width()-width)/2;
     $('#enlarge-c').dequeue().animate({top:_top,left:_left},enlarge.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('enlarge')>-1 && _id[q]!='enlarge-black')
       close=false;
     if(close)
      enlarge.close();
    });
    $(document).keypress(function(e){
     var key=e.which;
     if(!key) enlarge.close();
    });
   };
   _loader.src=src;
   return false;
  });
 },
 close:function(){
  $('#enlarge-black').animate({opacity:0},{duration:enlarge.time});
  $('#enlarge-c').animate({opacity:0,top:'+=50px'},enlarge.time,function(){
   $('#enlarge-c,#enlarge-black').remove();
   enlarge.showing=false;
   $(document).click(function(){});
  });
  return false;
 }
}


function set_style(obj){
 if(!document.getElementById) return false;

 $(strPNG).iunfixpng(true);
 
 var sheet=document.createElement('link');
 sheet.setAttribute('rel','stylesheet');
 sheet.setAttribute('type','text/css');
 sheet.setAttribute('media','all');
 sheet.setAttribute('href',_siteRoot+obj.id+'/colour.css');
 
 if($.browser.safari)
  $('head').append(sheet);
 else
  document.body.appendChild(sheet);

 var img=new Image();
 img.src=obj.href;

 $('#colours a').removeClass('on');
 $(obj).addClass('on');
 
 $(strPNG).ifixpng();
 
 /*var hex=['','#d00000','#ffcc00','#63e900','#00c8f3','#e9007e'];
 $('embed').each(function(){
  this.src=this.src.replace(/color=(.*?)&/ig,'color='+hex[obj.rel]+'&');
 });*/
}

function scroll(obj,offset){
 if(location.pathname.replace(/^\//,'')==obj.pathname.replace(/^\//,'') && location.hostname==obj.hostname){
  var $target=$(obj.hash);
  $target=$target.length&&$target||$('[name='+obj.hash.slice(1)+']');
  if($target.length){
   var targetOffset=$target.offset().top+offset;
   $('html,body').stop().animate({scrollTop:targetOffset},500,'swing');
   return false;
  }
 }
}

var welcome={
 time:1000,
 show:2,
 auto:1,
 showing:false,
 
 init:function(){
  if(welcome.auto){
   welcome.show=Math.round(ji_data.length*0.05);
   if(welcome.show<10) welcome.show=10;
   if(welcome.show>25) welcome.show=25;
  }
  welcome.show=10;
   
  ji_data.splice(0,1);

  $(document.body).append('<a href="#" id="wlcm_open"></a>');
  var al=$('p.welcome').offset().left+$('p.welcome').width()+3;
  var $pw=$('p.welcome');
  var $so=$pw.find('span:not(.ji)');
  var ow=$so.width()+18,oh=$so.height(),ol=$so.offset().left-5-($.browser.msie?2:0),nh=welcome.show*17;

  $('#wlcm_open').css('left',al).focus(function(){this.blur();}).click(function(){
   var $t=$(this);
	 
   if(welcome.showing){
    welcome.showing=false;

    $so.hide().fadeIn();
    $t.hide().removeClass('up');
    $ws=$('#wlcm_scroll').animate({left:ol+ow-8},welcome.time,'swing')
     .find('div').css({height:0,opacity:0},welcome.time,'swing');
    $('#wlcm_exp').animate({height:welcome.oh,width:welcome.ow},welcome.time,'swing',function(){
     $t.css({left:al,top:0}).fadeIn();
     $('#wlcm_exp,#wlcm_scroll').remove();
    }).find('span').fadeOut();
    $pw.css({paddingRight:16});
   }else{
    welcome.showing=true;

    var nw=ow;
    welcome.ow=ow;
    welcome.oh=oh;

    $pw.before('<div id="wlcm_exp"></div>');
    $we=$('#wlcm_exp');

    $.each(ji_data,function(i){
     var ji=this.toString();
     $we.append(ji);
     $cu=$we.find('span:last');
     $cu.css({position:'absolute',height:17,left:5,top:17*(i+1)});
     nw=Math.max(nw,$cu.width()+24);
     if(i<welcome.show) welcome.ticker_init($cu.get(0),Math.round(i*(welcome.time/welcome.show)));
    });
    $t.fadeOut().addClass('up');
    $we.css({height:oh,width:ow,left:ol}).animate({height:nh,width:nw},welcome.time,function(){
     $t.css({left:ol+nw-12,top:nh-17}).fadeIn();
     $pw.css({paddingRight:0});
    });
    $pw.after('<div id="wlcm_scroll"><div class="ui-slider-handle"></div></div>');
    $ws=$('#wlcm_scroll');
    $ws.css({left:ol+ow-8,height:nh-17-8}).animate({left:ol+nw-8},welcome.time);//Math.round(welcome.show/ji_data.length*nh)
    $ws.find('div').css({height:0,opacity:0}).show().animate({height:15,opacity:0.5},welcome.time)
     .hover(function(){$(this).animate({opacity:1},150);},function(){$(this).animate({opacity:0.5},150);});
		
    var snum=ji_data.length-welcome.show,onum=0;
    $ws.slider({slide:function(e,ui){
     nnum=Math.round(ui.value/100*snum);
     if(nnum!=onum){
      if(nnum>0) $so.hide();
      else $so.show();

      $we.find('span').each(function(i){
       $(this).css({top:(i-nnum)*17+2});
      });
      onum=nnum;
     }
    }});
   }
   return false;
  });
 },
 ticker_init:function(obj,start){
  var $$=$(obj);
  var text=$$.html();
  $$.html('');
  window.setTimeout(function(){welcome.ticker_run(obj,text,0);},start);
 },
 ticker_run:function(obj,text,n){
  var $$=$(obj);
  var ctext=$$.html();
  if(ctext!=text){
   $$.html(text.substring(0,n));
   window.setTimeout(function(){welcome.ticker_run(obj,text,n+1);},5);
  }
 }
}

// Preloader
var colours=['red','yellow','green','blue','pink'];
var files=['comment.png','comments-bg.gif','comment-submit.png','date-bg.png','goingup.png','id-logo.gif',
           'nav.png','perma-bg.gif','right-ul-bg.png','rss-news-feed.png','search-submit.png','close.png','colour.css'];
var preload=[],p=0,total=colours.length*files.length;

window.onload=function(){
 for(var i=0;i<colours.length;i++){
  preload[colours[i]]=[];
  for(var j=0;j<files.length;j++){
   preload[colours[i]][j]=new Image();
   preload[colours[i]][j].src=_siteRoot+colours[i]+'/'+files[j];
  }
 }
 for(var k=1;k<10;k++){
  window.setTimeout(
   'preload['+k+']=new Image();'+
   'preload['+k+'].src="'+_siteRoot+'images/bg/bg'+k+'.jpg";',k*5000); 
 }
}

$(document).ready(function(){
 $('a').focus(function(){this.blur();});
 
 $('input.text,textarea.text').focus(function(){$(this).addClass('textfocus');}).blur(function(){$(this).removeClass('textfocus');});

 var _c=$('#container').offset();
 $('#scrolltop').css('left',(_c.left+1010)+'px').fadeIn('slow');
 $(window).resize(function(){
  var _c=$('#container').offset();
  $('#scrolltop').css('left',(_c.left+1010)+'px');
 });

 if($(window).width()<=1030){
  $('#scrolltop,#nowhere').hide();
 }else{
  if($(window).scrollTop()!=0){
   $('#scrolltop').css('opacity',1).stop().animate({top:$(window).scrollTop()+301},750,'swing');}

  $(window).scroll(function(){
   $('#scrolltop').css('opacity',1).stop().animate({top:$(window).scrollTop()+301},750,'swing');});
 }
 	
 enlarge.init();
 
 if($.browser.safari){
  $('p.welcome').css({fontSize:'10px'});
 }
 
 welcome.init();
});

/*
 * jQuery ifixpng plugin
 * (previously known as pngfix)
 * Version 2.1  (23/04/2008)
 * @requires jQuery v1.1.3 or above
 *
 * Examples at: http://jquery.khurshid.com
 * Copyright (c) 2007 Kush M.
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
(function($){
 $.ifixpng=function(customPixel){
  $.ifixpng.pixel=customPixel;
 };
 $.ifixpng.getPixel=function(){
  return $.ifixpng.pixel || _siteRoot+'images/pixel.gif';
 };
 var hack={
  ltie7:$.browser.msie && $.browser.version<7,
  filter:function(src){
   return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
  }
 };
 $.fn.ifixpng=hack.ltie7?function(){
  return this.each(function(){
   var $$=$(this);
   // in case rewriting urls
   var base=$('base').attr('href');
   if(base){
    // remove anything after the last '/'
    base=base.replace(/\/[^\/]+$/,'/');
   }
   if(($$.is('img') || $$.is('input')) && !$$.is('.button')){ // hack image tags present in dom
    if($$.attr('src')){
     if($$.attr('src').match(/.*\.png([?].*)?$/i)){ // make sure it is png image
      // use source tag value if set 
      var source=(base && $$.attr('src').search(/^(\/|http:)/i))?base+$$.attr('src'):$$.attr('src');
      // apply filter
      $$.css({filter:hack.filter(source), width:$$.width(), height:$$.height()})
        .attr({src:$.ifixpng.getPixel()})
        .positionFix();
     }
    }
   }else{ // hack png css properties present inside css
    var image=$$.css('backgroundImage');
    if(image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)){
     image=RegExp.$1;
     image=(base && image.substring(0,1)!='/')?base+image:image;
     $$.css({backgroundImage:'none',filter:hack.filter(image)})
       .children().children().positionFix();
    }
   }
  });
 }:function(){return this;};
 $.fn.iunfixpng=hack.ltie7?function(destroy){
  return this.each(function(){
   var $$=$(this);
   var src=$$.css('filter');
   if(src.match(/src=["']?(.*\.png([?].*)?)["']?/i)){
    src=RegExp.$1;
    if(($$.is('img') || $$.is('input')) && !$$.is('.button')){
     $$.attr({src:src}).css({filter:''});
    }else{
     $$.css({filter:'',backgroundImage:''}).clone().insertAfter(this).remove();
    }
   }
  });
 }:function(){return this;};
 $.fn.positionFix=function(){
  return this.each(function(){
   var $$=$(this);
   var position=$$.css('position');
   if(position!='absolute' && position!='relative'){
    $$.css({position:'relative'});
   }
  });
 };
})(jQuery);
var strPNG='div#container div#nav ul li a,div#container div#column-right form .button,div#container div.blog p.date,'+
'div#container div.blog div.comment,div#container div#column-right ul li a,div#container div#column-right a.rssnews,'+
'div#container div.blog div p.perma,div#enlarge-c a,div#game-flash a,div#scrolltop a,.button';
$(function(){$(strPNG).ifixpng();});

// sIFR -->
var helvetica={src:_siteRoot+'flash/helvetica.swf'};
var trebuchet={src:_siteRoot+'flash/trebuchet.swf'};
sIFR.activate(helvetica,trebuchet);

sIFR.replace(trebuchet,{
 selector:'div.blog h2,div.page h2',
 css:['.sIFR-root{color:#ffffff;font-weight:normal;}',
      'a{color:#ffffff;text-decoration:none;}',
      'a:hover{color:#ffffff;text-decoration:underline;}'],
 fitExactly:true,
 fixHeight:-5,
 wmode:'transparent',
 ratios:[10,1.32,12,1.26,16,1.27,19,1.23,25,1.22,31,1.21,33,1.2,37,1.19,38,1.2,51,1.19,55,1.18,56,1.19,90,1.18,92,1.17,94,1.18,98,1.17,99,1.18,1.17]
});
sIFR.replace(helvetica,{
 selector:'div#container div#column-right h3,div#container div.blog h3',
 css:['.sIFR-root{color:#ffffff;text-transform:uppercase;font-weight:normal;'],
 tuneHeight:-5,
 wmode:'transparent',
 ratios:[7,1.32,8,1.31,12,1.27,15,1.21,16,1.22,22,1.2,23,1.17,28,1.19,30,1.17,31,1.18,33,1.17,38,1.16,39,1.17,54,1.16,81,1.15,84,1.14,86,1.15,87,1.14,88,1.15,91,1.14,92,1.15,1.14] 
});