/*
 * Google analytics.
 */
try {
	var pageTracker = _gat._getTracker("UA-7669090-1");
	pageTracker._trackPageview();
} catch(err) {}

/*
 * With the following code, Slimbox will activate itself automatically on all links pointing to images,
 * or more specifically all links having URLs ending with: ".jpg" or ".png" or ".gif".
 * As a result, you will not need to set the rel="lightbox" attribute on any link to activate Slimbox.
 * Furthermore, all image links contained in the same block or paragraph (having the same parent element)
 * will automatically be grouped together in a gallery, so you will not need to specify groups either.
 * Images that are alone in their block or paragraph will be displayed individually.
 */
Slimbox.scanPage = function() {
	$$('a').filter(function(el) {
		return el.getProperty('href') && el.getProperty('href').test(/\.(jpg|png|gif)$/i);
	}).slimbox({}, null, function(el) {
		return (this == el) || (this.getParent() && (this.getParent() == el.getParent()));
	});
};

/*
 * Add roar queue.
 */
var Notifier = null;

/*
 * Toggle a class.
 */
function toggleClass(el, name) {
	$(el).toggleClass(name);
}

/*
 * Add dom-ready event.
 */
window.addEvent('domready', function() {
	
	/*
	 * Fire up notifier instance.
	 */
	Notifier = new Roar({});

	/*
	 * Let slimbox scan the page.
	 */
	Slimbox.scanPage();

	/*
	 * Check for presence of navigation menu
	 * and add mouseover/out effects, if found.
	 */
	if ($$('#menu ul').length) {
		$$('#menu ul').each(function(ul) {
			ul.getElements('li').each(function(li) {
				var el = li.getElement('a');
				li.addEvents({
					'mouseover': function(e) {
						this.addClass('mouseover');
					},
					'mouseout': function(e) {
						this.removeClass('mouseover');
					},
					'click': function(e) {
						document.location = li.getElement('a').getProperty('href');
					}
				});
			});
		});
	}
	
	/*
	 * Check if we're viewing a car listing.
	 * Add mouseover effects.
	 */
	if ($('listing')) {
		$$('.car').addEvents({
			'mouseenter': function(e) {
				if (!this.hasClass('mouseover')) {
					this.addClass('mouseover');
				}
			},
			'mouseleave': function(e) {
				if (this.hasClass('mouseover')) {
					this.removeClass('mouseover');
				}
			},
			'click': function(e) {
				var url = this.getElement('a').getProperty('href');
				window.location.href = url;
			}
		});
	}

	/*
	 * Get links that can possibly be ajaxified.
	 */
	if ($$('.ajaxify').length) {
		$$('.ajaxify').each(function(a) {
			a.set('send', {
				url: a.getProperty('href'),
				onSuccess: function(responseText, responseXML) {
					Notifier.alert('<img src="/assets/images/icons/accept.png"/>', responseText);
					if (this.hasClass('toggler')) {
						var target = this.getElement('img');
						var newsrc = this.getProperty('rel')
						this.setProperty('rel', target.getProperty('src'));
						target.setProperty('src', newsrc);
					} else if (this.hasClass('delete')) {
						$$('.'+this.getProperty('rel')).destroy();
					}
				}.bind(a),
				onFailure: function(XHR) {
					window.location.href = this.getProperty('href');
				}.bind(a)
			});
			a.removeEvents('click').addEvent('click', function(e) {
				new Event(e).stop();
				if (this.hasClass('delete')) {
					if (confirm('Verwijderen is permanent! Doorgaan?')) {
						this.send()
					}
				} else {
					this.send();
				}
			});
		});
	}
	
	/*
	 * Change all elements with class hide to hidden.
	 */
	if ($$('.hideit').length) {
		$$('.hideit').addClass('hidden').removeClass('hideit');
	}
	
	/*
	 * Check if uploadtool should be loaded.
	 */
	if ($('images')) {
		var swiffy = new FancyUpload2($('demo-status'), $('demo-list'), {
			url: $('form-demo').getProperty('action'),
			fieldName: 'photoupload',
			path: '/assets/flash/uploader.swf',
			onLoad: function() {
				$('demo-status').removeClass('hide');
				$('demo-fallback').destroy();
			},
			typeFilter: {'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'},
			debug: true,
			target: 'demo-browse'
		});
		/**
		 * Various interactions
		 */
		$('demo-browse').addEvent('click', function() {
			swiffy.browse();
			return false;
		});
		$('demo-clear').addEvent('click', function() {
			swiffy.removeFile();
			return false;
		});
		$('demo-upload').addEvent('click', function() {
			swiffy.upload();
			return false;
		});
	}

});