var current_video_type = '';

if (typeof series != 'undefined') {
	/** 专辑播放跳转
	 *
	 */
	function playnext(){
		window.location.href = next_video_url;
	}
}


/** 编辑视频用到的检测视频数据信息
 * 
 * @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;
	}
	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 + 'shared/views/default/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(
		site_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(
		site_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 +']';
}

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();
		});
		
	});
} else if(current_act == 'video/manage') {
	$().ready(function(){
		$("#check_all").click(function(){
			check_all(this);
		});
		$("#user_photo").bind('error', function(){
			this.src = base_url + 'front/views/default/media/images/no_photo.jpg';
		});
	})
	
} else if(current_act == 'video/index') {
	$().ready(function(){
		// 统计视频点击
		$.getJSON(site_url + "ajax/count_view/video/" + vid, function(result) {
			if(result.msg) {
				poc_alert(result.msg);
			}
		});
		
		// digg绑定
		$('[name="digg_border"]').click(function (event) {
			$.getJSON(site_url + "ajax/digg_video/" + vid, function(result) {
				if(result.msg) {
					poc_alert(result.msg, event);
				}
				if(result.success) {
					$('#digg_border').text(result.data.digcount);
					copy_url('purl', true);
				}
			});
		})
		
		// 收藏视频绑定
		$('#coll_video').click(function (event) {
			$.getJSON(site_url + "ajax/coll_video/" + vid, function(result){
				poc_alert(result.msg, event);
			});
		})
		
		// 添加专辑
		$('#add_playlist').click(function (event) {
			
			$.getJSON(site_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
						}
					});
					$('#ajaxMsgTxt').append($(new_select));
					$('<input type="button" value="+" />')
						.click(function(event){
							$.getJSON(site_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;
		})
	})
}

function upload_finish(vid){
	$.get(site_url + "ajax/upload_finish/" + vid, function(data){
	  if(data == 'true') {
	  	top.location = site_url + 'video/upload/' + vid + '?status=upload_finish';
	  }
	});
}