var MIT = {
	initialize: function(){
		Nifty("div.announce","bl br");
		Nifty("div.header form");
		Nifty("div.block-blog");
		Nifty("div.block-video-details");
		Nifty("div.helper");
		Nifty("div.rounded");
	},

	inPlaceEditor: function(id, options){
		var newOptions = {};
		if(options) {
			newOptions = options;
		}
		if(!newOptions.highlightcolor) newOptions.highlightcolor = '#FFF6BF';
		if(!newOptions.cancelText) newOptions.cancelText = 'Cancel';
		if(!newOptions.okText) newOptions.okText = 'Save';
		if(!newOptions.url) newOptions.url = 'frame-ajax-save.html';
		if(!newOptions.image) newOptions.image = '../images/spinner_small.gif';
    $(id).addClassName('editable');
		new Ajax.InPlaceEditor(id, newOptions.url, newOptions);
	},

	inPlaceEditorUnobtrusive: function(id, options){
		var newOptions = {};
		if(options) {
			newOptions = options;
		}
		if(!newOptions.highlightcolor) newOptions.highlightcolor = '#FFF6BF';
		if(!newOptions.cancelText) newOptions.cancelText = 'Cancel';
		if(!newOptions.okText) newOptions.okText = 'Save';
		if(!newOptions.url) newOptions.url = 'frame-ajax-save.html';
		if(!newOptions.image) newOptions.image = '../images/spinner_small.gif';

		new Ajax.InPlaceEditor(id, newOptions.url, newOptions);
	},

	addSpinner: function(el) {
		if(!$(el).next('img.spinner'))$(el).insert({after: '<img src="' + newOptions.image + '" alt="" class="spinner" />'})
	},

	removeSpinner: function(el) {
		$(el).next('img.spinner').remove();
	}
}


MIT.Forms = {
	fieldID: '',
	addStr: function(str, cursorBack){
		var field = $(this.fieldID);
		if(field && str!=''){
			field.value = field.value + str;
			Modalbox.hide({
				afterHide: function(){
					cursor = field.value.length - cursorBack;
					if(field.setSelectionRange) {
						field.focus();
						field.setSelectionRange(cursor, cursor);
					}
					else {
						if(field.createTextRange) {
							range=field.createTextRange();
							range.collapse(true);
							range.moveEnd('character', cursor);
							range.moveStart('character', cursor);
							range.select();
						}
					} 
				}
			});
		}
	}
}

MIT.Comments = {
	url: 'frame-ajax-comments.html',
	comments: [],

	init: function(){
		$$('a.report-spam').each(function(el) {
			el.observe('click', function() { MIT.Comments.reportSpam(this, 'Comment moved to the spam list', 1) });
		});
		$$('a.not-spam').each(function(el) {
			el.observe('click', function() { MIT.Comments.reportSpam(this, 'Comment moved to the general list', 0) });
		});
		$$('a.i-remove').each(function(el) {
			el.observe('click', function() { MIT.Comments.remove(this); return false; });
		});
	},

	reportSpam: function(el, msg, isSpam){
		var container = $(el).up('.block-container');
		new Ajax.Request(this.url, {
			parameters: {'id': container.id, 'spam': isSpam},
			onSuccess: function(response){
				container.update('<p class="success">' + msg + '</p>')
				setTimeout(function() {
					container.fade();
				}, 2000);
			}
		});
	},
	remove: function(el){
		var obj = this;
		var container = $(el).up('.block-container');
		new Ajax.Request(this.url, {
			parameters: {'id': container.id, 'remove': 1},
			onSuccess: function(response){
				var i = obj.comments.push(container.innerHTML)-1;
				container.update('<p class="success">Comment sucessfully removed, <a href="#" onclick="MIT.Comments.undoRemove(\''+container.id+'\', '+i+'); return back;">Undo</a></p>');
				obj.t = setTimeout(function() {
					container.fade();
				}, 5000);
			}
		});
	},
	undoRemove: function(id, i){
		var obj = this;
		var container = $(id);
		clearTimeout(this.t);
		new Ajax.Request(this.url , {
			parameters: {'id': id, 'remove': 0},
			onSuccess: function(response){
				container.update(obj.comments[i]);
				container.show();
				obj.init();
			}
		});
	},

	initTimestamps: function(player){
		$$('a.i-timestamp').each(function(el) {
			el.title = 'Timestamp';
			var value = el.innerHTML;
			el.observe('click', function(e) { 
        seconds = MIT.Comments.timestampToSeconds(value);
        if (seconds) {
					$(player).viddlerSeek(seconds);
        }
			});
		});
	},
                  
  timestampToSeconds: function(value) {
    var re = new RegExp("(\d)*(:)*(\d)*(:)*(\d)*");
    if(re.test(value)){
      var time = value.split(':');
      var seconds = 0;
      for(var i = 0; i < time.length; i++) {
        seconds += time[i] * Math.pow(60, time.length-i-1);
      }
      return seconds;
    } else {
      return null;
    }

  }
}


document.observe("dom:loaded", function(){
	MIT.initialize();
});


