﻿var object = {

	extend: function(oDefine)
	{
		!oDefine && (oDefine = {}); 

		var oExtendClass = function() {
			for (var eachMember in oDefine) this[eachMember] = oDefine[eachMember];
		}

		oExtendClass.prototype = this; 

		return (new oExtendClass());
	},

	New: function()
	{
		var aParams = [];
		
		for (var i=0; i<arguments.length; i++) {
			aParams.push(arguments[i]);
		}

		var oNewInstance = function() { 
			this.construct && this.construct.apply(this, aParams);
		};

		oNewInstance.prototype = this;
		
		return (new oNewInstance());
	}
	
};

var Page = object.extend({	
	
	navigator: window.ActiveXObject ? window.XMLHttpRequest ? 2 : 1 : window.opera ? 5 : navigator.userAgent.indexOf("KHTML") == -1 ? 3 : 4,
	
	onload: [],

	jobList: [],
	
	_oJobTimeout: null,
	
	getHeadElement: function()
	{
		return document.getElementsByTagName('head')[0];
	},	
	
	loadHeadElement: function(sTagName, oAttrs, fOnload)
	{
		var newElement = document.createElement(sTagName);
		
		for (var sEachAttr in oAttrs)
		{
			newElement.setAttribute(sEachAttr, oAttrs[sEachAttr]);
		}
		
		if (typeof(fOnload) == "function")
		{
			if(this.navigator < 3) // ie
			{
				newElement.onreadystatechange = function()
				{
					if (this.readyState == 'loaded' || this.readyState == 'complete')
					{
						fOnload();
					}
				};
			}
			else // mozilla like
			{
				newElement.onload = function()
				{
					fOnload();
				};
			}
		}
		
		this.getHeadElement().appendChild(newElement);
	},
	
	loadScript: function(sSrc, fOnload)
	{
		this.loadHeadElement("script", {"type": "text/javascript", "src": sSrc}, fOnload);
	},
	
	loadStyle: function(sHref, fOnload)
	{	
		this.loadHeadElement("link", {"type": "text/css", "rel": "stylesheet", "href": sHref}, fOnload);
	},
	
	setCookie: function(oItems)
	{
		var sDate = (new Date()).getTime() + (oItems.expires || 0) * 86400000;
		
		var aCookie = [];
		
			oItems.name && aCookie.push(oItems.name + "=" + escape(oItems.value));
			oItems.expires && aCookie.push("expires=" + (new Date(sDate)).toGMTString());
			oItems.path && aCookie.push("path=" + oItems.path);
			oItems.domain && aCookie.push("domain=" + oItems.domain);
		
		document.cookie = aCookie.join(";");
	},

	getCookie: function(sName)
	{
		var sValue = document.cookie.match(new RegExp("(^| )" + sName + "=([^;]*)(;|$)"));
		
		return sValue ? unescape(sValue[2]) : null;
	},
	
	
	delCookie: function(sName)
	{
		this.setCookie({name: sName, value: "", expires: -1});
	},
	
	doJob: function()
	{
		if (this.jobList.length ==0) {
			return false;
		}

		var arrCurrJob = this.jobList.shift();
		this._oJobTimeout = window.setTimeout(function(){eval(arrCurrJob[0]); Page.doJob();}, arrCurrJob[1]);
	}

});

var Style = object.extend({
	
	pick: function(oElement, sProperty)
	{
		var bIsW3cMode = document.defaultView && document.defaultView.getComputedStyle;
		var _sProperty = sProperty;
		
		if (_sProperty == "float") _sProperty = bIsW3cMode ? "cssFloat" : "styleFloat";
		
		if (bIsW3cMode) {

			var oStyle = document.defaultView.getComputedStyle(oElement, '');
			
			return oElement.style[sProperty] || oStyle[sProperty];
		}
		else
		{
			return(oElement.style[sProperty] || oElement.currentStyle[sProperty]);
		}
	}
	
});

