// wojtek at icenter dot pl
var tolpa = {}
// inputy
tolpa.inputEdit = function(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
tolpa.submitForm = function()
{
	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
tolpa.maps = function(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
tolpa.match = function(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;
}
// start
tolpa.start = function()
{
	// 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]});
	}
}
jso.aE(window,'load',tolpa.start)