var panel_h = 0;

if (isIE6)
{
    $('#css-project').attr("href", "/css/project-ie6.css");
}

function leftPosition (target)
{
	var left = 0;
	if (target.offsetParent)
	{
		while(1)
		{
			left += target.offsetLeft;
			if (!target.offsetParent)
			{
				break;
			}
			target = target.offsetParent;
		}
	}
	else if (target.x)
	{
		left += target.x;
	}
	return left;
}

function tweetProject ()
{
	var project_div = document.getElementById("div-project-id");

	if (project_div)
	{
		var project_id = project_div.innerHTML;
		var tweet_div = document.getElementById("div-tweet-button-" + project_id);
		var name_span = document.getElementById("span-project-name-" + project_id);
		var project_name = name_span.innerHTML;
	
		$(tweet_div).append('<a href="http://twitter.com/share" class="twitter-share-button" data-url="www.adwe.ru/project/' + project_id + '" data-text="' + project_name + '" data-count="none">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>');
	}
}

function projectAdjustment ()
{
	var project_div = document.getElementById("div-project-id");
	if (project_div)
	{
		var page_elem = document.getElementById("div-page");

		if (isIE6)
			var page_l = leftPosition (page_elem);
		else
			var page_l = page_elem.offsetLeft;

		var project_id = project_div.innerHTML;
		var elem_id = "div-project-panel-" + project_id;
		var elem = document.getElementById(elem_id);
		if (elem)
		{
			var project_elem_id = "#" + 'div-project-' + project_id;
			var project_div = document.getElementById('div-project-' + project_id);
			var project_class = project_div.className;
			var project_l = project_div.offsetLeft;
			var project_h = project_div.offsetTop;

			if (project_l == page_l + 40)
			{
				$(project_elem_id).css('left',(0)+'px');
				$(project_elem_id).css('opacity','1');
			}

			if (project_h != 158)
				$(project_elem_id).css('top',(158 - project_h + 33)+'px');

			panel_h = elem.offsetHeight;
	
			var img_t = '-' + (panel_h + 82) + 'px';
			var img_id = "#" + 'img-main-' + project_id;
			$(img_id).css('top',img_t);

			var img_elem = document.getElementById('img-main-' + project_id);
			var img_h = img_elem.offsetHeight;
			var project_h = Math.max (img_h, panel_h + 420);
			$(project_elem_id).css('height',project_h + 30);
			$(project_elem_id).css('overflow','hidden');
			$("#div-project-panel-" + project_id).stop().animate({'opacity':'0.95'});
			var butts_id = "#" + 'div-social-buttons-' + project_id;
			$(butts_id).css('top',0);
			var t_elem = document.getElementById('div-social-buttons-' + project_id);
			var t_h = t_elem.offsetTop;
			if (project_class == 'div-project-right')
				t_h = t_h - 83;
			$(butts_id).css('top',-(t_h - project_h - 10));

			var page_h = (158 + project_h + 95) + 'px';
			$("#div-page").css('height',page_h);
			$("#div-page").css('overflow','hidden');
	
			var bottom_h = (158 + project_h + 88) + 'px';
			$("#div-bottom").css('top',bottom_h);
			$("#div-bottom").css('left',(page_l)+'px');
			$("#div-bottom").css('opacity','1');

			addthis.update('share', 'url', 'www.adwe.ru/project/' + project_id);
		}
	}
}

function showAddThis ()
{
	$("#div-addthis-button").css('left','200px');
	$("#div-addthis-button").css('top','-116px');
	$("#div-addthis-button").css('display','block');
}

function hidePanel (project_id)
{
	var p_h = panel_h + 'px';
	var panel_button_id = "#img-panel-button-" + project_id;
	var panel_manage_id = "#div-panel-manage-" + project_id;
	var project_photo_id = "#div-project-photo-" + project_id;
	var project_panel_id = "#div-project-panel-" + project_id;
	$(panel_button_id).attr('onclick','');
	$(panel_manage_id).css('visibility','hidden');
	$(project_photo_id).css('visibility','hidden');
	$(project_panel_id).css('height',p_h);
	$(project_panel_id).stop().animate({'width':'33px', 'opacity':'0.5'}, "slow", "linear");
	$(panel_button_id).stop().animate({'left':'-117px'}, "slow", "linear");
	$(panel_button_id).click(function() {showPanel(project_id)});
	$(panel_button_id).attr('src','/img/panel-show.png');
	if (isIE6)
	{
		var img_elem = document.getElementById("img-panel-button-" + project_id);
		img_elem.src = '/img/panel-show.png';
		if (img_elem)
			fixPNG (img_elem);
	}
}

