var C = 5, images, part;
var prop = function(width,height,paddingTop,img,marginLeft,dep) {
	this.width=width;
	this.height=height;
	this.paddingTop=paddingTop;
	this.marginLeft=marginLeft;
}

var pos=[{img:{width:'124px',height:'166px'},a:{marginLeft:'0px',paddingTop:'37px'}},{img:{width:'149px',height:'199px'},a:{marginLeft:'124px',paddingTop:'20px'}},{img:{width:'180px',height:'240px'},a:{marginLeft:'273px',paddingTop:'0px'}},{img:{width:'149px',height:'199px'},a:{marginLeft:'453px',paddingTop:'20px'}},{img:{width:'124px',height:'166px'},a:{marginLeft:'602px',paddingTop:'37px'}}];

function diplomMove(d){
	if(images.diploms.length < C) return;

	if(d>0) {
		for(var i = 0; i < C; i++){
			if(i+1 == C) {
				var img = nextImg($('#diplom div.img a:first img').get(0).src);
				$('#diplom div.img a:eq('+i+')').fadeOut('normal', function(){$(this).remove()});
				html = '<a href="'+ img.big_img +'" class="thickbox dep'+ img.dep +'"><img src="'+ img.small_img +'" alt="'+ img.header+'"/></a>';
				$('#diplom div.img').prepend(html).find('a:first').hide().fadeIn('normal').css(pos[0].a).find('img').css(pos[0].img);
			} else {
				$('#diplom div.img a:eq('+i+')').animate(pos[i+d].a).find('img').animate(pos[i+d].img);
			}
		}
	} else {
		for(var i = C-1;i>=0;i--){
			if(i == 0) {
				var img = prevImg($('#diplom div.img a:last img').get(0).src);
				$('#diplom div.img a:eq('+i+')').fadeOut('normal', function(){$(this).remove()});
				html = '<a href="'+ img.big_img+'" class="thickbox dep'+ img.dep +'"><img src="'+ img.small_img +'" alt="'+ img.header+'"/></a>';
				$('#diplom div.img').append(html).find('a:last').hide().fadeIn('normal').css(pos[C-1].a).find('img').css(pos[C-1].img);
			} else {
				$('#diplom div.img a:eq('+i+')').animate(pos[i+d].a).find('img').animate(pos[i+d].img);
			}
		}
	}
}

function nextImg(src){
	for(var i=images.diploms.length-1;i>=0;i--){
		if( src.match(images.diploms[i].small_img)){
			if(i <= 0)
				return images.diploms[images.diploms.length-1];
			return images.diploms[i-1];
		}
	}
}

function prevImg(src){ 
	for(var i=0,l=images.diploms.length;i<l;i++){
		if( src.match(images.diploms[i].small_img)){
			if(i >= l-1)
				return images.diploms[0];
			return images.diploms[i+1];
		}
	}
}

function changePart(id){
	$.getJSON('/ajax/diploms.php?id='+id, function(img){
		var d, html;
		images = img;
		$('#diplom div.img').empty();
		for(var i=0;i<C;i++){
			d = img.diploms[i];
			if(!d) continue;
			html = '<a href="'+ d.big_img+'" class="thickbox dep'+ d.dep +'">'
			+ '<img src="'+ d.small_img +'" alt="'+ d.header+'"/>'
			+ '</a>';
			$('#diplom div.img').append(html).find('a:last').css(pos[i].a).find('img').css(pos[i].img);
		}
	})
}

$(function(){
	changePart(0);
	
	$('ul.menu a.parts').click(function(){
		var part = $(this).attr('class').match(/dep([0-9]+)/)[1];
		changePart(part);
		return false;
	});
	
	$("#diplom span:first").click(function(){
		diplomMove(-1);
	});

	$("#diplom span:last").click(function(){
		diplomMove(1);
	});
})
