	var rotInterval;

	var rotThumb = function(v) {

		var img_el = $('a-' + v).getChildren('img').shift();

		if(img_el.retrieve('loading') == 0) {

			// console.log("changing image...");

			var parts = img_el.retrieve('current').split('-');
			var newThumb;

			if(parts[0] == parts[1]) {
				newThumb = 1;
			} else {
				newThumb = Number(parts[0]) + 1;
			}

			img_el.store('current', newThumb + '-' + parts[1]);

			img_el.set('src', img_el.get('src').replace(/\/\d+\.jpg/, '/' + newThumb + '.jpg'));

		} else {
			// console.log("not loaded yet (" + img_el.retrieve('loading') + ")");
		}
	}

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

		$$('img.rot').addEvent('mouseover', function() {

			// get ID and thumb count from element id
			var parts = this.get('id').split('-');

			if(this.retrieve('loading') == null) {

				var img_el = this;
				var featured_src = this.get('src');
				this.getParent().grab(new Element('span'));

				this.store('current', parts[1] + '-' + parts[2]);
				this.store('loading', 0);

				for(var i = 1; i <= parts[2]; i++) {

					if($(parts[0] + '-' + i + '-' + parts[2]) == null) {

						// console.log(img_el.retrieve('loading') + 1);
						this.store('loading', this.retrieve('loading') + 1);

						this.getParent().grab(new Element('img', {
							src: featured_src.replace(/\/\d+\.jpg$/, '/' + i + '.jpg'),
							id: parts[0] + '-' + i + '-' + parts[2],
							style: 'display: none;',
							events: {
								load: function() {

									// console.log(img_el.retrieve('loading') - 1);
									img_el.store('loading', img_el.retrieve('loading') - 1);

									if(img_el.retrieve('loading') == 0) {
										// finished loading
										var siblings = img_el.getSiblings('span');
										if(siblings.length > 0) {

											clearInterval(rotInterval);
											rotThumb(parts[0]);
											siblings.destroy();
											rotInterval = rotThumb.periodical(600, null, parts[0]);

										}

									}

								}
							}
						}));
					}
				}
			}

			rotThumb(parts[0]);
			rotInterval = rotThumb.periodical(600, null, parts[0]);

		}).addEvent('mouseout', function() {
			this.getSiblings('span').destroy();
			clearInterval(rotInterval);
		});
		$$('img.rot1').addEvent('mouseover', function() {

			// get ID and thumb count from element id
			var parts = this.get('id').split('-');

			if(this.retrieve('loading') == null) {

				var img_el = this;
				var featured_src = this.get('src');
				this.getParent().grab(new Element('span'));

				this.store('current', parts[1] + '-' + parts[2]);
				this.store('loading', 0);

				for(var i = 1; i <= parts[2]; i++) {

					if($(parts[0] + '-' + i + '-' + parts[2]) == null) {

						// console.log(img_el.retrieve('loading') + 1);
						this.store('loading', this.retrieve('loading') + 1);

						this.getParent().grab(new Element('img', {
							src: featured_src.replace(/\/\d+\.jpg$/, '/' + i + '.jpg'),
							id: parts[0] + '-' + i + '-' + parts[2],
							style: 'display: none;',
							events: {
								load: function() {

									// console.log(img_el.retrieve('loading') - 1);
									img_el.store('loading', img_el.retrieve('loading') - 1);

									if(img_el.retrieve('loading') == 0) {
										// finished loading
										var siblings = img_el.getSiblings('span');
										if(siblings.length > 0) {

											clearInterval(rotInterval);
											rotThumb(parts[0]);
											siblings.destroy();
											rotInterval = rotThumb.periodical(600, null, parts[0]);

										}

									}

								}
							}
						}));
					}
				}
			}

			rotThumb(parts[0]);
			rotInterval = rotThumb.periodical(600, null, parts[0]);

		}).addEvent('mouseout', function() {
			this.getSiblings('span').destroy();
			clearInterval(rotInterval);
		});
	});


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

		$$('a.saveLink').addEvent('click', function() {
			saveVideo(this.getProperty('id').replace(/[^\d]+/, ''));
		}).setProperty('href','javascript:;');

		$$('a.unsaveLink').addEvent('click', function() {
			unsaveVideo(this.getProperty('id').replace(/[^\d]+/, ''));
		}).setProperty('href','javascript:;');

		$$('a.link_out').addEvent('mousedown', function() {
			var req = new Request({
				url: '/count/' + this.getProperty('data-site'),
				method: 'get'
			});
			req.send();
		});
		
	});


var saveVideo = function(id) {
	ajAction('/save_video/' + id + '/json', function() { saveSuccess(id) });
}


var unsaveVideo = function(id) {
	ajAction('/unsave_video/' + id + '/json', function() { unsaveSuccess(id) });
}


var deleteVideo = function(id) {
	ajAction('/delete/' + id + '/json', function() { deleteSuccess(id) });
}
