﻿var current_video_type = '';

/** 设置视频信息
 * 
 * @param string vid 联盟的视频id
 * @param string thumb 联盟的视频图片
 */
function setVideoInfo(ccvid, thumb) {
	$("#videourl").val(ccvid);
	$("#pic_remote").val(thumb);
}

/** 联盟传递的视频id 和时间
 * 
 * @param {string} ccvid
 * @param {string} vtime
 */
function videoPlay(ccvid, vtime) {
	if(!ccvid || !vtime) {
		return false;
	}
	$.get(front_url + "ajax/set_video_info/" + ccvid + "/" + vtime);
}

if (typeof series != 'undefined') {
	/** 专辑播放跳转
	 *
	 */
	function playnext(){
		window.location.href = next_video_url;
	}
}

/** flv获取视频信息
 * 
 */
function getInfo() {
	var returnStr = '';
	var video_add_form = document.getElementById("video_add_form");
	
	if(!video_check_data(video_add_form)) {
		return false;
	}
	
	returnStr += 'class=' + video_add_form.channelsub.value.substr(video_add_form.channelsub.value.indexOf(',')+1,2) + '|';

	returnStr += 'title=' + video_add_form.title.value + '|';

	returnStr += 'tags=' + video_add_form.tags.value + '|';

	returnStr += 'ccvid=' + video_add_form.videourl.value + '|';
	
	returnStr += 'description=' + video_add_form.description.value + '|';

	var type = $('#flv_type').val();
  	if(type == 'upload'){
  		returnStr += 'isup=' + 1 + '|';
  	}
	returnStr += 'share=' + 1 + '|';

	//alert(returnStr);return false;
	return returnStr;
}

/** flv触发表达提交
 * 
 */
function toSubmit() {
	document.getElementById("video_add_form").submit();
}

// 以上为联盟调用接口

/** 切换视频的类型
 * 
 * @param {string} type 视频的类型
 */
function change_flv_type(change_type) {
	
	var change_flag = false;
	var current_type = false;
	
	var types = ['quote', 'upload'];
	var confirm_msg = {
		'quote' : '您已经引用了视频文件，切换到视频类型将丢失引用的内容。您确定要这样做？',
		'upload' : '您已经上传了视频文件，切换到录制模式将丢失上传的内容。您确定要这样做？'
	};
	
	for(i=0; i<types.length; i++) {
		if($('#' + types[i] + 'Swf').css('display') != 'none') {
			current_type = types[i];
			current_video_type = change_type;
			break;
		}
	}
	
	if($("#videourl").val() != '' && current_type != change_type) {		
		if(current_type && confirm(confirm_msg[current_type])) {
			change_flag = true;
		}
	} else {
		change_flag = true;
	}
	
	if(change_flag) {
		$('#videourl').val('');
		$('#pic_remote').val('');
		$('#flv_type').val(change_type);
		$('#'+current_type+'Swf').hide();
		//$('#'+change_type+'Swf').css('display','table-row');
		$('#'+change_type+'Swf').addClass("dis-table-row").css('display','');	
		
		if(change_type == 'quote') {
			$('#submitSwf').hide();
			$('#quote_submitSwf').show();
			
			// 自动截图隐藏
			$('#pictype_auto').hide();
			$('#pictype_auto_label').hide();
			$('#pic_auto').hide();
			
		} else {
			$('#submitSwf').show();
			$('#quote_submitSwf').hide();
			
			// 自动截图显示
			$('#pictype_auto').show();
			$('#pictype_auto_label').show();
			$('#pic_auto').show();
			
			
			// 上传图片显示
			$('#pictype_upload').show();
			$('#pictype_upload_label').show();
			
			$('#pic_url').hide();
			
			// 选中自动截图
			$('#pictype_auto').attr('checked', true);
		}
	}
	
}

/** 编辑视频用到的检测视频数据信息
 * 
 * @param {Object} video_add_form
 */
