 ///////////////////////////////////////////////////////////
 
  window.addEvent('domready', function() {
  var product_desc = $('product_desc');
  if ( product_desc ){  
    $$('#product_desc_link , #product_details_link, #product_addons_link').addEvent('click', function(e){
      product_desc.set('class', this.get('rel') );
      Cookie.write('product_desc_class',this.get('rel'),{'path':'/'});
      });
    }
 
 var actual_promotions = $('actual_promotions');
 if ( actual_promotions ){
  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
  var actual_promotion_slider = actual_promotions.getElement('#actual_promotion_slider');
  var actual_promotions_list = actual_promotions.getElements('.promo_pager a');
  if ( actual_promotions_list ){
    actual_promotions_list.each(function(el,i){
      el.addEvent('click',function(e){
        new Event(e).stop();
        var marginLeft = -(240 * i);
        actual_promotions_list.removeClass('active');
        //console.log( el );
        el.addClass('active');
        Cookie.write('actual_promotion',i,{'path':'/'});
        actual_promotion_slider.morph({'marginLeft':marginLeft+'px'});  
        });
      });    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     var Parms = { actual_promotions_list:actual_promotions_list , actual_promotions_count : actual_promotions_list.lenght };
     var changePromo = function(){  
      var actual_promotion = 0;
      this.actual_promotions_list.each(function(el,i){
        if ( el.hasClass('active') ){
          actual_promotion = i;
          }
        });  
      var index = ( actual_promotions_list[actual_promotion+1] ) ? actual_promotion+1 : 0;  
      actual_promotions_list[index].fireEvent('click',actual_promotions_list[index]);
      actual_promotions_list.removeClass('active');
      actual_promotions_list[index].addClass('active');
      }; 
     changePromo.periodical(10000,Parms);
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    }
  } 
 });




////////////////////////////////////////////////////////////////////








function add_to_basket(prod_basket){
 var prod_basket = $(prod_basket);
 if ( prod_basket ){
   var valid = true;
   var choose_options = prod_basket.getElements('.choose_options');
   if ( choose_options ){
     choose_options.each(function(el){
       if ( el.get('value') == '' && valid ){
         valid = false;
         alert('Wybierz '+el.get('title')+'.' );      
         el.focus();          
         }
       });
     }
   if ( valid ){
     prod_basket.submit();
     }      
   }  
 }







