$(document).ready(function() {

	// open external links in new window
	$("a[rel=external]").click(function() {
		window.open(this.href);
		return false;
	});

	// front-page box effects
	$('#front_page_box h2').children().remove();
	$('#mainpic').innerfade({'speed' : 2000, 'timeout' : 10000});

	$('#front_page_box h2').append('<img class="first" style="position: relative; left: 520px;" src="/images/slide-reliable.png">');
	$('#front_page_box h2').append('<img class="second" style="position: relative; left: 520px;" src="/images/slide-fast.png">');
	$('#front_page_box h2').append('<img class="third" style="position: relative; left: 520px;" src="/images/slide-friendly.png">');
	$('#front_page_box h2').append('<img class="fourth" style="position: relative; left: 520px;" src="/images/slide-service.png">');

	$('.first').animate({'left' : '12px'}, 1500, 'swing', function() {
		$('.second').animate({'left' : '12px'}, 1500, 'swing', function() {
			$('.third').animate({'left' : '12px'}, 1500, 'swing', function() {
				$('.fourth').animate({'left' : '12px'}, 1500, 'swing');
			});
		});
	});

	$('#front_page_box ul li a').hover(function() {
		$(this).parent().siblings().children().fadeTo(300, 0.5);
	}, function() {
		$(this).parent().siblings().children().fadeTo(50, 1);
	});

	// form stuff
	$('input[type=text], textarea, select').focus(function() {
		$(this).parent().addClass('active');
		$(this).parent().prev().addClass('active');
	});

	$('input[type=text], textarea, select').blur(function() {
		$(this).parent().removeClass('active');
		$(this).parent().prev().removeClass('active');
	});

	$('input[type=radio]').focus(function() {
		$(this).parent().parent().prev().addClass('active');
	});

	$('input[type=radio]').blur(function() {
		$(this).parent().parent().prev().removeClass('active');
	});

	$('td input').focus(function() {
		$(this).parent().parent('tr').addClass('active');
	});

	$('td input').blur(function() {
		$(this).parent().parent('tr').removeClass('active');
	});

	//$('.default input').focus();

	// order form add an item
	$('a#add_item').click(function () {
		var cur = parseInt(this.rel);
		var nxt = cur + 1;
		var row_class = '';
		if(nxt%2) {
			var row_class = 'odd';
		}

		var table_row = $('<tr style="display: none;" id="row-' + nxt + '"><td>' + nxt + '.</td></tr>');

		$(table_row).append('<td id="cat-number-' + nxt + '"><input name="catnumber-row' + nxt + '" id="catnumber-row' + nxt + '" /></td>');
		$(table_row).append('<td id="descr-' + nxt + '"><input name="descr-row' + nxt + '" id="descr-row' + nxt + '" /></td>');
		$(table_row).append('<td id="qty-' + nxt + '"><input name="qty-row' + nxt + '" id="qty-row' + nxt + '" /></td>');
		$(table_row).append('<td id="cost-' + nxt + '"><input name="cost-row' + nxt + '" id="cost-row' + nxt + '" /></td>');

		$('table#order tbody').append(table_row);
		$(table_row).addClass(row_class).fadeIn('slow');

		$(this).attr('rel', nxt);

		return false;
	});

	// argos form validation
	$('div.required').append('<span class="required">*</span>').children('input').addClass('required');
	$('#form_argos').validate({
		ignore: '.ignore',
		errorClass: 'form_validation_error',
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if(errors) {
				var message = errors == 1
					? 'There were errors with one field of the form. It has been highlighted.'
					: 'There were errors in ' + errors + ' fields of the form. They have been highlighted';
				$('#form_error_message').html(message).fadeIn('fast');
			} else {
				$('#form_error_message').hide();
			}
		},
		highlight: function(element, errorClass) {
			$(element).parent().addClass('error');
		},
		unhighlight: function(element, errorClass) {
			$(element).parent().removeClass('error');
		}
	});


});