function video_check_data(video_form) {
	if(video_form.channelsub.value == '') {
		alert("请选择频道！")
		video_form.channelsub.focus()
		return false;
	}
	if(video_form.title.value == '') {
		alert("请填写标题！")
		video_form.title.focus()
		return false;
	}
	if(video_form.tags.value == '') {
		alert("请填写标签！")
		video_form.tags.focus()
		return false;
	}
	if(video_form.videourl.value == '') {
		alert("没有上传视频！");
		//video_form.videourl.focus()
		return false;
	}
	return true;
}

function copy_url(url_id, no_alert){
	var txt_value = $("#" + url_id).val();
	if(!txt_value) {
		return false;
	}
	if(window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt_value);
	} else {
		var flashcopier = 'flashcopier';
		if(typeof($("#flashcopier")[0]) == 'undefined') {
			$('<div id="' + flashcopier + '"></div>').insertAfter($(document.body));
		}
		$("#flashcopier").html(
			'<embed src="' + base_url + 'system/template/share/media/swf/clipboard.swf" FlashVars="clipboard='+escape(txt_value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>'
		);
	}
	$("#" + url_id).select();
	if(!no_alert) {
		alert("代码复制成功，请粘贴到你的BBS或BLOG上了。");
	}
}

function comment_submit(theForm) {
	if(theForm.content.value == '') {
		poc_alert('内容不能为空!');
		theForm.content.focus;
		return false;
	}
	if(theForm.content.value.length >= '300') {
		poc_alert('最大不能超过300字符!');
		theForm.content.focus;
		return false;
	}
	
	$.post(
		front_url + "ajax/add_video_comment/" + vid,
		{content:theForm.content.value},
		function(data) {
			if(/^\d+$/.test(data)) {
				$('#commentcount').html(data);
				theForm.content.value = '';
				load_comment_content('comment_list', vid);
			} else {
				poc_alert(data);
			}			
		}		
	);	
	return true;
}

function load_comment_content(comment_wrap_id, vid, page) {
	
	if(typeof(page) != 'number') {
		page = 1;
	}
	
	$.getJSON(
		front_url + 'ajax/list_video_comment/' + vid + '/' + page,
		function(json){
			if (json) {
				var html = '';
				
				$.each(json.comment, function(key, comment){
					html += '<dl class="commentlist">' +
					'<dt>' +
					comment.username + '&nbsp;' +
					comment.date + '&nbsp;' + comment.time +
					'</dt>' +
					'<dd>' +
					comment.content +
					'</dd>' +
					'</dl>';
				});
				
				//alert(html);
				$('#' + comment_wrap_id).html(html + json.pagination);
			} else {
				$('#' + comment_wrap_id).html('暂时没有评论');
			}
		}
	);
}

function addface(id) {
	document.getElementById('comment_form').content.value += '[PS' + id +']';
}

function getData() {
	var message = '';
	if($.browser.msie) {
		try {
			textobj.load('CCVMS');
			var oXMLDoc = textobj.XMLDocument;
			var nodes = oXMLDoc.documentElement.childNodes;
			message = nodes.item(nodes.length - 1).getAttribute('message');
		} catch(e) {}
	} else if(window.sessionStorage) {
		try {
			message = sessionStorage.getItem('CCVMS');
		} catch(e) {}
	}
	message = message.toString();

	if(!confirm('此操作会覆盖当前内容，确定继续吗？')) {
		return false;
	}
	var formdata = message.split(/\x09\x09/);
	for(var i = 0; i < document.getElementById('video_add_form').elements.length; i++) {
		var el = document.getElementById('video_add_form').elements[i];
		if(el.name != '' && (el.tagName == 'TEXTAREA' || el.tagName == 'SELECT' || el.tagName == 'INPUT' && (el.type == 'text' || el.type == 'hidden' || el.type == 'radio'))) {
			for(var j = 0; j < formdata.length; j++) {
				var ele = formdata[j].split(/\x09/);
				if(ele[0] == el.name) {
					elvalue = (typeof ele[3] != 'undefined') ? ele[3] : '';
					
					if(ele[0] == 'flv_type') {
						$("li[title='"+elvalue+"']").click();
					}
					
					if(ele[1] == 'INPUT') {
						if(ele[2] == 'text' || ele[2] == 'hidden') {
							el.value = elvalue;
						} else if(ele[2] == 'radio' && ele[3] == el.value) {
							el.checked = true;
							el.click();
						}
					} else if(ele[1] == 'TEXTAREA') {
//						if(ele[0] == 'description') {
//							textobj.value = elvalue;
//						} else {
//							el.value = elvalue;
//						}
						el.value = elvalue;
					} else if(ele[1] == 'SELECT') {
						for(var k = 0; k < el.options.length; k++) {
							if(el[k].value == elvalue) {
								el[k].selected = true;
								break;
							}
						}
						if(ele[0] == 'channelmain') {
							$('#channelmain').change();
						}
					}
					break;
				}
			}
		}
	}
	
	if($("#videourl").val()) {
		alert('数据完全恢复成功，您可以直接点击“提交”来发布视频。（不需重新上传）');
	} else {
		alert('视频信息恢复成功，请上传视频并提交发布。');
	}
}

