jQuery.fn.detach = function( selector )
{
	return jQuery(this).remove( selector, true );
};


jQuery.fn.contestoScroll = function(e, m) {
	var $this = e;
	
	var first 		= 0;
	var speed 		= 1000;
	var pause 		= 3000;
	var liwidth 	= $this.children('li:first').width();
	var limargin 	= m;
	var margin 		= liwidth + limargin;

	setInterval(
		function (){
			content = '<li style="display:none">'+ $this.children('li:first').html() +'</li>';
			$this.children('li:first')
			.animate({'marginLeft': '-'+ margin +'px'}, {easing: 'bounceout'}, speed)
			.fadeOut('200', 
				function() {
					jQuery(this).remove();
				}
			);
			$this.append(content)
			$this.children('li:last').fadeIn('slow');
		}, 
	pause);
		

};

$(document).ready(function() {

	
	$( '.polaroid' ).click(function()
	{
		var content = $( '#gallery-html' ).html();
		var id_album = $( this ).attr( 'rel' );
	
		$.fn.colorbox(
		{
			opacity: 0.15,
			innerWidth: 900,
			innerHeight: 600,
			html: content,
			onComplete: function()
			{
				$.ajax({
					cache: 		true,
					type: 		'post',
					url:  		'/ajax/get-galleries.php',
					data: 		'id_album=' + id_album,
					success: 	function( response )
					{
						$( '#g-side' ).html( response );
					}
				});

			}
		});
		
		return false;
	});
	
	
	$( '#list-galleries a' ).live( 'click', function()
	{
		
		var id_gallery 	= $( this ).attr( 'rel' );
		var $cur 		= $( '#cur-image' );
		var $bar 		= $( '#bottom' );
		
		$( this ).parent().addClass( 'active' ).siblings().removeClass( 'active' );
		
		$cur.addClass( 'loading' );

		$.ajax({
			cache: 		true,
			type: 		'post',
			url:  		'/ajax/get-photo.php',
			data: 		'id_gallery=' + id_gallery,
			success: 	function( response )
			{
				$cur.html( response );
				
				setTimeout(function()
				{
					$cur.children().fadeIn();
				}, 1500);
			}
		});

		$.ajax({
			cache: 		true,
			type: 		'post',
			url:  		'/ajax/get-photos.php',
			data: 		'id_gallery=' + id_gallery,
			success: 	function( response )
			{
				$bar.html( response ).slideDown();
			}
		});
		
		return false;
	});
	
	$( '#thumbs-list a' ).live( 'click', function()
	{
	
		if( $( this ).parent().hasClass( 'active' ) ){
			return false;
		}
	
		var $cur = $( '#cur-image' );
		var src  = $( this ).attr( 'rel' );

		$cur.children().fadeOut(function()
		{
			$( this ).remove();

			var cover = new Image();

			$( cover ).load( function()
			{
				$( this ).hide();
				
				$cur.append( this );
				
				$( this ).fadeIn();
				
			}).attr( 'src', '/lib/timthumb.php?src='+src+'&w=740&h=488&z=1&q=100' );

		});

		$( this ).parent().addClass( 'active' ).siblings().removeClass( 'active' );
		
		return false;
	});
	
	$( '.single-news' ).live( 'click', function()
	{
		
		var news = $( this ).next().html();
		
		$.fn.colorbox(
		{
			opacity: 0.15,
			innerWidth: 600,
			innerHeight: 400,
			html: news
		});
	
		return false;
	});
	
	$("#easySlider").easySlider({
		auto: true, 
		continuous: true,
		nextId: "sliderNext",
		prevId: "sliderPrev",
		pause: 3000
	})
	
	$('a.mattoni')
		.click(
			function(){
				$('div.video').slideDown({ duration: 800, easing: 'bounceout'})
				return false;
			}
		)
	
	$('.video > .close')
		.click(
			function(){
				$('div.video').stop().slideUp();
				var video = $('div.vimeo').html();
				$('div.vimeo').html('');
				$('div.vimeo').html(video);
				
				return false;
			}
		);
	
	$('.bandiere a')
		.click(
			function(){
				var $this = $(this);
				var $cart = $('div.cartello');
				var name = $(this).attr('href');
				var content = $('div#'+name).html();
				
				if( ( $cart.hasClass('open') ) ){
					$cart.slideUp(
						200
						,function(){
							$cart.children('div').html(content);
							$cart.slideDown({ duration: 800, easing: 'bounceout'}).addClass('open');
						}
					);
				}else{
					$cart.children('div').html(content);
					$cart.slideDown({ duration: 800, easing: 'bounceout'}).addClass('open');
				}
				
				return false;
			}
		)
	
	//$('a[rel=lightbox]').lightBox();
	
	//$('ul.ticket').newsScroll($('ul.ticket'), 13);

	var newsTick = function()
	{
		
		$( 'ul.ticket li:first' ).animate( { marginTop: -65, opacity: 0 }, move2last );
		
		setTimeout( newsTick, 2000 );
	};

	var move2last = function()
	{
		
		var li = $( this ).html();
	
		$( 'ul.ticket ').append( '<li class="hidden">'+ li +'</li>' );
		
		$( this ).remove();
		
		$( 'ul.ticket li:last' ).fadeIn();
	};
	
	var sTimer = setTimeout( newsTick, 1500 );
	
	//$('ul.pizze').newsScroll($('ul.pizze'), 6);
	$('ul.contesto').contestoScroll($('ul.contesto'), 0);
	
	setTimeout(
		function(){
			$('div#home .airplane').animate({'marginLeft':'0px'}, 1600);
		}
	, 1000)
	
	var hash = self.document.location.hash.substring(1);
	
	if(hash){
		if(hash == 'il-ristorante'){
			var left = 1400; 
			var $voice = $('#menu-ristorante');
		}
		
		else if(hash == 'pizzeria'){
			var left = 4200;
			var $voice = $('#menu-pizzeria');
		}
			
		else if(hash == 'il-contesto'){
			var left = 5600;
			var $voice = $('#menu-contesto');
		}
		
		else if(hash == 'photogallery'){
			var left = 7000;
			var $voice = $('#menu-photogallery');
		}
		
		else if(hash == 'contatti'){
			var left = 8400;
			var $voice = $('#menu-contatti');
		}

		if($voice){
			var left = left - 85;
			$('#menu').animate({left: left+'px'},{queue: false, duration: 150});
		
			var css = $voice.css('background-position');
			var cssArray = css.split(' ');
			var y = cssArray[1];
			$voice.css({'background-position': '-101px '+ y})
		}
	}
	
	$('a.home')
		.click(
			function(){
				$voices = $('ul.nav > li > a');
				$voices
					.each(
						function(i, el) {
   						var $this = $(el);
							$this.attr('style','');
   					}
   				);
			}
		);
	
	$('ul.nav > li > a')
		.click(
			function(){
				// ciclo tutte le voci del menu
				$voices = $('ul.nav > li > a');
				$voices
					.each(
						function(i, el) {
   						var $this = $(el);
							$this.attr('style','');
   					}
   				);
   			
   			// imposto il background attivo alla voce cliccata
				$this = $(this)
				var css = $this.css('background-position');
				var cssArray = css.split(' ');
				var y = cssArray[1];
				$this.css({'background-position': '-101px '+ y})
			}
		)

	$(window)
		.scroll(
			function(){
				var left = $(window).scrollLeft();
				var left = left + 15;
				$('#menu').animate({left: left+'px'},{queue: false, duration: 150});
			}
		);
});
