$(document).ready(function(e) {
	// 计算间距
	var m = Math.ceil($('#fileContent > strong').outerHeight()/30.6);
	if(m > 1) $('#fileContent').css('margin',90-((m-1)*20)+'px auto');
});

// --- 下载 ---
function downurlGet(id) {
	$('#downurlGetMask').css({'left':$(window).width()/2-$('#downurlGetMask').outerWidth()/2+'px','top':$(window).height()/2-$('#downurlGetMask').outerHeight()/2+$(window).scrollTop()+'px','opacity':'0'}).show().animate({opacity:'.7'}, 200, function() {
		$.ajax({
			type:'POST',
			cache:false,
			data:{id:id,'do':'downurl'},
			url:'ajax.php?ac=tokenget',
			success: function(j) {
				j = $.parseJSON(j);
				if(j.result == '1') {
					if(document.all) {
						var linka = document.createElement('a');
						linka.href = j.url;
						document.body.appendChild(linka);
						linka.click();
					} else {
						window.location.href = j.url;
					}
				} else {
					alert(j.msg);
				}
				$('#downurlGetMask').fadeOut(200);
			}
		});
	});
}

PREVIEWED = false;
function previewGet(id) {
	if(PREVIEWED===false) {
		$('#previewGetMask').css({'left':$(window).width()/2-$('#downurlGetMask').outerWidth()/2+'px','top':$(window).height()/2-$('#downurlGetMask').outerHeight()/2+$(window).scrollTop()+'px','opacity':'0'}).show().animate({opacity:'.7'}, 200, function() {
			$.ajax({
				type:'POST',
				cache:false,
				data:{id:id,'do':'preview'},
				url:'ajax.php?ac=tokenget',
				success: function(j) {
					j = $.parseJSON(j);
					if(j.result == '1') {
						PREVIEWED = true;
						if(FILE_EXT == 'txt') {
							$('#previewTxt').show();
							$.ajax({
								type:'GET',
								cache:false,
								dataType:'jsonp',
								url:j.url+'&encode=1&cb=?',
								success: function(j) {
									$('#previewGetMask').fadeOut(200);
									$('#mainView').fadeOut(200, function() {
										$('#previewView').fadeIn(200);
									});
									$('#previewTxt').html(j.text.replace(/\r\n/g,'\n').replace(/\r/g,'\n').replace(/\n/g,'<br />'));
								}
							});
						} else if(FILE_EXT == 'jpg' || FILE_EXT == 'gif' || FILE_EXT == 'jpeg' || FILE_EXT == 'png') {
							$('#previewPic').show();
							$('#previewPic>img').attr('src', j.url);
							$('#previewGetMask').fadeOut(200);
							$('#mainView').fadeOut(200, function() {
								$('#previewView').fadeIn(200);
							});
							// --- 绑定事件 ---
							$('#previewPic').on('mousemove', function(e) {
								if($('#previewPic>img').width() > $('#previewPic').width()) {
									var x = e.pageX - $('#previewPic').offset().left;
									var y = e.pageY - $('#previewPic').offset().top;
									var rw = $('#previewPic>img').width() - $('#previewPic').width();
									var px = x / $('#previewPic').width();
									var rh = $('#previewPic>img').height() - $('#previewPic').height();
									var py = y / $('#previewPic').height();
									$('#previewPic>img').css({'left':'-'+(rw*px)+'px','top':'-'+(rh*py)+'px'});
								}
							});
						} else if(FILE_EXT == 'mp4' || FILE_EXT == 'flv' || FILE_EXT == 'f4v') {
							$.getScript('exts/FAVideo/favideo.js', function() {
								FAVideo.init('exts/FAVideo', 'previewVideo', function() {
									FAVideo.load(j.url);
								});
								$('#previewVideo').show();
								$('#previewGetMask').fadeOut(200);
								$('#mainView').fadeOut(200, function() {
									$('#previewView').fadeIn(200);
								});
							});
						} else if(FILE_EXT == 'mp3') {
							$.getScript('exts/ECJSound/ecjsound.min.js', function() {
								$.getScript('skins/common/images/jquery-ui.min.js', function() {
									$('#previewSoundControlPlay').on('mouseenter', function() {
										$(this).css({'background-position':$(this).attr('play')=='0'?'0 -30px':'-50px -30px'});
									}).on('mouseleave', function() {
										$(this).css({'background-position':$(this).attr('play')=='0'?'0 0':'-50px 0'});
									}).on('mousedown', function() {
										$(this).css({'background-position':$(this).attr('play')=='0'?'0 -60px':'-50px -60px'});
									}).on('mouseup', function() {
										$(this).css({'background-position':$(this).attr('play')=='0'?'0 -30px':'-50px -30px'});
									}).on('click', function() {
										if($(this).attr('play')=='0') {
											$(this).attr('play', '1');
											$(this).css({'background-position':'-50px -30px'});
											ECJSound.play();
										} else {
											$(this).attr('play', '0');
											$(this).css({'background-position':'0 -30px'});
											ECJSound.pause();
										}
									});
									$('#previewSoundControlVolume .slider').draggable({
										axis: 'x',
										drag: function(e, ui) {
											if(ui.position.left < -4)
												ui.position.left = -4;
											if(ui.position.left > 46)
												ui.position.left = 46;
											var vol = (ui.position.left + 4) / 50;
											ECJSound.setVolume(vol);
										}
									});
									$('#previewSoundControlSlide').on('mousedown', function(e) {
										var p = $(this).attr('totalMsec') * ((e.pageX - $(this).offset().left) / $('#previewSoundControlSlide').width());
										ECJSound.setPosition(p);
										if($('#previewSoundControlPlay').attr('play')=='0') {
											$('#previewSoundControlPlay').attr('play', '1');
											$('#previewSoundControlPlay').css({'background-position':'-50px 0'});
										}
									});
									ECJSound.init('exts/ECJSound', function() {
										ECJSound.load(j.url);
										ECJSound.onPlaying = function(o) {
											$('#previewSoundControlInfo .left').text(o.nowTime);
											$('#previewSoundControlInfo .right').text(o.totalTime);
											$('#previewSoundControlInline').css({'width':o.per+'%'});
											$('#previewSoundControlSlide').attr('totalMsec', o.totalMsec);
										};
										ECJSound.onSoundComplete = function() {
											$('#previewSoundControlPlay').attr('play', '0').css('background-position', '0 0');
										};
										ECJSound.play();
										$('#previewSound').show();
										$('#previewGetMask').fadeOut(200);
										$('#mainView').fadeOut(200, function() {
											$('#previewView').fadeIn(200);
											$('#previewSoundScroll').css({'top':$('#previewSound').outerHeight()/2-34-$('#previewSoundScroll').outerHeight()/2+'px'});
										});
									});
								});
							});
						}
					} else {
						alert(j.msg);
					}
				}
			});
		});
	} else {
		$('#mainView').fadeOut(200, function() {
			if(FILE_EXT == 'mp3') {
				ECJSound.play();
				$('#previewSoundControlPlay').attr('play', '1').css('background-position', '-50px 0');
			}
			$('#previewView').fadeIn(200);
		});
	}
}

function previewBack() {
	$('#previewView').fadeOut(200, function() {
		if(FILE_EXT == 'mp3') {
			ECJSound.stop();
		} else if(FILE_EXT == 'mp4' || FILE_EXT == 'flv' || FILE_EXT == 'f4v') {
			FAVideo.pause();
		}
		$('#mainView').fadeIn(200);
	});
}