    var moveTo;
    var animTo;
    var moving=true;

 (function ($) {
     if (!$) return;
     $.fn.extend({
         flitter: function(options) {
                 this.each(function() {
                         this.style.position='absolute';
                         this.style.height=options.height+'px';
                         this.style.width=options.width+'px';
                         this.style.backgroundImage = "url("+options.url+")";
                         this.style.zIndex = 1000;
                         var item=this;
			 if(options.moveDelay) {
				moveItem(item);
			 }
                         animTo=setTimeout(function(){animItem(item)},options.animDelay);
    
			 $(window).resize(function() {
  				$(item).css('left',($('#col_1').position().left)+'px');
    			 });
 
                         function animItem(item) {
				 if (!moving) return;
				 if (options.seq) {
					 var idx=Math.round(Math.random()*options.seq.length);
					 var elapsed=0;
					 var localTimer;
					 for (var frame in options.seq[idx]) {
						elapsed+=options.seq[idx][frame].t;
						localTimer=setTimeout("$('#"+item.id+"').css('background-position','"+options.seq[idx][frame].p+"px');",elapsed);
 					 } 
					

				 } else {
					 var idx=Math.round(Math.random()*options.img.length);

					 item.style.backgroundPosition=options.img[idx]+'px';

				}
					 animTo=setTimeout(function(){animItem(item)},Math.round(Math.random()*options.animDelay));

                         }
 
                         function moveItem(item,xPos,yPos) {
				 if (!moving) return;
				 //$(item).fadeIn();
				
                                 var idx=Math.round(Math.random()*(options.pos.length-1));
				 if (!xPos) xPos=options.pos[idx][0];
				 if (!yPos) yPos=options.pos[idx][1]; 
				 
				 var bez={start:
						{x:$(item).position().left,y:$(item).position().top,angle:-40},
					  end:
						{x:$('div#col_1').position().left+xPos,y:yPos,angle:30}}

				 if (bez.start.x>bez.end.x)  item.style.backgroundPosition=options.img[0]+'px';
				 if (bez.start.x<bez.end.x)  item.style.backgroundPosition=options.img[9]+'px';
				 
				 $(item).animate({path:new $.path.bezier(bez)},150);
				 delay=Math.random()*options.moveDelay; 
                                 moveTo=setTimeout(function(){moveItem(item)},Math.round(delay));
                         }
 
 
                 });
                 return this;
         }
     });
 })(jQuery);


//$('body').css('cursor','none');

    $(document).ready(function(){

   

    var l={ //left
			d:{o:0,i:-200},    //down: out/in
			u:{o:-400,i:-300}}; //up: out/in
    var r={ //right
			d:{o:-600,i:-1000}, //down: out/in
			u:{o:-800,i:-900}};  //up: out/in

    var sequences=[	[{t:100,p:l.d.i},{t:100,p:l.d.o},{t:100,p:l.d.i},{t:100,p:l.d.i},{t:100,p:l.d.o},{t:100,p:l.d.i}], //left tail flitter with head down
			[{t:100,p:r.d.i},{t:100,p:r.d.o},{t:100,p:r.d.i},{t:100,p:r.d.o},{t:100,p:r.d.i},{t:100,p:r.d.o},{t:100,p:r.d.i},{t:100,p:r.d.o}], //right tail flitter with head down
			[{t:100,p:l.u.i},{t:100,p:l.u.o},{t:100,p:l.u.i},{t:100,p:l.u.i},{t:100,p:l.u.o},{t:100,p:l.u.i}], //left tail flitter with head up
			[{t:100,p:r.u.i},{t:100,p:r.u.o},{t:100,p:r.u.i},{t:100,p:r.u.o},{t:100,p:r.u.i},{t:100,p:r.u.o},{t:100,p:r.u.i},{t:100,p:r.u.o}], //right tail flitter with head up
			[{t:200,p:l.u.o},{t:200,p:r.u.o},{t:200,p:l.u.o}], //left right left with tail out and head up
			[{t:200,p:l.d.o},{t:200,p:r.d.o},{t:200,p:l.d.o}] //left right left with tail out and head down
    		[{t:200,p:r.u.i},{t:200,p:l.u.i},{t:200,p:r.u.i}], //right left right with tail in and head up
    		[{t:200,p:r.d.i},{t:200,p:l.d.i},{t:200,p:r.d.i}] //right left right with tail in and head down
		  ];

    var fantail={url:"/_r/img/Fantail-2-1.png",
                        img:[0,-100,-200, -300, -400, -500, -600, -700 , -800, -900, -1000, -1100, -1200, -1300, -1400, -1500],
			seq: sequences,
                        pos:[[+140,53],[+10,53]], //,[+10,403]],
                        width:100,
                        height:100,
                        moveDelay:7000,
                        animDelay:3000};
    var star={url:"/_r/img/star.png",
		img:[0,-10,-20,-30,-40],
		seq:[[{t:100,p:-10},{t:100,p:-00},{t:100,p:-20},{t:100,p:-30}]],
		width:10,
		height:10,
		animDelay:2000};
    $('#bird').flitter(fantail);

    $('#star1').flitter(star);
    $('#star2').flitter(star);
    $('#star3').flitter(star);
    $('#star4').flitter(star);
    $('#star5').flitter(star);
    $('#star6').flitter(star);
    $('#star7').flitter(star);
    $('#star8').flitter(star);
    $('#star9').flitter(star);
    $('#star10').flitter(star);


//    var vin=setInterval(lights,500);
    var lightImg =['_r/img/bg_vines_lit.png','_r/img/bg_vines_dim.png','_r/img/bg_vines_mid.png'];
    var lightIdx=0;

    function lights() {
        if (++lightIdx>lightImg.length-1) lightIdx=0;	
	$('#vines').css('background-image','url('+lightImg[lightIdx]+')');
    }

    function killbird() {
	clearTimeout(moveTo);
	clearTimeout(animTo);
	$('#crosshair').hide();
	$('body').css('cursor','default');
	$('#bird').css('background-image','url(/_r/img/splatter.png)');
	$('#bird').css('background-position','0 0');
	$('#bird').css('width','133px');
	$('#bird').css('height','122px');
	moving=false;
	$('body').unbind('click');
	setTimeout("$('#bird').fadeOut('slow')",2000);
	
    }
    if ( window.addEventListener ) {
      var inplay=false;
      var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";

      window.addEventListener("keydown", function(e){
        kkeys.push( e.keyCode );
        if ( !inplay && kkeys.toString().indexOf( konami ) >= 0 ) {
    	  inplay=true; 
          $('body').click(function(e) {
		var snd = new Audio("/_r/mp3/gunshot.ogg");
		snd.play();
		if (e.pageX>$('#bird').position().left+20 && e.pageX<$('#bird').position().left+70 && e.pageY>$('#bird').position().top+30 && e.pageY<$('#bird').position().top+100) {
			killbird();
		}
		return false
	  });
        
          $(document).mousemove(function(e){
    		$("#crosshair").css({
        		top: (e.pageY -15) + "px",
        		left: (e.pageX - 15) + "px"
    	  	});
      	  });

    	  $("#crosshair").show();
	  $('body').css('cursor','none');
	  var snd = new Audio("/_r/mp3/guncock.ogg");
	  snd.play();
       }
     }, true);
   }
});

function hidebird(){

}

