$(function(){
	$('.content').hide();
	$('.aboutus').hide();
	$('.services').hide();
	$('.portfolio').hide();
	$('.btn-about').bind('click', function(){
		if($(this).parent().hasClass('selected')){
			$(this).parent().removeClass('selected');
		} else {
			$(this).parent().parent().find('li').removeClass('selected');
			$(this).parent().addClass('selected');
		}
		$('.aboutus').toggle(200);
		$('.services').hide();
		$('.portfolio').hide();
		return false;
	});
	$('.btn-services').bind('click', function(){
		if($(this).parent().hasClass('selected')){
			$(this).parent().removeClass('selected');
		} else {
			$(this).parent().parent().find('li').removeClass('selected');
			$(this).parent().addClass('selected');	
		}
		$('.aboutus').hide();
		$('.services').toggle(200);
		$('.portfolio').hide();
		return false;
	});
	$('.btn-portfolio').bind('click', function(){
		if($(this).parent().hasClass('selected')){
			$(this).parent().removeClass('selected');
		} else {
			$(this).parent().parent().find('li').removeClass('selected');
			$(this).parent().addClass('selected');
		}
		$('.aboutus').hide();
		$('.services').hide();
		$('.portfolio').toggle(200);
		return false;
	});
	$('.serviceslist a').bind('click', function(){
		if($(this).hasClass('closed')){
			$(this).removeClass('closed');
			$(this).addClass('open');
			$(this).parent().find('.content').toggle();
		} else {
			$(this).removeClass('open');
			$(this).addClass('closed');
			$(this).parent().find('.content').toggle();
		}
		return false;
	});

	var hashtag = window.location.hash;
	var modal = '<div class="modal"><div class="container"></div></div>';
	if(hashtag == '#help'){
		$('body').prepend(modal);
		$('.modal').css('min-height', $('body').height());
		$.ajax({
			url: "help.html",
			dataType: "html",
			success: function(page){
				$('.container').append(page);

				$('#a').keypress(function(){
					if($(this).val().length >= 2){
						$('#b').focus();
					}
				});

				$('#b').keypress(function(){
					if($(this).val().length >= 2){
						$('#c').focus();
					}
				});

				$('#submitHelp').live('click', function(){
					$('#hPersonName').css('border', '');
					$('#hCompanyName').css('border', '');
					$('#hEmailAddress').css('border', '');
					$('#hCity').css('border', '');
					$('#hState').css('border', '');
					$('#hValidator').css('border', '');
					$('.error').hide();

					var valid = true;
					var personName = $('#hPersonName').val();
					var companyName = $('#hCompanyName').val();
					var emailAddress = $('#hEmailAddress').val();
					var city = $('#hCity').val();
					var state = $('#hState').val();
					if(personName == ''){
						$('#hPersonName').css('border', '1px solid #cf7474');
						$('#hPersonName').parent().find('.error').show();
						valid = false;
					}
					if(companyName == ''){
						$('#hCompanyName').css('border', '1px solid #cf7474');
						$('#hCompanyName').parent().find('.error').show();
						valid = false;
					}
					if(emailAddress == ''){
						$('#hEmailAddress').css('border', '1px solid #cf7474');
						$('#hEmailAddress').parent().find('.error').show();
						valid = false;
					}
					if(city == ''){
						$('#hCity').css('border', '1px solid #cf7474');
						$('#hCity').parent().find('.error').show();
						valid = false;
					}
					if(state == 'default'){
						$('#hState').css('border', '1px solid #cf7474');
						$('#hState').parent().find('.error').show();
						valid = false;
					}

					var validator = $('#hValidator').val() + '';
					if(validator.toLowerCase() != 'insight'){
						$('#hValidator').css('border', '1px solid #cf7474');
						valid = false;
					}

					if(valid == true){
						$('#submitHelp').attr('disabled', 'disabled');
						$.ajax({
							type: 'POST',
							url: 'process_help.php',
							data: $('.helpform').serialize(),
							success: function(){
								$('.helpform').parent().html('<h2>Help</h2><p>Your message has been sent. We will be in contact with you shortly. Thank You.</p>');
								$('.container').css('height', '300px');
							},
							error: function(){
								
							}
						});
					}

					return false;
				});
			}
		});
	}

	$('.fill').animate({
		width: '23%'
	},{
		duration: 2000,
		step: function(){
			$('.labels p').html('|<br />' + parseInt(100 * $('.fill').width() / 928) + '%');
		}
	});
	$('.labels').animate({
		left: '23%'
	}, 2000);
	$('.labelsdetail').animate({
		left: '23%'
	}, 2000);

	$('.milestones a').bind('click', function(){
		if($(this).parent().find('.detail').css('display') == 'none'){
			$(this).parent().parent().find('.detail').hide();
			$(this).parent().find('.detail').show();	
		} else {
			$(this).parent().parent().find('.detail').hide();
		}
		return false;
	});

	$('.mapGF').bind('click', function(){
		toggleMapArea();
		$('.mapArea').html('<iframe width="430" height="175" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Insight+Technologies+Inc,+Demers+Avenue,+Grand+Forks,+ND&amp;aq=0&amp;sll=46.859314,-96.852998&amp;sspn=0.011122,0.026457&amp;vpsrc=6&amp;ie=UTF8&amp;hq=Insight+Technologies+Inc,&amp;hnear=Demers+Ave,+Grand+Forks,+North+Dakota&amp;t=m&amp;ll=47.918643,-97.063694&amp;spn=0.020133,0.073643&amp;z=13&amp;iwloc=near&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=Insight+Technologies+Inc,+Demers+Avenue,+Grand+Forks,+ND&amp;aq=0&amp;sll=46.859314,-96.852998&amp;sspn=0.011122,0.026457&amp;vpsrc=6&amp;ie=UTF8&amp;hq=Insight+Technologies+Inc,&amp;hnear=Demers+Ave,+Grand+Forks,+North+Dakota&amp;t=m&amp;ll=47.918643,-97.063694&amp;spn=0.020133,0.073643&amp;z=13" style="color:#80ABAA;text-align:left">View Larger Map</a></small>');
		return false;
	});

	$('.mapFargo').bind('click', function(){
		toggleMapArea();
		$('.mapArea').html('<iframe width="430" height="175" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Insight+Technologies,+43rd+Street+South,+Fargo,+ND&amp;aq=0&amp;sll=47.916586,-97.064294&amp;sspn=0.043604,0.105829&amp;vpsrc=6&amp;ie=UTF8&amp;hq=Insight+Technologies,&amp;hnear=43rd+St+S,+Fargo,+Cass,+North+Dakota+58103&amp;t=m&amp;ll=46.859677,-96.852894&amp;spn=0.002568,0.009205&amp;z=16&amp;iwloc=near&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=Insight+Technologies,+43rd+Street+South,+Fargo,+ND&amp;aq=0&amp;sll=47.916586,-97.064294&amp;sspn=0.043604,0.105829&amp;vpsrc=6&amp;ie=UTF8&amp;hq=Insight+Technologies,&amp;hnear=43rd+St+S,+Fargo,+Cass,+North+Dakota+58103&amp;t=m&amp;ll=46.859677,-96.852894&amp;spn=0.002568,0.009205&amp;z=16" style="color:#80ABAA;text-align:left">View Larger Map</a></small>');
		return false;
	});

	function toggleMapArea(){
		if($('.mapArea').css('display') == 'none')
			$('.mapArea').show();
	}

	$('input[name=phonea]').keypress(function(){
		if($(this).val().length >= 2){
			$('input[name=phoneb]').focus();
		}
	});

	$('input[name=phoneb]').keypress(function(){
		if($(this).val().length >= 2){
			$('input[name=phonec]').focus();
		}
	});

	$('#submit').live('click', function(){
		$('#personName').css('border', 'none');
		$('#emailAddress').css('border', 'none');

		var valid = true;
		var personName = $('#personName').val();
		if(personName == ''){
			$('#personName').css('border', '1px solid #cf7474');
			$('#personName').parent().find('.error').show();
			valid = false;
		}
		var emailAddress = $('#emailAddress').val();
		if(emailAddress == ''){
			$('#emailAddress').css('border', '1px solid #cf7474');
			$('#emailAddress').parent().find('.error').show();
			valid = false;
		}

		if(valid == true){
			$('#formContent').hide();
			$('#formValidator').show();	
		}

		return false;
	});

	$('#validate').bind('click', function(){
		var valid = false;
		var validlist = [
			'IT',
			'Citrix',
			'Network Monitoring',
			'Software Development',
			'Surveillance',
			'Video Conferencing',
			'VOIP Telephony',
			'Website Development',
			'Custom Programming'
		];
		var validator = $('#validator').val();
		for(var i = 0; i < validlist.length; i++){
			if(validator == validlist[i]){
				valid = true;
				break;
			}
		}
		if(valid == true){
			$.ajax({
				type: 'POST',
				url: 'process_form.php',
				data: $('.contact').serialize(),
				success: function(){
					$('.contact').parent().html('<h2>Contact Us</h2><p>Thank you for contacting us, we will reply back soon.</p>');					
				},
				error: function(){
					
				}
			});
		} else {
			$('#validator').css('border', '1px solid #cf7474');
		}
		return false;
	});
});
