$(document).ready(function() {
	$('.fancy').fancybox();
	$('.iframe').fancybox({
		'autoDimensions' : false,
		'width' : 800,
		'height' : 600
	});
	$('body.machines li#machine-nav').addClass('active');
	$('body#services li#machine-nav').addClass('active');
	$('body#parts li#machine-nav').addClass('active');
	$('body.films li#film-nav').addClass('active');
	$('body#printed-films li#film-nav').addClass('active');
	$('body#laminations li#film-nav').addClass('active');
	$('body#contract-slitting li#film-nav').addClass('active');
	$('body#slitting-process li#film-nav').addClass('active');
	$('body#contact li#contact-nav').addClass('active');
	$('body#home li#home-nav').addClass('active');
	$('#gravity img').css('z-index', 0);
	
	
	
	
	/**** Add ons ****/
	/** Hide Stuff **/
	
	
	$('#home #main .company-content').each(function(){
		$length = 3;
		
		if($(this).attr('data-section') == 'rtg-ilrpa'){
			$length = 5;
		}
		
		$view = $(this).html().split('.').slice(0, $length).join('.')
		$view +='.'
		$hidden = $(this).html().split('.').slice($length).join('.')

		$(this).html($view + '<div class="hidden">' +$hidden + '</div>')
	})
	
	/*
	$('#sollas p:not(:nth-child(3),:nth-child(4))').hide()
	$('#rtg-film p:not(:nth-child(3))').hide()
	$('#rtg-film p:not(:nth-child(4))').hide()
	$('#rtg-machine p:not(:nth-child(2))').hide()
	
	
	$('#rtg-film .read_more').click(function(){
		if($(this).text() == 'Read More...'){
			$(this).text('Read Less')
		}else{ $(this).text('Read More...')}
		
		
		$('#rtg-film p:not(:nth-child(3))').slideToggle('slow')
		$('#rtg-film p:not(:nth-child(4))').slideToggle('slow')
		
		
	})
	
	$('#sollas .read_more').click(function(){
	
		if($(this).text() == 'Read More...'){
			$(this).text('Read Less')
		}else{ $(this).text('Read More...')}
		
		$('#sollas p:not(:nth-child(3), :nth-child(4))').slideToggle('slow')
		
	})
	
	$('#rtg-machine .read_more').click(function(){
		
		if($(this).text() == 'Read More...'){
			$(this).text('Read Less')
		}else{ $(this).text('Read More...')}
	
		$('#rtg-machine p:not(:nth-child(3))').slideToggle('slow')
		
	})
	
	*/
	$('.read_more').click(function(){
		if($(this).text() == 'Read More...'){
			$(this).text('Read Less')
		}else{
			$(this).text('Read More...')	
		}
		$('#'+$(this).attr('data-js')+' .hidden').toggle()
	
	})
	
	
	/*---------------------
		Private Page
	---------------------*/
	var error_list = '';
	
	$('[name=new-user-submit]').click(function(){
		
		error_list = validate($('[name=new-user]'));
		if(error_list == ''){
			$.post('/wp-content/themes/RTGPKG/email.php', {name : $('[name=new-user-name]').val(), email : $('[name=new-user-email]').val(), company : $('[name=new-user-company]').val(), address : $('[name=new-user-address]').val(), city : $('[name=new-user-city]').val(), state : $('[name=new-user-state]').val(), zip : $('[name=new-user-zip]').val(), website : $('[name=new-user-website]').val(), phone : $('[name=new-user-phone]').val(), password : $('[name=new-user-password]').val() }, function(response){
				$('[data-js=sign-up-area]').fadeOut();
				$('[data-js=sign-up-area-success]').fadeIn();
				
			})
		}else{
			show_errors(error_list)		
		}
	
	})
	
	
	$('[name=existing-user-submit]').click(function(){
	
		error_list = validate($('[name=existing-user]'));
		if(error_list == ''){
		
			$.post('/wp-admin/admin-ajax.php', { action: 'validate_user', email : $('[name=existing-user-email]').val(), password : $('[name=existing-user-password]').val() }, function(response){
				
				if(response == 'accept'){
					setCookie('user', 'valid', 1000);
				
					location.reload(true)
				}else if(response == 'unauthorized'){
					alert('Unauthorized Account');
				}else if(response == 'pending'){
					alert('This Account is still pending. Please allow 24 hours for approval.')
				}else{
					alert('Incorrect Login')
				}
			})
	
		}else{
			show_errors(error_list)		
		}
	
	})
	

function validate(form){
	var errors = '';
	$('input', form).each(function(){
		if($(this).attr('name').indexOf('email') >= 0){
			if(!isValidEmailAddress($(this).val())){
				errors += '&email'
				$(this).val('Invalid Email');
				
			}
		}else{
			if($(this).val() == '' || $(this).val() == 'Required Field'){
				errors += '&'+$(this).attr('name')
			}
		}
	})
	return errors;

}
	

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};
	
	

function show_errors(error_list){
	error_list = error_list.split('&')
			for(var i = 1; i <= error_list.length; i++){
				$('[name='+error_list[i]+']').val('Required Field')
			}
		
			$('[name='+error_list[1]+']').select().focus()
	

}	
	

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}


	
});
