var WIDTH;
var HEIGHT;
var canvas;
var con;
var g;
var pxs = new Array();
var rint = 60;
var G_vmlCanvasManager;

var S = {};
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("windows") > -1 || ua.indexOf("win32") > -1) {
	S.isWindows = true
} else {
	if (ua.indexOf("macintosh") > -1 || ua.indexOf("mac os x") > -1) {
		S.isMac = true
	} else {
		if (ua.indexOf("linux") > -1) {
			S.isLinux = true
		}
	}
}
S.isIE = ua.indexOf("msie") > -1;
S.isIE6 = ua.indexOf("msie 6") > -1;
S.isIE7 = ua.indexOf("msie 7") > -1;
S.isGecko = ua.indexOf("gecko") > -1 && ua.indexOf("safari") == -1;
S.isWebKit = ua.indexOf("applewebkit/") > -1;
S.isIpad = ua.indexOf("ipad") > -1;
S.isIphone = ua.indexOf("iphone") > -1;
S.isAndroid = ua.indexOf("android") > -1;
S.isMobile = ua.indexOf("mobile") > -1;

$(document).ready(function(){
	
	if($('#lights').length){
  if(typeof(window.innerWidth ) != 'undefined' ) {
  	WIDTH = window.innerWidth;
  	HEIGHT = window.innerHeight;
  }else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  	//IE 6+ in 'standards compliant mode'
    WIDTH = document.documentElement.clientWidth;
    HEIGHT = document.documentElement.clientHeight;
  }else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  	//IE 4 compatible
    WIDTH = document.body.clientWidth;
    HEIGHT = document.body.clientHeight;
  }
	
	if(!S.isIphone && !S.isIpad){
		canvas = document.getElementById('lights');
		//$(canvas).attr('width', WIDTH).attr('height',HEIGHT);
		//$('#lights').css('width', WIDTH).css('height',HEIGHT);
		//$('#lights').attr('width', WIDTH).attr('height',HEIGHT);
		if (typeof(G_vmlCanvasManager) != 'undefined')
	  	canvas = G_vmlCanvasManager.initElement(canvas);
	  //canvas.setAttribute("width", WIDTH);
		//canvas.setAttribute("height", HEIGHT);
		con = canvas.getContext('2d');
		for(var i = 0; i < 100; i++) {
			pxs[i] = new Circle();
			pxs[i].reset();
		}
		setInterval(draw,rint);
	setInterval(draw,rint);
	}else{
		$('html').css('background', 'url(http://www.ide-website.net/default/templates/front/001/imagenes/XMAs_BG_IE.jpg) #4a0101 center top no-repeat');
	}

	var $mountains = $('#mountains');
	var $grass = $('#grass');
	var $container = $('#container');
	$container.mousedown(function(ev){
		var ox = ev.clientX;
		var om = parseInt($mountains.css('background-position').substr(0, $mountains.css('background-position').search(' ')));
		var og = parseInt($grass.css('background-position').substr(0, $grass.css('background-position').search(' ')));
		$container.mousemove(function(e){
			$mountains.css('background-position', om+((e.clientX-ox)/10)+'px 0px');
			$grass.css('background-position', og+((e.clientX-ox)/4)+'px 10px');
		});
		$container.mouseup(function(){
			$container.unbind('mousemove');
			$container.unbind('mouseup');
		});
	});
	
	}
});

function draw() {
	con.clearRect(0,0,WIDTH,HEIGHT);
	for(var i = 0; i < pxs.length; i++) {
		pxs[i].fade();
		pxs[i].move();
		pxs[i].draw();
	}
}

function Circle() {
	this.s = {ttl:8000, xmax:5, ymax:2, rmax:10, rt:1, xdef:960, ydef:540, xdrift:4, ydrift: 4, random:true, blink:true};

	this.reset = function() {
		this.x = (this.s.random ? WIDTH*Math.random() : this.s.xdef);
		this.y = (this.s.random ? HEIGHT*Math.random() : this.s.ydef);
		this.r = ((this.s.rmax-1)*Math.random()) + 1;
		this.dx = (Math.random()*this.s.xmax) * (Math.random() < .5 ? -1 : 1);
		this.dy = (Math.random()*this.s.ymax) * (Math.random() < .5 ? -1 : 1);
		this.hl = (this.s.ttl/rint)*(this.r/this.s.rmax);
		this.rt = Math.random()*this.hl;
		this.s.rt = Math.random()+1;
		this.stop = Math.random()*.2+.4;
		this.s.xdrift *= Math.random() * (Math.random() < .5 ? -1 : 1);
		this.s.ydrift *= Math.random() * (Math.random() < .5 ? -1 : 1);
	}

	this.fade = function() {
		this.rt += this.s.rt;
	}

	this.draw = function() {
		if(this.s.blink && (this.rt <= 0 || this.rt >= this.hl)) this.s.rt = this.s.rt*-1;
		else if(this.rt >= this.hl) this.reset();
		var newo = 1-(this.rt/this.hl);
		con.beginPath();
		con.arc(this.x,this.y,this.r,0,Math.PI*2,true);
		con.closePath();
		var cr = this.r*newo;
		g = con.createRadialGradient(this.x,this.y,0,this.x,this.y,(cr <= 0 ? 1 : cr));
		g.addColorStop(0.0, 'rgba(255,255,255,'+newo+')');
		g.addColorStop(this.stop, 'rgba(255,255,255,'+newo+')');
		g.addColorStop(1.0, 'rgba(255,255,255,0)');
		con.fillStyle = g;
		con.fill();
	}

	this.move = function() {
		this.x += (this.rt/this.hl)*this.dx;
		this.y += (this.rt/this.hl)*this.dy;
		if(this.x > WIDTH || this.x < 0) this.dx *= -1;
		if(this.y > HEIGHT || this.y < 0) this.dy *= -1;
	}

	this.getX = function() { return this.x; }
	this.getY = function() { return this.y; }
}

