var player;
var currentIndex = 0;
var params = {'allowfullscreen': 'true', 'allowscriptaccess': 'always', 'bgcolor': '#000000'};
var attributes = {'id':'yinyuetaiplayer', 'name':'yinyuetaiplayer'};
var hiddenMvs = {};
var showHiddenMvs = false;
var sroll = {
	initialize: function() {
		var _this = this;
		this.mvlist = $('mvlist');
		this.items = $$('#mvlist>li');
		this.mylist_width = this.items.length * 122;
		this.mvlist.setStyle('width', this.mylist_width);
		this.mylist_box_width = this.mvlist.getParent().getSize().x;
		this.mvlistFx = new Fx.Tween(_this.mvlist, {
			fps: 50,
			duration: '800'
		});
		this.des = this.mvlist.getParent('div.wrap').getElement('span.des');
		this.desFx = new Fx.Tween(this.des, {
			fps: 50,
			duration: '500'
		});
		this.tasklist = new taskList(800);
		this.mylist_target_list = 0;
	},
	bind: function() {
		var _this = this;
		this.items.each(function(item, index) {
			var myFx = new Fx.Tween(item.getElement('a'), {
				fps: 50,
				duration: '150'
			});
			item.addEvents({
				'mouseenter': function() {
					var distance = item.getPosition(item.getParent('div.wrap')).x;
					var distance2 = item.getPosition(item.getParent('div.mvlistarea')).x;
					var distance3 = item.getPosition(_this.mvlist).x;
					distance = distance2 > 158 && distance3 > 0 ? distance - 158 : distance + 110;
					myFx.cancel();
					myFx.start('margin-top', -67);
					if (item.getElement('a').get('title')) {
//						_this.des.set('html', item.getElement('a').get('title'));
						_this.des.set('html', item.getElement('span[name=description]').get('html'));
						_this.desFx.cancel();
						_this.desFx.set('opacity', 0);
						_this.desFx.set('left', distance);
						_this.desFx.start('opacity', 1);
					}
					distance = distance2 = distance3 = null;
				},
				'mouseleave': function() {
					myFx.cancel();
					myFx.start('margin-top', 0);
					_this.desFx.cancel();
					_this.desFx.set('opacity', 0);
				},
				'click': function() {
					playItem(index);
				}
			})
		});
		if (this.mylist_width > this.mylist_box_width) {
			this.check_status();
		}
	},
	check_status:function() {
		var _this = this;
		if (this.mylist_target_list == 0) {
			this.tasklist.empty();
			$('mvlist_next').removeEvents().set('class', 'default').addEvent('click', function() {
				this.blur();
				return false;
			});
		} else {
			$('mvlist_next').set('class', 'allow');
			$('mvlist_next').removeEvents().addEvents({
				'mouseenter':function() {
					this.addClass('hover');
				},
				'mouseleave':function() {
					this.removeClass('hover');
				},
				click:function() {
					_this.tasklist.addFn(function() {
						_this.next();
					});
					this.blur();
					return false;
				}
			});
		}
		if ((this.mylist_box_width - this.mylist_width) == this.mylist_target_list) {
			this.tasklist.empty();
			$('mvlist_prev').removeEvents().set('class', 'default').addEvent('click', function() {
				this.blur();
				return false;
			});
		} else {
			$('mvlist_prev').set('class', 'allow');
			$('mvlist_prev').removeEvents().addEvents({
				'mouseenter':function() {
					this.addClass('hover');
				},
				'mouseleave':function() {
					this.removeClass('hover');
				},
				'click':function() {
					_this.tasklist.addFn(function() {
						_this.prev();
					});
					this.blur();
					return false;
				}
			});
		}
		//$('mvlist_next').removeEvents().set('class','default');
		//$('mvlist_prev').set('class','default');
	},
	tween:function(index) {
		if (index >= 0) {
			var col = Math.floor(index / 7);
			var distance = this.mylist_width - this.mylist_box_width;
			if (distance > 0) {
				distance = distance > 854 * col ? 854 * col : distance;
				this.mylist_target_list = 0 - distance;
			}
		}
		this.mvlistFx.start('left', this.mylist_target_list);
		this.check_status();
	},
	prev:function() {
		var mylist_left = this.mvlist.getStyle('left').toInt();
		var distance = this.mylist_width + mylist_left - this.mylist_box_width;
		if (distance > 0) {
			distance = distance > 854 ? 854 : distance;
			this.mylist_target_list = mylist_left - distance;
			this.mvlistFx.cancel();
			this.tween();
		}
		return false;
	},
	next:function() {
		var mylist_left = this.mvlist.getStyle('left').toInt();
		if (mylist_left < 0) {
			mylist_left = mylist_left < -854 ? mylist_left + 854 : 0;
			this.mylist_target_list = mylist_left;
			this.mvlistFx.cancel();
			this.tween();
		}
		return false;
	}
};
var taskList = new Class({
	initialize: function(delayTime) {
		this.delayTime = delayTime;
		this.fnList = [];
		this.flag = true;
	},
	addFn: function(fn) {
		this.fnList.push(fn);
		this.run();
	},
	run: function() {
		if (this.fnList.length && this.flag) {
			this.flag = false;
			this.fnList[0]();
			this.fnList.shift();
			var _this = this;
			setTimeout(function() {
				_this.flag = true;
				_this.run();
			}, _this.delayTime);
		}
	},
	empty:function() {
		this.fnList = [];
	}
});
function tabToggle(arr, arr2, type) {
	type = type || 'mouseenter';
	arr.each(function(item, index) {
		if (item.hasClass('current')) {
			arr2[index].setStyle('display', 'block');
		} else {
			arr2[index].setStyle('display', 'none');
		}
		item.addEvent(type, function() {
			arr2.setStyle('display', 'none');
			arr2[index].setStyle('display', 'block');
			arr.removeClass('current');
			item.addClass('current');
			item.blur();
			return false;
		})
	})
}
function playerReady(obj) {
	player = document.getElementById(obj['id']);
}
/**
 * 悦单播放器播放某视频时触叄1�7
 * @param itemIndex 视频在悦单中的index,悦单的第丄1�7个视预1�7 itemIndex丄1�7
 */