function delData() {
	if($.browser.msie) {
		putData('', 'delete');
	} else if(window.sessionStorage) {
		try {
			sessionStorage.removeItem('CCVMS');
		} catch(e) {}
	}
}

function putData(data, del) {
	var formdata = '';
	if(typeof del == 'undefined') {
		for(var i = 0; i < document.getElementById('video_add_form').elements.length; i++) {
			var el = document.getElementById('video_add_form').elements[i];
			if(el.name != '' && (el.tagName == 'TEXTAREA' || el.tagName == 'SELECT' || el.tagName == 'INPUT' && (el.type == 'text' || el.type == 'hidden' || el.type == 'radio')) && el.type != 'file') {
				//var elvalue = el.name == 'description' ? data : el.value;
				var elvalue = el.value;
				if((((el.type == 'checkbox' || el.type == 'radio') && !el.checked) || !elvalue)) {
					continue;
				}
				formdata += el.name + String.fromCharCode(9) + el.tagName + String.fromCharCode(9) + el.type + String.fromCharCode(9) + elvalue + String.fromCharCode(9, 9);
			}
		}
	}

	if($.browser.msie) {
		try {
			var oXMLDoc = textobj.XMLDocument;
			var root = oXMLDoc.firstChild;
			if(root.childNodes.length > 0) {
				root.removeChild(root.firstChild);
			}
			var node = oXMLDoc.createNode(1, 'POST', '');
			var oTimeNow = new Date();
			oTimeNow.setHours(oTimeNow.getHours() + 24);
			textobj.expires = oTimeNow.toUTCString();
			node.setAttribute('message', formdata);
			oXMLDoc.documentElement.appendChild(node);
			textobj.save('CCVMS');
		} catch(e) {}
	} else if(window.sessionStorage) {
		try {
			sessionStorage.setItem('CCVMS', formdata);
		} catch(e) {}
	}
}

//////////////////// 引用资源接口  ////////////////////
function get_proxy_url() {
//	$.get(front_url + "ajax/get_remote_html/" + url, '', function(date){
//		return date;
//	});
	return front_url;
}

function set_temp_id(parse_str) {
	try {
		document.getElementById('video_add_form').videourl.value = parse_str;
		return true;
	} catch(e) {
		return false;
	}	
}

function get_source_info() {
	var returnStr = '';
	var video_add_form = document.getElementById("video_add_form");
	
	if(!video_check_data(video_add_form)) {
		return false;
	}	

	returnStr += 'title=' + video_add_form.title.value + '|';

	returnStr += 'tag=' + video_add_form.tags.value + '|';

	returnStr += 'code=' + video_add_form.videourl.value + '|';
	
	returnStr += 'pic=' + video_add_form.pic_url.value + '|';
	
	returnStr += 'description=' + video_add_form.description.value + '|';

	//alert(returnStr);return false;
	return returnStr;
}

function set_source_id(source_id) {
	try {
		document.getElementById('video_add_form').videourl.value = source_id;
		return true;
	} catch(e) {
		return false;
	}	
}

function source_submit() {
	toSubmit();
}

