﻿//==========================================================================================================
//PC home Online 網路家庭的文字、圖片、資料、商標、標識、及網頁設計等皆受中華民國相關智慧財產權法令的保護。
//嚴禁任何未經事先同意的非法轉載和使用。若經查知，PC home Online 網路家庭將採取必要之法律行動
//==========================================================================================================

function RollingPrint(id, DataObj) {
	this.id = id;
	this.DataObj = DataObj;
	this.CodeSection = this.DataObj.CodeSection;
	this.IconWidth = this.DataObj.IconWidth;
	this.IconHeight = this.DataObj.IconHeight;
	this.IconAmt =  this.DataObj.IconInfo.length;
	this.ColAmt = this.DataObj.ColAmt;
	this.Timer = this.DataObj.Timer; //ms
	this.FuncID = this.IconAmt - 1;
	this.VaryMode = 'Right';
	this.getPrintSet = '';
	this.getPrintElement = [];
	this.BlockAction = false;
	this.IntervalID = '';
	this.TimerIntervalID = '';
	//method
	this.VaryAction = function() {
		clearInterval(this.IntervalID);
		(this.VaryMode == 'Right') ? this.FuncID++ : this.FuncID--;
		if (this.FuncID > this.DataObj.IconInfo.length-1) this.FuncID -= this.DataObj.IconInfo.length;
		if (this.FuncID < 0) this.FuncID = this.DataObj.IconInfo.length - 1;
		this.VaryMode = 'Right';
		this.getPrintSet.MoveFlagX = false;
		this.getPrintSet.MoveFlagY = false;
		this.getPrintSet.GoalX = this.getPrintElement[this.FuncID].GoalX;
		this.getPrintSet.GoalY = this.getPrintElement[this.FuncID].GoalY;
		//doMove
		var speed = 10;
		this.IntervalID = setInterval('RollingPrint'+this.id+'.realMoveNew('+speed+')', 15);
		this.BlockAction = true;
	}
	this.realMoveNew = function(speed) {
		var ObjX = this.getPrintSet.PosX;
		var ObjY = this.getPrintSet.PosY;
		
		var a = this.getPrintSet.GoalX - ObjX;
		var speedx = a/speed;
		var SignX = (speedx < 0) ? -1 : 1;
		speedx = Number(SignX * ((Math.abs(speedx) <= 1) ? 1 : Math.abs(speedx)));
		
		var b = this.getPrintSet.GoalY - ObjY;
		var speedy = b/speed;
		var SignY = (speedy < 0) ? -1 : 1;
		speedy = Number(SignY * ((Math.abs(speedy) <= 1) ? 1 : Math.abs(speedy)));
		
		//check x position
		if (!this.getPrintSet.MoveFlagX) {
			if (Math.abs(ObjX-this.getPrintSet.GoalX) <= Math.abs(speedx)) { this.getPrintSet.PosX = this.getPrintSet.GoalX; this.getPrintSet.MoveFlagX = true;	}
			else this.getPrintSet.PosX = ObjX + speedx;
		}
		
		//check y position
		if (!this.getPrintSet.MoveFlagY) {
			if (Math.abs(ObjY-this.getPrintSet.GoalY) <= Math.abs(speedy)) { this.getPrintSet.PosY = this.getPrintSet.GoalY; this.getPrintSet.MoveFlagY = true;	}
			else this.getPrintSet.PosY = ObjY + speedy;
		}
		
		this.getPrintSet.style.left = this.getPrintSet.PosX + 'px';
		this.getPrintSet.style.top = this.getPrintSet.PosY + 'px';
		
		if (this.getPrintSet.MoveFlagX && this.getPrintSet.MoveFlagY) {
			clearInterval(this.IntervalID);
			clearTimeout(this.TimerIntervalID);
			$('ImgCounter').innerHTML = (this.FuncID + 1) + '/' + this.IconAmt;
			$('ImgTitleSet').innerHTML = TextLengthLimit(this.DataObj.IconInfo[this.FuncID].Title);
			this.BlockAction = false;
			this.TimerIntervalID = setTimeout('RollingPrint'+this.id+'.VaryAction()', this.Timer);
		}
	}
	this.SwitchContent = function(IconObj) {
		clearInterval(this.IntervalID);
		clearTimeout(this.TimerIntervalID);
		this.DataObj.IconInfo = IconObj.IconInfo;
		this.IconAmt =  this.DataObj.IconInfo.length;
		this.FuncID = IconObj.FuncID - 1;
		if (this.FuncID < 0) this.FuncID = this.DataObj.IconInfo.length - 1;
		this.getPrintSet = '';
		this.getPrintElement = [];
		this.constructor();
	}
	this.ElementClickAction = function(SerialID) {
		var WinOptions = 'width=700,height=650,status=yes,toolbar=yes,menubar=yes,resizable=yes,scrollbars=yes,left=10,top=10';
		window.open(this.DataObj.IconInfo[SerialID].linkset, 'PChomePhoto', WinOptions);
	}
	this.constructor = RollingPrintConstructor;
	this.constructor();
}

