// wojtek at icenter dot pl
var tolpa = {}
// inputy
function tolpa_inputEdit(string)
{
	var T = this
	this.domInput = jso.gI(string)
	if(!this.domInput)
		return;
	this.defaultText = jso.gAV('label',this.domInput.parentNode,'htmlFor',string)[0].firstChild;
	if(!this.defaultText)
		return;
	this.defaultText = this.defaultText.nodeValue.replace(/^[ 	\n]*(.*)[ 	\n]*$/gi,'$1');
	if(!this.defaultText)
		return;
	this.inputType = this.domInput.type.toLowerCase();
	this.domInput.onfocus = function(){T.eventAction.call(T,1)}
	this.domInput.onblur = function(){T.eventAction.call(T,0)}
	this.domInput.form.inputsToClean[this.domInput.form.inputsToClean.length] = this;
	this.domInput.title = this.defaultText
	this.eventAction(0)
}
tolpa_inputEdit.prototype.eventAction = function(plus)
{
	if(plus && this.domInput.value == this.defaultText)
	{
		this.domInput.value = '';
		if(this.inputType == 'password' && !jso.ie)
			this.domInput.type = 'password';
	}
	else if(!plus && !this.domInput.value)
	{
		this.domInput.value = this.defaultText;
  if(this.inputType == 'password' && !jso.ie)
			this.domInput.type = 'text';
	}
}
// inputsToClean
function tolpa_submitForm()
{
	var forms = jso.gT('form')
	for(var i in forms)
	{
		if(!forms[i].inputsToClean)
		{
			forms[i].inputsToClean = []
			forms[i].onsubmit = function()
			{
				if(this.inputsToClean)
				{
					for(var j in this.inputsToClean)
					{
						this.inputsToClean[j].eventAction.call(this.inputsToClean[j],1);
					}
				}
			}
		}
	}
}
// mapki
function tolpa_maps(params)
{
	for(var i in params)
		this[i]=params[i];
	var domItems = jso.gAV('li',this.domList,'className','item');
	for(var i in domItems)
		new this.map({domItem:domItems[i],maps:this});
}
tolpa_maps.prototype.eventAction = function(item)
{
	if(this.active)
		this.active.domItem.className = 'item';
	this.active = item
	this.active.domItem.className = 'item active'
	this.domList.style.paddingBottom = this.active.domContent.offsetHeight + 'px'
	return false;
}
tolpa_maps.prototype.map = function(params)
{
	var T = this
	for(var i in params)
		this[i]=params[i];
	this.domHeader = jso.gAV('h3',this.domItem,'className','header')[0]
	this.domContent = jso.gAV('div',this.domItem,'className','item_content')[0]
	this.domHeader.onclick = function(){return T.maps.eventAction.call(T.maps,T)}
	this.domHeader.onfocus = function(){return T.maps.eventAction.call(T.maps,T)}
	if(this.domItem.className.indexOf('active') > -1)
			this.maps.eventAction.call(this.maps,this);
	var domItems = jso.gAV('li',this.domItem,'className','point')
	if(domItems)
	{
		this.domSelect = jso.cE
		(
			'select',
			{
				onchange:function()
				{
					var point = this.options[this.selectedIndex].point
					if(point)
						T.eventAction.call(T,point,true);
					else
						T.eventAction.call(T);
				}
			},
			[jso.cE('option',{value:0},[i18n.city])]
		);
		for(var i in domItems)
			new this.point({domItem:domItems[i],map:this});
		jso.cE('label',{className:'city'},[jso.cE('span',{className:'auraltext'},[i18n.city]),this.domSelect],this.domContent)
	}
}
tolpa_maps.prototype.map.prototype.eventAction = function(item,status)
{
	if(item)
	{
		if(this.active == item && item.status == status && !status)
		{
			this.active.domItem.className = 'point';
			this.active.status = false;
			this.active = false;
			this.domSelect.options[0].selected="selected";
		}
		else
		{
			if(this.active != item)
			{
				if(this.active)
				{
					this.active.domItem.className = 'point';
					this.active.status = false;
				}
				this.active = item;
				this.active.domItem.className = 'point active_point';
				this.active.domOption.selected = "selected";
			}
			this.active.status = status;
		}
	}
	else
	{
		if(this.active)
		{
			this.active.domItem.className = 'point';
			this.active.status = false;
			this.active = false;
		}
		this.domSelect.options[0].selected="selected";
	}
	return false;
}
tolpa_maps.prototype.map.prototype.point = function(params)
{
	var T = this
	for(var i in params)
		this[i]=params[i];
	this.status = false;
	this.domHeader = jso.gAV('h4',this.domItem,'className','header')[0]
	this.domOption = jso.cE('option',{point:this},[jso.gT('span',this.domHeader)[0].firstChild.nodeValue],this.map.domSelect)
	this.domHeader.onclick = function(){return T.map.eventAction.call(T.map,T)}
	this.domHeader.onfocus = function(){return T.map.eventAction.call(T.map,T,true)}
	this.domHeader.onblur = function(){return T.map.eventAction.call(T.map)}
	this.domHeader.onmouseover = function(){T.domItem.className += " hover_point"}
	this.domHeader.onmouseout = function(){T.domItem.className = T.domItem.className.replace(/ *hover_point/gi,'')}
}
// match
function tolpa_match(params)
{
	var T = this;
	for(var i in params)
		this[i]=params[i];
	this.domContent = false;
	this.status = false;
	this.domAnachor.onclick = function(){return T.eventAction.call(T)}
}
tolpa_match.prototype.eventAction = function()
{
	var T = this;
	if(this.ajax)
	{
		if(this.domContent)
		{
			this.domContent.style.display = this.status ? 'none' : '';
			this.status = !this.status;
		}
	}
	else
	{
		this.ajax = new jso.ajax
		(
			{
				uri:this.domAnachor.href.replace('/show/','/ajax/'),
				onend:function(ajax)
				{
					var domTmp = jso.cE('div')
					domTmp.innerHTML = ajax.responseText;
					T.domContent = jso.gAV('div',domTmp,'className','ajax')[0];
					if(T.domContent)
					{
						T.domAnachor.parentNode.appendChild(T.domContent)
						T.status = true;
					}
				}
			}
		);
	}
	return false;
}
// rate
function tolpa_rate(params)
{
	var T = this;
	for(var i in params)
		this[i]=params[i];
	this.domLabel = this.domInput.parentNode;
	this.domInput.onclick = function(){T.click.call(T,true)}
	this.domLabel.onclick = function(){T.click.call(T,true)}
	this.domLabel.onchange = function(){T.click.call(T)}
}
tolpa_rate.prototype.click = function(clicked)
{
	if(clicked)
	{
		this.domInput.checked = true;
	}
	var inputs = this.domInput.form[this.domInput.name],i=0,className;
	for(;i<inputs.length;i++)
	{
		className = inputs[i].parentNode.className.replace(/ *checked */gi,'') + (inputs[i].checked ? ' checked' : '');
		inputs[i].parentNode.className = className;
	}
}
// rateScroll
function tolpa_rateScroll(domElement)
{
	var T = this;
	this.mouse = false;
	this.X = 0;
	this.domInputs = jso.gT('input',domElement);
	jso.aE(document,'mouseup',function(){T.mouse = false});
	jso.aE(document,'mousemove',function(e){T.move.call(T,e)});
	this.scrollElement = jso.cE('span',{className:'rate_scroll'},0,domElement,
	{
		'click':function(e){T.getOffset.call(T);T.move.call(T,e,true);jso.sE(e)},
		'mousedown':function(e){T.getOffset.call(T);T.mouse = true;jso.sE(e)},
		'dragstart':function(e){jso.sE(e)}
	})
}
tolpa_rateScroll.prototype.getOffset = function()
{
	this.X = 0;
	element = this.scrollElement;
	while(element && element.nodeName && element.nodeName.toLowerCase() != 'body')
	{
		this.X += element.offsetLeft;
		element = element.offsetParent;
	}
}
tolpa_rateScroll.prototype.move = function(e,force)
{
	if(this.mouse || force)
	{
		e = e||window.event;
		var X = ( e.pageX == undefined ? e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft : e.pageX ) - this.X;
		X = X < 9 ? 9 : (X > 191 ? 191 : X);
		this.scrollElement.style.backgroundPosition = (X - 4) + "px 0";
		this.domInputs[Math.floor(X/40)].checked = true;
		jso.sE(e)
	}
}
// ambasadorki
function tolpa_ambasadorki(domElement)
{
	var T = this;
	this.ile = 0;
	this.interval = false;
	this.scroll = 0;
	this.stepTo = 0;
	this.constX = 60;
	this.itemWidth = 100;
	this.domElement = domElement;
	this.domUl = jso.gT('ul',domElement)[0];

	this.domNext = jso.gT('a',jso.gI('a_menu_next'))[0];
	this.domNext.onclick = function(){T.startF.call(T,1)};
	this.domPrev = jso.gT('a',jso.gI('a_menu_prev'))[0];
	this.domPrev.onclick = function(){T.startF.call(T,-1)};
	this.moveItems();
	var domLis = jso.gT('li',this.domUl);
	this.domUl.style.width = (domLis.length * 100) + 'px';
	this.person = document.location.toString().match(/\/person\//) ? true : false;
	var person = false;
	for(var i=0;i<domLis.length;i++)
	{
		new tolpa_ambasadorka(domLis[i]);
 		if(this.person === true && (i < 1 || i > 8))
 		{
 			if(jso.gAV('img',domLis[i],'className','active')[0])
 			{

 				this.stepTo = i<1?0:i-domLis.length;
				this.ile = -1;
 				this.startF(-1);
				person = true;
 			}
 		}
	}
	if(this.person === false)
	{
		this.timeoutAuto = false;
		this.startAuto();
	}
	else if(!person)
	{
		this.person = false;
	}
}
tolpa_ambasadorki.prototype.moveItems = function()
{
	var domLis = jso.gT('li',this.domUl);
	if(this.ile > 0)
	{
		if(this.stepTo)
		{
			this.stepTo --;
		}
		this.domUl.appendChild(domLis[0]);
	}
	else
	{
		if(this.stepTo)
		{
			this.stepTo ++;
		}
		this.domUl.insertBefore(domLis[domLis.length-1], domLis[0]);
	}
	this.domUl.style.left = this.constX - this.itemWidth + 'px';
	this.scroll = 0;
}
tolpa_ambasadorki.prototype.startAuto = function()
{
	if(!this.timeoutAuto)
	{
		var T = this;
		this.timeoutAuto = window.setTimeout
		(
			function()
			{
				this.timeoutAuto = false;
				T.startF.call(T,T.ile?T.ile:1);
			}
			,1500
		)
	}
}
tolpa_ambasadorki.prototype.stopAuto = function()
{
	if(this.timeoutAuto)
	{
		window.clearTimeout(this.timeoutAuto);
		this.timeoutAuto = false;
	}
}
tolpa_ambasadorki.prototype.startF = function(ile)
{
	this.stopAuto()
	if(this.ile != ile)
	{
		this.ile = ile;
		this.stepTo = this.stepTo != 0 ? 0 : this.ile;
	}
	else
	{
		this.stepTo += this.ile;
	}
	if(!this.interval)
	{
		var T = this;
		this.interval = window.setInterval(function(){T.scrollF.call(T)},20);
	}
}
tolpa_ambasadorki.prototype.stopF = function()
{
	if(this.interval)
	{
		window.clearInterval(this.interval);
		this.interval = false;
	}
	if(!this.person)
	{
		this.startAuto();
	}
	else
	{
		this.person = false;
	}
}
tolpa_ambasadorki.prototype.scrollF = function()
{
	if(this.stepTo != 0 || this.scroll != 0)
	{
		this.scroll += Math.abs(this.stepTo) > 1 ? this.ile * Math.abs(this.stepTo) * 2: Math.round(this.ile*(1+(100-Math.abs(this.scroll))/30));
		if(Math.abs(this.scroll) >= this.itemWidth)
		{
			this.moveItems();
		}
		else
		{
			this.domUl.style.left = (-this.scroll) + this.constX - this.itemWidth + 'px';
		}
	}
	else
	{
		this.stopF();
	}
}
function tolpa_ambasadorka(domNode)
{
	var images = jso.gT('img',domNode);
	if(images[1])
	{
		var T = this;
		this.ile = 0;
		this.zoom = 68;
		this.domNode = domNode;
		this.domImage = images[1];
		this.interval = false;
		this.display = '';
		this.domNode.onmouseover = function(){T.startF.call(T,2)};
		this.domNode.onmouseout = function(){T.startF.call(T,-1)};
	}
}
tolpa_ambasadorka.prototype.startF = function(ile)
{
	this.ile = ile;
	if(!this.interval)
	{
		var T = this;
		this.interval = window.setInterval(function(){T.zoomF.call(T)},15);
	}
}
tolpa_ambasadorka.prototype.stopF = function()
{
	if(this.interval)
	{
		window.clearInterval(this.interval);
		this.interval = false;
	}
}
tolpa_ambasadorka.prototype.zoomF = function()
{
	if((this.zoom > 68 || this.ile > 0) && ( this.zoom < 88 || this.ile < 0))
	{
		this.zoom += this.ile;
		this.zoom = this.zoom < 68 ? 68 : (this.zoom > 88 ? 88 : this.zoom);
		var xy = Math.round((88 - this.zoom) / 2) + 1 + 'px';
		this.domImage.width = this.zoom - 4;
		this.domImage.height = this.zoom - 4;
		this.domImage.style.top = xy;
		this.domImage.style.left = xy;
		jso.sO(this.domImage,(this.zoom-68)/20);
	}
	else
	{
		this.stopF();
	}
	var display = (this.ile > 0 || this.zoom > 68) ? 'block' : '';
	if(this.display != display)
	{
		this.display = display;
		this.domImage.style.display = display;
	}
}
// questions
function tolpa_question(domAnachor)
{
	var T = this;
	this.active = false;
	this.domAnachor = domAnachor;
	this.id = this.domAnachor.hash.replace(/#/,'');
	this.domElement = jso.gI(this.id)
	if(this.domElement)
	{
		this.domAnachor.onclick = function(){return T.click.call(T);}
		if(document.location.hash.replace(/#/,'') == this.id)
			this.click();
	}
}
tolpa_question.prototype.click = function()
{
	this.active = !this.active;
	if(this.active)
	{
		this.domElement.className = '';
		this.domAnachor.style.textDecoration = 'none';
	}
	else
	{
		this.domElement.className = 'auraltext';
		this.domAnachor.style.textDecoration = '';
	}
	return false;
}
// start
function tolpa_start()
{
	// guzik drukowania
	var domList = jso.gI('footer_menu');
	if(domList)
		jso.cE('a',{id:'print',href:"#print",onclick:function(){window.print();return false;}},[jso.cE('img',{src:'/images/print.png'}),i18n.print],domList)
	// mapki
	var domList = jso.gI('maps')
	if(domList)
		new tolpa_maps({domList:domList});
	// wynik sondy
	var domList = jso.gI('match_result')
	if(domList)
	{
		var domAnachors = jso.gT('a',domList);
		for(var i in domAnachors)
			new tolpa_match({domAnachor:domAnachors[i]});
	}
	// formularzyki
	tolpa_submitForm();
	var inputs = ['search_q','newsletter_email']
	for(var i in inputs)
		new tolpa_inputEdit(inputs[i]);
	// add MyBox
	var images = jso.gAV('a',document,'rel','lightbox')
	if(images)
	{
		myBox = new jso.myBox()
		for(var i in images)
			myBox.addImage.call(myBox,{domAnachor:images[i]});
	}
	// rate
	var pes = jso.gAV('p',document,'className','rate_inputs');
	for(var i in pes)
	{
// 		var inputs = jso.gAV('input',pes[i],'type','radio');
// 		for(var j in inputs)
// 		{
// 			new tolpa_rate({domInput:inputs[j]});
// 		}
		new tolpa_rateScroll(jso.gT('strong',pes[i])[0])
	}
	// ambasadorki
	var domElement = jso.gI('a_menu')
	if(domElement)
		new tolpa_ambasadorki(domElement);
	// questions
	var domElement = jso.gI('other_questions')
	if(domElement)
	{
		domElement = jso.gT('a',jso.gT('dl',domElement)[0]);
		for(var i in domElement)
		{
			new tolpa_question(domElement[i]);
		}
	}
}
jso.aE(window,'load',tolpa_start)



function maps_show(id_li, id_li_ml)
{
	var length_list = $('ul#maps_list li').length;
	for(var k=0; k<length_list; k++)
	{
		$('ul#maps_list li').removeClass('active');
		$('ul#maps li').removeClass('active');
	}
	$('ul#maps li#'+id_li).addClass('active');
	$('ul#maps_list li#'+id_li_ml).addClass('active');
}