//	StarryStickyScroll Class
//	
//	Written by Koji Kimura @ STARRYWORKS INC.
//	Version 0.5.0		2007-11-21
//	Copyright (c) 2007 STARRYWORKS INC.		http://www.starryworks.co.jp/
//	
//	licensed under LGPL licenses.
//
//	Required Libraries:	YAHOO UI Library(DOM,Event,Animation)
//
if ( YAHOO && YAHOO.util && YAHOO.util.Dom && YAHOO.util.Anim && YAHOO.util.Event ) {
	var StarryStickyScroll = function () {
		this.initialize.apply(this,arguments);
	};
	
	StarryStickyScroll.prototype = {
		//	elmId	:String		Id of the element to move with scrolling
		//	vPos		:Integer		Vertical position (percentage)
		//	hPos		:Integer		Horizental position (percentage)
		initialize: function (elmId,vPos,hPos) {
			this.elmId = elmId;
			this.vPos = vPos;
			this.hPos = hPos;
			this.duration = 0.5;
			this.animateOnLoad = false;
			this.moveOnLoad = true;
			this.method = YAHOO.util.Easing.easeOut;
			this.addListener(window,"resize",this.bind(this.animate));
			this.addListener(window,"scroll",this.bind(this.animate));
			this.addListener(window,"load",this.bind(this.load));
		},
		bind: function(func,arg) {
			var _this = this;
			var _arg = (arg)?arg:[];
			return function() {func.apply(_this,_arg);};
		},
		addListener: function (elm,ev,func,cap) {
			if ( elm.addEventListener ) {
				elm.addEventListener(ev,func,cap);
			} else {
				elm.attachEvent("on"+ev,func);
			}
		},
		load: function () {
			YAHOO.util.Dom.get(this.elmId).style.position = "absolute";
			if ( this.animateOnLoad ) {
				this.animate();
			} else if ( this.moveOnLoad ) {
				var pos = this.getXY();
				YAHOO.util.Dom.setXY(this.elmId,pos);
			}
		},
		getXY: function() {
			var pos = YAHOO.util.Dom.getXY(this.elmId);
			var scrollY = document.body.scrollTop || document.documentElement.scrollTop;
			var scrollX = document.body.scrollLeft || document.documentElement.scrollLeft;
			var clientWidth = document.body.clientWidth || document.documentElement.clientWidth;
			var clientHeight = document.body.clientHeight || document.documentElement.clientHeight;
			if ( !isNaN(this.vPos) ) {
				pos[1] = scrollY+(YAHOO.util.Dom.getViewportHeight()-YAHOO.util.Dom.get(this.elmId).clientHeight)*(this.vPos/100);
				if ( pos[1] > clientHeight-YAHOO.util.Dom.get(this.elmId).clientHeight )
					pos[1] = clientHeight-YAHOO.util.Dom.get(this.elmId).clientHeight;
			}
			if ( !isNaN(this.hPos) ) {
				pos[0] = scrollX+(YAHOO.util.Dom.getViewportWidth()-YAHOO.util.Dom.get(this.elmId).clientWidth)*this.hPos/100;
				if ( pos[0] > clientWidth-YAHOO.util.Dom.get(this.elmId).clientWidth )
					pos[0] = clientWidth-YAHOO.util.Dom.get(this.elmId).clientWidth;
			}
			return pos;
		},
		animate: function (e) {
			this.anim = new YAHOO.util.Motion(this.elmId);
			this.anim.duration = this.duration;
			this.anim.method = this.method;
			var pos = this.getXY();
			this.anim.attributes.points = { to: pos }
			this.anim.animate();
		}
	};
} else {
	alert("Error : StarryStickyScroll.js needs YAHOO UI Library(DOM,Event,Animation)");
}

