var visiblePages = 4
var _pos_table = {};
var _c1 = 0.95;
var _c2 = 0.1;


function __createPosTable(c_p)
{
	var pages = c_p.find('.pages LI');
	var cntnr_width = c_p.innerWidth(true);  
	var page_width = c_p.find('.pages LI.top').outerWidth(true);
	var page_height = c_p.find('.pages LI.top').outerHeight(true);
	var ctrl = c_p.find('.page-control'); 
	var ctrl_width = ctrl.outerWidth(true); 
	var offset = 0;
	
	for(offset = 1; offset <= page_width; offset++) {
		var c = offset * visiblePages * (_c1 - _c2 * Math.abs(visiblePages));
		
		if(cntnr_width / 2 - (page_width + ctrl_width) / 2 + c + ctrl_width + page_width >= cntnr_width)
			break;
	}
	
	_pos_table = {};
	
	for(var i = -visiblePages - 10; i <= visiblePages + 10; i++) {
		var coef = i == 0 ? 0 : offset * i * (_c1 - _c2 * Math.abs(i));
		
		if(i < 0)
			coef -= ctrl_width;

		_pos_table[i] = {
				left: cntnr_width / 2 - (page_width + ctrl_width) / 2 + coef + ctrl_width + (i > 0 ? page_width - (page_width - Math.abs(i) * 15) : 0),
				top: (page_height - (page_height - Math.abs(i) * 15)) / 2,  
				width: (page_width - Math.abs(i) * 15),
				height: (page_height - Math.abs(i) * 15),
				fontSize: (100 - Math.abs(i) * 8) + '%',
				'z-index': 1000 * visiblePages - Math.abs(i)
			};
	}
}


function carouselSwitch(o, c_id)
{
	var ca = $('.carousel'); 
	var ph = $('.c-container .page-control'); 
	var c = $(o).parents('.carousel-minimized');
	var cm = $(o).parents('.cm-container');
	var c_w = c.width(); 
	var c_h = c.height(); 
	
	c.width(c_w);

	var src_ofs = c.offset();
	var dst_ofs = ph.offset();

	c.appendTo('BODY').css({top: src_ofs.top, left: src_ofs.left, position: 'absolute', 'z-index': 5000});
	
	c.animate({left: dst_ofs.left, top: dst_ofs.top}, 300, 'swing', function() { 
			c.removeClass('carousel-minimized').addClass('carousel').appendTo('.c-container');
			c.find('.pages LI.top').css({width: ''});
			c.css({left: 0, top: 0, width: '', height: '', position: 'relative'});
			
			buildCarousel(c.attr('id'));
			
			c.find('.page-control .tbl A').attr('onclick', '').unbind('click').click(function() {
					//
				});
		});
	

	ca.removeClass('carousel').addClass('carousel-minimized').appendTo('BODY');
	ca.css({position: 'absolute', width: c_w, height: c_h, left: dst_ofs.left, top: dst_ofs.top});
	ca.find('.pages LI.top').css({left: 0, top: 0, width: '100%', height: '100%', 'font-size': ''});
	
	ca.animate({left: src_ofs.left, top: src_ofs.top}, 300, 'swing', function() {
			ca.css({left: 0, top: 0, width: '', height: '', position: 'relative'});
			ca.appendTo(cm);
			
			ca.find('.page-control .tbl A').attr('onclick', '').unbind('click').click(function() {
					carouselSwitch(this, 'crsl1')		
				});
		});
}


function buildCarousel(c_id)
{
	var c_p = $('#' + c_id);
	var pages = c_p.find('.pages LI');
	var top_page = c_p.find('.pages LI').index(c_p.find('.pages LI.top')); //Math.floor(pages.length / 2); 
	var cntnr_width = c_p.innerWidth();  
	var ctrl = c_p.find('.page-control'); 
	var ctrl_width = ctrl.outerWidth(); 
	var page_width = c_p.find('.pages LI.top').outerWidth(true);
	var page_height = c_p.find('.pages LI.top').outerHeight(true);
	var c = 0;
	
	__createPosTable(c_p);
	
	top_page = top_page < 0 ? Math.floor(pages.length / 2) : top_page; 
	ctrl.css('left', cntnr_width / 2 - (page_width + ctrl_width) / 2 + 10);

	pages.each(function() {
			var p = $(this); 
			var num = c - top_page;

			p.attr('num', num);
			
			p.css(_pos_table[num]);
			
			p.find('IMG').each(function() {
				 		$(this).load(function() {
								if(!$(this).attr('orig_width')) {
									$(this).attr('orig_width', $(this).width()).css('width', $(this).width() - Math.abs(num) * 10);
								}
						 	});		
				});

			if(c < top_page - visiblePages || c > top_page + visiblePages) {
				p.hide();	
			}
			
			c++;
		});
	
	c_p.find('.carousel-sl-left').unbind('onclick').click(function() {
			if(parseInt(c_p.find('.pages LI:last').attr('num')) == 0)
				return;

			pages.each(function() {
					var p = $(this);
					var p_num = parseInt(p.attr('num'));
					var new_pos = _pos_table[p_num - 1];

					if(p_num - 1 >= -visiblePages && p_num - 1 <= visiblePages) {
						p.animate(new_pos, 400, 'linear', function() {
									p.css('z-index', new_pos['z-index']); 
								});
						
					}
					else {
						p.hide();

						p.css(new_pos);
					}
					
					p.attr('num', p_num - 1);

					p.find('DIV DIV DIV IMG').each(function() {
							 $(this).animate({width: $(this).attr('orig_width') - Math.abs(p_num - 1) * 10});
						});
					
				});
		});			
		
	c_p.find('.carousel-sl-right').unbind('onclick').click(function() {
			if(parseInt(c_p.find('.pages LI:first').attr('num')) == 0)
				return;
				
			//console.log(_pos_table);
			pages.each(function() {
					var p = $(this);
					var p_num = parseInt(p.attr('num'));
					var new_pos = _pos_table[p_num + 1];
					
					if(p_num + 1 >= -visiblePages && p_num + 1 <= visiblePages) {
						p.animate(new_pos, 400, 'linear', function() {
									p.css('z-index', new_pos['z-index']); 
								});
					}
					else {
						p.hide();

						p.css(new_pos);
					}

					p.attr('num', p_num + 1);

					p.find('DIV DIV DIV IMG').each(function() {
							//console.log($(this).attr('orig_width') + ' ' + p_num + ' ' + parseInt($(this).attr('orig_width') - Math.abs(p_num + 1) * 15));
							$(this).animate({width: parseInt($(this).attr('orig_width') - Math.abs(p_num + 1) * 10)});
							//$(this).animate({width: parseInt(50 - p_num)});
						});
							
					
				});
		});			

	$(window).unbind('resize').bind('resize', function() {
			buildCarousel(c_id);
		});
	

}


$(document).ready(function(){
		buildCarousel('crsl1');
	});
