	var services_carousel = {
	
		// services container
		container: null,
		
		// current service index
		current_index: 0,
		// number of services
		services_max: 0,
		
		// array of services, they should have class="ivillage" and be saved inside templates/homepage_services/ as ivillage.html
		services: [
		    { id: 'managed-services', img: 'common/img/main/carousel/managed-services.png', url:'/services/history/' },
			{ id: 'professional-staffing', img: 'common/img/main/carousel/professinal-staffing.png', url:'/services/adultswim/' },
			{ id: 'network-infrastructure', img: 'common/img/main/carousel/network-infrastructure.png', url:'/services/ivillage/' },
			{ id: 'repair-services', img: 'common/img/main/carousel/repair-services.png', url:'/services/nutrisystem/' }
		],
		
		// fix zIndex position for IE6
		fixIE: function(service) {
			if(service.fixIE) { 
				this.container.css({zIndex:10050});
			} else {
				this.container.css({zIndex:999});
			}
		},
		
		// check if service is loaded, if not, load it
		checkService: function(service) {
			var div = $('div.'+service.id);
			var len = div.length;
			this.fixIE(service);
			if(len < 1) {
				// hide all services
				$('div.home-service').hide();
				this.container.addClass('loading');
				// load new service
				$.get(service.url, function(data){
					// preload image before show content
					var img = new Image();
  					$(img).load(function () {
      					$(this).hide();
						services_carousel.container.append(data).removeClass('loading');
						$('div.home-service').hide();
						$('div.home-service:last').show();
    				}).attr('src', service.img);
				});			
			} else {
				// hide other services and show active one
				$('div.home-service').hide();
				div.show();
			}
		},

		// previous function
		prev: function() {
			this.current_index--;
			if(this.current_index < 0) this.current_index = this.services_max-1;
			this.checkService( this.services[this.current_index] );
		},
			
		// next functions	
		next: function() {
			this.current_index++;
			this.current_index = this.current_index%this.services_max;
			this.checkService( this.services[this.current_index] );
		},

		init: function() {
		
			// add nex/prev click events
			$('#carousel-navigation a.next').click(function(e) {
				e.preventDefault();
				services_carousel.next();
			});
			$('#carousel-navigation a.previous').click(function(e) {
				e.preventDefault();
				services_carousel.prev();
			});
					
					
					$('div.home-service').hide();
					$('div.home-service:first').show();
			// get current loaded service
			var current_service = $('div.carousel-content:first div');
			var current_service_class = current_service.get(0).className;

			var temp_services = [];
			temp_services.push('');
			// recreate the services array according to the loaded service ()
			for(var i=0,len=this.services.length;i<len;i++) {
				var service = this.services[i];
				if(current_service_class.indexOf(service.id) > -1) temp_services[0] = service;
				else temp_services.push(service);
			}
			this.services = null;
			this.services = temp_services;
			this.services_max = this.services.length;
			this.container = $('#carousel-wrapper');
			this.fixIE(this.services[0]);
		}
	
	};
	
	$(document).ready(function(){ 	// begin home carousel
	services_carousel.init();
				setInterval("services_carousel.next()", 10000);
			});