function make_video(id)
{
	var video = '';
	video += '<object width="498" height="300">';
	video += '<param name="movie" value="http://www.youtube.com/v/'+id+'&hl=it_IT&fs=1&rel=0"></param>';
	video += '<param name="allowFullScreen" value="true"></param>';
	video += '<param name="allowscriptaccess" value="always"></param>';
	video += '<embed src="http://www.youtube.com/v/'+id+'&hl=it_IT&fs=0&rel=0&autoplay=1" type="application/x-shockwave-flash" ';
	video += 'allowscriptaccess="always" allowfullscreen="true" width="498" height="300">';
	video += '</embed>';
	video += '</object>';
	
	return video;
}

jQuery(document).ready(function($) {

	$.navigation();	
	$.removeNavBorders();
	
	$( '#qtranslate-chooser' ).find( '.qtrans_flag_pl' ).parent().hide();
	
	$( '.technical tr' ).children( 'td' ).css( 'width', '300px' );
	
	$('form#rete-vendita').submit(function()
	{
		var inputs = ['#nome', '#cognome', '#cellulare', '#email'],
			totinputs = inputs.length,
			filter= /^[\w](([_\.-]?[\w]+)*)@([\w]+)(([\.-]?[\w]+)*)\.([A-Za-z]{2,})$/,
			error = false;
		
		for( var i=0; i<totinputs; i++ ){
			
			var obj = $( inputs[i] );
			
			if( obj.val() == '' || ( obj.hasClass('email') && !filter.test( obj.val() ) ) ){
				obj.addClass('error').show();
				error = true;
			}
		}
		
		if( error ){
			$('div.error').eq(0).fadeIn();
			return false;
		}
		
		return true;
	});

	$('form#maggiori-info').submit(function()
	{
		var inputs = ['#nome', '#cognome', '#email', '#telefono'],
			totinputs = inputs.length,
			filter= /^[\w](([_\.-]?[\w]+)*)@([\w]+)(([\.-]?[\w]+)*)\.([A-Za-z]{2,})$/,
			error = false;
		
		for( var i=0; i<totinputs; i++ ){
			
			var obj = $( inputs[i] );
			
			if( obj.val() == '' || ( obj.hasClass('email') && !filter.test( obj.val() ) ) ){
				obj.addClass('error').show();
				error = true;
			}
		}
		
		if( error ){
			$('div.error').eq(0).fadeIn();
			return false;
		}
		
		return true;
	});

	$('form#curriculum').submit(function()
	{
		var inputs = ['#nome', '#cognome', '#cellulare'],
			totinputs = inputs.length,
			filter= /^[\w](([_\.-]?[\w]+)*)@([\w]+)(([\.-]?[\w]+)*)\.([A-Za-z]{2,})$/,
			error = false;
		
		for( var i=0; i<totinputs; i++ ){
			
			var obj = $( inputs[i] );
			
			if( obj.val() == '' || ( obj.hasClass('email') && !filter.test( obj.val() ) ) ){
				obj.addClass('error').show();
				error = true;
			}
		}
		
		if( error ){
			$('div.error').eq(0).fadeIn();
			return false;
		}
		
		return true;
	});

	$('a.terms').colorbox(
	{
		href: function()
		{
			return '/wp-content/themes/ravelli/ajax/terms.php?lang=' + $( this ).attr( 'rel' );
		},
		width: '600px'
	});

	$('#video-box').fadeTo(0, 0);

	$('.jScroll').jScrollPane({showArrows:true});
	
	$('#product-page .main #one-col div.product').click(
		function()
		{
			var ref = $(this).children('a').attr('href');
			location.href = ref;
		}
	);
	
	$('#show-foto').click(
		function()
		{
			if( $(this).hasClass('active') )
				return false;
			
			$(this)
				.addClass('active')
				.next().removeClass('active')
				.parent().css('background-position', '0 0');
			
			$('#video-box').fadeTo(300, 0);
			
			return false;
		}
	);

	$('#show-video').click(
		function()
		{
			if( $(this).hasClass('active') )
				return false;
			
			$(this)
				.addClass('active')
				.prev().removeClass('active')
				.parent().css('background-position', '0 -40px');
			
			$('#video-list').jScrollPane({showArrows:true});
			
			$('#video-box').fadeTo(400, 1);
			
			$('#video-list ul li a').click(
				function()
				{
					var video_id = $(this).attr('rel');
					
					var video = make_video(video_id);
					$('#video-overlay').fadeTo(600, 0.5).show();
					$('#video-place').html(video);
					
					$('#video-overlay').click(
						function()
						{
							$('#video-box').fadeTo(400, 0, function()
							{
								$('#show-foto').addClass( 'active' );
								$('#show-video').removeClass( 'active' );
								$('#video-place').html('');
							});
							
							$(this).fadeTo(400, 0, function(){
								$(this).removeAttr('style');
							});
							return false;
						}
					);
					
					return false;
				}
			);
			
			return false;
		}
	);
	
	// tickets
	var ticket = function()
	{
		setTimeout(function(){
			
			$('#mac-name').animate({opacity: '0', top: '-10px'}, 500, 'easeInBack', function(){
				var text = $('#ticket li:first').next().children().attr('alt');
				$(this).html(text).animate({opacity: '1', top: '20px'}, 400);
			});
			
			$('#ticket li:first')
				.animate({marginLeft: '-465px'}, 800, 'easeInBack', function(){
					$(this).detach().appendTo('ul#ticket').removeAttr('style');	
				});
			ticket();
		}, 4000);
	};
	
	ticket();

	
	if( !$.isPage('contatti') && !$.isPage('prodotti') ){
	
		$('.ban-slider').easySlider({
			auto: true,
			pause: 4000,
			continuous: true 
		});

	}else if($.isPage('prodotti')){
		
		$('a#pdf').click(
			function()
			{
				var lang = $( this ).attr( 'rel' );
				$.fn.colorbox({
					  opacity: 0.6
					, close: '[x]'
					, href: '/wp-content/themes/ravelli/ajax/dwn_pdf.php?lang=' + lang
					, onComplete: function()
						{
							var $email = $('#dwn-email'),
								$submit = $('a#submit-mail');
							
							$email.click(
								function()
								{
									this.select();
								}
							);
							
							$('#privacy').click(
								function()
								{
									$('#privacy-box').slideDown(
										function()
										{
											$.fn.colorbox.resize()
										}
									);
									return false;
								}
							);
							
							$submit.click(
								function()
								{
									var filter 	= /^[\w](([_\.-]?[\w]+)*)@([\w]+)(([\.-]?[\w]+)*)\.([A-Za-z]{2,})$/,
										mail = $email.val();
									
									if( !filter.test(mail) ){
										var m = document.getElementById('dwn-email');
										var message = lang == 'it' ? 'inserisci un\'indirizzo email valido.' : 'insert a valid email address.';
										
										if( lang == 'pl' )
											message = 'wpisz prawidłowy adres email.';
										
										alert( message );
										m.focus();
										m.select();
										return false;
									}
									else{
										$('#all').slideUp(100);
										$('#thanks').slideDown(function(){
												$.fn.colorbox.resize();
										});
			
										$.post('/wp-content/themes/ravelli/ajax/save_email.php', {email: mail});
									}
									return false;
								}
							);
								
						}
				});
				
				return false;
			}
		);
		
		$('a.prod-icon')
			.hover(
				function(){
					$(this).nextAll('div.preview').children('img.off').stop().fadeTo(600, 0.6);
					$(this).nextAll('div.name').children().css({'color':'#f18034'});
				}, function(){
					$(this).nextAll('div.preview').children('img.off').stop().fadeTo(300, 1);
					$(this).nextAll('div.name').children().css({'color':'#0D6D9F'});
				}
			);

		$('a[rel=dimensioni]').colorbox({
			  opacity: 0.6
			, next: '[>>]'
			, previous: '[<<]'
			, current: '{current} / {total}'
			, close: '[x]'
		});
		
		$('a.dime').hover(
			function(){
				$(this).children('img').stop().fadeTo(800, 0);
			}, function(){
				$(this).children('img').stop().fadeTo(400, 1);
			}
		);
		
		var apps = function()
		{
			setTimeout(function()
			{
				$( '.app-bar a' ).find( 'img:last' ).fadeOut( 1100, function()
				{
					
					$( this ).parent().find( 'img:first' ).before( $( this ).detach().removeAttr( 'style' ) );
				});
				
				apps();
				
			}, 3000 );
		};
		
		apps();
		
	/*
		var apps1 = function()
		{
			setTimeout(function(){
				$("a#app1").children('img:first').animate({marginLeft: '-183px'}, 800, function(){
					$(this).detach().appendTo('#app1').removeAttr('style');
				});
				apps1();
			}, 4000);
		};
	
		apps1();

		var apps2 = function()
		{
			setTimeout(function(){
				$("a#app2").children('img:first').animate({marginLeft: '-183px'}, 800, function(){
					$(this).detach().appendTo('#app2').removeAttr('style');
				});
				apps2();
			}, 4000);
		};
	
		apps2();
		*/
		
		/*
		$("a#[id*='app']")
			.hover(
				function(){
					$(this).children().stop().fadeTo(700, 0);
				}, function(){
					$(this).children().stop().fadeTo(300, 1);
				}
			)
		*/
		
		$("a#[id*='app']")
			.colorbox({
				opacity: 0.6,
				html: function(){
					var div = $(this).attr('rel');
					var content = $('#'+div+'').html();
					
					if( !$('#'+div+' ul li').size() ){
						content = '<div style="width: 300px; text-align: center; background: #fff; padding: 25px 5px; color: #666;">Al momento non ci sono elementi associati a questa categoria</div>';
					}
					return content;
				},
				onComplete: function(){
					$('#pop-gallery ul li a.pop-tip')
						.each(
							function(){
								
								var thetitle = $(this).attr('name');
								
								$(this)
									.click(function(){return false;})
									.qtip(
										{
											content: {
												title: {
													text: thetitle
												},
												text: '<img src="'+$(this).attr('href')+'" alt="" />'
											},
											position: {
												corner: {
													target: 'rightMiddle',
													tooltip: 'leftMiddle'
												},
												adjust: { x: -10 }
											},
											style: {
												width: 'auto',
												padding: '14px',
												border: {
													width: 9,
													radius: 9,
													color: '#457a9d'
											    },
												tip: {
													corner: 'leftMiddle',
													size: { x: 8, y: 14 }
												},
												name: 'light'
											},
											api: {
												onHide: function(){
												}
											}
										}
									)
							}
						)
				}
			});

		if( $('.slider > ul > li').size() > 1 ){
			$('.slider').easySlider({
				auto: true,
				pause: 3000,
				continuous: true 
			});
		}

		var $btns = $('#prevBtn, #nextBtn');
		
		$btns.fadeTo(0, 0);
		
		$('.box-image').hover(
			function(){
				$btns.stop().fadeTo(400, 1);
			}, function(){
				$btns.stop().fadeTo(200, 0);
			}
		);

		$('li.page-item-6').addClass('current_page_parent');
		$('li.page-item-8').removeClass('current_page_parent');


		$.callTip();

	}

	$.expr[':'].icontains = function(obj, index, meta, stack){
		return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
	};

	$('input[name=search]')
		.click(
			function(){
				this.select();
			}
		)
		.keyup(
			function(){
				var str = $(this).val();
				if( str.length > 0 && str != ' ' ){
					$('ol > li > a').not(":icontains('"+str+"')").removeAttr('style').parent().removeAttr('style');
					$("ol > li > a:icontains('"+str+"')").css({'color': '#015c8b'}).parent().css({'background-color':'#fff'})
				}else{
					$('ol > li').removeAttr('style');
				}
			}
		);

});

jQuery.easing.easeInBack = function( x, t, b, c, d, s )
{
	if (s == undefined) s = 1.70158;
	return c*(t/=d)*t*((s+1)*t - s) + b;
}

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



