var Anim = {

	gury : "",
	canvasSize : $(window).width(),
	canvasHeight : $(window).height(),
	currentSec : 0,
	timer : null,
	died : false,
	
	Init : function() {
		
		// Check for canvas support and if it is supported then import Gury
		/*if(Site.IsCanvasSupported()) {
			var s = document.createElement('script');
			s.setAttribute('type','text/javascript');
			s.setAttribute('src','js/gury.min.js');
			$('head').append(s);
		} else { */
			// Display a static image
			/*$('#home').css('background','url(../img/static-home.jpg) no-repeat center center');
			setTimeout(function() {
				$('#anim').fadeOut();
				$.address.value('work');
			},9500);
			setTimeout(function(){
				$('#home header h1').animate({
					'opacity' : 0
				}, 500);
			}, 7000);
			setTimeout(function(){
				$('#home header h2').animate({
					'opacity' : 0
				}, 500);
			}, 6500);
			setTimeout(function(){
				$('#home header a').animate({
					'opacity' : 0
				}, 500);
			}, 6000);
			return;*/
		//}
		
		// Start the canvas animation
		//Anim.gury = $g('anim').size(Anim.canvasSize, Anim.canvasHeight);
		
		$.address.change(function(event) {  
			var hashValue = $.address.value();
			var hash = (hashValue!=='/') ? hashValue.substring(1) : "home";
			if (hash=='home') {
				if (Anim.died){
					$.address.value('work');
				} else {
					$('#anim').show(); 
					Anim.Populate();
					Anim.Play();
				}
			} else { 
				Anim.Stop(); 
			}
		});
		
		$.address.value('work');
		
	},
	
	StartCountdown : function() {
		var totalTime = 9;
		if (Anim.currentSec == totalTime) {
			Anim.Die();
		}
		if (Anim.currentSec == totalTime+3) {
			clearInterval(Anim.timer);
		}
		$('#home header a span').html('View our work <em>(in '+ (totalTime - Anim.currentSec + 3) +' secs)</em>');
		Anim.currentSec += 1;
	},
	
	Populate : function() {
		// Add the dots
		for (var x = 0; x < $(window).width() + 150; x += Math.random()*60 + 60) {
			for (var y = 0; y < $(window).height(); y += Math.random()*60 + 60) {
		  	this.gury.add('circle', new ColourObject(x, y, Math.random()*30));
		  }
		}
	},
	
	Play : function() {
		this.timer = setInterval(Anim.StartCountdown, 1000);
		Anim.gury.play(15);
		if (Site.IsMobile()) {
			//Anim.gury.stop();
		}
	},
	
	Pause : function() {
		Anim.gury.pause();
	},
	
	Stop : function() {
		Anim.gury.stop();
	},
	
	Die : function() {
		Anim.died = true;
		setTimeout(Anim.Stop, 4000);
		setTimeout(function() {
			$('#anim').fadeOut();
			$.address.value('work');
		},4500);
		setTimeout(function(){
			$('#home header h1').animate({
				'opacity' : 0
			}, 500);
		}, 2000);
		setTimeout(function(){
			$('#home header h2').animate({
				'opacity' : 0
			}, 500);
		}, 1500);
		setTimeout(function(){
			$('#home header a').animate({
				'opacity' : 0
			}, 500);
		}, 1000);
	}
	
}

function ColourObject(x,y,width) {
	this.x = x;
  this.y = y;
	this.width = width;
	this.originalWidth = width;
	this.maxWidth = this.width + Math.random()*30;
	this.expanding = true;

	var newRed = Math.floor((Math.random()*225) + 30);
	var newGreen = 225;
	var newBlue = Math.floor((Math.random()*225) + 30);

  this.rgba = [
    newRed | 0,
    newGreen | 0,
    newBlue | 0
  ];

  this.delta = [
   	4 * Math.random() | 0,
    0 | 0,
    4 * Math.random() | 0
  ];

}

ColourObject.prototype = {
  update: function() {
    for (var i = 0; i < this.rgba.length; i++) {
      this.rgba[i] += this.delta[i];
      if (this.rgba[i] <= 0) {
        this.rgba[i] = 0;
        this.delta[i] *= -1;
      }
      else if (this.rgba[i] >= 255) {
        this.rgba[i] = 255;
        this.delta[i] *= -1;
      }
    }
		if (this.expanding) {
			this.width += 0.2;
			if (this.width > this.maxWidth) this.expanding = false;
		} else {
			if (this.width > 1) {
				this.width -= 0.2;
				if (Anim.died) {
					this.width -= 0.7;
				}
			}
			if (this.width < this.originalWidth) {
				if (!Anim.died) {
					this.expanding = true;
				}
			}
		}
  },
  
  draw: function(ctx) {
		ctx.fillStyle = "rgba(" + this.rgba.join(", ") + ", .9)";
		ctx.beginPath();
		ctx.arc(this.x, this.y, this.width, 0, Math.PI*2, true); 
		ctx.closePath();
		ctx.fill();
  }
};
