
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 = 5;
			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 .filmstrip .button .control').each(function(elm){
			  
			  var parent = elm.parentNode;
			  var direction = $w(parent.className).first();
			  
			  elm.observe('click',this.switch_page.bindAsEventListener(this,direction));
				//elm.observe('mouseover',this.over_button.bindAsEventListener(this,elm));
				//elm.observe('mouseout',this.move_from_button.bindAsEventListener(this,elm));
				
			}.bind(this));

			this.get_list('');
		}
		
	},
	
	switch_button: function(elm) {
	
	  Element.addClassName(elm,'control_down');
	  Element.removeClassName.delay(0.2,elm,'control_down');
	  
	},
	
	over_button: function(e,elm) {
	  elm.addClassName('control_down');
	},
	
	move_from_button: function(e,elm) {
	   elm.removeClassName('control_down');
	},
	
	switch_page: function(e,direction) {
	  
	  var elm = Event.element(e);
	  
	  this.switch_button(elm);
	  if( this.next_page_available(elm) ) this.get_list(direction,elm);
	  
	},
	
	show_loaders: function() {	
		$('others_bought').select('li').each(function(elm){
			if( elm.hasClassName('bought') ) {
				elm.update();
				elm.addClassName('loader');
			}
		});
	},
	
	next_page_available: function(elm) {
	  
	  if( elm.hasClassName('disabled') ) return false;
	  return true;
	  
	},
	
	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();
					
  				th.offset = result.offset;
  				th.create_products(result.list,direction);
  				th.total_others_bought = result.total_others_bought;
  				th.set_page_text(result.page);
  				th.disable_buttons(result);
  				
  			}
  		});
		
		}
		
	},
	
	disable_buttons: function(info) {
	  
	  var left_button = $$('.filmstrip .left .control').first();
	  var right_button = $$('.filmstrip .right .control').first();
	  
	  if( info.disable_left_arrow == "1" ) { 
	    left_button.addClassName('disabled');
	    left_button.removeClassName('enabled'); 
	  } else { 
	    left_button.removeClassName('disabled'); 
	    left_button.addClassName('enabled'); 
	  }
	  
	  
	  if( info.disable_right_arrow == "1" ) { 
	    right_button.addClassName('disabled'); 
	    right_button.removeClassName('enabled'); 
	  } else {
	    right_button.removeClassName('disabled');
	    right_button.addClassName('enabled'); 
	  }
	
	},
	
	add_basket_handlers: function(elm) {
	
		elm.select('a.add_to_basket').each(function(button){
		  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);