window.onload = function()
{
	for(var eachFunction in Page.onload)
	{
		Page.onload[eachFunction]();
	}
};

var _ = function(sId){return document.getElementById(sId);};


var Physical = object.extend({

	smooth: function(nFrom, nTo, nSteps, nType)
	{
		var arrList = [];
		
		switch (nType) {
			
			case 1: {
				
				var nPt = 0, nData = 0;
			
				for (var i=1; i<=nSteps; i++) {

					nPt = i/nSteps;
					nPt != 0.5 && arrList.push(nFrom + (nTo - nFrom) * ((Math.sqrt(Math.abs(2 * nPt - 1)) * (2 * nPt - 1) / Math.abs(2 * nPt - 1) + 1) / 2));
					
				}
				
				break;
			}
			
			default: {
				
				for (var i=1; i<=nSteps; i++) {
					
					arrList.push(nFrom + (nTo - nFrom) * Math.sqrt(i / nSteps));	
								
				}
				
			}
			
		}
		
		return arrList;
	}

});


var Scale = object.extend({
	
	construct: function(oFrameDiv, oButtonDiv, nHeight)
	{
		this.oFrameDiv = oFrameDiv;
		this.oButtonDiv = oButtonDiv;
		this.nHeight = nHeight;
		
		this.currFrame = -1;
		this.nTargetFrame = -1;
		this.arrButtons = [];
		this.oDelay = null;
		
		var _this = this;
		var nFrames = Page.navigator < 3 ? oFrameDiv.childNodes.length : (oFrameDiv.childNodes.length - 1) / 2;
		
		for (var i=1; i<=nFrames; i++)
		{
			var theButton = document.createElement("A");
				theButton.tabIndex = i;
				theButton.className = "s-normal";
				theButton.href = "javascript:void(0)";
				theButton.innerHTML = i;

				theButton.onmouseover = function()
				{
					var nTabindex = this.tabIndex;
					var nTargetFrame = nTabindex-1;

					_this.nTargetFrame = nTargetFrame;
					_this.oDelay && window.clearTimeout(_this.oDelay);
					_this.oDelay = window.setTimeout(function(){_this.doScale(nTargetFrame);}, 200);
				};

				theButton.onmouseout = function()
				{
					_this.autoScale();
				};

			oButtonDiv.appendChild(theButton) && this.arrButtons.push(theButton);
			
			if (this.currFrame == -1)
			{
				this.currFrame = 0;
				this.highLight(0);
			}
		}
		
		this.autoScale();
	},
	
	scaleTo: function(nFrame)
	{
		var nCurrTop = this.currFrame * this.nHeight;
		var arrStepList = Physical.smooth(nCurrTop, nFrame * this.nHeight, 20, 0);

		for(var i=0; i<arrStepList.length; i++)
		{
			Page.jobList.push(['_("' + this.oFrameDiv.id + '").style.top = "-' + arrStepList[i] + 'px";', 10]);
		}
		
		Page.doJob();
	},
	
	highLight: function(nFrame)
	{
		this.arrButtons[this.currFrame].className = "s-normal";
		this.arrButtons[nFrame].className = "s-current";
	},
	
	doScale: function(nFrame)
	{
		// 核对是否需要滚动以及滚动过快的情况
		if(nFrame == this.currFrame || nFrame != this.nTargetFrame) return false;
		
		this.highLight(nFrame);
		this.scaleTo(nFrame);
		this.currFrame = nFrame;
	},
	
	autoScale: function()
	{
		var _this = this;
		var nNextFrame = this.currFrame + 1 <= this.arrButtons.length - 1 ? this.currFrame + 1 : 0;
		
		_this.nTargetFrame = nNextFrame;
		_this.oDelay = window.setTimeout(function(){_this.doScale(nNextFrame); _this.autoScale();}, 3000);
	}

});
