/* ei.js 
 * dependencies: 
 * 		MooTools 1.2.2 Core
 * 		MooTools 1.2.2 More
 */

// let's create a global scope
var ei = $H({
	version: '1.0'
});

ei.fixedAd = new Class({
	 
	initialize : function(id,options){
		window.addEvent('domready',function(){
			if(!document.id(id)) return;
			this.fixed = document.id(id);
			this.body = document.getElement('body');
			this.footernav = document.getElement('.footernav');
			this.offset = {
				'top': this.fixed.getCoordinates().top,
				'bottom': this.footernav.getPosition().y-this.fixed.getCoordinates().height
			};
			if (Browser.Engine.trident4) {
				this.ie6scrolled();
				window.addEvent('scroll', this.ie6scrolled.bind(this));
				return;
			}
			this.scrolled();
			window.addEvent('scroll', this.scrolled.bind(this));
		}.bind(this));
	},
 	
	ie6scrolled: function(){
		var scroll = window.getScroll();
		if (this.aboveZone(scroll)) {
			this.fixed.setStyles({
				"position":"absolute"
			});
			this.fixed.tween('top',5);
			return;
		}
		if (this.belowZone(scroll)) {
			this.fixed.setStyles({
				"position":"absolute"
			});
			this.fixed.tween('top',this.offset.bottom);
			return;
		}
		this.fixed.setStyle('position','absolute');
		this.fixed.tween('top',(scroll.y-this.offset.top));
	},
	
	scrolled : function(){
		var scroll = window.getScroll();
		if(this.aboveZone(scroll)){
			this.fixed.setStyles({
				top: this.offset.top - scroll.y + "px",
				'bottom':'auto',
				'position':'fixed'
			});
			return;
		}
		if(this.belowZone(scroll)){
			var bodyScrollSize = this.body.getScrollSize();
			var bodyCoordinates = this.body.getCoordinates();
			var footerPos = this.footernav.getPosition();
			this.fixed.setStyles({
				top: "auto",
				bottom: ((bodyScrollSize.y - footerPos.y) - (bodyScrollSize.y - scroll.y - bodyCoordinates.height)) + "px",
				'position':'fixed'
			});
			//console.log((bodyScrollSize.y - footerPos.y) - (bodyScrollSize.y - scroll.y - bodyCoordinates.height));
			//console.log(bodyScrollSize.y,footerPos.y,bodyScrollSize.y,scroll.y,bodyCoordinates.height);
			return;
		}
		if (this.fixed.getStyle('top') != 5) {
			this.fixed.setStyles({
				"top": "5px",
				'bottom':'auto',
				'position':'fixed'
			});
		}
	},
	aboveZone: function(scroll){
		return (scroll.y <= this.offset.top);
	},
	belowZone: function(scroll){
		return (scroll.y > this.offset.bottom);
	}
});

/* With permission from paal@api.no 24.09.09. - Peter Haza */
ei.moveAd  = function (elementMoveFrom,elementMoveTo)
{
	var newPos=document.getElementById(elementMoveTo);
	var oldPos=document.getElementById(elementMoveFrom);
	if(newPos && oldPos) {
		var x=oldPos.getElementsByTagName('script');
		if(x!==null) {
			for(i=0;i<x.length;i++) {
				x[i].parentNode.removeChild(x[i]);
			}
		}
		var body=oldPos.parentNode;
		body.removeChild(oldPos);
		newPos.innerHTML='';
		newPos.appendChild(oldPos);
	}
}


ei.poll = new Class({
		initialize: function(container){
			this.container = container;
			this.attachEvents();
		},
		attachEvents: function(){
			var poll = this;
			this.container.addEvents({
				'click:relay(.pollswitchview)': function(e,el){
					e.stop();
					this.loadView('url',el.get("href"));
				}.bind(this),
				'click:relay(.pollform input[type=radio])': function(e,el){
					e.stop();
					var form = $(el.form)
					form.removeProperty('target');
					form.set('send',{
						onComplete:function(r){
							this.loadView('html',r);
						}.bind(this)
					});
					form.send();
				}.bind(this)
			});
		},
		loadView:  function(type,content){
			var poll = this;
			if(type == "url"){
				var myRequest = new Request({
					url: content,
					onComplete:function(r){
						poll.container.set('html',r);
					}
				}).send();
			}else{
				poll.container.set('html',content);
			}
		}
	});

window.addEvent('domready',function(){
		document.getElements('.poll').each(function(item){
			var bd = item.getParent('.bd');
			bd.store('poll',new ei.poll(bd));
		});
	})
	
/*
window.addEvent('domready',function(){
	ei.domready.run();
});
*/