jQuery(document).ready(function($){
	
	jQuery("#breadCrumb0").jBreadCrumb({easing:'swing'});
	jQuery("#breadCrumb1").jBreadCrumb({easing:'swing'});
	
	// Lists last element
	
	$('ul').each(function(){
		$(this).find('li:last').addClass('last');
	});
	
	// Lists break in 2 Columns
	
	var ols = $('.formated ol');
	/*
	ols.each(function(index){
		
		var olWidth		= $(this).width() - parseFloat($(this).css('padding-left')) - parseFloat($(this).css('padding-right'));
		var li 			= $(this).find('li');
		var liAmount 	= li.size();
		
		console.log(olWidth);
		
		if( liAmount > 15 )
		{
			var leftSide	= Math.ceil(liAmount/2);
			var halfWidth	= Math.floor(olWidth/2);
			var liSample	= li.filter(':first');
			var liWidth		= halfWidth - parseFloat(liSample.css('padding-left')) - parseFloat(liSample.css('padding-right'));
			
			li.each(function(index){
				if ( index < leftSide )
				{
					$(this).css({ 'width': liWidth + 'px', 'float': 'left' });
				}
				else
				{
					$(this).css({ 'width': liWidth + 'px', 'float': 'right' });
				}
			});			
		}
		
	});
	*/
	
	// List Color Bullets
	
	var formatedAreas = $('div.formated');
	
	if ( formatedAreas.size() )
	{
		formatedAreas.each(function(){
			var lists = $('ul li', $(this));
			lists.each(function(){
				$(this).wrapInner('<span></span>');
			});
		});
	}
	
	// Categories List Label
	
	var post_cats = $('#body #records-list .blog-post .post-categories');
	
	if ( post_cats.size() )
	{
		post_cats.prepend('<li class="label">Categories:</li>');
	}
	
	// Images boxes
	
	// $('.formated .aligncenter').each(function(index){
	// 	$(this).wrap('<span class="imgBorder"></span>');
	// });
	
	// Browser detection
	
	var browserClass = false;
	
	if ( $.browser.msie )
		browserClass = 'msie';
	if ( $.browser.webkit )
		browserClass = 'webkit';
	if ( $.browser.mozilla )
		browserClass = 'mozilla';
	if ( $.browser.opera )
		browserClass = 'opera';
	
	if ( browserClass )
		$('body').addClass(browserClass);
	
	// Newsletters
	
	$('#newsletter-email-add').click(function(e){
		// Act on the event
		e.preventDefault();
	});
	
	// Header Links (Icons)
	
	$('#header .social a').add( $('#header .languages a') ).each(function(){
		
		if ( !$(this).hasClass('active') )
		{
			$(this).css({ opacity: .5});

			$(this).hover(function() {
				$(this).animate({ opacity: 1}, 100);
			}, function() {
				$(this).animate({ opacity: .5}, 100);
			});
		}
				
	});
	
	// Dead links
	
	$('a[href=#]').click(function(e){
		e.preventDefault();
	});
	
	// Footer Gallery Slide
	
	var footerGalleryLinks = $('#section-footer-gallery .scrollable .items div.page a');
	
	if ( footerGalleryLinks.size() )
	{
		footerGalleryLinks
			.hover(function(){
				$(this).find('span').animate({ opacity: 0 }, { duration: 150, queue: false });
			}, function(){
				$(this).find('span').animate({ opacity: .4 }, { duration: 150, queue: false });
			})
			.find('span').css({ opacity: .4 });

		footerGalleryLinks.fancybox({
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'speedIn'			: 600, 
			'speedOut'			: 200,
			'overlayOpacity'	: 0.4,
			'overlayColor'		: '#444444',
			'overlayShow'		: true,
			'padding'			: 0
		});
	}
		
	var footerGallerySlide = $("#section-footer-gallery .scrollable");
	
	if ( footerGallerySlide.find('.page').size()>1 )
	{
		$('#section-footer-gallery a.right').removeClass('disabled');
		footerGallerySlide.scrollable({
			easing		: 'easeInOutQuad',
			keyboard	: false
		});
	}
	
	// FAQs
	
	var faqlist = $('#faqs-list');
	
	if ( faqlist.size() )
	{
		var questions 	= $('.question', faqlist);
		var answers 	= $('.answer', faqlist);
		
		$('.faq:last', faqlist).addClass('faq-last');

		questions.each(function(){			
			var q		= $(this);
			var a 		= q.next();
			var span	= a.find('span');
			
			q
			.hover(function(){
				q.addClass('question-hover');
			}, function(){
				q.removeClass('question-hover');
			})
			.click(function(e){
				if ( !q.hasClass('question-open') )
				{
					questions.removeClass('question-open');
					q.addClass('question-open');
					answers.animate({ 'height': '1px'}, 300, 'swing', function(){
						a.animate({ 'height': (span.height() + 5) + 'px'}, 300, 'swing');
					});
				}					
				e.preventDefault();
			});
		});
	}
	
	// --- LOGIN ---
	
	$(".download-login-trigger").fancybox({
		'scrolling'				: 'no',
		'padding'				: 0,
		'width'         		: 583,
		'height'        		: 'auto',
		'titleShow'				: false,
		'hideOnOverlayClick'	: false,
		'hideOnContentClick'	: false,
		'enableEscapeButton'	: false,
		'onClosed'				: function( el ){
			// console.log( el );
		    // $("#login_error").hide();
			// console.log('closed');
		}
	});
	
	$('#user-identification').each(function(){
		
		var form 			= $(this);
		var form_working 	= false;
		var submit 			= $(this).find('.submit');
		var notice 			= $(this).find('.notice');
		var fields_holder 	= $(this).find('.fields');
		var fields			= $(this).find(':input[requires]');
		
		fields.each(function(){
			var label = $(this).prev();
			label.html( label.html() + '*' );
		});
		
		submit
		.hover(function() {
			submit.animate({ opacity: .8}, 100);
		}, function() {
			submit.animate({ opacity: 1}, 100);
		})
		.click(function(e){
			
			if ( !form_working )
			{
				form_working = true;
				var errors = 0;

				fields.each(function(){

					var container 	= $(this).closest('p');
					var required 	= $(this).attr('requires');

					switch ( required )
					{
						case 'text':
							if( $(this).val() == '' ){
								container.addClass('error');
								errors++;
							} else {
								container.removeClass('error');
							}
						break;
						case 'email':
							if( !$(this).val().match( /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+\_]+\.[\w\-\+_]{2,4}(\.[\w\-\+_]+)?$/ ) ){
								container.addClass('error');
								errors++;
							} else {
								container.removeClass('error');
							}
						break;
					}
				});

				if ( errors == 0 )
				{
					var form_data 	= form.serialize();
					form_data 		+= '&submited=1';
					
					$.ajax({
						url			: JSIN.template_url + '/ajax/register-information.php',
						type		: 'POST',
						data		: form_data,
						dataType	: 'json',
						beforeSend	: function(){
							fields_holder.animate({ opacity: .5}, 200);
						},
						success		: function(data, textStatus, xhr){
							
							if ( data.result > 0 )
							{
								$('#fancybox-close').hide();								
								form.html('<h4>Registration Successful</h4><div class="message">You are now registered to the site and you can download the information you requested. You will need to click on the link again in order to download the PDF.</div><span class="continue"><em>Continue</em></span><div style="clear:both;"></div>');
								$.fancybox.resize;
								$.fancybox.center;
								
								var continueBtn = form.find('.continue');
								
								continueBtn
									.click(function(e){
										location.reload(true);
										e.preventDefault();
									})
									.hover(function() {
										continueBtn.animate({ opacity: .8}, 100);
									}, function() {
										continueBtn.animate({ opacity: 1}, 100);
									});			
							}
							else
							{
								fields_holder.animate({ opacity: 1}, 200, function(){
									form_working = false;
								});
							}						
						}
					});					
				}
				else
				{
					form_working = false;
				}
			}
			
		});
	});
	
	// --- CONTACT LOCATIONS
	
	var c_locations = $('#contact-locations');
	
	if ( c_locations.size() )
	{
		c_locations.find('.region').each(function(i){
			
			var region 		= $(this);
			var countries 	= region.find('dl');
			
			countries.each(function(i){
				
				var name = $(this).find('dt');
				var list = $(this).find('dd');
				
				name.click(function(e){
					
					if ( !$(this).hasClass('opened') )
					{
						countries.find('dd').slideUp( 300, 'swing', function(){
							countries.find('dt').removeClass('opened');
							name.addClass('opened');
							setTimeout(function(){
								list.slideDown( 300, 'swing' );
							}, 300);
						});
					}
					
					e.preventDefault();
				});
			});
		});
	}
	
	// --- Form: Contact
	
	/*
	$('form#dl_leadForm').contact({
		scrolltoerrors	: true,
		scrolloffset	: -40,
		callback		: function( form )
		{
			$('#dl_qs').val(window.location.search);
			$('#dl_r').val(document.referrer);
			form.submit();
		}
	});
	*/
	
	$('form#dl_leadForm').contact({
		scrolltoerrors	: true,
		scrolloffset	: -40,
		confirmation	: 'Your message has been sent. Thank You.',
		callback		: function( form )
		{
			var confirmation 	= form.find('.confirmation');
			var form_data 		= form.serialize();
			form_data 			+= '&submited=1';
			
			$.ajax({
				url			: JSIN.template_url + '/ajax/send-message-contact.php',
				type		: 'POST',
				data		: form_data,
				dataType	: 'json',
				beforeSend	: function()
				{
					form.animate({ opacity: .5}, 200);
				},
				success		: function( data, textStatus, xhr )
				{				
					if ( data.result == 1 )
					{
						form.find(':input').filter(':visible').val('');
						form.find(':checkbox').removeAttr("checked");
						
						$.scrollTo( $('h1'), 500, {
							axis		: 'y',
							offset		: -40,
							onAfter		: function(){
								confirmation.slideDown( 200, function(){
									setTimeout(function(){
									confirmation.slideUp( 200 );
									}, 3000);
								});
							}
						});
					}
					
					form.animate({ opacity: 1}, 200, function(){
						form.data('working', false);
					});
				}
			});
		}
	});
	
	// --- Form: Summit
	
	$('form#form_summit').contact({
		scrolltoerrors	: true,
		scrolloffset	: -40,
		confirmation	: 'Your message has been sent. Thank You.',
		callback		: function( form )
		{
			var confirmation 	= form.find('.confirmation');
			var form_data 		= form.serialize();
			form_data 			+= '&submited=1';
			
			$.ajax({
				url			: JSIN.template_url + '/ajax/send-message-summit.php',
				type		: 'POST',
				data		: form_data,
				dataType	: 'json',
				beforeSend	: function()
				{
					form.animate({ opacity: .5}, 200);
				},
				success		: function( data, textStatus, xhr )
				{				
					if ( data.result == 1 )
					{
						form.find(':input').filter(':visible').val('');
						confirmation.slideDown( 200, function(){
							setTimeout(function(){
								confirmation.slideUp( 200 );
							}, 3000);
						});
					}
					
					form.animate({ opacity: 1}, 200, function(){
						form.data('working', false);
					});
				}
			});
		}
	});
	
	// --- Form: Webinars
	
	$('form#form_webinars').contact({
		scrolltoerrors	: true,
		scrolloffset	: -40,
		confirmation	: 'Your message has been sent. Thank You.',
		callback		: function( form )
		{
			var confirmation 	= form.find('.confirmation');
			var form_data 		= form.serialize();
			form_data 			+= '&submited=1';
			
			$.ajax({
				url			: JSIN.template_url + '/ajax/send-message-webinars.php',
				type		: 'POST',
				data		: form_data,
				dataType	: 'json',
				beforeSend	: function()
				{
					form.animate({ opacity: .5}, 200);
				},
				success		: function( data, textStatus, xhr )
				{				
					if ( data.result == 1 )
					{
						form.find(':input').filter(':visible').val('');
						form.find(':checkbox').removeAttr("checked");
						
						confirmation.slideDown( 200, function(){
							setTimeout(function(){
								confirmation.slideUp( 200 );
							}, 3000);
						});
					}
					
					form.animate({ opacity: 1}, 200, function(){
						form.data('working', false);
					});
				}
			});
		}
	});
	
	// --- Site Search
	
	$('#site-search-field').labelValue({
		button			: $('#site-search-do'),
		defaultText		: 'search site...',
		callback		: function( o ){
			o.input.closest('form').submit();
		}
	});
	
	
	// --- Newsletters - Footer
	
	$('#newsletter-email').labelValue({
		button			: $('#newsletter-email-add'),
		defaultText		: 'enter your e-email address...',
		callback		: function( o ){
			
			if ( !o.input.val().match( /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+\_]+\.[\w\-\+_]{2,4}(\.[\w\-\+_]+)?$/ ) )
			{
				o.input.data('working', false);
				alert('Please provide a valid email address.');
			}
			else
			{
				$.ajax({
					url			: JSIN.template_url + '/ajax/send-message-newsletter.php',
					type		: 'POST',
					data		: { submited: 1, email: o.input.val() },
					dataType	: 'json',
					success		: function( data, textStatus, xhr )
					{				
						if ( data.result == 1 )
						{
							alert('Your subscription to our newsletter is now active.');
							o.input.val( o.defaultText );
						}

						o.input.data('working', false);
					}
				});
			}
		}
	});
	
	
	// --- Newsletters - Sidebar
	
	$('#newsletter-email-sidebar').labelValue({
		button			: $('#newsletter-email-sidebar-add'),
		defaultText		: 'enter your e-email address...',
		callback		: function( o ){
			
			if ( !o.input.val().match( /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+\_]+\.[\w\-\+_]{2,4}(\.[\w\-\+_]+)?$/ ) )
			{
				o.input.data('working', false);
				alert('Please provide a valid email address.');
			}
			else
			{
				$.ajax({
					url			: JSIN.template_url + '/ajax/send-message-newsletter.php',
					type		: 'POST',
					data		: { submited: 1, email: o.input.val() },
					dataType	: 'json',
					success		: function( data, textStatus, xhr )
					{				
						if ( data.result == 1 )
						{
							alert('Your subscription to our newsletter is now active.');
							o.input.val( o.defaultText );
						}

						o.input.data('working', false);
					}
				});
			}
		}
	});
	
	
	// --- 
	
	$(".manufacturing-event-trigger").fancybox({
		'scrolling'		: 'no',
		'padding'		: 0,
		'width'         : 583,
		'height'        : 'auto',
		'titleShow'		: false,
		'onStart'     	: function(e){
	        $('#manufacturing-event-title').val($(e).attr('event'));
	    },
		'onClosed'     	: function(e){
	        $('#manufacturing-event-title').val('');
	    },
		'onComplete'	: function(){
			$('form#manufacturing-event-register').contact({
				confirmation	: false,
				callback		: function( form )
				{
					var form_data = form.serialize();
					var form_data 		= form.serialize();
					form_data 			+= '&submited=1';

					$.ajax({
						url			: JSIN.template_url + '/ajax/send-message-events.php',
						type		: 'POST',
						data		: form_data,
						dataType	: 'json',
						beforeSend	: function()
						{
							form.animate({ opacity: .5}, 200);
						},
						success		: function( data, textStatus, xhr )
						{				
							if ( data.result == 1 )
							{
								alert('Your information has been submited. Thank you.');
								form.find(':input').filter(':visible').val('');
								form.find(':checkbox').removeAttr("checked");
								$.fancybox.close();
							}

							form.animate({ opacity: 1}, 200, function(){
								form.data('working', false);								
							});
						}
					});
				}
			});
		}
	});
	
});
