try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

/**
 * Singleton that holds information about the current pattern.
 */
EP.PatternManager = new function()
{
	this.instance = {
		patternId: null,
		fabricId: null,
		positionId: null,
		
		setActivePatternId: function(id) {
			this.patternId = id;
		},
		
		setActiveFabricId: function(id) {
			this.fabricId = id;
		},
		
		setActivePositionId: function(id) {
			this.positionId = id;
		},
		
		getActivePatternId: function() {
			return this.patternId;
		},
		
		getActiveFabricId: function() {
			return this.fabricId;
		},
		
		getActivePositionId: function() {
			return this.positionId;
		},
		
		getActiveFabricURL: function() {
			return '/images/config/fabrics/'+this.getActivePatternId()+'/'+this.getActiveFabricId()+'/'+this.getActivePositionId()+'.gif';
		}
		
	};
	return this.instance;
};

EP.FinalPattern = {
	finalPattern: {},
	prepFinalPattern: function(id) {
		this.finalPattern.id = id;
		this.finalPattern.sections = [];
		EP.Pattern.sections.each(function(section) {
			this.finalPattern.sections.push({id:section.id});
		}.bind(this));
	},
	setSection: function(sectionId,fabricId) {
		this.finalPattern.sections.each(function(s){
			if(s.id == sectionId) {
				s.fabric = fabricId;
			}
		}.bind(this));
	},
	patternIsComplete: function() {
		var complete = true;
		this.finalPattern.sections.each(function(s){
			if(!s.fabric) complete = false;
			$break;
		}.bind(this));
		return complete;
	}
};

EP.positionPatterns = function(e) {
	// Positions the window with all the swatches.
	var newX = Event.pointerX(e);
	var newY = Event.pointerY(e) - 67; /* puts the pointer exactly at the mouse click */
	var pointerY = 60; /* move the pointer down the left side of the popup a bit */
	var bheight = EP.WindowProperties.getBrowserSize().height;
	var vScroll = EP.WindowProperties.getVerticalScroll();

	//If we're too close to the bottom of the browser
	if(newY + 350 >= bheight + vScroll) {
		var bottom = bheight + vScroll - 351;
		pointerY = pointerY + (newY - bottom);
		newY = bottom;
	}
	
	$('patterns').setStyle({
		top: newY +'px',
		left: newX+'px'
	});
	$('patterns_pointer').setStyle({
		top: pointerY+'px'
	});
	
	if(document.all) {
		var selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "hidden";
		}
	}
	
	new Effect.Appear('patterns',{duration: .5});
};

EP.closePatterns = function() {
	new Effect.Fade('patterns',{duration: .5, 
		afterFinish: function() {
			if(document.all) {
				var selects = document.getElementsByTagName("select");
				for (i = 0; i != selects.length; i++) {
					selects[i].style.visibility = "visible";
				}
			}
		}
	});
	
};

EP.loadFabricPiece = function(e) {
	e = (e) ? e : window.event;
	
	//get ID of fabric from the id of the a tag.
	EP.PatternManager.setActiveFabricId(Event.findElement(e,'a').id.split('_')[1]);
	
	// find the object that contains the x,y coords for the section
	var activeSection = EP.Pattern.sections.find(function(s) {
		return s.id == EP.PatternManager.getActivePositionId();
	});
	
	var fabric = document.createElement('IMG');
	fabric.src = EP.PatternManager.getActiveFabricURL();
	fabric.width = activeSection.w;
	fabric.height = activeSection.h;
	var container = document.createElement('DIV');
	
	// have to create a container and hide, because of some goofy Safari problem.
	container.style.display = 'none';
	
	// If the section already has a pattern in it, get rid of it.
	if($('position_'+EP.PatternManager.getActivePositionId())) {
		 $('position_'+EP.PatternManager.getActivePositionId()).remove();
	}
	
	// Set the ID of the fabric to the section it resides in.
	container.id = 'position_'+EP.PatternManager.getActivePositionId();
	container.appendChild(fabric);
	$('configurator').appendChild(container);
	
	$(container).setStyle({
		'position': 'absolute',
		'z-index': 9980,
		'top': activeSection.y + 'px',
		'left': activeSection.x + 'px',
		'padding': '9px' /* configPattern padding + border */
	});
	
	container.style.display = '';
	
	// Set the fabric in the section in an object that we'll send to the server later.
	EP.FinalPattern.setSection(EP.PatternManager.getActivePositionId(),EP.PatternManager.getActiveFabricId());
	
	// Check if the object has fabric values for all the sections
	if(EP.FinalPattern.patternIsComplete()) {
		new Effect.Appear('prodForm');
	}
	
	EP.closePatterns();
	Event.stop(e);
};
