/* Copyright (c) 2008 Inticco Solutions ISSA SA, www.inticco.com */

window.onload = function()
{
	if(!document.getElementById)
	{
		return;
	}
	var i, x, y = document.getElementById('form-compare');
	if(y)
	{
		for(i in x = y.getElementsByTagName('fieldset')[0].getElementsByTagName('div'))
		{
			x[i].onmouseover = function()
			{
				this.className = 'show-hint';
			};
			x[i].onmouseout = function()
			{
				this.className = '';
			};
		}
		myFancySlider.init();
		x = document.getElementById('form-compare-action');
		x.onmouseover = function()
		{
			this.src = this.src.replace(/\-1\.png$/, '-2.png');
		};
		x.onmouseout = function()
		{
			this.src = this.src.replace(/\-2\.png$/, '-1.png');
		};
	}
};

var myFancySlider = {
	el: ['form-compare-type-1','form-compare-type-2','form-compare-type-3'],
	objs: [],
	funs: [],
	init: function()
	{
		var i, x, y, z;
		myFancySlider.funs['form-compare-type-1'] = function(st)
		{
			document.getElementById('form-compare-type-1').value = st;
			myFancySlider.recalculate();
		};
		myFancySlider.funs['form-compare-type-2'] = function(st)
		{
			document.getElementById('form-compare-type-2').value = st;
			myFancySlider.recalculate();
		};
		myFancySlider.funs['form-compare-type-3'] = function(st)
		{
			document.getElementById('form-compare-type-3').value = st;
			myFancySlider.recalculate();
		};
		for(i = 0, x = myFancySlider.el; i < x.length; i++)
		{
			y = document.getElementById(x[i]);
			y.parentNode.style.display = 'none';
			y.onchange = function()
			{
				myFancySlider.recalculate();
			};
			z = (y.value >= 0) ? parseInt(y.value) : 33;
			y = document.getElementById(x[i] + '-slider');
			y.parentNode.style.display = 'block';
			myFancySlider.objs[x[i]] = new Slider(y, x[i] + '-knob', {steps: 100, onChange: myFancySlider.funs[x[i]]}).set(z);
		}
		document.getElementById('form-compare-cost').onchange = function()
		{
			myFancySlider.recalculate();
		};
		myFancySlider.recalculate();
	},
	recalculate: function()
	{
		var i, v, x, y = 0, z;
		for(i = 0, x = myFancySlider.el; i < x.length; i++)
		{
			y += parseInt(document.getElementById(x[i]).value);
		}
		y = parseInt(y);
		for(i = 0, x = myFancySlider.el; i < x.length; i++)
		{
			v = parseInt(document.getElementById('form-compare-cost').value);
			z = (y > 0) ? Math.round(parseInt(document.getElementById(x[i]).value) / y * 100) : 0;
			document.getElementById(x[i] + '-value').innerHTML = '(' + z + '%' + ((v > 0 && z != 0) ? ', ok. ' + Math.round(v * z / 100) + ' PLN)' : ')');
		}
	}
}
