/*
	Move submenu up/down
*/
$.subscribe ('content/show',
	function ($content, speed) {
		$content.fadeIn (speed);
	}
);
$.subscribe ('content/hide',
	function ($content, speed) {
		$content.fadeOut (speed);
	}
);

/*
	Move submenu up/down
*/
$.subscribe ('submenu/down',
	function ($submenu) {
		$submenu.animate ({top: 500});
	}
);
$.subscribe ('submenu/up',
	function ($submenu) {
		$submenu.animate ({top: 165});
	}
);

/*
	Set a specific background
*/
$.subscribe ('background/set',
	function ($backgrounds, label) {		
		var $bg		= $backgrounds.filter ('.home');
		var $follow	= $backgrounds.filter ('.follow');
		
		if (label === 'home') {
			$follow.hide ();
			$bg.fadeIn ();
		} else {
				$follow.show ();
			if ($bg.is (':visible')) {
				$bg.fadeOut ();
			}
		}
		
		/*
		var $bg	= $backgrounds.filter ('.' + label + '');

		if (!$bg.is ('.active')) {
			$bg.siblings ('.active').fadeOut (
				function () {
					var $this	= $(this);
					$bg.css ('z-index', 10).addClass ('active');
					$this.removeClass ('active').css ('z-index', 0).show ();
				}
			);
		}
		*/
	}
);
$.subscribe ('background/get',
	function ($container) {
		var $bg	= $container.find ('.active');
		
		return $bg;
	}
);

/*
	Move the arrows
*/
$.subscribe ('arrow/big',
	function ($big, $small, tl) {
		$small.find('img').each(function(idx,val){
			setTimeout(function(){
				$(val).fadeOut();
			},500 - (250*idx));
			setTimeout(function(){
				//$small.css({left: -3000});
			},500);
		});
		$big.find('img').hide().each(function(idx,val){
			setTimeout(function(){
				$(val).fadeIn();
			},250*idx);
		}).end()
	}
);
$.subscribe ('arrow/small',
	function ($big, $small, tl) {
		$big.find('img').each(function(idx,val){
			setTimeout(function(){
				$(val).fadeOut();
			},500 - (250*idx));
			setTimeout(function(){
				//$big.css({left: -3000});
			},500);
		});

			$small.find('img').hide().each(function(idx,val){
				setTimeout(function(){
					$(val).fadeIn();
				},250*idx);
			}).end()

	}
);


/*
	Gallery interieur
*/
$.subscribe ('gallery/in',
	function ($gallery, $arrow) {
		$gallery.gallery ();
		
		if ($arrow && $arrow.length) {
			$arrow.css ({top: 50});
		}
	}
);


/*
	Gallery interieur
*/
$.subscribe ('gallery/out',
	function ($gallery) {
		$gallery.remove ();
		
		
	}
);


/*
	Lightbox open
*/
$.subscribe ('lightbox/init',
	function ($lightbox, $content) {
		
		if (!$lightbox.data ('lightbox.init')) {
			
			$lightbox.data ('lightbox.init', true);
			
			$lightbox.find ('a.close').bind ('click',
				function (e) {
					e.preventDefault ();
					$.publish ('lightbox/close', $lightbox);
				}
			);
			
			
		}
	}
);

/*
	Lightbox open
*/
$.subscribe ('lightbox/open',
	function ($lightbox, $title, $current) {
		$lightbox.unbind('clickoutside');
		setTimeout(function(){
			$lightbox.one('clickoutside',function(){
				if (typeof console != 'undefined') console.log('click');
				$.publish ('lightbox/close',$lightbox);
			});
		},10);
		$lightbox.find ('.content').html ($current);
		$lightbox.find ('.title').html ($title);
		$.publish ('content/show', [$lightbox, 0]);
	}
);

/*
	Lightbox close
*/
$.subscribe ('lightbox/close',
	function ($lightbox) {
		$lightbox.unbind('clickoutside');
		$.publish ('content/hide', [$lightbox, 0]);
		$lightbox.find ('.content').empty ();
		$lightbox.find ('.title').empty ();
	}
);

/*
	Switch car
*/
$.subscribe ('car/change',
	function (nr, type) {


		var type	= type || 'rtl'
		var $car	= $('img.car');

		if (typeof nr === 'number' && $car.hasClass ('car-' + nr)) {
			return;			
		} else {
			var list	= [1,2,3,4];
			nr	= typeof nr === 'number' ? nr : list[Math.random () * list.length | 0];
		}
		
		if (type === 'ltr') {
			anima	= -1200;
			animb	= 1200;
		} else {
			anima	= 1200;
			animb	= -1200;
		}
		
		$car.stop (true).animate ({right: anima},
			function () {
				var $this	= $(this);
				
				$this.attr ('src', 'assets/img/Autos/' + nr + '.png').attr ('class', 'car car-' + nr);

				$this.css ({right: animb}).animate ({right: nr === 0 ? 20 : 0});
			}
		);
		
		
		
		
		
	/*
		var $car	= $orig.clone ().insertAfter ($orig);;
		
		
		$car.attr ('src', 'assets/img/Autos/' + nr + '.png')[0].className = 'car';;
		$car.addClass ('car-' + nr);
		
		var anima, animb;

		$car.css ({left: anima});
		
		$car.delay (500).animate ({left: $orig.position ().left});
		$orig.animate ({left: animb},
			function () {
				$orig.remove ();
			}
		);
	*/	
	}
);




