/**
 * This slider code was given by Elvin in Tallinn,
 * who knows where he got it.
 *
 * This code had bunch of global variables, no indentation,
 * no comments whatsoever.
 *
 * Indented and removed global variables. It even passes JSLint.
 * Still, this code seems quite bad, as it relies heavily on user-agent sniffing.
 */
$(function() {
	
	var Browser=function(){
		var ua = navigator.userAgent.toLowerCase();
		return {
			isSafari : (ua.indexOf('webkit') > -1),
			isGecko : !this.isSafari && (ua.indexOf('gecko') > -1),
			isIE : (ua.indexOf('msie') > -1),
			isOpera : (ua.indexOf('opera') > -1),
			isKonqueror: (ua.indexOf("konqueror") != -1),
			isMac: (ua.indexOf("mac")!=-1),
			isMozilla: !!(document.implementation && document.implementation.createDocument),
			hasDOM: !!(document.getElementById),
			version: parseInt(navigator.appVersion, 10)
		};
	}();

	var SLIDER = function() {
		
		function getRealTop(el2) {
			if (!el2) {
				return null;
			}
			
			var yPos = el2.offsetTop;
			var tempEl = el2.offsetParent;
			
			while (tempEl !== null) {
				yPos += tempEl.offsetTop;
				tempEl = tempEl.offsetParent;
			}
			
			return yPos;
		}
		
		var timerIds = [];
		
		function windowPos(Id, Ys, xtra, Yb) {
			var Yp = 0;
			var decr;
			var ob;
			
			if (Browser.isIE) {
				Yp = document.documentElement.scrollTop;
			}
			else if (Browser.isNS || Browser.isMozilla || Browser.isOpera || Browser.isKonqueror) {
				Yp = self.pageYOffset;
			}
			
			if (Yp <= Ys) {
				decr = 0;
			}
			else {
				decr = Yp - Ys;
			}
			
			var Ystart = Ys;
			
			if ( decr != Yb ) {
				if (decr > 0) {
					if (decr > Yb) {
						Ystart = Yb + Math.round((decr - Yb) / 3);
					}
					else if (decr < Yb) {
						Ystart = Yb - Math.round((Yb - decr) / 3);
					}
					if (Math.abs(decr - Ystart) <= 1) {
						Ystart = decr;
					}
					Ystart += xtra;
				}
				else {
					Ystart = xtra;
				}
				
				ob = SLIDER.get(Id);
				
				if (Browser.isIE) {
					ob.style.pixelTop = Ystart;
				}
				else if (Browser.isNS && !Browser.isMozilla) {
					ob.top = Ystart;
				}
				else if (Browser.isOpera && Browser.hasDOM) {
					ob.style.top = Ystart;
				}
				else if (Browser.isOpera || Browser.isMozilla || Browser.isKonqueror) {
					ob.style.top = Ystart + "px";
				}
				
				Yb = Ystart;
				timerIds[Id] = setTimeout(function() { windowPos(Id, Ys, xtra, Yb); }, 50);
			}
			else {
				timerIds[Id] = setTimeout(function() { windowPos(Id, Ys, xtra, Yb); }, 300);
			}
		}
		
		return {
			get: function(id) {
				return document.getElementById(id);
			},
			getYfromTop: function(el1) {
				if (!el1) {
					return null;
				}
				if (Browser.ns4) {
					return el1.y;
				}
				else {
					return getRealTop(el1);
				}
			},
			startSlide: function(Id, Ystart, xtra) {
				if (!SLIDER.get(Id)) {
					return;
				}
				if (!Ystart) {
					Ystart = SLIDER.getYfromTop( SLIDER.get(Id) );
				}
				if (!xtra) {
					xtra = 0;
				}
				
				timerIds[Id] = setTimeout(function() { windowPos(Id, Ystart, xtra, 0); }, 300);
			}
		};
	}();
	
	SLIDER.startSlide('bnrFloat1');
});
