
var Products =
{

	run: function()
	{
		Products.pictures("#product_pictures");
		$$('#group_pictures .group').each(function(item) {
			Products.pictures("#group_pictures #"+ item.id);
		});
		Products.mtm_products();
		Products.slide_width = 600;
		Products.slide_speed = 15;
	},

	pictures: function(parent_element)
	{
		var list = $$(parent_element +' div.list').first();
		var table = $$(parent_element +' div.list table').first();
		var paging = $$(parent_element +' div.list div.list_paging').first();
		
		if (list) {
			list._page = 1;
			Products.pictures_set_link(parent_element, list, table, paging);
		}
	},
	pictures_set_link: function(parent_element, list, table, paging)
	{
		$$(parent_element +' div.list ul.paging a').each(function(item) {
			Event.observe(item, 'click', function(event) {
				Event.stop(event);
				var page = item.getAttribute("rel");
				if (parent_element == '#product_pictures') {
					var url = '/?ajax=getProductPicturesPaging&product=' + selected_product + '&page=' + page;
				} else {
					var selected_group = $$(parent_element).first().getAttribute('rel');
					var url = '/?ajax=getReferencePicturesPaging&group=' + selected_group + '&page=' + page;
				}
				new Ajax.Request(url, {
					method: 'get',
					onSuccess: function(transport) {
						var page_from = list._page;
						paging.innerHTML = transport.responseText;
						list._page = page;
						Products.pictures_set_link(parent_element, list, table, paging);
						Products.pictures_slide(table, page_from, page);
					}
				});
			});
		});
	},
	pictures_slide: function(table, page_from, page_to)
	{
		Products.slide_start = (page_from - 1) * 600;
		Products.slide_diff = page_to - page_from;
		clearTimeout(Products.slide_timeout);
		Products.pictures_do_slide(table, 0);
	},
	pictures_do_slide: function(table, index)
	{
		var pos = Products.slide_diff * (Math.round(Math.cos(index + Math.PI) * Products.slide_width) + Products.slide_width) / 2 + Products.slide_start;
		table.style.marginLeft = '-' + pos + 'px';
		if (index < Math.PI) Products.slide_timeout = setTimeout(function () { Products.pictures_do_slide(table, index + Math.PI * Products.slide_speed / Products.slide_width) }, 40);
	},

	mtm_products: function()
	{
		$$('#product_mtm div.list').each(function(list) {
			var group_id = list.down('h5').getAttribute('rel');
			var table = list.down('table');
			var paging = list.down('div.list_paging');

			if (group_id && table && paging) {
				Products.mtm_product_set_link(list, group_id, table, paging);
			}
		});
	},
	mtm_product_set_link: function(list, group_id, table, paging)
	{
		$$('#product_mtm div#'+ list.id +'.list ul.paging a').each(function(link) {
			Event.observe(link, 'click', function(event) {
				Event.stop(event);
				var page = link.getAttribute("rel");
				
				table.style.marginLeft = '-'+ ((page-1) * 600) +'px';

				new Ajax.Request('/?ajax=getProductMTMPaging&group='+ group_id +'&product='+ selected_product +'&page='+ page, {
					method: 'get',
					onSuccess: function(transport) {
						var page_from = list._page;
						paging.innerHTML = transport.responseText;
						list._page = page;
						Products.mtm_product_set_link(list, group_id, table, paging);
						Products.mtm_product_slide(table, page_from, page);
					}
				});
			});
		});
	},
	mtm_product_slide: function(table, page_from, page_to)
	{
		Products.mtm_product_slide_start = (page_from - 1) * 600;
		Products.mtm_product_slide_diff = page_to - page_from;
		clearTimeout(Products.mtm_product_slide_timeout);
		Products.mtm_product_do_slide(table, 0);
	},
	mtm_product_do_slide: function(table, index)
	{
		var pos = Products.mtm_product_slide_diff * (Math.round(Math.cos(index + Math.PI) * Products.slide_width) + Products.slide_width) / 2 + Products.mtm_product_slide_start;
		table.style.marginLeft = '-' + pos + 'px';
		if (index < Math.PI) Products.mtm_product_slide_timeout = setTimeout(function () { Products.mtm_product_do_slide(table, index + Math.PI * Products.slide_speed / Products.slide_width) }, 40);
	}

}

new StartUp(Products);