function RollingPrintConstructor() {
	if (!$(this.CodeSection)) return;
	var RollingPrint = $(this.CodeSection);
	while (RollingPrint.childNodes.length > 0) RollingPrint.removeChild(RollingPrint.firstChild);
	RollingPrint.style.width = this.IconWidth + 'px';
	RollingPrint.style.height = this.IconHeight + 'px';
	RollingPrint.style.clip = 'rect(0px, ' + this.IconWidth + 'px, ' + this.IconHeight + 'px, 0px)';
	disableSelection(RollingPrint);
	
	var PrintSet = document.createElement('div');
	PrintSet.className = 'RollingPrint';
	PrintSet.style.left = '0px';
	PrintSet.style.top = '0px';
	PrintSet.PosX = 0;
	PrintSet.PosY = 0;
	PrintSet.MoveFlagX = false;
	PrintSet.MoveFlagY = false;
	PrintSet.GoalX = 0;
	PrintSet.GoalY = 0;
	RollingPrint.appendChild(PrintSet);
	this.getPrintSet = PrintSet;
	this.getPrintElement = [];
	for (var i=0 ; i<this.IconAmt; i++) {
		var Column = i % this.ColAmt;
		var Row = Math.floor(i/this.ColAmt);
		var PosX = Column * this.IconWidth;
		var PosY = Row * this.IconHeight;
		var RollingPrintElement = document.createElement('img');
		RollingPrintElement.src = this.DataObj.IconInfo[i].img;
		RollingPrintElement.style.width = this.IconWidth + 'px';
		RollingPrintElement.style.height = this.IconHeight + 'px';
		RollingPrintElement.style.left = PosX + 'px';
		RollingPrintElement.style.top = PosY + 'px';
		RollingPrintElement.ClassID = this.id;
		RollingPrintElement.Serial = i;
		RollingPrintElement.GoalX = PosX * -1;
		RollingPrintElement.GoalY = PosY * -1;
		RollingPrintElement.onmouseover = function() {
			var ObjMapping = eval('RollingPrint'+this.ClassID);
			if (ObjMapping.BlockAction) return;
			clearTimeout(ObjMapping.TimerIntervalID);
		}
		RollingPrintElement.onmouseout = function() {
			var ObjMapping = eval('RollingPrint'+this.ClassID);
			if (ObjMapping.BlockAction) return;
			clearTimeout(ObjMapping.TimerIntervalID);
			ObjMapping.TimerIntervalID = setTimeout('RollingPrint'+ObjMapping.id+'.VaryAction()', ObjMapping.Timer);
		}
		RollingPrintElement.onclick = function() {
			var ObjMapping = eval('RollingPrint'+this.ClassID);
			ObjMapping.ElementClickAction(this.Serial);
		}
		PrintSet.appendChild(RollingPrintElement);
		this.getPrintElement.push(RollingPrintElement);
	}//end for
	
	//WinInfoBTR
	$('WinInfoBTR').innerHTML = '共 ' + this.IconAmt + ((MapType == 'Photo') ? ' 張相片' : ' 本相簿');
	
	//VaryAction
	this.VaryAction();
}