/***************************************************\
  GSSI Spinning Panels widget
  Requires: Base, Environment, Events, DOM
\***************************************************/

if (! GSSI ) { alert("gssi.accordion.js: gssi.base.js not loaded") };
if (! GSSI.Environment ) { alert("gssi.accordion.js: gssi.environment.js not loaded") };
if (! GSSI.Events ) { alert("gssi.accordion.js: gssi.environment.js not loaded") };
if (! GSSI.DOM ) { alert("gssi.accordion.js: gssi.dom.js not loaded") };

document.write('<style type="text/css"> .spinpanel_revealonload { visibility:hidden; } </style>');

GSSI.Spinpanel = GSSI.Class({
	
	_animation: { running:false, frame:-1, position:0.0 },

	_built: false,
	
	_autospinenabled:false,

	initialize: function( target, options ) {
		options = GSSI.Base.Extend( {
			period:2.0,
			fps:24,
			sizefactor: 2.0,
			autospin: true,
			autospinforward:true,
			autospinperiod: 8.0
		}, options || {});
		var _this = this;
		this.options = options;
		
		GSSI.Spinpanel._list.push(this);
		
		this._frames = Math.floor(options.period * options.fps);
		this._framechange = 1.0 / (0.0 + this._frames);
		this._framedelay = (1.0 / options.fps) * 1000;
		this._autospinenabled = options.autospin;
		this._autospinperiod = options.autospinperiod * 1000;

		this.target = target;
		GSSI.Events.onDOMReady( function() { _this._build(); } );
		this._animtick = function() { _this._animate(); };
		this._autospintick = function() { _this._autospin(); };
	},
	
	_build: function() {
		var i, an, imgs, firstimg;
		this.target = GSSI.$(this.target);
		this.target._spinpanel = this.target._spinpanel || {};
		var _this = this;
		this.target._spinpanel.Device = this;
		this.target.style.zIndex = this.target.style.zIndex || 1;
		
		var t = this.target;
		var tpos = GSSI.Spinpanel._getPositioning(t);
		if ((tpos != "absolute") && (tpos != "relative")) {
			t.style.position = "relative";
		}		
		
		an = t.getElementsByTagName('a');
		this.anchors = [];
		for(i=0; i<an.length; i++) {
			this.anchors.push(an[i]);
		}
		
		for(i=t.childNodes.length-1; i>=0; i--) {
			t.removeChild(t.childNodes[i]);
		}
		
		for(i=0; i<this.anchors.length; i++) {
			imgs = (this.anchors[i]).getElementsByTagName('img');
			if(imgs && (firstimg=imgs[0])) {
				t.appendChild(this.anchors[i]);
				this.anchors[i]._img = firstimg;
				if (!this.templateimage) {
					this.templateimage = new Image();
					this.templateimage.onload = function () { _this._build_2(); }
					this.templateimage.src = firstimg.src;
				}
				firstimg.style.height = "100%";
				firstimg.style.width = "100%";
				firstimg.style.top = "0";
				firstimg.style.left = "0";
				firstimg.style.position = "absolute";
				firstimg.style.zIndex = "-10";
			}
		}
		
		if(!this.templateimage) {
			alert( "Spinpanel: Cannot find any anchored images." );
		}
	},
	
	_build_2: function() {
		var a, as, aimg, ti;
		var i,j;
		ti = this.templateimage;

		this.sizes = {};
		this.sizes.normal = {
			width: ti.width,
			height: ti.height
		};
		this.sizes.small = {
			width: ti.width / this.options.sizefactor,
			height: ti.height / this.options.sizefactor
		};
		this.sizes.tiny = {
			width: ti.width / (this.options.sizefactor * this.options.sizefactor),
			height: ti.height / (this.options.sizefactor * this.options.sizefactor)
		};
		
		this.positions = {};
		this.positions.normal = {
			left: (this.target.offsetWidth/2) - (ti.width/2),
			top: (this.target.offsetHeight/2) - (ti.height/2)
		};
		this.positions.left = {
			left: this.positions.normal.left - (this.sizes.small.width/2),
			top: (this.target.offsetHeight/2) - (this.sizes.small.height/2)
		};
		if( this.positions.left.left < 0 ) { this.positions.left.left = 0; }

		this.positions.right = {
			left: this.target.offsetWidth - this.positions.left.left - (this.sizes.small.width),
			top: this.positions.left.top
		};
		this.positions.leftgone = {
			left: this.positions.left.left + this.sizes.small.width - this.sizes.tiny.width,
			top: (this.target.offsetHeight/2) - (this.sizes.tiny.height/2)
		};
		this.positions.rightgone = {
			left: this.target.offsetWidth - this.positions.leftgone.left - (this.sizes.tiny.width),
			top: this.positions.leftgone.top
		};

		var _spinner = this; 
		for(i=0; i<this.anchors.length; i++) {
			a = this.anchors[i];
			as = a.style;
			as.width = ti.width + "px";
			as.height = ti.height + "px";
			as.display = "block";
			as.position = "absolute";
			as.top = (20*i) + "px";
			as.left = (20*i) + "px";
			as.visibility = "visible";
			a._prev = (i==0) ? this.anchors[this.anchors.length-1] : this.anchors[i-1];
			a._next = (i==this.anchors.length-1) ? this.anchors[0] : this.anchors[i+1];
			a.onclick = function() {
				_spinner._autospinenabled = false;
				if (this == _spinner._front) { return true; }
				if (this == _spinner._front._next) { _spinner._back(); }
				if (this == _spinner._front._prev) { _spinner._forward(); }
				return false;
			}
			if ( GSSI.Spinpanel._oldIE ) {
				var il = a.getElementsByTagName("img");
				var aisrc;
				for(j=0; j<il.length; j++) {
					aimg = il[j];
					aisrc = aimg.src;
					if (aisrc.lastIndexOf(".png") == aisrc.length-4)  {
						as.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src='" + aimg.src + "')";
						aimg.src=GSSI.Spinpanel._path + "gssi.1x1.gif";
					}
				}
			}
		}
		
		this._drawposition( this.anchors[0], 1.0);
		this._front = this.anchors[0];
		this.target.style.visibility = "visible";
		this._built = true;
		if (this._autospinenabled) { this._autospin(); }
	},
	
	_autospin: function() {
		if (! this._autospinenabled) { return; }
		if (this.options.autospinforward) {
			this._forward();
		} else {
			this._back();
		}
		window.setTimeout( this._autospintick, this._autospinperiod );
	},
	
	_drawposition: function( to, linearparam ) {  // move all to a position linearparam of the way of "to" scrolling FORWARD TO CENTRE
		var a;
		for(i=0; i<this.anchors.length; i++) {
			this.anchors[i].___dpmark = true;
		}
		var p = linearparam;
		if (p<0.0) { p=0.0; }
		if (p>1.0) { p=1.0; }

		var q = 1.0-p;
		var eip = 1.0 - GSSI.Spinpanel._ease(1.0 - p);
		var eiq = 1.0-eip;
		var eop = GSSI.Spinpanel._ease(p);
		var eoq = 1.0-eop;

		var tw, fw;

		to.style.left = ((eip * this.positions.normal.left) + (eiq * this.positions.left.left)) + "px";
		to.style.top = ((eip * this.positions.normal.top) + (eiq * this.positions.left.top)) + "px";
		to.style.width = (tw = ((eip * this.sizes.normal.width) + (eiq * this.sizes.small.width))) + "px";
		to.style.height = ((eip * this.sizes.normal.height) + (eiq * this.sizes.small.height)) + "px";
		to.___dpmark = false;

		to._prev.style.zIndex = 200;
		to._prev.style.left = ((p * this.positions.left.left) + (q * this.positions.leftgone.left)) + "px";
		to._prev.style.top = ((p * this.positions.left.top) + (q * this.positions.leftgone.top)) + "px";
		to._prev.style.width = ((p * this.sizes.small.width) + (q * this.sizes.tiny.width)) + "px";
		to._prev.style.height = ((p * this.sizes.small.height) + (q * this.sizes.tiny.height)) + "px";
		to._prev.___dpmark = false;

		to._next.style.left = ((eop * this.positions.right.left) + (eoq * this.positions.normal.left)) + "px";
		to._next.style.top = ((eop * this.positions.right.top) + (eoq * this.positions.normal.top)) + "px";
		to._next.style.width = (fw = ((eop * this.sizes.small.width) + (eoq * this.sizes.normal.width))) + "px";
		to._next.style.height = ((eop * this.sizes.small.height) + (eoq * this.sizes.normal.height)) + "px";
		to._next.___dpmark = false;

		to._next._next.style.zIndex = 200;
		to._next._next.style.left = ((p * this.positions.rightgone.left) + (q * this.positions.right.left)) + "px";
		to._next._next.style.top = ((p * this.positions.rightgone.top) + (q * this.positions.right.top)) + "px";
		to._next._next.style.width = ((p * this.sizes.tiny.width) + (q * this.sizes.small.width)) + "px";
		to._next._next.style.height = ((p * this.sizes.tiny.height) + (q * this.sizes.small.height)) + "px";
		to._next._next.___dpmark = false;

		to.style.zIndex = (tw > fw) ? 5000 : 1000;
		to._next.style.zIndex = (tw > fw) ? 1000 : 5000;

		
		for(i=0; i<this.anchors.length; i++) {
			a = this.anchors[i];
			if(a.___dpmark) {
				a.style.visibility="hidden";
				a.style.zIndex = 100;
			} else {
				a.style.visibility = "visible";
			};
		}

	},
	
	_animate: function() {
		var anim = this._animation;
		anim.frame++;
		if(anim.frame == this._frames) {			
			if (anim.reverse) {
				this._drawposition( this._front, 0.0);
				this._front = this._front._next;
			} else {
				this._drawposition( this._front._prev, 1.0);
				this._front = this._front._prev;
			}
			anim.running = false;
			return;
		}
		
		anim.position += this._framechange;
		
		if (anim.reverse) {
			this._drawposition( this._front, 1.0 - (anim.position))
		} else {
			this._drawposition( this._front._prev, anim.position)
		}
		
		window.setTimeout( this._animtick, this._framedelay );
	},
	
	forward: function() {
		this._autospinenabled = false;
		this._forward();
	},
	
	_forward: function() {
		if (!this._built) { return; }
		if ( this._animation.running ) { return; }
		this._animation.reverse = false;
		this._animation.position = 0.0;
		this._animation.frame = 0;
		this._animation.running = true;
		this._animate();
	},
	
	back: function() {
		this._autospinenabled = false;
		this._back();
	},
	
	_back: function() {
		if (!this._built) { return; }
		if ( this._animation.running ) { return; }
		this._animation.reverse = true;
		this._animation.position = 0.0;
		this._animation.frame = 0;
		this._animation.running = true;
		this._animate();
	}

});