/*----------------------------------------------------------*/
function popup( url , name , width , height ){
  var left = (screen.width > width ) ? (screen.width-width)/2 : 0;
  var top = (screen.height > height ) ? (screen.height-height)/2 : 0; 
  var strWindowFeatures  = 'modal=yes,menubar=no,toolbar=no,location=no,resizable=yes,scrollbars=yes,status=no,titlebar=no';   
  url += ( url.contains('?') ? '&' : '?' ) + 'width='+width+'&height='+height+'&top='+top+'&left='+left;
  return window.open(url,name,strWindowFeatures+",width="+width+",height="+height+",top="+top+",left="+left);
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/
 
function $save(id){
  if ( $(id) ) $(id).submit();
  return false;
  }	

/*--------------------------------------------------------------------------------------------------------------------------------*/

function menu (obj){
  var li = $(obj).getParent('li');
  var ul = li.getElement('ul');
  ul.toggleClass('hide');  
  if ( li.hasClass('active' ) ){
  	li.removeClass('active');
  	}    
  else if ( !ul.hasClass('hide') ){
  	li.addClass('active');
  	}
  if ( li.hasClass('expanded') ){
  	li.removeClass('expanded');
  	}
  else {
  	li.addClass('expanded');
  	}
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/
  
function input_clone( input , type ){
  var input = $(input);  
  input.removeEvents();
  var new_input = new Element('input',{'type':type,'class':input.className,'name':input.name,'id':input.id,'style':input.style,'value':input.value,'title':input.title});  
  //var new_input = new Element( 'input' , { 'type' : type } ); new_input.setProperties( input.getProperties( 'class','name','id','style','value','title' ) );    
  new_input.replaces(input);
  if ( type == 'password' ){
    new_input.focus();
    new_input.focus();//IE...
    }
  else{
    new_input.blur();
    }
  clear_input(new_input);
  return new_input;
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/

function clear_input( input ){
  var input = $(input);
  if ( input && input.title ){
    if ( input.value == '' ){
      input.value = input.title;
      }
    input.addEvent('focus',function(){
      if ( input.title.trim() == input.value.trim() ){
        input.value = '';
        if ( input.name == 'password' || input.name == 'password_confirm' ){
          input_clone( input , 'password' );
          }
        }
      });
    input.addEvent('blur',function(){
      if ( input.value == '' ){
        if ( input.name == 'password' || input.name == 'password_confirm' ){
          input = input_clone( input , 'text' );
          }
        input.value = input.title;
        }
      });
    }
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/

function ajax_loader( container , mode ){	
  if ( !mode || mode == 'add' ){
    container = $(container);
    container.set('opacity',.7);
  	var size = container.getSize();
  	var al = new Element('div',{'class':'ajax_loader'});
  	al.inject(container,'top');
  	var ml = ( size.x / 2 ) - ( al.getStyle('width').toInt() / 2 ) - container.getStyle('paddingLeft').toInt();
  	var mt = ( size.y / 2 ) - ( al.getStyle('height').toInt() / 2 ) - container.getStyle('paddingTop').toInt();
  	al.setStyles({'marginTop':mt+'px','marginLeft':ml+'px'});	
  	return al;
    }
  else if ( mode == 'delete' ) {
    container.set('opacity',1);
    container.getElement('.ajax_loader').dispose();
    }
	}

/*--------------------------------------------------------------------------------------------------------------------------------*/

function module_refresh(obj,options){

			var parent_module = ( options && options.parent_module ) ? options.parent_module : obj.getParent('.module');
      var tag = ( options && options.tag ) ? options.tag : obj.get('tag');
      var parms = ( options && options.parms ) ? options.parms : ( tag == 'form' ) ? obj.toQueryString() : obj.get('href').replace(/[^\?]+\?/,'');
			var module_body = ( options && options.module_body ) ? options.module_body : obj.getParent('.module_body');
			var module_name = ( options && options.module_name ) ? options.module_name : parent_module.get('class').replace(/(module| |col[0-9]+)+/,'').trim();
      //var url = ( options && options.url ) ? options.url : $('body').get('lang')+'/xml/module.xml?module='+module_name;
      if ( options && options.url ){
      	var url = options.url;
      	}
      else {
      	var url = location.href + ( location.href.contains('?') ? '&' : '?' ) + 'xml=module&module='+module_name;
      	}
      var method = ( options && options.method ) ? options.method : ( ( tag == 'form' ) ? obj.get('action') : 'get' ) ;
			if ( parent_module ){
				var col = new RegExp(/col([0-9]+)/i).exec( parent_module.get('class') );
				if ( col ){
					parms += ( parms ? '&' : '' ) + '_col=' + col[1];
					}				
				}			
      new Request({method:method,url:url,evalResponse:false,
        onRequest:function(){
        ajax_loader(module_body);
        if ( options && options.start ){
          options.start.run();
          }
        },
        onSuccess:function(responseText){
          module_body.set('opacity',1);
          module_body.set('html',responseText);
          add_links(module_body);
          if ( options && options.finish ){
            options.finish.run();
            }
          }}).send(parms);  
  return false;
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/

function module_open( obj , parms ){
	return module_refresh( obj , {'parms':parms} );
	}

/*--------------------------------------------------------------------------------------------------------------------------------*/

function add_module_refresh(obj,options){
  if ( !options ){
    var options = {};
    }
  if ( obj ){
    options.tag = obj.get('tag');
    obj.addEvent(((options.tag=='form')?'submit':'click'),function(e){
      if ( e ){
      	new Event(e).stop();
      	}      
      options.module_obj = ( options && options.module_obj ) ? options.module_obj : obj.getParent('.module');
      options.module_name = options.module_obj.get('class').replace(/(module| |col[0-9]+)+/,'').trim();
      options.module_body = options.module_obj.getElement('.module_body') || options.module_obj;
      module_refresh(obj,options);
      });
    }
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/

function add_links(obj){
	
  if ( !obj ){
		obj = document;
		}	
	
	//obj.getElements('ul li:first-child').addClass('first_child');
	//obj.getElements('ul li:last-child').addClass('last_child');
	
  var forms = obj.getElements('form');
  if ( forms ){
    forms.addEvent('submit',function(e){
      $(this).getElements('input[type=text],textarea').each(function(el){      
        el = $(el);
        if ( el.title && el.title == el.value ){ 
          el.value = '';
          }
        });
      });
    }
  	
  var clear_inputs = obj.getElements('.clear_input');
  if ( clear_inputs ){
    clear_inputs.each(function(el){
      clear_input(el);
      });
    }	

	var _module_refresh = obj.getElements('.module_refresh');
	if ( _module_refresh ){
		_module_refresh.each(function(el){
		 	add_module_refresh(el);
		 	});
		}	

	obj.getElements('a[rel*=popup]').addEvent('click',function(e){
    new Event(e).stop();
    var size = this.get('rel').match(/\[(\d+),\s*(\d+)/);
    popup( this.get('href') , this.get('name') || 'popup' , size[1] , size[2] ).focus();
    });
    	
	}

/*--------------------------------------------------------------------------------------------------------------------------------*/

window.addEvent('domready',add_links);

/*--------------------------------------------------------------------------------------------------------------------------------*/

function ZagielPoliczRate(koszyk,wariantSklepu,numerSklepu) {
  window.open('https://www.eraty.pl/symulator/oblicz.php?numerSklepu='+numerSklepu+'&wariantSklepu='+wariantSklepu+'&typProduktu=0&wartoscTowarow='+koszyk,'Policz_rate',
  'width=630,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
  }
  
/*--------------------------------------------------------------------------------------------------------------------------------*/  

function ZagielJakKupic() {
  window.open('https://www.zagiel.com.pl/kalkulator/jak_kupic.html', 'nowe_okno','width=600,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
  }

/*--------------------------------------------------------------------------------------------------------------------------------*/  

Element.implement({
	hasEvent: function(eventType,fn) {
		//get the element's events
		var myEvents = this.retrieve('events');
		//can we shoot this down?
		return myEvents && myEvents[eventType] && (fn == undefined || myEvents[eventType].keys.contains(fn));
	}
});