function showPanel (project_id)
{
	var panel_button_id = "#img-panel-button-" + project_id;
	var panel_manage_id = "#div-panel-manage-" + project_id;
	var project_photo_id = "#div-project-photo-" + project_id;
	var project_panel_id = "#div-project-panel-" + project_id;
	$(panel_button_id).attr('onclick','');
	$(project_panel_id).stop().animate({'width':'940px', 'opacity':'0.95'}, "slow", "linear");
	$(panel_button_id).stop().animate({'left':'789px'}, "slow", "linear");
	$(panel_button_id).click(function() {hidePanel(project_id)});
	$(panel_button_id).attr('src','/img/panel-hide.png');
	setTimeout(function(){$(panel_manage_id).css('visibility','visible')}, 700);
	setTimeout(function(){$(project_photo_id).css('visibility','visible')}, 700);
	if (isIE6)
	{
		var img_elem = document.getElementById("img-panel-button-" + project_id);
		if (img_elem)
			fixPNG (img_elem);
	}
}

function showArrows (project_id)
{
	var left_id = "#img-project-left-" + project_id;
	var right_id = "#img-project-right-" + project_id;

	$(left_id).css('opacity','1.0');
	$(right_id).css('opacity','1.0');
}

function hideArrows (project_id)
{
	var left_id = "#img-project-left-" + project_id;
	var right_id = "#img-project-right-" + project_id;

	$(left_id).css('opacity','0.0');
	$(right_id).css('opacity','0.0');
}

function mainImgChange (project_id, new_src)
{
	var img_id = "#img-main-" + project_id;
	$(img_id).animate({'opacity': '0.0'}, 1000);
	setTimeout('imgChange("' + 'img-main-' + project_id + '", "' + new_src + '")', 1000);
	$(img_id).animate({'opacity': '1.0'}, 1000);
	setTimeout('projectAdjustment()', 1200);
}

function projectChange (next_id, move_side, sort_field, add_clause)
{
	$.get('/get-project.php', {'project': next_id, 'move_side': move_side, 'sort_field': sort_field, 'add_clause': add_clause}, function (html){
		$("#div-addthis-button").css('display','none');
		$("#div-addthis-button").css('left','0px');
		$("#div-addthis-button").css('top','0px');

		$(html).appendTo("#div-page");

		var project_div = document.getElementById("div-project-id");
		if (project_div)
		{
			var project_id = project_div.innerHTML;
			var curr_id = "#div-project-" + project_id;
			var new_id = "#div-project-" + next_id;

			project_div.innerHTML = next_id;
			projectAdjustment();
			tweetProject ();

			var page_elem = document.getElementById("div-page");
			if (isIE6)
				var page_l = leftPosition (page_elem);
			else
				var page_l = page_elem.offsetLeft;
			var div_elem = document.getElementById("div-project-" + project_id);
			if (move_side == 'right')
				$(new_id).css('left', (page_l + 940) + 'px');
			if (move_side == 'left')
				$(new_id).css('left', (-940) + 'px');
	
			$(curr_id).stop().animate({'opacity':'0.0'}, "normal", "linear");
			if (isIE6)
				$(new_id).stop().animate({'width':'940px', 'left':0}, "normal", "linear");
			else
				$(new_id).stop().animate({'width':'940px', 'left':0, 'opacity':'1.0'}, "normal", "linear");
			window.location.hash = '#url=/project/' + next_id;
			setTimeout ('projectClean("' + curr_id + '", "' + new_id + '", "' + move_side + '")', 500);

		}
	}, 'html');
}

function projectClean (curr_id, new_id, move_side)
{
	showAddThis ();
	$(curr_id).remove();
	$(new_id).css('top',(33)+'px');
	$(new_id).addClass('div-project');
	$(new_id).removeClass('div-project-left');
	$(new_id).removeClass('div-project-right');
}
