(function() {

	var CountdownTimer = this.CountdownTimer = function(element, progress, start, end, wide) {
		this.element = document.getElementById(element);
		this.progress = document.getElementById(progress);
			
		this.dateStart = new Date(start.y, (start.m - 1), start.d, (start.h || 0), (start.i || 0), (start.s || 0));
		this.dateEnd = new Date(end.y, (end.m - 1), end.d, (end.h || 0), (end.i || 0), (end.s || 0));
			
		this.is_wide = (wide !== undefined) ? wide : true;
			
		this.countdown = false;
		this.setCountdown();
	
		return this;
	};
	
	this.CountdownTimer.Titles = {
		days: { s: "dag", p: "dagar" },
		hours: { s: "timme", p: "timmar" },
		minutes: { s: "minut", p: "minuter" },
		seconds: { s: "sekund", p: "sekunder" },
		separator: ',',
		penultimate: 'och'
	};
	
	CountdownTimer.prototype = {

		getPercent: function() {
				
			var now = new Date();
			var remaining = this.dateEnd - this.dateStart;
			var elapsed = now - this.dateStart;
			var percent = (100 - Math.round((elapsed / remaining) * 100));
	
			return percent;
		},
		
		getCountdown: function() {
	
			var now = new Date();
			var end = this.dateEnd;
			var diff = end.getTime() - now.getTime();
	
			var time = {
				hasPassed: false,
				days: 0,
				hours: 0,
				minutes: 0,
				seconds: 0
			};
				
			if(diff < 0) {
				time.hasPassed = true;
				time.percent = 100;
			} else {
				// Convert milliseconds to seconds
				diff = Math.floor(diff / 1000);
					
				// Days
				time.days = Math.floor(diff / 86400);
				diff = diff % 86400;
					
				// Hours
				time.hours = Math.floor(diff / 3600);
				diff = diff % 3600;
					
				// Minutes
				time.minutes = Math.floor(diff / 60);
				diff = diff % 60;
					
				// Seconds
				time.seconds = Math.floor(diff);
						
				// Percent
				time.percent = this.getPercent();
			}
		return time;
		},
		
		setCountdown: function() {
	
			var output = [];
			var time = this.getCountdown();
			
			if(time.hasPassed) {
					this.stop();
					return true;
			}
			
			var days = (time.days == 1) ? CountdownTimer.Titles.days.s : CountdownTimer.Titles.days.p;
			var hours = (time.hours == 1) ? CountdownTimer.Titles.hours.s : CountdownTimer.Titles.hours.p;
			var minutes = (time.minutes == 1) ? CountdownTimer.Titles.minutes.s : CountdownTimer.Titles.minutes.p;
			var seconds = (time.seconds == 1) ? CountdownTimer.Titles.seconds.s : CountdownTimer.Titles.seconds.p;
			
			output.push(
					time.days, ' ', days,
					CountdownTimer.Titles.separator, ' ',
					time.hours, ' ', hours,
					CountdownTimer.Titles.separator, ' ',
					time.minutes, ' ', minutes, ' ',
					CountdownTimer.Titles.penultimate, ' ',
					time.seconds, ' ', seconds
			);
			
			this.element.style.marginTop = '15px';
			this.element.innerHTML = output.join('');
			
			this.progress.parentNode.style.marginTop = '5px';
			this.progress.parentNode.style.backgroundColor = '#efefef';
			this.progress.parentNode.style.border = '#ccc 1px solid';
			this.progress.parentNode.style.height = '10px';
			this.progress.parentNode.style.width = (this.is_wide == true) ? '425px' : '280px';
			
			this.progress.style.height = '10px';
			this.progress.style.width = time.percent + '%';
			
			if(time.percent < 25)
				this.progress.style.backgroundColor = '#b51b4c';
			else 
				this.progress.style.backgroundColor = '#660066';
		},
		
		start: function() {
			var self = this;
			this.$timer = setInterval(function() {
				self.setCountdown();
			}, 1000);
		},
		
		stop: function() {
			clearInterval(this.$timer);
			delete this.$timer;
		}
	
	};

})(window);
