document.observe('dom:loaded', function() {
	new MadCart();
});
var MadCart = Class.create({
	initialize: function() {
		if ( $('GalleryView') ) {
			$('GalleryView').down('.btnDownload img').observe('click',this.addCart.bind(this));
			this.target = $('smallCart');
		}
	},
	addCart: function(ev) {
		var el = this.el = $('GalleryView').down('.contentArea img');
		new Ajax.Request('/mad/cart/add?id=gallery', {
			parameters: {
				'no' : $F('photoNo'),
				'loc' : location.href,
				'src' : el.src,
				'title' : el.alt
			},
			onSuccess: this.addInSmallCart.bind(this)
		});
	},
	addInSmallCart: function( transport ) {
		if ( transport.responseText != '1' ) {
			return false;
		}
		var aTags = this.target.select('a');
		if ( aTags.length > 3 ) {
			var te = aTags.pop();
			te.remove();
		}
		var count = parseInt($('smallCartCount').innerHTML);
		count = count + 1;
		$('smallCartCount').innerHTML = count;
		var el = this.el;
		var inner = new Template("<a href='#{loc}'><img alt='#{title}' src='#{src}' width='80px'; /><span>#{title}</span></a>");
		var values = {
		'loc' : location.href,
		'src' : el.src,
		'title' : el.alt
		};
		var converted = inner.evaluate(values);
		this.target.insert({'top': converted});
		this.target.hide();
		this.target.appear();
		alert('다운로드 보관함에 이미지가 담겼습니다.');
	}
});

