uShow.tagger = {};

uShow.tagger.checkStatus = function(container, tag_id, counter) {

	var url = container.attr("status_url");
	if (!url) return;
	$.post(url, "tag_id="+tag_id, function(data) {
		if (data.is_ok) {
			uShow.tagger.refreshOne(container);
		}
		else {
			var interval = Math.log(counter*2) * 4000;
			counter += 1;
			setTimeout(function() { uShow.tagger.checkStatus(container, tag_id, counter); }, interval);
		}
	}, "json");
}

uShow.tagger.checkVideo = function(url, video_id, counter) {

	$.post(url, "video_id="+video_id, function(data) {
		if (data.is_ok) {
			uShow.loadHomeFeed();
		}
		else {
			var interval = Math.log(counter*2) * 8000;
			counter += 1;
			setTimeout(function() { uShow.tagger.checkVideo(url, video_id, counter); }, interval);
		}
	}, "json");
}

uShow.tagger.refreshOne = function(container) {
	var url = container.attr("url");
	$.get(url, function(data) { 
		if (data) {
			container.html(data);
			uShow.setScroll();
			uShow.setStoryCount();
		}
	});
}

uShow.tagger.refreshAll = function(tag_id, video_id, index) {

	$(".videoplayer-tagged[video_id='"+video_id+"']").each(function() {
		var container = $(this);
		uShow.tagger.refreshOne(container);
		uShow.tagger.checkStatus(container, tag_id, 1);
	});
	if (index == "0") {
		var container = $("#home_feed");
		uShow.tagger.refreshOne(container);
		uShow.tagger.checkStatus(container, tag_id, 1);
	}
}

uShow.tagger.videoPlay = function(index) {
	uShow.tagger.flashSuccess(index, 0);
}

uShow.tagger.videoStarted = function(index) {
	uShow.tagger.flashSuccess(index, 0);
}

uShow.tagger.flashSuccess = function(index, mode) {
	var div = $("div.tag-notice[index='"+index+"']").css('visibility','visible'),
	dne = div.find(".tag-done"),
	ins = div.find(".tag-instruct"),
	anm = uShow.tagger.animate;

	if (mode) { 
		ins.hide(); 
		dne.show(); 
		anm(index, -1);
	}
	else { 
		dne.hide(); 
		ins.show(); 
		anm(index, 1);
	}
}

uShow.tagger.animate = function(index, mode) {
	var div = $("div.tag-notice[index='"+index+"']").find(".tag-instruct"), speed=750;
	if (mode == -1) {
		div.stop();
	}
	else if (mode == 1)
	  div.animate({color: "#990033", backgroundColor: "#D9F7FF"}, speed, null, function() { uShow.tagger.animate(index, 0) });
	else
	  div.animate({color: "#000000", backgroundColor: "#8AAEC4"}, speed, null, function() { uShow.tagger.animate(index, 1) });
}

uShow.tagger.videoTagged = function(tag_id, is_yt, index) {
	var url = $(".data_url").attr("data_url");

	if (tag_id == -1) {
	}
	else {
		$.post(url, "tag_id="+tag_id, function(data){
			uShow.tagger.refreshAll(tag_id, data.video_id, index);
			uShow.FB.feed(data);
			uShow.tagger.flashSuccess(index, 1);
		}, "json");
	}
}

uShow.tagger.videoPublish = function(media_url, duration) {
	var block = $("div.video_title_entry"),
	url = block.attr("url"),
	title = block.find("textarea")[0].value;
	$.post(url, "media_url="+media_url+"&duration="+parseInt(duration)+"&title="+title, function(data) {
		if (data.is_ok) {
			$("#videoRecorder").remove();
			$("div.video_title_entry").remove();
			$("div.video_success").show();
			uShow.tagger.checkVideo(data.url, data.video_id, 1);
		}
	}, "json");

}

uShow.tagger.count = function(id) {
	var t = $("#"+id);
	var s = t.parent().find("span.char_count");
	s.html(117-t[0].value.length);
}