// JavaScript Document

function MattressHandler(){
	var self = this;
    var itemId = null;
    var itemUrl = null;
    var targetDiv = null;
    var itemParent = null;
    var prevParent = null;
    var mattress = null;
	
	// Attach event listeners to each menu item
    $('.mattress').click(function(e) {
          self.openMattress($(this));
		  return false;
    });
	
	this.openMattress = function(e){
		mattress = $(e);
		var row = $(e).attr('data-row');
		var delay = 10;
		var currentTarget =  $('.display_'+row);
		
		if (targetDiv != null)
		{
			targetDiv.animate({
				height: '0'}, 500);
			delay = 500;
		} 
		
		targetDiv = currentTarget;
			
		//targetDiv.delay(delay).openDisplay();
		setTimeout(self.openDisplay, delay);
		
		
		
		
		
	};
	
	this.openDisplay = function(){
		targetDiv.find('h2').html(mattress.find('h3').html())
		targetDiv.find('h3 a').attr('href', '/pdf/' + mattress.attr('data-name') + '.pdf');
		targetDiv.find('img.label').attr('src', mattress.find('img').attr('src').replace('-small', '-label'));
		targetDiv.find('img.large').attr('src', mattress.find('img').attr('src').replace('-small', '-large'));
		targetDiv.show();
		
		targetDiv.animate({
			height: '520'}, 750, function() {
                var pos = $(this).offset();
                pos.top -= 250;
                $('html, body').animate({ scrollTop: pos.top }, 500);
            });
	};
	
}
