﻿var current_video_type = '';

/** 联盟传递的视频id 和时间
 * 
 * @param {string} ccvid
 * @param {string} vtime
 */
function videoPlay(ccvid, vtime) {
	if(!ccvid || !vtime) {
		return false;
	}
	$.get(base_dir + "index.php/ajax/set_video_info/" + ccvid + "/" + vtime);
}

if (typeof series != 'undefined') {
	function playnext(){
		window.location.href = next_video_url;
	}
}

/** 
 * js获取表单内容，返回一个对象
 */
function get_video_meta() {
	var returnStr = '';
	var video_add_form = document.getElementById("video_add_form");
	
	if(video_add_form.filename.value == '') {
		alert("未选择上传视频")
		return false;
	}
	
	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 = video_add_form.type.value;
	
  	if(type == 1){
  		returnStr += 'isup=' + 1 + '&';
  	}
	
	returnStr += 'share=' + 1;

	//alert(returnStr);return false;
	return returnStr;
}

/** 
 * 引用触发
 */
function quote_submit() {
	
	var video_add_form = document.getElementById("video_add_form");
	
	if(video_add_form.videourl.value.isEmpty()) {
		alert("视频地址不能为空");
		video_add_form.videourl.focus();
		return false;
	}
	if(!video_add_form.videourl.value.isURL()) {
		alert("视频地址不是一个合法的url地址");
		video_add_form.videourl.focus();
		return false;
	}
	
	if(!video_check_data(video_add_form)) {
		return false;
	}
	
	video_add_form.submit();
}

/** 
 * flash触发
 */
function upload_finish() {
	
	var video_add_form = document.getElementById("video_add_form");
	
	if(video_add_form.videourl.value == '') {
		alert("视频未上传")
		return false;
	}
	
	video_add_form.submit();
}


/** 检测视频数据信息
 * 
 * @param {Object} video_add_form
 */
function video_check_data(video_add_form) {
	if(video_add_form.channelsub.value == '') {
		alert("请选择频道！")
		video_add_form.channelsub.focus()
		return false;
	}
	if(video_add_form.title.value == '') {
		alert("请填写标题！")
		video_add_form.title.focus()
		return false;
	}
	if(video_add_form.tags.value == '') {
		alert("请填写标签！")
		video_add_form.tags.focus()
		return false;
	}
	
	if(typeof $('#editPicToggle')[0] == 'undefined' 
		|| typeof $('#editPicToggle:hidden')[0] == 'undefined') {
		var pic_type = get_redio_value('pic_type');	
		if(pic_type == 3) {		
			if(!video_add_form.pic_url.value.isURL()) {
				alert("网络地址不是一个合法的url地址");
				video_add_form.pic_url.focus()
				return false;
			}
		}
	}
	
	return true;
}

function set_source_id(source_id) {
	try {
		document.getElementById('video_add_form').videourl.value = source_id;
		return true;
	} catch(e) {
		return false;
	}	
}

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(
		base_dir + "index.php/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(
		base_dir + 'index.php/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 +']';
}

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);
			});
		}
		
		$("#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();
		});
		
	});
} else if(current_act == 'video/manage') {
	$().ready(function(){
		$("#check_all").click(function(){
			check_all(this);
		});
	})
	
} else if(current_act == 'video/index') {
	$(function(){
		// 统计视频点击
		$.getJSON(base_dir + "index.php/ajax/count_view/video/" + vid, function(result) {
		});
		
		// digg绑定
		$('[name="digg_border"]').click(function (event) {
			$.getJSON(base_dir + "index.php/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(base_dir + "index.php/ajax/coll_video/" + vid, function(result){
				poc_pop(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(base_dir + 'index.php/ajax/video_statistic/' + vid, function(data) {
			$('#digg_border').text(data.digcount);
			$('#viewcount').text(data.viewcount);
		});
	})
}
