﻿var MoveSpeed = 10;
var MoveSpace = 20;
var PageLength = 107 * 6;
var lock = false;
var distance = 0;
var fill_position = 0; //镜头初始位置

function ScrollAlbum() {
    if (distance == 0) {
        lock = false;
        return;
    }
    var num, tempSpeed = MoveSpeed, tempSpace = MoveSpace;
    if (Math.abs(distance) < PageLength / 2) {
        tempSpace = Math.round(Math.abs(distance / MoveSpace));
        if (tempSpace < 1)
            tempSpace = 1;
    }
    if (distance > 0) {//move right.
        if (distance > tempSpace) {
            distance -= tempSpace;
            num = tempSpace;
        }
        else {
            num = distance;
            distance = 0;
        }
        var scroll_panel = document.getElementById("Album_Came");
        var albumlist = document.getElementById("Album_list");
        scroll_panel.scrollLeft += num;
        if (scroll_panel.scrollLeft >= albumlist.scrollWidth)
            scroll_panel.scrollLeft = scroll_panel.scrollLeft - albumlist.scrollWidth;

        setTimeout("ScrollAlbum()", tempSpeed);
    }
    else {//move left
        if (distance < -tempSpace) {
            distance += tempSpace;
            num = tempSpace;
        }
        else {
            num = -distance;
            distance = 0;
        }
        var scroll_panel = document.getElementById("Album_Came");
        var albumlist = document.getElementById("Album_list");

        var temp = scroll_panel.scrollLeft;
        temp -= num;
        if (temp < 0)
            temp = temp + albumlist.offsetWidth;
        scroll_panel.scrollLeft = temp;

        setTimeout("ScrollAlbum()", tempSpeed);
    }
}
//move right
function RightScrollPage() {
    var came = document.getElementById("Album_Came");
    var list = document.getElementById("Album_list");

    var length = list.offsetWidth - came.scrollLeft;
    if (Math.abs(length) / PageLength <= 1)
        return;
    
    if (lock)
        return;
    distance = PageLength;
    lock = true;
    ScrollAlbum();
}
//move left
function LeftScrollPage() {
    var came = document.getElementById("Album_Came");
    
    if (came.scrollLeft <= 0) {
        came.scrollleft = 0;
        return;
    }
    if (lock)
        return;
    distance = -PageLength;
    lock = true;
    ScrollAlbum();
}

//initialize the camera position
function Scroll_Init(currentIndex) {
    var pageIndex = Math.floor(currentIndex / 6);
    if (currentIndex % 6 == 0)
        pageIndex -= 1;
    fill_position = pageIndex * PageLength;
    var copy = document.getElementById("Copy_list");
    var list = document.getElementById("Album_list");
    var came = document.getElementById("Album_Came");
    if (list.offsetWidth <= came.offsetWidth)
        return;
    //copy.innerHTML = list.innerHTML;
    came.scrollLeft = fill_position >= 0 ? fill_position : list.scrollWidth + fill_position;
}

//link the previous picture address.
function PreviousPicture(src) {
    if (src == "")
        return;
    else
        window.location.href = src;
}

//link the next picture address.
function NextPicture(src) {
    if (src == "")
        return;
    else
        window.location.href = src;
}

//display the current picture
function DisplayCurrentPicture() {
    var img = document.getElementById("img_right");
    DrawImage(img, 550, 400);
}

//图片按比例缩放
//var flag = false;
function DrawImage(img, iwidth, iheight) {
    //参数(picture,允许的宽度,允许的高度)
    var image = new Image();
    image.src = img.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                image.height = iwidth / image.width * image.height;
                image.width = iwidth;
            }
        }
        else {
            if (image.height > iheight) {
                image.width = iheight / image.height * image.width;
                image.height = iheight;
            }
        }
        var parentWidth = iwidth / 2;
        var parentHeight = iheight;
        var left = document.getElementById("img_left");
        var right = document.getElementById("img_right");
        left.style.display = right.style.display = "block";
        left.style.width = right.style.width = image.width + "px";
        left.style.height = right.style.height = image.height + "px";
        left.style.marginLeft = (parentWidth - image.width / 2) + "px";
        left.style.marginTop = right.style.marginTop = (parentHeight - image.height) / 2 + "px";
        right.style.marginLeft = "-" + (image.width / 2) + "px";

        //        left.style = "margin-left:" + (parentWidth - image.width / 2) + "px; margin-top:" + (parentHeight - image.height) / 2 + "px; width:" + image.width + "px; height:" + image.height + "px; display:block";
        //        right.style = "margin-left:-" + image.width / 2 + "px; margin-top:" + (parentHeight - image.height) / 2 + "px; width:" + image.width + "px; height:" + image.height + "px; display:block";
    }
    else
        window.setTimeout("DisplayCurrentPicture()", 10);
}
