var blood_x_save,blood_y_save;
var drip_speed= 0.1; // was 0.1
var blood_drops = 300; // was 500
var star=new Array(blood_drops);
var context;
var bloodtimeout = 0;
var bloodfps= 18; // was 25


var Page = {
            initialize: function() {
 
           	initBlood();
           	doLogo();
 
           	 // nice one David Walsh	
           	  var ss = new ScrollSpy({
				 min: 0,
				 mode: 'horizontal',
				 onEnter: function(position,enters) {
				
				 },
				 onLeave: function(position,leaves) {
				
				 },
				 onTick: function(position,state,enters,leaves) {
				 	$("sidePanel").style.left = -position.x+"px";
				 },
				 container: window
				}); 
           	 
           	 
           	 	var readButtons =  $$(".read_more_botton");
           	 	
           	 	readButtons.each(function(item, index){
					item.href = item.parentNode.parentNode.parentNode.getElement("h2").getElement("a").href;
				});
			 	readButtons.extend($$(".view_demo_button"));
           	 readButtons.addEvents({
           	 	'mouseover': function(){
			 		this.getElement(".fader").fade(0);
			 		this.getElement(".buttonText").get('tween', {property: 'color', duration: '200'}).start("#fff"); 
			 	},
				'mouseout': function(){
					this.getElement(".fader").fade(1);
					this.getElement(".buttonText").get('tween', {property: 'color', duration: '200', onComplete:function(el){
						
					  //  el.style.color = '';
					 
					}}).start("#999"); 	
				}
			});
			
			if($("superCheck") || location.href == "http://rickyh.co.uk/" || location.href == "http://rickyh.co.uk/#" || location.href == "http://www.rickyh.co.uk/" || location.href == "http://www.rickyh.co.uk/#"){
	 		
			 $$(".smart_Image").getElement("img").addEvents({
           	 	'mouseover': function(){
			 		this.get('tween', {property: 'opacity', duration: '200', onComplete:function(el){
			 		 
					}}).start("1"); 
			 		 
			 	},
				'mouseout': function(){
					this.fade(0.6);
					this.get('tween', {property: 'opacity', duration: '200', onComplete:function(el){
						
					el.style.opacity = "";
					 
					}}).start("0.6"); 	
				},
				"click": function(){
					   	// alert(this.parentNode.parentNode.parentNode.getElement("h2").getElement("a").href);//.click(); 
					   	
					   	window.location = this.parentNode.parentNode.parentNode.getElement("h2").getElement("a").href;
				}
			});
			
				$$(".monthOfPosts").each(function(item, index){
					if(item.getElement(".post")){
						item.parentNode.get('tween', {property: 'height', transition: Fx.Transitions.Back.easeOut, duration: '1500', onComplete:function(el){
						
					 
						 
						}}).start("450"); 	
					 	item.parentNode.setStyle("width", 271+(556*item.getElements(".post").length)+"px"); 
					 	item.setStyle("width", 20+(556*item.getElements(".post").length)+"px"); 
					}
					else{
					 
					}
				}); 
				
			 
			 
				
				
				
			
			}
           	 
            }
       }
       
           window.addEvent("domready", Page.initialize);

 
  
  
function doLogo(){
	          	
          $("headerimg").getElement("a").addEvents({
				'mouseover': function(){
			 			$("bloodyMess").fade(1);
			 			animBlood();
				 	 //this.parentNode.get('tween', {property: 'background-color', duration: '300'}).start("#950404"); 
					 
				},
				'mouseout': function(){
			  		//this.parentNode.get('tween', {property: 'background-color', duration: '100'}).start("#141414"); 
			 		$("bloodyMess").fade(0);
					clearTimeout(bloodtimeout);
				}
			});
 
} 

