var syncable_media_basic = function () {
	var that = this;
	
	that.HANDLER_TYPE_NAME = 'media';

	that.run = function(seconds, record) {		
		if (record['data']['type'] == 'start') {
			//close video if one is showing
			if (my_media_tracking.get_media_id() != null && record['data']['media_id'] != my_media_tracking.get_media_id()) {
				$('#sync_player_container').remove();
				my_media_tracking.init();
			}
			
			//get anchor information
			var sync_media_anchor = $('#sync_media_anchor').val();
			var sync_media_offset = $("#" + sync_media_anchor).offset();
			
			//get current time code of main video
			try {
				var main_timecode = $f("player").getTime();
				//var main_timecode = Main.getTimeCode("flash_player");
			} catch (err) {
				return;
			}
			
			//compare with start time of this video
			var start_offset = 0;
			if (Math.abs(main_timecode - record['timecode']) > 2) {
				var start_offset = main_timecode - record['timecode'];
			}

			//get the html for the flash player

			//initialize
			$("sync_player").stop();
			$('#sync_player_container').remove();

			//place video in page
			//$("body").prepend('<div id="sync_player_container" style="background-color: #000; position: absolute; z-index: 2000; top: ' + sync_media_offset['top'] + 'px; left: ' + sync_media_offset['left'] + 'px; width: ' + sync_media_width + 'px; height: ' + sync_media_height + 'px"><div id="sync_player"></div></div>');
			
			$("body").prepend('<div id="sync_player_container" style="background-color: #000; position: absolute; z-index: 2000; top: ' + sync_media_offset['top'] + 'px; left: ' + sync_media_offset['left'] + 'px; width: ' + record['data']['width'] + 'px; height: ' + record['data']['height'] + 'px"><div id="sync_player" style="width: ' + record['data']['width'] + 'px; height: ' + record['data']['height'] + 'px"></div></div>');

			flowplayerSYNCCONFIG["clip"]["url"] = record['data']['organization_id'] + '/' + record['data']['media_id'];
			$f("sync_player", flowplayerSYNCSWF, flowplayerSYNCCONFIG);
			$f("sync_player").play();
			
			//mark this video as playing
			my_media_tracking.set_media_id(record['data']['media_id']);
			my_media_tracking.set_start_time(record['timecode']);
			my_media_tracking.check_pause_or_play();
			my_media_tracking.check_seek();
		} else {
			//remove the item from the page
			$('#sync_player_container').remove();
			my_media_tracking.set_media_id(null);
		}
	};
	
	return that;
};

