// wojtek at icenter dot pl
var im = {}
// inputy
im.inputEdit = function(string)
{
	var T = this
	this.domInput = jso.gI(string)
	if(!this.domInput)
		return;
	if(this.domInput.parentNode.htmlFor == string)
		this.defaultText = jso.gT('span',this.domInput.parentNode)[0];
	else
		this.defaultText = jso.gAV('label',this.domInput.parentNode,'htmlFor',string)[0];
	this.defaultText = this.defaultText.firstChild.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)
}
im.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
im.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);
					}
				}
			}
  	}
	}
}
// products
im.products = function(params)
{
	var T = this
	for(var i in params)
		this[i]=params[i];
	var domSelects = jso.gT('select',this.domForm)
	for(var i in domSelects)
	{
		this[domSelects[i].name] = domSelects[i][domSelects[i].selectedIndex].value;
		domSelects[i].onchange = function(){T[this.name]=this.options[this.selectedIndex].value;T.change.call(T)}
	}
	var domInputs = jso.gT('input',this.domForm)
	for(var i in domInputs)
	{
		if(domInputs[i].name)
			this[domInputs[i].name] = domInputs[i].value;
	}
}
im.products.prototype.change = function()
{
	var href = [this.site_root + this.lang,'products',this.id,0,this.order,this.manufacturer,this.pages,this.title]
	document.location = href.join('/')
}
// rounds
im.rounds = function(domElement)
{
	jso.cE('span',{className:'round rTL'},0,domElement)
	jso.cE('span',{className:'round rTR'},0,domElement)
	jso.cE('span',{className:'round rBR'},0,domElement)
	jso.cE('span',{className:'round rBL'},0,domElement)
}

// start
im.start = function()
{
	// formularzyki
	im.submitForm();
	var inputs = ['box_search_q','box_other_newsletter_email','box_top_login_login','box_top_login_password']
	for(var i in inputs)
		new im.inputEdit(inputs[i]);
	var rounds = ['button_change','button_register','button_buy','product_form']
	for(var i in rounds)
	{
		var domElement = jso.gI(rounds[i])
		if(domElement)
			new im.rounds(domElement);
	}
	var domForm = jso.gI('products_form')
	if(domForm)
		new im.products({domForm:domForm});
	var domAnachors = jso.gAV('a',document,'className','back')
	for(var i in domAnachors)
		domAnachors[i].onclick = function(){return !history.back();};
	// 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',im.start)

var imm = {
	load : function(){
		$('#index_advertisement').cycle('fade');
	}
}
$(document).ready(function(){
	imm.load();
})
