(
	function ($) {
		jQuery.ajaxSetup ();

		
		
		/* Dom READY */
		jQuery (
			function () {
				var $lightbox	= $('#lightbox');
				$.publish ('lightbox/init', $lightbox);
				/*
					Dirty fix make sure the interior gallery works
				*/
				$.each ([
						'pages/interieur/img/interieur1.png',
						'pages/exterieur/img/exterieur1.png',
						'pages/techniek/img/techniek1.png',
						'assets/img/Autos/1.png',
						'assets/img/Autos/2.png',
						'assets/img/Autos/3.png',
						'assets/img/Autos/4.png',
						],
					function (i, e) {
						$('<img>', {src: e});
					}
				);
				
				
				var $content		= $('#content-follow');
				
				var $arrowBig		= $('#arrow-big');
				var $arrowSmall		= $('#arrow-small');
			
				var $backgrounds	= $('.background');
				var $homeTitle		= $('#home-title');
				var $followTitle	= $('#follow-title');
				
				
			
			
				var $home	= $('#home');
			
				/* Menu management */
				var $header	= $('#header');
				
				$('a[data-ajax="true"][data-id]').live ('click',
					function (e) { e.preventDefault (); window.location.hash = '/' + this.getAttribute ('data-id'); }
				);
				
				
				/* Submenu management */
				var $submenu	= $('#submenu');
				
				
				
				$('a[data-link]').live ('click',
					function () {
						
						_gaq.push(['_trackPageview', '/' + this.getAttribute ('data-link')]);
					}
				);
				
				
				
				/* Page nav */
				
				$(window).bind ('hashchange',
					function (e) {
						
						
						
						var fragment	= e.fragment.substring (1) || 'home';
						var $this		= $('a[data-id="' + fragment + '"]');
						
						if ($this[0] && $this.eq (0).is ('a[data-ajax="true"]')) {
							$this		= $this.eq (0);
							
							_gaq.push(['_trackPageview', '/' + fragment]);
							
							$('.ajax-active').removeClass ('ajax-active').trigger ('mouseleave');
							$this.parent ().addClass ('ajax-active');
							
							var href	= $this.attr ('href');
	
							//if (!$this.data ('gallery')) {
								$.publish ('gallery/out', [$('div.gallery'), $arrowSmall]);
								$.publish ('lightbox/close', [$lightbox]);
								$('#frame').remove ();
							//}
							
							if (href !== '' && !/#/.test (href)) {
								$.get ('' + href,
									function (data) {
										var $data	= $(data);
										$data.appendTo ('#main');
										if ($this.data ('packs')) {
											$.publish ('car/change', [$this.data ('car') || 0, $this.hasClass ('next') ? 'rtl' : 'ltr']);
										}
										
										if ($this.data ('gallery')) {
											$.publish ('gallery/in', [$('div.gallery'), $arrowSmall]);
										}
										
									}
								);
							}
							
							
							if ($this.data ('home')) {
								$.publish ('car/change', [0]);
								$.publish ('submenu/up', $submenu);
								$.publish ('background/set', [$backgrounds, 'follow']);
								$.publish ('arrow/big', [$arrowBig, $arrowSmall, {top: 300, left: 37}]);
								$.publish ('content/show', [$homeTitle, 0]);
								$.publish ('content/hide', [$followTitle, 0]);
								$.publish ('content/hide', [$content, 0]);
								$('.home-info').delay (600).fadeIn ();
							} else if ($this.data ('gallery')) {
								$.publish ('car/change', [0]);
								$.publish ('submenu/down', $submenu);
								$.publish ('background/set', [$backgrounds, 'follow']);
								$.publish ('arrow/small', [$arrowBig, $arrowSmall, {top: 50, left: 37}]);
								$.publish ('content/hide', [$homeTitle, 0]);
								$.publish ('content/show', [$followTitle, 0]);
								$.publish ('content/show', [$content]);
								$('.home-info').hide ();
							} else if ($this.data ('packs')) {
								$.publish ('submenu/down', $submenu);
								$.publish ('background/set', [$backgrounds, 'follow']);
								$.publish ('arrow/small', [$arrowBig, $arrowSmall, {top: 50, left: 37}]);
								$.publish ('content/hide', [$homeTitle, 0]);
								$.publish ('content/show', [$followTitle, 0]);
								$.publish ('content/show', [$content]);
								$('.home-info').hide ();
							}
							
						}
					}
				);
				
				if (location.hash !== '') {
					$(window).trigger ('hashchange');
				} else {
					window.location.hash = '#/home';
				}
				
				
				
				/* Clone  */
				var $div	= $('<div class="carret"/>');
				
				/* li's */
				var $li		= $('#menu li:has(a)');
				
				
				/* Add clone to li's */
				$div.appendTo ($li);
				
				$li.bind ('mouseenter',
					function () {
						var $this	= $(this);
						
						/* Store in data, for caching */
						var $carret	= $this.data ('menu.carret') || $this.data ('menu.carret', $this.find ('div.carret')).data ('menu.carret');
						
						$carret.animate ({width: 33, marginLeft: -16.5}, 150);
					}
				).bind ('mouseleave',
					function () {
						var $this	= $(this);
						/* retrieve from cache */
						var $carret	= $this.data ('menu.carret'); 
						if (! $carret)
							return;
						if (!$this.hasClass ('ajax-active')) {
							$carret.animate ({width: 0, marginLeft: 0}, 150);
						}
					}
				);
				
				
				/* Twitter */
				$.getJSON ('http://search.twitter.com/search.json?q=mitobpm&since_id=12834208418373632&callback=?',
					function (data) {
						$('button.twitter-count').text (data.results.length);
					}
				);
		
			}
		);
	}
)(jQuery);
