var dropSelect = Class.create({
	initialize: function(o) {
		this.target = o;
		this.onclick = this.click.bindAsEventListener(this);
		this.onmouseover = this.mouseover.bindAsEventListener(this);
		this.onmouseout = this.mouseout.bindAsEventListener(this);
		this.onscrolldown = this.scrolldown.bindAsEventListener(this);
		this.onscrollup = this.scrollup.bindAsEventListener(this);
		this.onscrollstop = this.scrollstop.bindAsEventListener(this);
		this.onchange = this.change.bindAsEventListener(this);
		this.onclickout = this.clickout.bindAsEventListener(this);
		$$('body').first().observe('click', this.onclickout);
		this.target.observe('click', this.onclick);
		this.target.observe('dropselect:change', this.onchange);
		this.target.select('li').each(function(o) {
			o.observe('mouseover', this.onmouseover);
			o.observe('mouseout', this.onmouseout);
		}.bind(this));
		h = document.viewport.getHeight();
		if(h < 700) {
			this.height = 316;
			this.maxheight = 300;
		} else {
			this.height = 418;
			this.maxheight = 400;
		}
		if(this.target.down('div').getHeight() > this.maxheight) {
			list = this.target.down('div');
			this.deltay = this.height - list.getHeight();
			list.setStyle({height: this.height+'px', padding: '10px 0px', overflow: 'hidden'});
			list.insert({top: new Element('div', {'class': 'topscroll'}).observe('mouseover', this.onscrollup).observe('mouseout', this.onscrollstop)});
			list.insert({bottom: new Element('div', {'class': 'bottomscroll'}).observe('mouseover', this.onscrolldown).observe('mouseout', this.onscrollstop)});
		}
	},
	observe: function(a, f) {
		this.target.stopObserving('dropselect:'+a);
		this[a] = f;
		this['on'+a] = this[a].bindAsEventListener(this);
		this.target.observe('dropselect:'+a, this['on'+a]);
	},
	click: function(e) {
		clicked = Event.element(e);
		if(clicked.tagName == 'LI') {
			this.target.down('span').update(clicked.innerHTML);
		}
		if(!clicked.hasClassName('hover')) {
			Effect.toggle(this.target.down('div'), 'blind', {duration: 0.25, queue: {scope: 'dropmenu', position: 'end'}});
		}
		if(clicked.tagName == 'LI') {
			this.target.fire('dropselect:change', { value: clicked.innerHTML });
			return;
		}
		Event.stop(e);
	},
	mouseover: function(e) {
		Effect.Queues.get('dropmenu').each(function(q) { if (q.element == e.target) q.cancel(); });
		new Effect.Morph(Event.element(e), {style: 'background:#36bfe5;font-weight:bold;', duration: 0.25, queue: {scope: 'dropmenu'}});
	},
	mouseout: function(e) {
		Effect.Queues.get('dropmenu').each(function(q) { if (q.element == e.target) q.cancel(); });
		new Effect.Morph(Event.element(e), {style: 'background:#109cce;font-weight:normal;', duration: 0.25, queue: {scope: 'dropmenu'}});
	},
	scrolldown: function(e) {
		Event.element(e).addClassName('hover');
		deltay = this.deltay - this.target.down('ul').positionedOffset().top + 10;
		new Effect.Move(this.target.down('ul'), { duration: 2.0, x: 0, y: deltay, mode: 'relative', queue: {scope: 'dropmenu'}});
	},
	scrollup: function(e) {
		Event.element(e).addClassName('hover');
		deltay = this.target.down('ul').positionedOffset().top - 10;
		new Effect.Move(this.target.down('ul'), { duration: 2.0, x: 0, y: -deltay, mode: 'relative', queue: {scope: 'dropmenu'}});
	},
	scrollstop: function(e) {
		Event.element(e).removeClassName('hover');
		Effect.Queues.get('dropmenu').each(function(q) { q.cancel(); });
	},
	change: function(e) {
		alert('onchange');
	},
	clickout: function(e) {
		if(this.target.down('div') && this.target.down('div').visible()) {
			Effect.toggle(this.target.down('div'), 'blind', {duration: 0.25, queue: {scope: 'dropmenu', position: 'end'}});
		}
	}
});
var pieChart = Class.create({
	defaultOptions: {
		radius: 'auto'
	},
	initialize: function(o, w) {
		var browser=navigator.appName;
		var browserVer = (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;
    //alert(browserVer);
    
		this.options = Object.extend(Object.extend({ },this.defaultOptions), arguments[2] || { });
		this.target = o;

		this.height = o.getHeight();
		this.width = o.getWidth();
		this.left = 0;
		this.top = 0;
		this.radius = this.options.radius == 'auto' ? Math.min(this.width/2, this.height/2) : this.options.radius;
		this.wedges = w;
		
		for(sum=0, i=0; i<w.length; i++) {
			if (w[i])
				sum += w[i];
		}
		this.ratio = 100/sum;
	
		for(j=0; j<w.length; j++) {
			if (w[j] >= 1) {
				this.div = new Element('div', { 'class': 'piePercent' }).setStyle({'display':'none', 'position': 'absolute','font-size': '9px'});
				this.target.insert({'after': this.div});
				this.div.update(Math.floor(w[j])+'%');
				with(Math) {
					off = sqrt(pow(this.div.getWidth(),2) + pow(this.div.getHeight(),2))/2;
				}
	 			c = this.getWedgeCenter(j, off);
	 				
	 			if (browser.search(/Microsoft/) >= 0)
	 			{
	 				if (browserVer >= 7)
	 				{
	 					this.div.style.left = (this.left + this.width/2 + c[0] - this.div.getWidth()/2) + 'px';
	 					this.div.style.top = (this.top + this.height/2 + c[1] + 20 - this.div.getHeight()/2) + 'px';
	 				}
	 				else
	 				{
						this.div.style.left = (this.left + this.width/2 + c[0] - 215 - this.div.getWidth()/2) + 'px';
						this.div.style.top = (this.top + this.height/2 + c[1] + 22 - this.div.getHeight()/2) + 'px';
					}
				}
				else
				{
					this.div.style.left = (this.left + this.width/2 + c[0] - this.div.getWidth()/2) + 'px';
					this.div.style.top = (this.top + this.height/2 + c[1] - this.div.getHeight()/2) + 'px';
				}
	 			this.div.show();
			}
		}
	},
	getWedge: function(ex, ey) {
		this.x = (ex - this.left) - (this.width/2);
		this.y = (this.height/2) - (ey - this.top);
		with(Math) {
			this.radial = sqrt(pow(this.x,2) + pow(this.y,2));
			this.theta = atan2(this.y, this.x) + (this.y<0 ? 2*PI : 0);
			this.degree = this.theta * (180/PI);
			this.percent = (1-(this.degree/360))*100
		}
		slice = 0;
		for(i=0;i<this.wedges.length;i++) {
			if(this.percent <= slice+(this.wedges[i]*this.ratio)) {
				break;
			}
			slice += (this.wedges[i]*this.ratio);
		}
		return i;
	},
	getWedgeCenter: function(wedge, offset) {
		for(sum=0,i=0; i<wedge; i++) sum += (this.wedges[i]*this.ratio);
		sum += (this.wedges[wedge]*this.ratio)/2;
		//alert((this.wedges[wedge]*this.ratio)/2);
		degree = (sum/100)*360;
		with(Math) {
			theta = degree * (PI/180);
			dx = (this.radius + offset) * cos(theta) ;
			dy = (this.radius + offset) * sin(theta) ;
		}
		//alert("radius: " + this.radius + " this.wedges[wedge]: " + this.wedges[wedge] + " this.ratio: " + this.ratio + " sum: " + sum);
		return [dx, dy];
	}
});

function drawPiePercents() {
	if (!Prototype.Browser.IE) {
		$$('img.pie').each(function(e) {
			e.observe('load', function() {
				pcts = this.up('div.question').select('div.percent').collect(function(p) {
					return parseInt(p.innerHTML);
				});
				new pieChart(this, pcts, {radius: 91} );
			}.bindAsEventListener(e));
		});
	}
	else {
		$$('img.pie').each(function(e) {
			pcts = e.up('div.question').select('div.percent').collect(function(p) {
				return parseInt(p.innerHTML);
			});
			new pieChart(e, pcts, {radius: 91} );
		});
	}
}
