jQuery(document).ready(function(){
	if(jQuery("#products_slides").length>0) {
		products_init();
	}
});

function products_init() {
	width_slides=jQuery('.item_slideproduct').length*jQuery('#products_slides').width();
	jQuery('#slides').css('width',width_slides);
	manage_pager(0);
	jQuery('.page').click(function() {
		active = jQuery(this).attr('id').substr("page_".length);
		manage_pager(active); 
		the_margin=product_calculate_margin(active);
		products_goto(the_margin);
	});
	jQuery('#slide_right').click(function() {
		if(jQuery(this).hasClass('active')) {
			active=products_get_page_active(); 
			active++;
			manage_pager(active);
			the_margin=product_calculate_margin(active);
			products_slideto(the_margin);
		}
	});
	jQuery('#slide_left').click(function() {
		if(jQuery(this).hasClass('active')) {
			active=products_get_page_active(); 
			active=active-1;
			manage_pager(active);
			the_margin=product_calculate_margin(active);
			products_slideto(the_margin);
		}
	});
	products_set_timer();
	jQuery("#products_slides").hover(function() {
		products_clear_timer();	
	}, function() {
		products_set_timer();	
	});
}
function product_calculate_margin(page) {
	the_margin=0-(page*jQuery('#products_slides').width());
	return the_margin;
}
function products_set_timer() {
	timer = setInterval("products_auto_change()",10000);
}
function products_clear_timer() {
	clearInterval(timer);
}
function products_auto_change() {
	active = products_get_page_active();
	active++;
	if(active==jQuery(".item_slideproduct").length) active=0;
	manage_pager(active);
	the_margin=product_calculate_margin(active);
	products_slideto(the_margin);
}
function products_slideto(the_margin) {
	if(the_margin<0)
		jQuery("#slides").animate({"marginLeft":the_margin}, 800)	
	else
		products_goto(the_margin);
}
function products_goto(the_margin) {
	jQuery('#slides').animate({"marginLeft" : the_margin}, 0, 
		function() {jQuery('#slides').animate({"opacity" : 0}, 0, 
			function() {jQuery('#slides').animate({"opacity" : 1}, 800)  
			})	  
		});
}
function products_get_page_active() {
	the_page=jQuery("#pager .active").attr('id').substr("page_".length);
	return the_page;	
}
function manage_pager(the_page) {
	jQuery(".page").removeClass('active');
	jQuery("#page_"+the_page).addClass('active');
	products_manage_ends();
}
function products_manage_ends() {
	active = products_get_page_active();
	if(active==0) {
		jQuery('#slide_left').addClass('inactive').removeClass('active'); 
	}
	else {
		jQuery('#slide_left').addClass('active').removeClass('inactive'); 
	}
	if(active==(jQuery('.item_slideproduct').length-1)) {
		jQuery('#slide_right').addClass('inactive').removeClass('active'); 
	}
	else {
		jQuery('#slide_right').addClass('active').removeClass('inactive'); 
	}
	if(active!=0 && active!=(jQuery('.item_slideproduct').length-1)) {
		jQuery('#slide_left').addClass('active').removeClass('inactive'); 
		jQuery('#slide_right').addClass('active').removeClass('inactive'); 
	}
}


