﻿var current_video_type = '';

if (typeof series != 'undefined') {
	function playnext(){
		window.location.href = next_video_url;
	}
	
	// 选择框对象
	var SellerScroll = function(options){
		this.SetOptions(options);
		this.lButton = this.options.lButton;
		this.rButton = this.options.rButton;
		this.oList = this.options.oList;
		this.showSum = this.options.showSum;
		
		this.iList = $("#" + this.options.oList + " > li");
		this.iListSum = this.iList.length;
		this.iListWidth = this.iList.outerWidth(true);
		this.moveWidth = this.iListWidth * this.showSum;
		
		this.dividers = Math.ceil(this.iListSum / this.showSum);	//共分为多少块
		this.moveMaxOffset = (this.dividers - 1) * this.moveWidth;
		
		this.LeftScroll();
		this.RightScroll();
		this.SetCurrent();
	};
	SellerScroll.prototype = {
		SetOptions: function(options){
			this.options = {
				lButton: "left_scroll",
				rButton: "right_scroll",
				oList: "carousel_ul",
				showSum: 7	//一次滚动多少个items
			};
			$.extend(this.options, options || {});
		},
		SetCurrent: function() {
			var current_index = 0;
			$.each(this.iList, function(i) {
				if($(this).hasClass('playing')) {
					current_index = i;
					return;
				}
			});
			if(current_index > 0) {
				$("#" + this.oList).css({ left: -this.moveWidth * Math.floor(current_index / this.options.showSum) });
			}
			
		},
		ReturnLeft: function(){
			return isNaN(parseInt($("#" + this.oList).css("left"))) ? 0 : parseInt($("#" + this.oList).css("left"));
		},
		LeftScroll: function(){
			if(this.dividers == 1) return;
			var _this = this, currentOffset;
			$("#" + this.lButton).click(function(){
				currentOffset = _this.ReturnLeft();
				if(currentOffset == 0){
					$("#" + _this.oList + ":not(:animated)").animate( { left: "+=" + _this.moveWidth }, { duration: "slow", complete: function(){
						for(var j = _this.showSum + 1; j <= _this.iListSum; j++){
							$(_this.iList[_this.iListSum - j]).prependTo($("#" + _this.oList));
						}
						$("#" + _this.oList).css({ left: -_this.moveWidth * (_this.dividers - 1) });
					} } );
				}else{
					$("#" + _this.oList + ":not(:animated)").animate( { left: "+=" + _this.moveWidth }, "slow" );
				}
			});
		},
		RightScroll: function(){
			if(this.dividers == 1) return;
			var _this = this, currentOffset;
			$("#" + this.rButton).click(function(){
				currentOffset = _this.ReturnLeft();
				if(Math.abs(currentOffset) >= _this.moveMaxOffset){
					
					$("#" + _this.oList + ":not(:animated)").animate( { left: "-=" + _this.moveWidth }, { duration: "slow", complete: function(){
						for(var j = _this.showSum; j < _this.iListSum; j++){
							$(_this.iList[j]).appendTo($("#" + _this.oList));
						}
						$("#" + _this.oList).css({ left: 0 });
					} } );
				}else{
					$("#" + _this.oList + ":not(:animated)").animate( { left: "-=" + _this.moveWidth }, "slow" );
				}
			});
		}
	};
	
	$().ready(function(){
		var ff = new SellerScroll();
	});
}

