﻿var showImgsCount = 0;
var showImgPageCount = 0;
var showImgCurrentPage = 0;

var showImgWrapperWidth = 880;
var showImgWidth = 420;
var showImgHeight = 560;
var showImgWidth_Big = 860;

// o为DOM对象
function popupShowInit(o, id, sid, src)
{
	var popupShow = $("#popupShow");
	var itemName = $(".itemName", popupShow);
	var showWrapper = $(".showWrapper", popupShow);
	var showInnerWrapper = $(".showInnerWrapper", showWrapper);

	// 显示层
	var popupMask = $("#popupMask");
	var loading = $("#loading");
	popupMask.show();
	loading.show();

	// ajax加载数据
	$.ajax({
		type: "GET",
		url: "GetListData.aspx",
		data: "id=" + id + "&sid=" + sid,
		error: function ()
		{
			alert("数据加载失败或超时！");
			popupMask.hide();
			loading.hide();
		},
		success: function (text)
		{
			popupShowBuildList($(o).text(), text);
			loading.hide();
			popupShow.show();
			if (src)
				popupShowLocat(src);
		}
	});
}

function popupShowBuildList(title, text)
{
	var popupShow = $("#popupShow");
	var itemName = $(".itemName", popupShow);
	var showWrapper = $(".showWrapper", popupShow);
	var showInnerWrapper = $(".showInnerWrapper", showWrapper);

	showInnerWrapper.empty();
	var datas = text.split("\r\n");
	// 由于服务器返回的数据中最后会有一个空行，所以要去掉
	// 因此 i < datas.length - 1
	for (var i = 0; i < datas.length - 1; i += 2)
	{
		var url = datas[i];
		var isbig = datas[i + 1] == "0" ? false : true;
		var src = imgbasepath + "/" + url;
		var div = $("<div>");
		var img = $("<img>");
		img.attr({
			src: src,
			"class": "img",
			style: "width: " + showImgWidth + "px; height: " + showImgHeight + "px"
		});
		if (isbig)
		{
			img.addClass("bigImg");
			img.width(showImgWidth_Big);
		}
		img.data("src", src);
		div.append(img);
		div.addClass("imgWrapper");
		showInnerWrapper.append(div);
	}
	// 设置图片列表
	showInnerWrapper.append("<div class=\"clear\">");
	// 设置标题
	itemName.text(title);
	// 设置项数
	showImgsCount = $(".img", showInnerWrapper).length;
	// 计算页数
	var a = $(".bigImg", showInnerWrapper).length;
	var b = showImgsCount - a;
	var c = parseInt(b / 2);
	var d = (b % 2 == 0 ? 0 : 1);
	showImgPageCount = a + c + d;
	// 重置当前页
	showImgCurrentPage = 1;
	popupShow.data("isFirst", "1");
	// 设置包装器宽度
	showInnerWrapper.css("width", showImgPageCount * showImgWrapperWidth);
}

function popupShowScroll(direction)
{
	var popupShow = $("#popupShow");
	var showWrapper = $(".showWrapper", popupShow);
	if (direction)
	{
		// next
		if (showImgCurrentPage < showImgPageCount)
		{
			showImgCurrentPage++;
			showWrapper.animate({ scrollLeft: (showImgCurrentPage - 1) * (showImgWidth + 20) * 2 }, "slow");
		}
	}
	else
	{
		// previous
		if (showImgCurrentPage > 1)
		{
			showImgCurrentPage--;
			showWrapper.animate({ scrollLeft: (showImgCurrentPage - 1) * (showImgWidth + 20) * 2 }, "slow");
		}
	}
}

function popupShowLocat(src)
{
	var popupShow = $("#popupShow");
	var showWrapper = $(".showWrapper", popupShow);
	var showInnerWrapper = $(".showInnerWrapper", showWrapper);
	var items = $("img", showInnerWrapper);
	var page = 0;
	var bigCount = 0;
	items.each(function (index, element)
	{
		var item = $(element);
		if (item.data("src") == src)
		{
			page = index + 1 + bigCount;
			page = Math.ceil(page / 2);
			return false;
		}
		else if (item.width() == showImgWidth_Big)
		{
			bigCount++;
		}
	});
	if (page > 0)
	{
		showImgCurrentPage = page;
		showWrapper.animate({ scrollLeft: (showImgCurrentPage - 1) * (showImgWidth + 20) * 2 }, "slow");
	}
	else
	{
		showImgCurrentPage = 1;
		showWrapper.scrollLeft(0);
	}
}

function popupShow_hide()
{
	var popupShow = $("#popupShow");
	popupShow.hide();
	var popupMask = $("#popupMask");
	popupMask.hide();
}
