var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2765821-6']);
_gaq.push(['_gat._anonymizeIp']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

document.onload=function(){
	if(arguments.callee.inited) return;
	else arguments.callee.inited=1;
	
	_("table.base tr").event({onclick:function(){
	   var a=_(this, "a:0");
		if(a) location.href=a.href;
	}});

	_("select").repeat(function(){
		new Forms.select(this);
	});

	
	
	var form_group=_("form.contact ul.group");
	if(form_group) Calc.init(form_group);
	
	
	var fixed_block=_("div.full div.fixed");

	if(BROWSER.ie6){
		_("table.base tr").event({onmouseover:function(){this.classname("+hover")},onmouseout:function(){this.classname("-hover")}});
		if(fixed_block) window.onscroll=function(){
			fixed_block[0].style.top=document.documentElement.scrollTop+"px";
			fixed_block[1].style.top=document.documentElement.scrollTop+115+"px";
		}
	}
	
	if(fixed_block){
		var th=_("table.base tr").repeat(function(){
			var th=_(this, "td") || _(this, "th");								  
			th[0].css({width:70});
			th[1].css({width:150});
			th[th.length-2].css({width:75});
			th[th.length-1].css({width:75});
		});
		_("div.table_hdr").insert(_("table.base").cloneNode(true));
	}
};


var Calc={
	init:function(form_group){
		var _this=this;
		//init radio group sections
		_(form_group, "li").event({onclick:function(e){_this.onradio(this);}});
		_(form_group, "input").repeat(function(){if(this.checked) _this.onradio(_(this, "<li"))}); //call checked radio
		
		//init particip sections
		var form_particip=_("form.contact div.particip");
		this.select_particip=_(form_particip, ".select:0 input");
		this.select_particip.event({onchange:function(){
			_this.show_items(this.value, _(form_particip, "div.items div"));
		}});
		this.select_particip.onchange(); //call Calculator with default value

	},
	show_items:function(count, items){
		items.repeat(function(i){this.css({display: i<count?"block":"none"})});
		this.exec(1);
		this.exec(2);
	},
	onradio:function(obj, e){
		if(obj.parentNode.cur && obj.parentNode.cur!=obj) this.onradio(obj.parentNode.cur, 0);
		if(BROWSER.ie6 && (e==undefined || e)) _(obj, "input").checked=1;
		var b=_(obj, "input").checked && (e==undefined || e);
		
		obj.classname((b?"+":"-")+"act");
		obj.parentNode.cur=b?obj:null;
	},
	exec:function(num){
		_("count_"+num).innerHTML=this.select_particip.value-1
		_("count_"+num, "<").classname((this.select_particip.value<=1?"+":"-")+"gray");
		var res_netto=parseInt(_("price1_"+num).innerHTML)+parseInt(_("price2_"+num).innerHTML)*(this.select_particip.value-1);
		_("res_netto_"+num).innerHTML=this.intMoney(res_netto , 2);
		var perc_price=res_netto*_("perc_"+num).innerHTML/100;
		_("perc_price_"+num).innerHTML=this.intMoney(perc_price , 2);
		var res_total=res_netto+perc_price;
		_("res_total_"+num).innerHTML=this.intMoney(res_total , 2);
	},
	intMoney:function(double, c){
		var d=Math.pow(10, c);
		//return (Math.round(double*d)/d).toString().replace(/\./, ",");
		return Math.round(double*d).toString().replace(/(\d*)(\d{2})$/, "$1,$2").replace(/(\d+)(\d{3})(,\d{2})$/, "$1.$2$3");
	}
};


var RelSelects={
	curSelVals:{},
	construct:function(items, no){
		if(items){
			this.items=items;
			this.selects={};
		}
		for(var i in this.items) if(no!=i) this.createSelect(this.items[i], i);
		if(items){
			this.form=_("sel_block_3", "<form");
			if(window.SelectActive) this.autoSelect();
		}
	},
	createSelect:function(id, name){
		var _this=this;
		var data=this.curData?this.curData[name]:SelectData[name];
		var root=_(id);
		root.html("");
		var sel=root.insert("select").attr({name:name});
		sel.onchange=function(){_this.makeNewData(this.name)};
		//if(id=="sel_block_2") sel.onchange=function(){_this.makeNewData(this.name); _('sel_block_3', "input").disable(this.value=='0')};
		for(var i in data){
			var sel_item=sel.insert("option").attr({value:i}).html(data[i]);
			if(this.curSelVals[name] && i==this.curSelVals[name]) sel_item.attr({selected:"selected"})
		}
		var sel_obj=new Forms.select(sel);
		//if(id=="sel_block_3"){ sel_obj.disable(_('sel_block_2', "input").value=='0');}
		this.selects[name]=sel_obj;
	},
	makeNewData:function(sel_name){
		//alert(this.selects[sel_name].input);
		var curSelect=this.selects[sel_name].input;
		this.curSelVals[sel_name]=curSelect.value;
		this.curData={};//SelectData;
		for(var i in SelectData){
			if(i==sel_name){this.curData[i]=SelectData[i]; continue;}
			var cr=SelectRelations[sel_name+"_"+i][curSelect.value];
			if(cr===false ){this.curData[i]=SelectData[i]; continue;}
			this.curData[i]={};
			this.curData[i][0]=SelectData[i][0];
			if(cr===undefined) return ;
			for(var j=0; j<cr.length; j++){
				this.curData[i][cr[j]]=SelectData[i][cr[j]];
			}
		}
		this.construct(null, sel_name);
	},
	autoSelect:function(){
		for(var i in SelectActive){
			//alert("set("+i+")="+SelectActive[i]);
			this.selects[i].input.value=SelectActive[i];
			this.selects[i].update();
			//if(this.items[i]!="sel_block_3") 
			this.selects[i].input.onchange();
		}
	}
}
