﻿var current_video_type = '';

if (typeof series != 'undefined') {
	function playnext(){
		window.location.href = next_video_url + '#swfplayer';
	}
	
	// 选择框对象
	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 + '&';

	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 == '') {
		alert("未填写视频引用地址")
		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;
	}
	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;
	}
	
	if(page == 1) {
		var dir_number = Math.ceil(vid / 1000);
		$.ajax({
			url: base_dir + 'system/cache/video_statistic/'+dir_number+'/' + vid + '_comment.js',
			cache: false,
			dataType: 'json',
			error: function(XHR, error) {
				if(XHR.status == '404') {
					$.getJSON(base_dir + 'index.php/ajax/generate_video_comment/' + vid, function(data){
						if(data.success) {
							$.getJSON(
								base_dir + 'system/cache/video_statistic/'+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/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>' +
			comment.username + '&nbsp;' +
			comment.date + '&nbsp;' + comment.time +
			'</dt>' +
			'<dd>' +
			comment.content +
			'</dd>' +
			'</dl>';
		});
		
		//alert(html);
		$('#' + comment_wrap_id).html(html + data.pagination);
	} else {
		$('#' + comment_wrap_id).html('<a href="#comment_form" style="padding-left:95px;">岂有此理，居然没人发表评论，我来说几句</a>');
	}
}

function set_statistic(data) {
	$('#digg_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(){
		// 统计视频点击
		//$.getJSON(base_dir + "index.php/ajax/count_view/video/" + vid, function(result) {});
		if(typeof total_is_off == undefined || total_is_off != '1') {
			$.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;
		})
		
		// 视频各项统计数据：观看次数、顶次数、评论数
		var dir_number = Math.ceil(vid / 1000);		
		$.ajax({
			url: base_dir + 'system/cache/video_statistic/'+dir_number+'/' + vid + '_statistic.js',
			cache: false,
			dataType: 'json',
			error: function(XHR, error) {
				if(XHR.status == '404') {
					$.getJSON(base_dir + 'index.php/ajax/video_statistic/' + vid, set_statistic);	
				}
			},
			success: set_statistic
		});
		
		//$.getJSON(base_dir + 'index.php/ajax/video_statistic/' + vid, function(data) {
			//$('#digg_border').text(data.digcount);
			//$('#viewcount').text(data.viewcount);
		//});
	})
}