var media_tracking = function () {
		var media_id = null;
		var start_time = null;
		var sync_timecode = null;
		var main_timecode = null;
		var main_timecode_last = null;
		var mode = null;
		var seek_block = 0;
		var that = this;
		var debug_p = false;
		var locked_until = null;
		var locked_by = null;
		
		//contstants
		var seek_poll_rate = 2000;
		var pause_play_poll_rate = 1000;
		var seek_threshold = 4000;
		var seek_lock_duration = 10;
		var start_lock_duration = 3;
		
		debug = function (msg) {
			if (debug_p == true) {
				try {
					//console.log(msg);
				} catch (e) {}
			}
		};
		
		init = function () {
			media_id = null;
			start_time = null;
			sync_timecode = null;
			main_timecode = null;
			main_timecode_last = null;
			mode = null;
			seek_block = 0;
			locked_until = null;
			locked_by = null;
			debug('initialized');
		};
		
		that.set_start_time = function (start_time_in) {
			start_time = start_time_in;
		};
		
		that.get_media_id = function () {
			return that.media_id;
		};

		that.set_media_id = function (media_id_in) {
			init();
			media_id = media_id_in;
			lock('start', start_lock_duration);
			unlock();
		};
		
		lock = function (role, until) {
			locked_by = role; 
			if (until != null) {
				var delay = new Date;
				var unixtime_ms = delay.getTime();
				locked_until = parseInt(unixtime_ms / 1000) + until;
			}
		};
		
		unlock = function () {
			locked_by = null;
		};
		
		check_locked = function () {
			if (locked_by != null) {
				//debug('locked_by: ' + locked_by);
				return true;
			}
			if (locked_until != null) {
				var delay = new Date;
				var unixtime_ms = delay.getTime(); // Returns milliseconds since the epoch
				var now = parseInt(unixtime_ms / 1000);
				//debug('locked? ' + now + ' < ' + locked_until);
				if (now < locked_until) {
					//debug('locked_until: ' + (locked_until - now));
					return true;
				}
			}
			return false;
		};
		
		that.check_pause_or_play = function () {
			debug('check_pause_or_play');
			if (media_id == null) {
				debug('no media_id');
				return;
			}
			
			//debug('mode: ' + mode);
			if (!$f("player").getTime()) {
				debug('Pause Player: nothing yet');
				setTimeout(function () {
					that.check_pause_or_play();
				}, pause_play_poll_rate);
				return;
			}
									
			var time_diff;

			//sync play / pause
			main_timecode_last = main_timecode;
			main_timecode = $f("player").getTime();
			debug('main_timecode: ' + main_timecode);
			if (main_timecode == null || main_timecode < 1) {
				setTimeout(function () {
					that.check_pause_or_play();
				}, pause_play_poll_rate);
				return;
			}
			if (main_timecode_last != null) {
				if (main_timecode == main_timecode_last) {
					if (mode == 'play' || mode == null) {
						pause();
					}
				} else {
					if (mode == 'pause' || mode == null) {
						play();
					}
				}
			}
			
			//call self recursively
			setTimeout(function () {
				that.check_pause_or_play();
			}, pause_play_poll_rate);
		};

		that.check_seek = function () {
			//debug('check seek');
			if (media_id == null) {
				debug('no media_id');
				return;
			}

			if (!$f("sync_player").getTime()) {
				debug('Seek: nothing yet');
				setTimeout(function () {
					that.check_seek();
				}, seek_poll_rate);
				return;
			}
			
//			var last_access = Main.getScrubBarTimeFromLastAccess("sync_player");
			//debug('last_access: ' + last_access);
//			if (last_access < seek_threshold) {
				seek();
//			}
			
			//call self recursively
			setTimeout(function () {
				that.check_seek();
			}, seek_poll_rate);
		};
		
		var pause = function () {
			debug('pause');
			//sync_player
			$f("sync_player").pause();
			//MainSync.pause("sync_player");
			mode = 'pause';
		};
		
		var play = function () {
			debug('play');
			//MainSync.resume("sync_player");
			$f("sync_player").play();
			mode = 'play';
		};
		
		var seek = function (timecode) {
			var direction;
			if (check_locked() == true) {
				return;
			}
			var sync_timecode = $f("sync_player").getTime();
			if (sync_timecode < 1 || sync_timecode == null) {
				return;
			}	
			var main_timecode = $f("player").getTime();
			if (main_timecode < 1 || main_timecode == null) {
				return;
			}
			var diff = ((start_time + sync_timecode) - main_timecode);
			var seekto = sync_timecode + (diff * -1);
			if (seekto < main_timecode) {
				direction = 'back';
			} else {
				direction = 'forward';
			}
			debug(direction + ', (start_time: ' + start_time + ' + sync_timecode: ' + sync_timecode + ') - main_timecode: ' + main_timecode + ') = ' + diff);
			
			if (Math.abs(diff) > 3) {
				//debug('SEEK. now: ' + sync_timecode + ', goto: ' + seekto + ', diff ' + (diff * -1));
				lock('sync', seek_lock_duration);
				$f("sync_player").seek(seekto);
				//MainSync.setTimeCode("sync_player", seekto);
				unlock();
			}
		};
		
		return that;
};

var my_media_tracking = media_tracking(); 
