var FullBleedShop = Class.create();
FullBleedShop.prototype = {
	initialize: function(shop) {
		this.shop = shop;
		this.tabs = shop.getElementsBySelector('.tabs a');
		this.content = shop.getElementsBySelector('.data').first();
		
		this.tabify(this.tabs);
		this.setup();
	},

	tabify: function(tabs) {
		tabs.each(function(tab) {
			tab.onclick = function() {
				var match = tab.href.match(/product_id=(.+)$/);
				this.getProduct(match[1]);
				
				tabs.each(function(e) {
					(e==tab) ? e.addClassName('active') : e.removeClassName('active');
				});
				
				var tabName = tab.innerHTML.replace(/<br( \/)?>/i, '');
				GoogleAnalytics.trackEvent('Full Bleed Shop', 'Tab', tabName);
				
				return false;
			}.bind(this);
		}.bind(this));
	},

	setup: function() {
		this.prices  = this.content.getElementsBySelector('.qtyPrice').first();
		this.spinner = this.content.getElementsBySelector('.spinner').first();
		this.spin();
		
		this.content.getElementsBySelector('a.help').each(function(e){
			new Tip(e, e.rel, {title : ' ', fixed: true});
		});
		
		this.form = this.content.getElementsBySelector('form#uploadForm').first();
		Event.observe(this.form, 'submit', this.validateUpload.bindAsEventListener(this));
	},

	getProduct: function(productId) {
		this.spin();
		
		this.productId = productId;
		var params = {
			product_id: this.productId,
			priceTab: 'Print',
			prodTime: 'Standard',
			colorsFront: 'PFB',
			colorsBack: 'NONE'
		}
		var myAjax = new Ajax.Updater(this.content, '/ae/control/fullBleedProduct',
			{method: 'get', parameters: params, onComplete: this.setup.bind(this)});
	},
	spin: function() {
		if (!this.spinner) return;
		if (!this.spinner.visible()) {
			this.spinner.show();
		} else {
			setTimeout(function() {
				this.spinner.hide();
			}.bind(this), 150);
		}
	},
	validateUpload: function(event) {
		if (!this.form.elements['UPLOAD_NOW_FILE1'].value) {
			alert('Please choose a file to upload.');
			Event.stop(event);
		}
	}
};

Event.onDOMReady(function(){
	$$('.expressShop').each(function(e){
		new FullBleedShop(e);
	});
});

