var defaultFps= 30;
var MAX_TIMECODE_SECONDS = (23 *60 *60) + (59 *60) +59; 

function isTimecode(str) {
	return str.match(/^(\d+:)?(\d+:)?(\d+:)?\d+$/);
}

function truemod(num, mod) {
  return (mod + (num % mod)) % mod;
}

function offsetToTimecode(startTimecode, offset) {
	var startPosition = startTimecode.toPosition();
	var eventOffset = truemod((Number(offset) + startPosition),MAX_TIMECODE_SECONDS);
	return formatTimecode(eventOffset);
}

function Timecode(str, fps) {
	
	if (str == 'END') {
		this.timecode = str;
	} else if (str == '' || str == null) {
		this.timecode = '00:00:00';
	} else {
		this.timecode = str;
	}
	if (fps == undefined) {
		this.fps = defaultFps;
	} else {
		this.fps= fps;
	}
	
	this.toPosition = function(includeFrames) {
		if ( this.timecode == "END") {
			return  Number.MAX_VALUE;
		}
		if (includeFrames == undefined) {
			includeFrames = true;
		}
		var parts = this.timecode.split(/:|;/);
		var pos = 60*60 * Number(parts[0]) + 60* Number(parts[1]) + Number(parts[2]);
		if (parts.length == 4) {
			pos +=  Number(parts[3]) / this.fps;
		}
		if (!includeFrames) {
			pos = Math.floor(pos);
		}
		return pos;
	};
	
	
	this.isBefore = function(tc) {
		return this.toPosition(false) < tc.toPosition(false);
	};
	
	this.isAfter = function(tc) {
		return this.toPosition(false) > tc.toPosition(false);
	};
	
	this.isEqualTo = function(tc) {
		return this.toPosition() == tc.toPosition();
	};
	
	this.isInRange = function(tc1, tc2) {
		return this.toPosition() >= tc1.toPosition(false) && this.toPosition() <= tc2.toPosition(false);
	};

};

