
var newOthersBought;
 
var othersBought = Class.create();
othersBought.prototype = {
	
	clicked: false,
	
	initialize: function() {
		
		if( $('others_bought') ) {
			
			// setting up variables
			this.offset = 0;
			this.no_of_products = 4;
			this.total_others_bought = 0;
			this.show_loaders();
			this.load_next_products = true;
			this.load_prev_products = false;
		
			// loading in item elements
			var item_elms = new Array();
			$('others_bought').select('li').each(function(elm){
				if( elm.hasClassName('bought') ) {
					item_elms.push(elm);	
				}
			});
			this.item_elms = item_elms;
			
			// getting sku
			var base_classes = $w($('others_bought').className);
			this.sku = base_classes[0];
			
			$('others_bought').select('div').each(function(elm){
				if( elm.hasClassName('left') ) {
					elm.select('div').each(function(control_elm){
						Element.observe(control_elm,'click',function(e){ 
						  newOthersBought.switch_button(control_elm);
							newOthersBought.get_list('left');
						});
					});		
				}
				if( elm.hasClassName('right') ) {
					elm.select('div').each(function(control_elm){
						Element.observe(control_elm,'click',function(e){ 
						  newOthersBought.switch_button(control_elm);
							newOthersBought.get_list('right');
						});
					});			
				}
			});		

			this.get_list('');
		}
		
	},
	
	switch_button: function(elm) {
	
	  Element.addClassName(elm,'control_down');
	  Element.removeClassName.delay(0.2,elm,'control_down');
	  
	},
	
	show_loaders: function() {	
		$('others_bought').select('li').each(function(elm){
			if( elm.hasClassName('bought') ) {
				elm.update();
				elm.addClassName('loader');
			}
		});
	},
	
	get_list: function(direction) {
		
		var th = this;
		
		if( !th.clicked ) {
		
  		th.clicked = true;
		
  		new Ajax.Request('others_bought.html',{
  			method: 'post',
  			parameters: { 'sku': this.sku, 'offset': this.offset, 'direction': direction, 'sku_count': this.no_of_products },
  			onComplete: function(t) {
  				var result = t.responseText.evalJSON();
					
  				newOthersBought.offset = result.offset;
  				newOthersBought.create_products(result.list,direction);
  				newOthersBought.total_others_bought = result.total_others_bought;
  				newOthersBought.set_page_text(result.page);
  			}
  		});
		
		}
		
	},
	
	add_basket_handlers: function(elm) {
	
		elm.select('input').each(function(button){
		  if( button.name == 'add to basket' ) Element.observe(button,'click',function(e){ newBasket.add(e); });
		});
	  
	},
	
	create_products: function(list,direction) {
		
		var elm_list = this.item_elms;
		var max_delay = ( direction == 'right' ) ? (this.no_of_products / 10) : (list.size() - 1) / 10;
		
	  for( var i=0; i<this.no_of_products; i++ ) {
		  var delay = ( direction == 'right' ) ? (this.no_of_products-i)/10 : i/10;
			this.create_html.delay(delay,this,elm_list[i],list[i],delay,max_delay);
		}
		
	},
	
	create_html: function(e,elm,product,delay,max_delay) {
		
		// clearing elements contents
		elm.update();
		elm.addClassName('loader');
		
		var th = e;
		
		if( product ) {
			
			new Ajax.Request('/others_bought_item.html',{
			  method: 'post',
			  parameters: product,
			  onComplete: function(t) {
			    elm.removeClassName('loader');
			    elm.insert(t.responseText);
			    newOthersBought.add_basket_handlers(elm);
			    
			    if( delay == max_delay ) th.clicked = false;
			  }
			});
			
		} else {
			elm.removeClassName('loader');
		}
		
	},
	
	set_page_text: function(text) {
	  
	  $('others_bought').select('p').each(function(elm){
			if( elm.hasClassName('pages') ) {
				elm.update(text);
			}
		});
	  
	}
	
};

function initOthersBought() { newOthersBought = new othersBought; }
document.observe('dom:loaded',initOthersBought);