﻿/*
Layer 移動
設計:David Wang
*/
function moveLayer(strMovID) {
    this.iInitialpoint = 0;     //開始移動的位置
    this.iWidth = 0; 	        //圖片間隙
    this.iNumberOfImages = 0;   //移動物件總數量
    this.iCount = 0;            //畫面上顯示幾個物件
    var iLeader = 1;    //圖片id,用來換下張圖片	
    var iLeft = 0; 	    //向左或右移累加的寬度
    var oVisDiv = new Array(this.iNumberOfImages);				    
    var MediaData = new Array();				    
    var TimeOutID;
    var msobj = this;
    msobj.Start = function() { msobj.doIt('left', 1) }
    msobj.Startleft = function() { msobj.doIt('left', 4) }
    msobj.Startright = function() { msobj.doIt('right', 4) }
    
    var isFirefox = (navigator.userAgent.search("Firefox") != -1) ? true : false;  //FireFox處理Layer Move的cpu usage較高,所以設定較慢速度

    //啟動
    this.startScroll = function() {
        this.stopScroll();
        if (isFirefox)
			TimeOutID = (this.iNumberOfImages > this.iCount) ? window.setInterval(msobj.Start, 40) : "";
		else
			TimeOutID = (this.iNumberOfImages > this.iCount) ? window.setInterval(msobj.Start, 30) : "";
    }    
    //停止
    this.stopScroll = function() {
	    clearInterval(TimeOutID);
    }
    //向右移
    this.scrollmLeft = function() {
        this.stopScroll();
        if (isFirefox)
			TimeOutID = (this.iNumberOfImages > this.iCount) ? window.setInterval(msobj.Startleft, 20) : "";
		else
			TimeOutID = (this.iNumberOfImages > this.iCount) ? window.setInterval(msobj.Startleft, 15) : "";	
    }
    //向左移
    this.scrollmRight = function() {
        this.stopScroll();
        if (isFirefox)
			TimeOutID = (this.iNumberOfImages > this.iCount) ? window.setInterval(msobj.Startright, 20) : "";
		else
			TimeOutID = (this.iNumberOfImages > this.iCount) ? window.setInterval(msobj.Startright, 15) : "";	
    }
    //設定div區塊寬度、高度和編號
    makeData = function(imageclickId, Width, Height) {
	    this.imageclickId = imageclickId;
	    this.Width = Width;
	    this.Height = Height;
	    return true;
    }
    //讀取圖片的資訊
    this.loadVisibleDiv = function() {
        if (document.layers) {
            for (var iCnt = 1; iCnt <= this.iNumberOfImages; iCnt++) {
                oVisDiv[iCnt] = document.layers[strMovID + iCnt];
            }
        } else if (document.all) {
            for (var iCnt = 1; iCnt <= this.iNumberOfImages; iCnt++) {
                oVisDiv[iCnt] = document.all[strMovID + iCnt].style;
            }
        } else if (document.getElementById) {
            for (var iCnt = 1; iCnt <= this.iNumberOfImages; iCnt++) {
                oVisDiv[iCnt] = document.getElementById(strMovID + iCnt).style;
            }
        }
        for (var iCnt = 1; iCnt <= this.iNumberOfImages; iCnt++) {
            document.getElementById(strMovID + iCnt).onmouseover = function() { msobj.stopScroll(); }
            document.getElementById(strMovID + iCnt).onmouseout = function() { msobj.startScroll(); }
            MediaData[iCnt] = new makeData(iCnt, parseInt(oVisDiv[iCnt].width) + this.iWidth, 0);
        }
    }
    this.doDefault = function() {
	    var iLocal; //圖片id
	    var iAddIncr; //開始移動的位置
	    iLocal = iLeader;
	    iAddIncr = this.iInitialpoint;
	    //每張圖片的移動
	    for (var iCnt = 1; iCnt <= this.iNumberOfImages; iCnt++) {
		    //變更圖片位置
		    oVisDiv[iLocal].left = (iLeft + iAddIncr) + "px";
		    //取得上一張圖片的寬度,並累加
		    iAddIncr += MediaData[iLocal].Width;
		    iLocal += 1;
	    }
    }
    //移動圖片
    this.doIt = function(move, speed) {
	    var iLocal; //圖片id
	    var iAddIncr; //開始移動的位置
	    iLocal = iLeader;
	    iAddIncr = this.iInitialpoint;

	    //每張圖片的移動
	    for (var iCnt = 1; iCnt <= this.iNumberOfImages; iCnt++) {
		    //變更圖片位置
		    oVisDiv[iLocal].left = (iLeft + iAddIncr) + "px";
		    //取得上一張圖片的寬度,並累加
		    iAddIncr += MediaData[iLocal].Width;
		    iLocal += 1;
		    if (iLocal > this.iNumberOfImages) {
			    iLocal = 1
		    }
	    }
	    //向左移
	    if (move == "left") {
		    //單累加的寬度超過圖片寬度做處理 
		    if (iLeft <= -(MediaData[iLeader].Width + 1)) {
			    iLeft = 0;
			    //換下張圖片
			    iLeader += 1;
			    //圖片id,超過圖片數量時做處理
			    if (iLeader > this.iNumberOfImages) {
				    iLeader = 1;
			    }
		    }
		    iLeft -= speed;
		    //向右移  
	    } else {			    
	        if (iLeft >= 1) {
	            iLeader -= 1;

	            //圖片id,小於1時做處理
	            if (iLeader < 1) {
	                iLeader = this.iNumberOfImages;
	            }
	            iLeft = 0 - (parseInt(MediaData[iLeader].Width));
		    }
		    iLeft += speed;
	    }
    }
}
