// -------------------- портфолио на главной -----------------

// количество картинок
var portfolio_pics_num = 0;

// текущий сдвиг
var portfolio_current_step = 1;

// время перехода
var portfolio_change_time = 8000;

// картинок на странице
var portfolio_pics_per_page = 2;

// направление автоматической прокрутки
var portfolio_destination = 'forward';

// первый запуск
var portfolio_first_time = true;

// количество работ при первоначальной прокрутке
var portfolio_first_time_steps = 3;

// время на первоначальную прокрутку
var portfolio_first_time_interval = 2000;

// ---------- автоматическая прокрутка ----------

function switchPortfolioWork() {

	if (!portfolio_first_time) {
		if (portfolio_current_step == portfolio_pics_num - portfolio_pics_per_page && portfolio_destination == 'forward') portfolio_destination = 'backward';
		if (portfolio_current_step == 0 && portfolio_destination == 'backward') portfolio_destination = 'forward';
		if (portfolio_destination == 'forward') $("#portfolio_preview img.forward_btn").click();
		if (portfolio_destination == 'backward') $("#portfolio_preview img.back_btn").click();
	}
	else portfolio_first_time = false;
	portfolio_timer = setTimeout("switchPortfolioWork()", portfolio_change_time);
}

$(document).ready(function() {
						   
	// длина сдвига
	var pic_width = 343;

	// время сдвига
	var animation_time = 800;

	// прозрачность
	var pic_transparency = 0.6;

	// непрозрачность
	var pic_opacity = 1;

	if ($.browser.name == 'safari') pic_opacity = 0.99;

	$("#portfolio_preview div.portfolio_preview_picture img").animate({opacity: "0.6"}, 0);
	$("#portfolio_preview div.portfolio_preview_picture img:last").animate({opacity: pic_opacity}, 0);
	portfolio_pics_num = $("#sliding_portfolio_container div.portfolio_preview_picture").size();
	$("#sliding_portfolio_container").width(pic_width * portfolio_pics_num);

	// первоначальная прокрутка

	//$("#sliding_portfolio_container").animate({marginLeft: -portfolio_first_time_steps*pic_width+"px"}, portfolio_first_time_interval);

	//portfolio_current_step = portfolio_first_time_steps;

	//$("#portfolio_preview img.back_btn").attr("src", "/images/portfolio_preview_back.gif");

	

	// ----- прозрачность -------
	$("#portfolio_preview div.portfolio_preview_picture").live("mouseover", function() {
		$(this).find("img").animate({opacity: pic_opacity}, 200);
		$(this).find("div.hover").show();
		$(this).find("div.site_desc").show();
	});

	$("#portfolio_preview div.portfolio_preview_picture div.hover").live("mouseout", function() {
		$(this).parent().find("img").animate({opacity: pic_transparency}, 200);
		$(this).parent().find("div.site_desc").hide();
		$(this).hide();
	});


	// ------ назад -------
	$("#portfolio_preview img.back_btn").hover( 
		function() { if (portfolio_current_step > 0) $(this).attr("src", "/images/portfolio_preview_back_active.gif"); }, 
		function() { if (portfolio_current_step > 0) $(this).attr("src", "/images/portfolio_preview_back.gif"); }
	);

	// ------ вперед -------

	$("#portfolio_preview img.forward_btn").hover( 
		function() { if (portfolio_current_step < portfolio_pics_num - portfolio_pics_per_page) $(this).attr("src", "/images/portfolio_preview_forward_active.gif"); }, 
		function() { if (portfolio_current_step < portfolio_pics_num - portfolio_pics_per_page) $(this).attr("src", "/images/portfolio_preview_forward.gif"); }
	);


	// ------ сдвиг вперед -------
	$("#portfolio_preview img.forward_btn").click( function() {
		if (portfolio_current_step < portfolio_pics_num - portfolio_pics_per_page) {
			portfolio_current_step++;
			$("#portfolio_preview img.back_btn").attr("src", "/images/portfolio_preview_back.gif");
			$("#sliding_portfolio_container").animate({marginLeft: -portfolio_current_step*pic_width+"px"}, animation_time);
			portfolio_destination = 'forward';
		}

        if (portfolio_current_step == portfolio_pics_num - portfolio_pics_per_page) {
        	$(this).attr("src", "/images/portfolio_preview_forward_inactive.gif");
            $("#portfolio_preview img.back_btn").attr("src", "/images/portfolio_preview_back.gif");
        }
	});

	// ------ сдвиг назад -------

	$("#portfolio_preview img.back_btn").click( function() {
		if (portfolio_current_step > 0) {
			portfolio_current_step--;
			$("#portfolio_preview img.forward_btn").attr("src", "/images/portfolio_preview_forward.gif");
			$("#sliding_portfolio_container").animate({marginLeft: -portfolio_current_step*pic_width+"px"}, animation_time);
			portfolio_destination = 'backward';
		}

        if (portfolio_current_step == 0) {
        	$(this).attr("src", "/images/portfolio_preview_back_inactive.gif");
            $("#portfolio_preview img.forward_btn").attr("src", "/images/portfolio_preview_forward.gif");
        }
	});

	

	

	

	

	// ---------- автоматическая прокрутка ----------

	

	/*switchPortfolioWork();

	

	$("#portfolio_preview").bind("mouseenter", function() {

		clearTimeout(portfolio_timer);

	}).bind("mouseleave", function() {

		portfolio_timer = setTimeout("switchPortfolioWork()", portfolio_change_time/2);

	});*/

	

});
