var segments = function(){
	$.ajax({
		url: '/kstream/responses/segments.asp',
		type: 'post',
		data: {
			'item_id': segments.itemid
		},
		success: function(data){
			$('#segments').html(data);
		}
	});
};
segments.add = function(){
	segments.load(segments.full,segments.itemid);
	$('.segmentlink').find('em').hide();
	$('.empty').find('em').show();
};
segments.edit = function(source,eid){
	segments.load(source,segments.itemid);
	$('.segmentlink').find('em').hide();
	$($('.segmentlink')[eid-1]).find('em').show();
};
segments.load = function(source,uid,eid){
	if(window.console) console.log({
		's': source,
		'u': uid
	});
	var config = {
		width: 480,
		height: 290,
		allowscriptaccess: 'always',
		allowfullscreen: true,
		wmode: 'transparent'
	};
	var flashvars = {
		"autoplay":false,
		"flvURL":source,
		"saveURL":"http://www.knowledgestream.org/kstream/responses/add_segment.asp%3Frid="+uid
	};
	var so = swfobject.embedSWF('http://www.knowledgestream.org/modules/flv_editor/flv_editor.swf', 'segmentPreview', 480, 290, "8", false, flashvars, config, {'class':'module whtBackground'});
};
segments.del = function(eid){
	if (confirm('Are you sure you want to delete this segment?')) {
		$.ajax({
			url: '/kstream/responses/delete_segment.asp',
			type: 'post',
			data: {
				'segmentid': eid, 
				'itemid': segments.itemid
			},
			success: function(data){
				segments();
			}
		});
	}
};
segments.full = '';
segments.itemid = 0;

var KSresponse = {};
KSresponse.data = function(){
	$.ajax({
		url: 'update_response_title.asp',
		type: 'post',
		data: {
			'rid': segments.itemid,
			'response_title': $('#response_title').val(),
			'response_text': $('#response_text').val()
		},
		beforeSend: function(){
			$('#response_updated').show();
			$('#savingResponse').overlay().load();
		},
		success: function(){
			$('#response_updated').css({
				opacity: 1
			});
			$('#response_updated').fadeOut(3000);
			setTimeout("$('#savingResponse').overlay().close()",2000);
		}
	});
};
KSresponse.toggle = function(){
	$.ajax({
		url: 'update_response_status.asp',
		type: 'post',
		data: {
			'rid': segments.itemid
		},
		success: function(data){
			$('#response_status').attr({
				'title': 'Toggle Status '+(data=='ON'?'OFF':'ON') 
			}).text(data);
		}
	});
}
KSresponse.media = function(action,mid){
	if(action == 'delete'){
		$.ajax({
			url: 'delete_media.asp',
			type: 'post',
			data: {
				'rid': segments.itemid,
				'id': mid
			},
			success: function(data){
				location.href = 'edit.asp?item_id='+segments.itemid;
			}
		});
	}
}