GSSI.Spinpanel.Static({
//	_rawlistings: {},
//	_imagenameregex: /\.gif$|\.jpg$|\.jpeg$|\.png$|\.bmp$/,
//	_isimagename: function (s) { return GSSI.Spinpanel._imagenameregex.test(s); }

	_classregex: /GSSI\.Spinpanel(\[[^\]]+\])?/g,

	_ease: function(p) {
		if (p <= 0.0 ) { return 0.0; }
		if (p >= 1.0 ) { return 1.0; }
		return GSSI.Spinpanel._easetable[ Math.floor(p*GSSI.Spinpanel._easetable.length) ];
	},

	_list: [],

	_oldIE: ((GSSI.Environment.Browser.IEVersion > 0) && (GSSI.Environment.Browser.IEVersion < 7)),

	_buildall: function() {
		var divs = document.getElementsByTagName("div");
		var targets = [];
		var r;
		for(i=0; i<divs.length; i++) {
			c = divs[i];
			if (r = GSSI.Spinpanel._classregex.exec(c.className)) {
				targets.push(c);
				c._spinpanel = {};
				if(r[1]) {
					c._spinpanel.rawparams = r[1].substring(1, r[1].length-1).split(',');
				}
				c.className = c.className.replace(GSSI.Spinpanel._classregex, '');
				c.style.overflow="hidden";
			}
		}

		var options;
		for(i=0; i<targets.length; i++) {
			// Parse parameters here: GSSI.Spinpanel[height,period,fps]
			c = targets[i];
			options = {};
			if ( c._spinpanel.rawparams) {
				if (c._spinpanel.rawparams[0]) { // 0 is illegal for height 
					c.style.height = c._spinpanel.rawparams[0] + "px";
				}
				if (c._spinpanel.rawparams[1]) { // 0 is illegal for period 
					options.period = c._spinpanel.rawparams[1];
				}
				if (c._spinpanel.rawparams[2]) { // 0 is illegal for fps 
					options.fps = c._spinpanel.rawparams[2];
				}
			}
			c._spinpanel = new GSSI.Spinpanel(c, options);
		}
	},
	
	_getPositioning: function (element) {
		// from http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
		var strValue = "";
		if(document.defaultView && document.defaultView.getComputedStyle){
			strValue = document.defaultView.getComputedStyle(element, "").getPropertyValue("position");
		}
		else if(element.currentStyle){
			strValue = element.currentStyle["position"];
		}
		return strValue;		
	},
	
	_easetable : // Lookup for function  "Math.sin(p*Math.PI/2)"
		[ 0, 0.0052359638314195805, 0.010471784116245794, 0.015707317311820675, 0.02094241988335696, 0.02617694830787315,
		0.03141075907812829, 0.036643708706556276, 0.04187565372919963, 0.047106450709642665, 0.05233595624294383, 0.05756402695956728,
		0.06279051952931337, 0.06801529066524817, 0.07323819712763169, 0.07845909572784494, 0.0836778433323155, 0.08889429686644151,
		0.09410831331851433, 0.09931974974363901, 0.10452846326765346, 0.10973431109104526, 0.11493715049286658, 0.12013683883464706,
		0.1253332335643042, 0.13052619222005155, 0.13571557243430432, 0.14090123193758258, 0.14608302856241154, 0.15126082024721912,
		0.15643446504023076, 0.16160382110336102, 0.16676874671610215, 0.1719291002794094, 0.17708474031958316, 0.1822355254921473,
		0.1873813145857245, 0.19252196652590725, 0.197657340379126, 0.20278729535651233, 0.20791169081775915, 0.21303038627497636,
		0.21814324139654237, 0.22325011601095116, 0.2283508701106555, 0.2334453638559052, 0.23853345757858063, 0.24361501178602227,
		0.24868988716485455, 0.2537579445848054, 0.25881904510252046, 0.2638730499653726, 0.2689198206152654, 0.2739592186924321,
		0.27899110603922894, 0.2840153447039223, 0.28903179694447123, 0.2940403252323036, 0.2990407922560863, 0.30403306092548993,
		0.30901699437494706, 0.31399245596740455, 0.3189593092980696, 0.323917418198149, 0.32886664673858285, 0.3338068592337705,
		0.3387379202452909, 0.3436596945856156, 0.3485720473218148, 0.35347484377925664, 0.3583679495452998, 0.3632512304729779,
		0.36812455268467753, 0.37298778257580845, 0.3778407868184666, 0.3826834323650893, 0.3875155864521025, 0.3923371166035611,
		0.39714789063478023, 0.40194777665595977, 0.4067366430757999, 0.41151435860510843, 0.41628079226040093, 0.42103581336749074,
		0.42577929156507244, 0.43051109680829497, 0.43523109937232735, 0.439939169855915, 0.4446351791849274, 0.4493189986158965,
		0.45399049973954675, 0.45864955448431494, 0.4632960351198618, 0.46792981426057345, 0.4725507648690541, 0.4771587602596085,
		0.4817536741017154, 0.4863353804234907, 0.49090375361514105, 0.49545866843240777, 0.5000000000000002, 0.5045276238150195,
		0.5090414157503717, 0.5135412520581705, 0.5180270093731306, 0.5224985647159492, 0.526955795496678, 0.5313985795180833,
		0.5358267949789971, 0.5402403204776555, 0.5446390350150276, 0.5490228179981324, 0.5533915492433447, 0.5577451089796908,
		0.5620833778521312, 0.5664062369248335, 0.5707135676844324, 0.5750052520432792, 0.5792811723426795, 0.5835412113561184,
		0.5877852522924739, 0.5920131787992203, 0.5962248749656166, 0.6004202253258849, 0.6045991148623757, 0.6087614290087215,
		0.6129070536529774, 0.6170358751407496, 0.6211477802783113, 0.6252426563357062, 0.6293203910498384, 0.6333808726275513,
		0.6374239897486907, 0.6414496315691589, 0.6454576877239516, 0.6494480483301848, 0.6534206039901066, 0.6573752457940969,
		0.661311865323653, 0.665230354654362, 0.6691306063588595, 0.6730125135097745, 0.676875969682662, 0.680720868958919,
		0.68454710592869, 0.6883545756937554, 0.6921431738704081, 0.6959127965923156, 0.6996633405133668, 0.7033947028105053,
		0.7071067811865488, 0.7107994738729938, 0.7144726796328046, 0.71812629776319, 0.7217602280983634, 0.7253743710122887,
		0.7289686274214126, 0.7325428987873798, 0.7360970871197353, 0.7396310949786106, 0.7431448254773951, 0.7466381822853922,
		0.7501110696304604, 0.7535633923016386, 0.7569950556517572, 0.7604059656000316, 0.7637960286346429, 0.7671651518153002,
		0.7705132427757898, 0.7738402097265067, 0.7771459614569713, 0.7804304073383302, 0.7836934573258402, 0.7869350219613377,
		0.7901550123756907, 0.7933533402912355, 0.7965299180241966, 0.7996846584870908, 0.8028174751911148, 0.805928282248516,
		0.8090169943749476, 0.8120835268918063, 0.8151277957285543, 0.8181497174250235, 0.8211492091337041, 0.8241261886220157,
		0.8270805742745618, 0.8300122850953674, 0.8329212407100993, 0.8358073613682702, 0.8386705679454238, 0.841510781945306,
		0.8443279255020149, 0.8471219213821369, 0.8498926929868638, 0.852640164354092, 0.8553642601605064, 0.8580649057236442,
		0.8607420270039432, 0.8633955506067713, 0.8660254037844383, 0.8686315144381908, 0.8712138111201889, 0.8737722230354648,
		0.8763066800438631, 0.8788171126619648, 0.8813034520649916, 0.8837656300886929, 0.8862035792312142, 0.8886172326549482,
		0.8910065241883672, 0.8933713883278369, 0.8957117602394122, 0.898027575760615, 0.9003187714021929, 0.9025852843498599,
		0.9048270524660189, 0.9070440142914643, 0.9092361090470679, 0.9114032766354445, 0.9135454576426001, 0.9156625933395603,
		0.9177546256839804, 0.9198214973217368, 0.9218631515884997, 0.923879532511286, 0.925870584809994, 0.9278362538989191,
		0.9297764858882506, 0.931691227585548, 0.9335804264972009, 0.9354440308298665, 0.9372819894918906, 0.9390942520947082,
		0.9408807689542246, 0.9426414910921775, 0.9443763702374801, 0.9460853588275444, 0.9477684100095848, 0.949425477641903,
		0.9510565162951526, 0.9526614812535853, 0.9542403285162759, 0.9557930147983292, 0.9573194975320664, 0.9588197348681922,
		0.9602936856769422, 0.9617413095492104, 0.9631625667976572, 0.9645574184577972, 0.9659258262890674, 0.9672677527758758,
		0.9685831611286303, 0.9698720152847459, 0.9711342799096352, 0.9723699203976757, 0.9735789028731594, 0.9747611941912209,
		0.9759167619387465, 0.9770455744352627, 0.9781476007338048, 0.979222810621765, 0.980271174621721, 0.9812926639922444,
		0.9822872507286878, 0.9832549075639538, 0.9841956079692412, 0.9851093261547732, 0.9859960370705042, 0.9868557164068066,
		0.987688340595137, 0.9884938868086828, 0.9892723329629877, 0.9900236577165569, 0.9907478404714429, 0.9914448613738098,
		0.9921147013144772, 0.992757341929445, 0.9933727656003959, 0.9939609554551792, 0.9945218953682728, 0.9950555699612258,
		0.9955619646030796, 0.9960410654107691, 0.9964928592495039, 0.9969173337331275, 0.9973144772244577, 0.997684278835605,
		0.9980267284282712, 0.998341816614028, 0.9986295347545736, 0.9988898749619697, 0.9991228300988582, 0.999328393778656,
		0.9995065603657314, 0.9996573249755571, 0.9997806834748454, 0.9998766324816605, 0.999945169365512, 0.9999862922474267, 1.0 ]

	
});


if (GSSI.Spinpanel._oldIE) {
	(function() {
		var dir = ""
		try {
			var scripts = document.getElementsByTagName("script");
			var s = ""; var found = false;
			for(var i=0;i<scripts.length;i++){
				s = scripts[i].src;
				if ( s.substr(s.length-17) == "gssi.spinpanel.js" ) {
					found = true;
					break;
				}
			}
			if (found) {
				dir = s.substr(0,s.length-17);
			}
		} catch (ex) {
		}
		
		GSSI.Spinpanel._path = dir;
	})();
}


GSSI.Events.onDOMReady( GSSI.Spinpanel._buildall );