function jsPlayListItem(itemIndex) {
	if (sroll.mvlist) {
		currentIndex = itemIndex;
		updateNowPlaying(itemIndex);
		$("mvlist").getElements("li").each(function (item, index) {
			if (itemIndex == index) {
				item.addClass("current");
			} else {
				item.removeClass("current");
			}
		});
	}
}
function playItem(itemIndex) {
	//			try {
	getPlayer().playlistItem(itemIndex);
	//			} catch(e) { }
	sroll.tween(itemIndex);
}
function getPlayer() {
	if (window.document['yinyuetaiplayer']) {
		return window.document['yinyuetaiplayer'];
	} else {
		if (navigator.appName.indexOf("Microsoft") == -1) {
			if (document.embeds && document.embeds['yinyuetaiplayer']) {
				return document.embeds['yinyuetaiplayer'];
			}
		}
	}
	return document.getElementById('yinyuetaiplayer');
}
function updateNowPlaying(itemIndex) {
	try {
		var oneMv = mvList[itemIndex];
		var nowPlaying = $("nowPlaying");
		nowPlaying.href = "/video/" + oneMv.getVideoId();
		nowPlaying.set("html", oneMv.getTitle());
		var nowPlaying_artist = $('nowPlaying_artist');
		nowPlaying_artist.set('html', oneMv.getArtistName());
		nowPlaying_artist.href = '/fan/index.do?fid=' + oneMv.getArtistId();
	} catch(e) { }
}
function playPrevItem() {
	if (currentIndex > 0) {
		playItem(currentIndex - 1);
	}
}
function playNextItem() {
	if (currentIndex < mvList.length - 1) {
		playItem(currentIndex + 1);
	}
}
function jsLight(flag) {
	if ($('flash_overlay') && flag == 'off') {
		$('flash_overlay').setStyle('display', 'block');
		$('flash_overlay').tween('opacity', '0', '1');
		$('playArea').getElement('div').setStyles({
			'z-index':100
		});
		window.onkeydown = function(event) {
			event = new Event(event);
			if (event.key == 'esc') {
				getPlayer().light('off');
				jsLight('on');
				this.onkeydown = null;
			}
		};
	} else {
		if ($('flash_overlay') && flag == 'on') {
			$('flash_overlay').tween('opacity', '1', '0');
			setTimeout(function() {
				$('flash_overlay').setStyle('display', 'none');
			}, 300);
			$('playArea').getElement('div').setStyles({
				'z-index':1
			})
		} else {
			if (!$('flash_overlay') && flag == 'off') {
				new Element('div').setStyles({
					height:window.getScrollSize().y,
					opacity:0
				}).set('id', 'flash_overlay').set('html', '<span class="close_button">关闭</span>').inject(document.body);
				$('flash_overlay').getElement('span').addEvents({
					'click':function() {
						getPlayer().light('off');
						jsLight('on');
					},
					mouseover:function() {
						this.addClass('hover');
					},
					mouseout:function() {
						this.removeClass('hover');
					}
				});
				jsLight('off');
			}
		}
	}
	return false;
}
function jsComment() {
	getPlayer().light('off');
	jsLight('on');
	if ($('subNavTriggers')) {
		$('subNavTriggers').getElement('a').fireEvent('click');
	}
	showCommentForm();
	executeInProgress(function(num) {
		window.scrollTo(window.getScroll().y, num);
	}, $('main').getPosition().y, window.getScroll().y)
}
var setcopy_gettext = function(id, name) {
	try {
		var clipboardswfdata = document.getElementById(id).value;
		window.document[name].SetVariable('str', clipboardswfdata);
	} catch (e) {
	}
};
var floatwin = function() {
	alert('代码已经复制到粘贴板，您可以使用 Ctrl+V 将内容粘贴到需要的地方去了');
};
function toggle_sidebar_content(containerId) {
	if ($(containerId)) {
		var linkArray = $(containerId).getElements("div.mod_toggle a");
		var contentArray = $(containerId).getElements("div.toggle_cont");
		if (linkArray.length) {
			linkArray.each(function() {
				tabToggle(linkArray, contentArray);
			});
		}
	}
}
function showCommentForm(commentMaxLength) {
	$('comment_cp_open').tween('opacity', '0');
	$('comment_cp').setStyles({
		'display':'block',
		'height':'120px'
	});
	$('editor').focus();
	window.editorcheck = checkTextareaChar('editor', 'commentContentLength', commentMaxLength);
}
function hideCommentForm() {
	$('comment_cp').tween('height', '0');
	setTimeout(function() {
		$('comment_cp').setStyle('display', 'none');
	}, 500);
	$('comment_cp_open').tween('opacity', '1');
	$('commentForm')['content'].value = "";
	$('commentFormHint').set('html', "");
	clearInterval(window[editorcheck]);
}
function flashPlayerEventFunc(obj) {
	switch (obj[type]) {
		case "mvplayerViewCinemaModel": // 影院模式
			break;
		case "mvplayerViewFullscreen": // 全屏
			break;
		case "mvplayerReady": // 视频准备完毕
			break;
		case "mvplayerPlaylistItem": // 视频列表
			break;
		case "mvplayerViewNext": // 下一视频
			break;
		case "mvplayerViewPrev": // 上一视频
			break;
	}
}
function toggleMoreRelatedMvs(videoId) {
	var moreRelatedMvsLink = $('moreRelatedMvsLink');
	if (moreRelatedMvsLink) {
		if (!moreRelatedMvsLink.getProperty('ajaxFlag')) {
			moreRelatedMvsLink.setProperty('ajaxFlag', true);
			$('relatedMvs').innerHTML += '<div class="ajax_loading"></div>';
			new Request({
				url:'/mv/ajax/video_more_related?videoId=' + videoId,
				method:'get',
				onSuccess:function(responseText) {
					$$("#relatedMvs>div").dispose();
					$("relatedMvs").innerHTML += responseText;
					hiddenMvs = $("relatedMvs").getElements("li[class=hidden]");
					toggleMoreRelatedMvs(videoId);
				}
			}).send();
			return;
		}
		if (showHiddenMvs) {
			hiddenMvs.each(function (item) {
				item.removeClass("show");
				item.addClass("hidden");
			});
			showHiddenMvs = false;
			moreRelatedMvsLink.removeClass("ico_stats_open");
			moreRelatedMvsLink.addClass("ico_stats_close");
			moreRelatedMvsLink.set("html", "展开更多");
		} else {
			hiddenMvs.each(function (item) {
				item.removeClass("hidden");
				item.addClass("show");
			});
			showHiddenMvs = true;
			moreRelatedMvsLink.removeClass("ico_stats_close");
			moreRelatedMvsLink.addClass("ico_stats_open");
			moreRelatedMvsLink.set("html", "收起列表");
		}
	}
}
function setMainVideo(userId, videoId, artistName, videoTitle) {
	if (!check_login(userId)) {
		return;
	}
	var requestUrl = "/mv/set-main-video";
	var params = "videoId=" + videoId;
	new Request.JSON({
		url: requestUrl,
		method : 'post',
		data : params,
		onSuccess : function(result) {
			if (!result.error) {
				successMessage("OK啦，您已经把&nbsp;&nbsp;" + artistName + " - " + videoTitle + " 设为个人主页的主打歌!")
			} else {
				errorMessage("该曲目已经是您的个人主页主打歌了:)");
			}
		}
	}).send();
}
function addFavorite(userId, videoId, artistName, videoTitle) {
	if (!check_login(userId)) {
		return;
	}
	var requestUrl = "/mv/add-favorite";
	var params = "videoId=" + videoId;
	new Request.JSON({
		url: requestUrl,
		method : 'post',
		data : params,
		onSuccess : function(result) {
			if (!result.error) {
				$("totalFavorites").set("html", 1 * $("totalFavorites").get("html") + 1);
				successMessage("OK啦，您已经把&nbsp;&nbsp;" + artistName + " - " + videoTitle + " 添加到你的收藏夹")
			} else {
				errorMessage("忘了吧，你已经收藏过该曲目了:)");
			}
		}
	}).send();
}
function showRecommend(userId, recommendMinLength, recommendMaxLength, recommendCredit) {
	if (!check_login(userId)) {
		return;
	}
	var popup = showBoxRelative('推荐MV', 'videoRecommendDiv', 'media_cp', 396, 40);
	var form = popup.getElement("form");
	var span = $('TB_ajaxContent').getElement('span');
	var checkText = checkTextareaChar(form['content'], span, recommendMaxLength);
	form.ajaxJSON({
		onRequest: function() {
			var field = form['content'];
			var length = field.value.trim().length;
			if (length == 0) {
				errorMessage("请输入您的推荐语");
				field.focus();
				return false;
			}
			if (length < recommendMinLength) {
				errorMessage("推荐语应至少有" + recommendMinLength + "个字");
				field.focus();
				return false;
			}
			if (length > recommendMaxLength) {
				errorMessage("推荐语不能超过" + recommendMaxLength + "个字");
				field.focus();
				return false;
			}
			return true;
		},
		onComplete: function(action) {
			if (!action.error) {
				TB_remove();
				$("totalRecommends").set("html", 1 * $("totalRecommends").get("html") + 1);
				successMessage("有付出的推荐更有可读性，感谢分享，扣" + recommendCredit + "分。");
			} else {
				errorMessage(action.message);
			}
		}
	});
}
function checkComment(commentForm, commentMinLength, commentMaxLength) {
	var field = commentForm['content'];
	var length = field.value.trim().length;
	if (length == 0) {
		$('commentFormHint').set('html', "请输入评论内容");
		field.focus();
		return false;
	}
	if (length < commentMinLength) {
		$('commentFormHint').set('html', "评论应至少有" + commentMinLength + "个字");
		field.focus();
		return false;
	}
	if (length > commentMaxLength) {
		$('commentFormHint').set('html', "您的评论有点长哦，精简一下吧，请控制在" + commentMaxLength + "字以内");
		field.focus();
		return false;
	}
	$('commentFormHint').set('html', "");
	return true;
}

