Y(['connection', 'json', 'menu']);

DW.bigenough = function (item) {
	var md = item.mindim;
	if (! md) return true;

	var w = md[0] * 100;
	var h = md[1] * 100;

	return((DW.image_width >= w) && (DW.image_height >= h));
};

DW.pickitem = function (pattern) {

	var upat = (pattern == '.') ? 'All' : pattern;
	urchinTracker('/ezp/pick/' + upat);

	var re = new RegExp(pattern);
	var doc = document.createElement('div');
	doc.style.height = '95%';
	var runafter = [];
	var hd,item,cat,subcat,m,scbox,n;
	for (cat in DW.catalog) {
		if (! re.exec(cat)) continue;
		hd = document.createElement('img');
		hd.src = '/lib/suffixapp/album/img/logo' + cat + '.gif';
		hd.height = 56;
		hd.width = (cat == 'Stationery') ? 500 : 400;
		doc.appendChild(hd);
		doc.appendChild(document.createElement('br'));

		scbox = document.createElement('div');
		scbox.className = 'scbox';
		doc.appendChild(scbox);

		n = 0;

		for (subcat in DW.catalog[cat]) {
			runafter.push(
				(function () {

					var div = document.createElement('div');
                    var a = document.createElement('a');
					a.innerHTML = '<img src="/lib/suffixapp/album/img/ezp/rightarrow.png" width="15" height="13"> ' + subcat;
                    a.style.textDecoration = 'none';
					div.className = 'subcat';
                    div.appendChild(a);

					if (++n > 8) {
						scbox.style.cssFloat = 'left';
						scbox = document.createElement('div');
						scbox.className = 'scbox';
						scbox.style.cssFloat = 'right';
						doc.appendChild(scbox);
						n = 0;
					}

					scbox.appendChild(div);
	
					var id = 'subcat_' + escape(subcat);
					var menu = new YAHOO.widget.Menu(id, { context: [a, 'tl', 'bl'] });

					var disabled;
					for (m in DW.catalog[cat][subcat]) {
						item = DW.catalog[cat][subcat][m];
						disabled = ! DW.bigenough(item);
						menu.addItem({ text: item.desc, url: 'javascript:DW.buyprints("' + item.sku + '")', disabled: disabled });
					}
	
					a.onclick = function () { menu.show(); };
					return(function () { menu.render(a); });
				})()
			);
		}
	}
	document.body.style.backgroundColor = '#fff';
	document.body.style.backgroundImage = 'url()';
	document.body.background = '';
	var main = document.getElementById('main');
	main.align = '';
	main.innerHTML = '';
	main.appendChild(doc);
	for (var i in runafter) {
		runafter[i]();
	}

	// this would have been simpler w/jquery, I know
	var disabled = document.getElementsByTagName('a'), a;
	i=0;
	for (;a=disabled[i];++i) {
		if (a.className.match(/\byuimenuitemlabel-disabled\b/)) {
			a.title = 'This item is only available for large images';
		}
	}

	var note = document.createElement('p');
	note.innerHTML = 'Items that are greyed out are only available for large images.';
	note.style.fontSize = '120%';
	note.style.padding = '5px';
	note.style.textAlign = 'center';
	note.style.color = '#a6a6a6';
	main.appendChild(note);

	YAHOO.util.Connect.asyncRequest(
		'POST', DW.relative_url + '%3Aalbum%3Aezp%3Asend', {
			success: function (o) {},
			failure: function (o) {}
		}
	);
};

DW.addtocart = function (pid, sku, thumb) {
	document.cookie = 'dwezpcart=' + pid;
	urchinTracker('/ezp/addtocart/' + sku);
};

DW.buyprints = function (sku) {
	DW.ezpapp = ezp.apps.createTemplateApp({
		sku: sku,
		elementId: 'main',
		width: (document.body.clientWidth - 40),
		height: (document.body.clientHeight - 40),
		continueShoppingUri: location.href,
		checkoutReturnUri: location.protocol + '//' + location.host + '/lib/suffixapp/album/ui/ezp-thanks.html',
		addToCartCallback: DW.addtocart,
		templateLoadCallback: function () {
			DW.ezpapp.setPhoto({
				id:             DW.image_id,
				screenResUri:   DW.midsize,
				PrintResUri:    DW.ezp_fullsize,
				printResWidth:  DW.image_width,
				printResHeight: DW.image_height
			});
		}
	});
	urchinTracker('/ezp/pick/item/' + sku);
};

(function () {
	var p = window.opener;
	var b = document.body;
	var g = document.getElementById('gohome');
	var t = document.getElementById('gohometxt');
	try {
		p = p.document.body;
		b.background = p.background;
		b.bgColor = p.bgColor;
		b.text = p.text;
		b.style.fontFamily = p.style.fontFamily;
		g.onclick = function () { window.close(); return false; };
		t.innerHTML = 'Close Window';
	}
	catch (e) { /* */ }
})();

(function () {

	if (! document.getElementById) return false;
	var p = document.getElementById('photo');
	var r = p.src.match(/(.+\/)midsize(\.[a-z]+)$/);
	if (! r) return false;
	var resizer = document.getElementById('resizer');
	if (! resizer) return false;

	var original = r[1] + 'image' + r[2];
	var midsize = p.src;

	var orig_img = new Image();
	orig_img.onload = function () {
		p.src = this.src;
	};

	resizer.innerHTML = '<a href="javascript://">Original Size</a>';

	var show_original = function () {
		p.src = '/lib/suffixapp/album/img/pleasewait.gif';
		orig_img.src = original;
		resizer.onclick = show_midsize;
		resizer.innerHTML = '<a href="javascript://">Fit to Window</a>';
	};

	var show_midsize = function () {
		p.src = midsize;
		resizer.onclick = show_original;
		resizer.innerHTML = '<a href="javascript://">Original Size</a>';
	};

	resizer.onclick = show_original;

})();