/** 
 * 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 + '&';

  	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_radio_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, vid) {
	if(theForm.content.value == '') {
		poc_alert('内容不能为空!');
		theForm.content.focus;
		return false;
	}
	
	$.ajax({
		type: "POST",
		url: base_dir + "index.php?r=ajax/add_video_comment/" + vid,
		dataType: "json",
		data: 'content=' + theForm.content.value,
		success: function(result) {
			if(result.success) {
				theForm.content.value = '';
				if(/^\d+$/.test(result.msg)) {
					$('#commentcount').html(result.msg);
					load_comment_content('comment_list', vid);
				} else {
					poc_alert(result.msg);
				}
			} else {
				poc_alert(result.msg);
			}
		}
	});
	return true;
}

function load_comment_content(comment_wrap_id, vid, page) {
	
	if(typeof(page) != 'number') {
		page = 1;
	}
	
	if(page == 1) {
		var dir_number = Math.ceil(vid / 1000);
		$.ajax({
			url: base_dir + 'system/cache/comment/'+dir_number+'/' + vid + '_comment.js',
			cache: false,
			dataType: 'json',
			error: function(XHR, error) {
				if(XHR.status == '404') {
					$.getJSON(base_dir + 'index.php?r=ajax/generate_video_comment/' + vid, function(data){
						if(data.success) {
							$.getJSON(
								base_dir + 'system/cache/comment/'+dir_number+'/' + vid + '_comment.js', function(data){
									set_comment_content(comment_wrap_id, data);
								}		
							);
						}
					});	
				}
			},
			success: function(data){
				set_comment_content(comment_wrap_id, data);
			}
		});
	} else {
		$.getJSON(
			base_dir + 'index.php?r=ajax/list_video_comment/' + vid + '/' + page, function(data){
				set_comment_content(comment_wrap_id, data);
			}		
		);
	}
}

function set_comment_content(comment_wrap_id, data) {
	if (data) {
		var html = '';
		
		$.each(data.comment, function(key, comment){
			html += '<dl class="commentlist">' +
			'<dt id="comment_author_'+key+'">' +
			comment.username + '&nbsp;' +
			comment.date + '&nbsp;' + comment.time +
			'</dt>' +
			'<dd>' +
			'<span id="comment_content_'+key+'">'+ comment.content + '</span>' + 
			'<span class="reply_btn"><a href="javascript:;" onclick="reply_comment('+key+')">回应此评论</a></span>' +
			'</dd>' +
			'</dl>';
		});
		
		//alert(html);
		$('#' + comment_wrap_id).html(html + data.pagination);
	} else {
		$('#' + comment_wrap_id).html('暂时没有评论');
	}
}

function reply_comment(id) {
	var content = $('#comment_content_'+id).html()
		.replace(/<span[^>]*>[\s\S]*?<\/span>/gi, '')
		.replace(/<img[^>]*?src="[^>]*?\/(\d+).gif"[^>]*?>/gi, '[PS$1]')
		.replace(/<br>/ig, '')
		.replace(/^\n*(.*?)\n*$/, '$1');
	var author = $('#comment_author_'+id).html().replace(/&nbsp;/ig, ' ')+' 写到:';
	$('#comment_form > #textarea').focus().val('[quote]' + author + '\n' + content + '[/quote]\n');
	
	return false;
}

function set_statistic(data) {
	$('#dig_border').text(data.digcount);
	$('#viewcount').text(data.viewcount);
}

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(){
		// 统计视频点击
		if(typeof misc_statistic_update == "undefined" || misc_statistic_update != '1') {
			$.getJSON(base_dir + "index.php?r=ajax/count_view/video/" + vid, function(result) {});
		}
		
		// dig绑定
		$('[name="dig_border"]').click(function (event) {
			$.getJSON(base_dir + "index.php?r=ajax/dig_video/" + vid, function(result) {
				if(result.msg) {
					poc_pop(result.msg, event);
				}
				if(result.success) {
					$('#dig_border').text(result.data.digcount);
					copy_url('purl', true);
				}
			});
		})
		
		// 收藏视频绑定
		$('#collect_video').click(function (event) {
			$.getJSON(base_dir + "index.php?r=ajax/collect_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;
		})
		
		// 视频各项统计数据：观看次数、顶次数、评论数
		var dir_number = Math.ceil(vid / 1000);		
		$.ajax({
			url: base_dir + 'attachments/video_statistic/'+dir_number+'/' + vid + '_statistic.js',
			cache: false,
			dataType: 'json',
			error: function(XHR, error) {
				if(XHR.status == '404') {
					$.getJSON(base_dir + 'index.php?r=ajax/video_statistic/' + vid, set_statistic);	
				}
			},
			success: set_statistic
		});
		
	})
}