function initBlood()
	{
 
	$("bloodyMess").setStyle("opacity", "0");
	var a=0;
	for(var i=0;i<blood_drops;i++)
		{
		star[i]=new Array(5);
		star[i][0]=Math.random()*157*2-78*2;
		star[i][1]=Math.random()*188*2-89*2;
		star[i][2]=Math.round(Math.random()*172.5);
		star[i][3]=0;
		star[i][4]=0;
		}
	var bloodyMess=$('bloodyMess');
	context=bloodyMess.getContext('2d');
	bloodyMess.width = 157;
	bloodyMess.height = 188; 
	context.fillStyle='rgb(0,196,255)'; // 149, 4, 4
	context.strokeStyle='rgb(255,255,255)'; // 0,0,0
	}
 
 
function animBlood()
	{

	context.fillRect(0,0,157,188);

	for(var i=0;i<blood_drops;i++)
		{
		test=true;
		blood_x_save=star[i][3];
		blood_y_save=star[i][4];
		star[i][0]+=11>>4; if(star[i][0]>78<<1) { star[i][0]-=157<<1; test=false; } if(star[i][0]<-78<<1) { star[i][0]+=157<<1; test=false; }
		star[i][1]+=31>>4; if(star[i][1]>89<<1) { star[i][1]-=188<<1; test=false; } if(star[i][1]<-89<<1) { star[i][1]+=188<<1; test=false; }
		star[i][2]-=drip_speed; if(star[i][2]>172.5) { star[i][2]-=172.5; test=false; } if(star[i][2]<0) { star[i][2]+=172.5; test=false; }
		star[i][3]=78+(star[i][0]/star[i][2])*300;
		star[i][4]=89+(star[i][1]/star[i][2])*300;
		if(blood_x_save>0&&blood_x_save<157&&blood_y_save>0&&blood_y_save<188&&test)
			{
		 	context.fillStyle='rgba(0, 196, 255, 1)';
			context.lineWidth=(1-0.005*star[i][2])*500; // width
			context.beginPath();
			context.moveTo(blood_x_save,blood_y_save);
			context.lineTo(star[i][3],star[i][4]);
			context.stroke();
			context.closePath();
			}
		}
	bloodtimeout=setTimeout('animBlood()',bloodfps);
	}
 



			//logoTimer = setTimeout(function(){
			   
				//	 }, 130);

var ScrollSpy = new Class({
	
	/* implements */
	Implements: [Options,Events],

	/* options */
	options: {
		min: 0,
		mode: 'vertical',
		max: 0,
		container: window,
		onEnter: $empty,
		onLeave: $empty,
		onTick: $empty
	},
	
	/* initialization */
	initialize: function(options) {
		/* set options */
		this.setOptions(options);
		this.container = $(this.options.container);
		this.enters = this.leaves = 0;
		this.max = this.options.max;
		
		/* fix max */
		if(this.max == 0) 
		{ 
			var ss = this.container.getScrollSize();
			this.max = this.options.mode == 'vertical' ? ss.y : ss.x;
		}
		/* make it happen */
		this.addListener();
	},
	
	/* a method that does whatever you want */
	addListener: function() {
		/* state trackers */
		this.inside = false;
		this.container.addEvent('scroll',function() {
			/* if it has reached the level */
			var position = this.container.getScroll();
			var xy = this.options.mode == 'vertical' ? position.y : position.x;
			/* if we reach the minimum and are still below the max... */
			if(xy >= this.options.min && xy <= this.max) {
					/* trigger Enter event if necessary */
					if(!this.inside) {
						/* record as inside */
						this.inside = true;
						this.enters++;
						/* fire enter event */
						this.fireEvent('enter',[position,this.enters]);
					}
					/* trigger the "tick", always */
					this.fireEvent('tick',[position,this.inside,this.enters,this.leaves]);
			}
			else {
				/* trigger leave */
				if(this.inside) 
				{
					this.inside = false;
					this.leaves++;
					this.fireEvent('leave',[position,this.leaves]);
				}
			}
		}.bind(this));
	}
});
                