if(current_act == 'video/add' || current_act == 'video/edit') {
	$().ready(function(){
		if(current_act == 'video/edit') {
			$("#video_edit_form").bind('submit', function(){
				return video_check_data(this);
				//alert(this);return false;
			});
		}
		
		$("#pictype_auto").click(function(){
			$("#pic_upload").hide();
			$("#pic_url").hide();
			$("#pic_auto").show();
		});
		$("#pictype_upload").click(function(){
			$("#pic_auto").hide();
			$("#pic_url").hide();
			$("#pic_upload").show();
		});
		$("#pictype_url").click(function(){
			$("#pic_auto").hide();			
			$("#pic_upload").hide();
			$("#pic_url").show();
		});
		
		if(current_act == 'video/add') {
			textobj = document.getElementById('description');
			if($.browser.msie) {
				textobj.addBehavior('#default#userData');
			}
			$("#recover_data").click(function () {getData();return false;});
			$(window).bind('unload', function () {
				putData(textobj.value);
			});
		}
	});
} else if(current_act == 'video/manage') {
	$().ready(function(){
		$("#check_all").click(function(){
			check_all(this);
		});
		$("#user_photo").bind('error', function(){
			this.src = base_url + 'system/template/front/default/media/images/no_photo.jpg';
		});
	})
	
} else if(current_act == 'video/index') {
	$(function(){
		// 统计视频点击
		$.getJSON(front_url + "ajax/count_view/video/" + vid, function(result) {
			if(result.msg) {
				poc_alert(result.msg);
			}
		});
		
		// digg绑定
		$('[name="digg_border"]').click(function (event) {
			$.getJSON(front_url + "ajax/digg_video/" + vid, function(result) {
				if(result.msg) {
					poc_pop(result.msg, event);
				}
				if(result.success) {
					$('#digg_border').text(result.data.digcount);
					copy_url('purl', true);
				}
			});
		})
		
		// 收藏视频绑定
		$('#coll_video').click(function (event) {
			$.getJSON(front_url + "ajax/coll_video/" + vid, function(result){
				poc_pop(result.msg, event);
			});
		})
		
		// 添加专辑
		$('#add_playlist').click(function (event) {
			
			$.getJSON(front_url + 'ajax/add_playlist/' + vid, function(result){
				if(result.success) {
					poc_alert('', event);
					if(typeof($('#playlistid')[0]) != 'undefined') {
						return false;
					}
					var new_select = $('<select id="playlistid"></select>')[0];
					$.each(result.data, function(key, value){
						var new_option = new Option(value, key);
						if ($.browser.msie) {
							new_select.add(new_option); // IE only
						} else {
							new_select.add(new_option, null); // standards compliant
						}
						//var new_option = $('<option></option>')[0];
						//new_option.value = key;
						//new_option.text = value;
//						try {
//						    new_select.add($(new_option), null); // standards compliant
//						} catch(e) {
//						    new_select.add($(new_option)); // IE only
//						}
					});
					$('#ajaxMsgTxt').append($(new_select));
					$('<input type="button" value="+" />')
						.click(function(event){
							$.getJSON(front_url + 'ajax/add_playlist/' + vid + '/' + $('#playlistid')[0].options[$('#playlistid')[0].selectedIndex].value,function(result){
								poc_alert(result.msg, event);
							});
						})
						.appendTo($('#ajaxMsgTxt'));
				} else {
					poc_alert(result.msg, event);
				}
			});
		})
		
		// 评论设置
		load_comment_content('comment_list', vid);		
		$('#comment_form').bind('keydown', function(event) {
			if(event.ctrlKey && event.keyCode==13) {
				comment_submit(this, vid);			
				return false;
			}
		})
		$('#comment_form').bind('submit', function() {
			comment_submit(this, vid);			
			return false;
		})
		
		// 视频各项统计数据：观看次数、顶次数、评论数
		$.getJSON(front_url + 'ajax/video_statistic/' + vid, function(data) {
			$('#digg_border').text(data.digcount);
			$('#viewcount').text(data.viewcount);
		});
	})
}

