var initial;

$(document).ready(function(){
	
	$('#footer .location .section span').click(function() {
		if($(this).hasClass('inactive')) {
			$('#footer .location .section span').addClass('inactive');
			$(this).removeClass('inactive');
			$('#footer .location .text span').hide();
			$('#footer .location .text .'+$(this).attr('class')).show();
		}
	});

	var colorbox_wrapper = $('#colorbox-wrapper').html();

	//$('.request-demo').colorbox({ajax: true, html: colorbox_wrapper, width: '40%' });
	$('.request-demo').colorbox({
		inline: true,
		width: '40%',
		onClosed: function() {
			// reset form view
			$('#request-demo .response').addClass('hidden');
			$('#request-demo .request').removeClass('hidden');
		}
	});

	var testimonial_item = $('.testimonial-scroller .item');

	if(testimonial_item.length > 0) {

		var items = Math.round(parseInt(testimonial_item.length)/2);
		testimonial_item.addClass('hidden');
		testimonial_item.eq(0).removeClass('hidden');
		testimonial_item.eq(1).removeClass('hidden');

		if(items > 1) {
			for(var i = items - 1; i >= 0; i--) {
				var active_link = (i == 0) ? ' class="active"' : '';
				$('.testimonials .paging').append('<a' + active_link + ' id="testimonial-' + i + '" href="javascript: void(0)"></a>')
			}

			//Diable cycling for now
			//setTimeout('update('+items+')', 4000);

			$('.testimonials .paging a').click(function(){

				$('.testimonials .paging a').removeClass('active')
				$(this).addClass('active')
				testimonial_item.addClass('hidden');
				var element = (parseInt($(this).attr('id').replace('testimonial-', ''))*2);
				if(testimonial_item.eq(element).length > 0) {
					testimonial_item.eq(element).removeClass('hidden');
				}
				if(testimonial_item.eq(element+1).length > 0) {
					testimonial_item.eq(element+1).removeClass('hidden');
				}

				clearTimeout(initial);

			});

		}

	} else {
		$('.testimonials').addClass('hidden');
	}

	$('#request-demo form').submit(function(){
		var data = {
			name: $('#name').val(),
			telephone: $('#telephone').val(),
			email: $('#email').val(),
			interested: $('#interested').val(),
			comments: $('#comments').val()
		};

		$.getJSON('request_demo.php', data, function(response) {
			$('#request-demo form input').removeClass('error');
			$.each(response, function(key, val) {
				if(key == 'success') {
					$('#request-demo .response').removeClass('hidden').html(val);
					$('#request-demo .request').addClass('hidden');
					parent.$.colorbox.resize({height: $('#request-demo').height()+90+'px'});
				} else if($('#'+key).length > 0) {
					$('#'+key).focus().addClass('error');
				}
			});
		});

		return false;
	});

});

function update(items, testimonial_item) {

	var testimonial_item = $('.testimonial-scroller .item');

	if($('.testimonials .paging .active').length > 0) {
		var element = parseInt($('.testimonials .paging .active').attr('id').replace('testimonial-', ''));
	} else {
		element = 0;
	}

	if(element == parseInt(items-1)) {
		element = 0;
	} else {
		element = parseInt(element + 1);
	}

	$('.testimonials .paging a').removeClass('active');
	$('.testimonials .paging a#testimonial-' + element).addClass('active');

	element = (element*2);
	testimonial_item.addClass('hidden');
	if(testimonial_item.eq(element).length > 0) {
		testimonial_item.eq(element).removeClass('hidden');
	}
	if(testimonial_item.eq(element+1).length > 0) {
		testimonial_item.eq(element+1).removeClass('hidden');
	}

	initial = window.setTimeout('update('+items+')', 4000);

}
