
Shortlist = function() {
	this.DAYS = '60';
	this.COOKIE = 'shortlist';
	this.COOKIE_OPTS = {
			path: '/',
			domain: 'paparazzi.fi',
			duration: this.DAYS
	};
	this.items = new Hash(JSON.decode(shortlist_data));  // global variable set in page template
	this.counter = new SLCounter(this.items.getKeys().length);
};

Shortlist.prototype.clear = function() {
	Cookie.dispose(this.COOKIE, this.COOKIE_OPTS);
}

Shortlist.prototype.add = function(db, model_id, pic) {
	var _this = this;
	if (! this.items.has(model_id)) {
		this.items[model_id] = db;
		this.animate(pic, function() {
			_this.counter.add(function() {
				_this.save();
			});
		});
	}
}

Shortlist.prototype.remove = function(model_id) {
	if (this.items.has(model_id)) {
		this.items.erase(model_id);
		this.counter.subtract();
		this.save();
	}
}

Shortlist.prototype.save = function() {
	var req = new Request({ url: '/shortlist_update' });
	req.send(new Hash({
		cmf_0_0: this.key(),
		cmf_0_1: JSON.encode(this.items)
	}).toQueryString());
}

Shortlist.prototype.key = function() {
	var key = Cookie.read(this.COOKIE);
	if (null == key) {
		key = this.random();
		var cookie = Cookie.write(this.COOKIE, key, this.COOKIE_OPTS);
	}
	return key;
}

Shortlist.prototype.random = function() {
	var pool = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
	var str = '';
	while (str.length < 10)
		str += pool.charAt(Math.floor(Math.random() * pool.length));
	return str;
}

Shortlist.prototype.animate = function(original, callback) {
	var pos_start = $(original).getPosition();
	var pos_end = $$('#nav #shortlist A')[0].getPosition();
	var size = $(original).getSize();
	var clone = $(original).clone()
		.setStyle('position', 'absolute')
		.setStyle('width', size.x)
		.setStyle('height', size.y)
		.setStyle('top', pos_start.y + 20)
		.setStyle('left', pos_start.x)
		.setStyle('opacity', 0.5)
		.inject($$('BODY')[0]);
	
	var fx = new Fx.Morph(clone);
	fx.addEvent('complete', callback);
	fx.start({
		'height': 0,
		'width': 0,
		'top': pos_end.y,
		'left': pos_end.x
	});
}

SLCounter = function(num) {
	this.count = 0;
	this.initialized = false;
	if (num > 0) this.set(num);
}

SLCounter.prototype.init = function() {
	$$('#nav #shortlist SPAN').setStyle('background-image', 'url(/files/pprz/img/nav-shortlist-count.png)');
	this.elem = new Element('DIV', { 'class': 'counter' });
	var parent = $$('#nav #shortlist A')[0];
	this.elem.inject(parent);
	this.initialized = true;
}

SLCounter.prototype.add = function(callback) {
	this.animate(this.count + 1, callback);
}

SLCounter.prototype.subtract = function(callback) {
	this.animate(this.count - 1, callback);
}

SLCounter.prototype.animate = function(num, callback) {
	if (! this.initialized) this.init();
	var fx = new Fx.Tween(this.elem, { duration: 1000, transition: Fx.Transitions.Expo.easeOut });
	if (callback) fx.addEvent('complete', callback);
	fx.start('backgroundPosition', 'left -' + (this.count * 24) + 'px', 'left -' + (num * 24) + 'px');
	this.count = num;
}

SLCounter.prototype.set = function(num) {
	if (! this.initialized) this.init();
	this.elem.setStyle('backgroundPosition', 'left -' + (num * 24) + 'px');
	this.count = num;
}



var shortlist = null;

var refresh_shortlist_links = function() {

	$$('A.add-to-shortlist').each(function(elem) {
		elem.setStyle('display', shortlist.items.has(elem.get('id').split('-')[1]) ? 'none' : 'inline');
	});

	$$('A.remove-from-shortlist').each(function(elem) {
		elem.setStyle('display', shortlist.items.has(elem.get('id').split('-')[1]) ? 'inline' : 'none');
	});
};

window.addEvent('domready', function() {

	shortlist = new Shortlist();

	$$('A.shortlist-create').addEvent('click', function(e) {
		e.preventDefault();
		shortlist.clear();
		window.location.href = '/shortlist';
	});

	$$('A.add-to-shortlist').each(function(elem) {
		var id = elem.get('id').split('-');
		elem.addEvent('click', function(e) {
			e.preventDefault();
			var pic = $$('.model-sets A:first-child IMG')[0];
			shortlist.add(id[0], id[1], pic);
			refresh_shortlist_links();
		});
	});

	$$('A.remove-from-shortlist').each(function(elem) {
		var id = elem.get('id').split('-');
		elem.addEvent('click', function(e) {
			e.preventDefault();
			shortlist.remove(id[1]);
			$$('UL.shortlist #' + id[1]).destroy();
			if (shortlist.items.getKeys().length == 0)
				$$('#shortlist-no-models').setStyle('display', 'block');
			refresh_shortlist_links();
		});
	});

	$$('A.shortlist-print-pdf, A.shortlist-tell-friend').addEvent('mouseover', function(e) {
		// dirty hack
		this.href = this.href.replace(/model=[^&]*/, 'model=' + shortlist.items.getKeys().join(','));
	});

	refresh_shortlist_links();

});


