/**
 * @author Ideatronic
 */

function fieldBlur(e)
{
	field = e.target;
	if(field.value==='')
	{
		field.value=field.example; 
		field.className+= 'active'; 
	}
}
function fieldFocus(e)
{
	field = e.target;
	if(field.value==field.example)
	{
		field.value=''; 
		field.className=field.className.replace(" over", ""); 
	}
} 
 
function fieldInit(field, defValue)
{
	field = $(field);
	checkBefSubit(field,defValue);
	field.example = defValue;
	if(field.value === '')
		field.value = field.example;
	field.addEvent('blur',fieldBlur);
	field.addEvent('focus',fieldFocus);
}

function checkBefSubit(field,def){
	var $field = field;
	form = field.form;
	form = $(form);
	form.addEvent('submit',function(e){
		var ev = new Event(e);
		ev.stop();
		if(field.value == field.example || field.value === '')
		{
			field.value = '';
		}
		this.submit();
	});
}

var productTooltip = new Class({
    initialize:function(holder, tooltipHolder,options){
	this.ShowTips = new Tips( '', {
		showDelay: 400,
		hideDelay: 200,
		className:'tool-tip',
		fixed: false,
		title:'test',
		text:'some cos'
	});

	this.products = $$(holder);
	this.products.each(function(product, index){
	    product.SaveTip = product.getElement(tooltipHolder);  //product - obiekt z bierzacym prod
            if($defined(product.SaveTip)) {
                this.update(product); //dodaj tooltipsy
                var prod = product;
                product.getElement('select').addEvent('change', (function(){
                    this.update(prod);
                }).bind(this)); //i updejtuj po zminie))
            }
	}.bind(this));
    },

    update:function(currProduct){
	var SavePrice = currProduct.getElement('input.amount').value; //wartosc
	var ammount = currProduct.getElement('select').selectedIndex+1; // razy ilosc
	var SaveTxt = currProduct.getElement('span.savetxt').get('text'); // string : oszczędzasz

	var CenaProducenta = currProduct.getElement('p.priceValJS').get('text'); //wartosc ceny producenta
	var CenaProdTxt = currProduct.getElement('span.cena_prod').get('text'); // string : Cena producenta
	var CenaProducenta = parseFloat(CenaProducenta);
	var price = parseFloat(SavePrice);

	currProduct.getElement('p.discountJS').set('html',this.calcValue(price,ammount)+'zł'); // razy ilosc

	this.ShowTips.detach(currProduct.SaveTip);
//	currProduct.SaveTip.store('tip:title',SaveTxt+':</br>');
	currProduct.SaveTip.store('tip:text','<div class="tip-title">'+ CenaProdTxt+ ':</div>'+ CenaProducenta+  'zł / szt<div class="tip-title">'+ SaveTxt+ ':</div>'+ammount+' x '+parseInt(price)+'zł = '+this.calcValue(price,ammount)+'zł'); //bo inaczej nie chwyta textu od tagu
	this.ShowTips.attach(currProduct.SaveTip);
    },
    	
   	calcValue: function(price,amount)
   	{
   		return parseInt(price*amount);
   	}
})

function showPreloader()
{
	var container1 = $('Search_2');
	var preloader = $('Preload');
	
	if(container1)
	{
		container1.setStyle('display','none');
		preloader.setStyle('display','block');
	}
}

window.addEvent('domready',function(){
	var container1 = $('Search_2');
	var menuContainer = $('prodMenu');
	if(container1)
	{
		var inputs = container1.getElements('select');
		inputs.each(function(input,i){
			if(i<3)
				input.addEvent('change',showPreloader)
		})
	}
	if(menuContainer)
	{
		var menuElements = menuContainer.getElements('li');
		var expandLi = $('ExpandAll');
		expandLi.addEvent('click',function(){
			menuElements .each(function(element,i){
				element.setStyle('display','block')
				if(i>=(menuElements .length-1))
					element.setStyle('display','none')
			})
		})	
	}
});
