var curPhotoID, maxPhotoID, prevPhotoID, nextPhotoID;
var photoTimer;
var photoSpeed = 10000; // скорость смены блоков на витрине

var cur_mark = 0;
var old_msg = '';

$(document).ready(function(){
	maxPhotoID = $('.vitrina_content').size();
	if ($('.vitrina').size()) {
		setCurPhotoTimer(1);
		$('.vitrina .prev').click(function(){ setCurPhotoTimer(prevPhotoID); });
		$('.vitrina .next').click(function(){ setCurPhotoTimer(nextPhotoID); });
	}
	else if ($('.vitrina_b').size()) {
		setCurPhoto(1);
		$('.vitrina_b .prev').click(function(){ setCurPhoto(prevPhotoID); });
		$('.vitrina_b .next').click(function(){ setCurPhoto(nextPhotoID); });
	}
	else if ($('.vitrina_b2').size()) {
		setCurPhoto(1);
		$('.vitrina_b2 .prev').click(function(){ setCurPhoto(prevPhotoID); });
		$('.vitrina_b2 .next').click(function(){ setCurPhoto(nextPhotoID); });
	}

	$('.author').click(function() { 
		showAuthorInfo($(this).attr('id'));
		return false;
	});
	
	// обработка ссылок открывающих комментарии
	$('a.comment_replay').click(function () {
		var rel = $(this).attr('rel');
		var params = rel.split('.');
		$('div.block_comment_replay').css({'display':'none'});
		$('div#replay'+params[1]).css({'display':'block'});
		return false;
	});

	// сабмит формы поиска фото
	$('a.search_submit').click(function () {
		$('form#search_form').submit();
		return false;
	});

	// сабмит формы логина
	$('a.login_button').click(function () {
		$(this).parents('form').submit();
		return false;
	});

	// сабмит формы видео
	$('a.video_submit').click(function () {
		var form = $(this).parents('form');
		if (($(form).find('input[name="youtube"]').val().slice(0, 7) == 'пример:') != '') {
			$(form).find('input[name="youtube"]').val('');
		}
		if (($(form).find('textarea[name="tags"]').val().slice(0, 4) == 'Теги') != '') {
			$(form).find('textarea[name="tags"]').val('');
		}
		$(form).submit();
		return false;
	});

	// сабмит формы коммента
	$('a.comment_submit').click(function () {
		if ($(this).parents('form').find('textarea[name="text"]').val() != '') {
			$(this).parents('form').submit();
		}
		return false;
	});

	// голосование за комменты
	$('img.mark_selectable').hover (
		function () {
			if ($(this).attr('rel').length > 0) {
				var marks = $(this).attr('rel').split('|');	
				cur_mark = marks[1];
				$('img.mark_selectable').each (function (id, item) {
					var marks_item = $(item).attr('rel').split('|');	
					if (marks_item <= marks[0]) {
						$(this).attr({'src': '/img/star_a.png'});
					} else {
						$(this).attr({'src': '/img/star.png'});
					}
				});
				$(this).attr({'src': '/img/star_a.png'});
			}
		}, 
		function () {
			if ($(this).attr('rel').length > 0) {
				$('img.mark_selectable').each (function (id, item) {
					var marks = $(item).attr('rel').split('|');	
					if (marks[0] <= cur_mark) {
						$(this).attr({'src': '/img/star_a.png'});
					} else {
						$(this).attr({'src': '/img/star.png'});
					}
				});
			}
		}
	);
	$('img.mark_selectable').click (function () {
		if ($(this).attr('rel').length > 0) {
			var marks = $(this).attr('rel').split('|');	
			if (typeof (marks[2]) != "undefined") {
				window.location = marks[2] + '&mark=' + marks[0];
			} else {
				var winloc = new String (window.location);
				if (winloc.indexOf("mark=") < 0) {
					window.location = winloc + '&mark=' + marks[0];
				} 
			}
		}
	});
	$('img.mark_unselectable').hover (
		function () {
			old_msg = $('td#msg_cell').html();
			$('td#msg_cell').html('<span style="font-size:12px;">Надо авторизоваться!</span>');
		}, 
		function () {
			$('td#msg_cell').html(old_msg);
		}
	);
	
});

function setCurPhoto (id) {
	$('.author_info').hide();
	$('.author').attr('class', 'orange author');
	$('.author span').remove();

	$('.vitrina_content').hide();
	$('.vitrina_content#photo' + id).show();
	
	if (id == maxPhotoID) {
		nextPhotoID = 1;
	}
	else {
		nextPhotoID = id+1;
	}
	
	if (id == 1) {
		prevPhotoID = maxPhotoID;
	}
	else {
		prevPhotoID = id-1;
	}
	
	curPhotoID = id;
}

function setCurPhotoTimer (id) {
	if(photoTimer) clearTimeout(photoTimer);

	$('.vitrina_title').hide();
	$('.vitrina_content').hide();
	$('.vitrina_title#title' + id).show();
	$('.vitrina_content#photo' + id).show();
	
	if (id == maxPhotoID) {
		nextPhotoID = 1;
	}
	else {
		nextPhotoID = id+1;
	}
	
	if (id == 1) {
		prevPhotoID = maxPhotoID;
	}
	else {
		prevPhotoID = id-1;
	}
	
	curPhotoID = id;

	photoTimer = setTimeout ("setCurPhotoTimer(nextPhotoID)", photoSpeed);
}

function showAuthorInfo(id) {
	if ($('#' + id).attr('class') == 'orange author') {
		$('#' + id).attr('class', 'bg_orange author');
		$('#' + id).append('<span class="s14">&nbsp;&rarr;</span>');
		$('#' + id + '_info').css({'top': $('#' + id).position().top + 'px', 'left': $('#' + id).position().left + $('#' + id).width() + 10 + 'px'});
		$('#' + id + '_info').show();
	}
	else {
		$('#' + id).attr('class', 'orange author');
		$('#' + id + ' span').remove();
		$('#' + id + '_info').hide();
	}
}

function add_to_fav (obj, type) {
	if (type!='image' && type!='video') { return false; }
	var id = $(obj).attr('rel');
	if (id <= 0) { return false; }
	$.get("favorite_js.php", {"type":type,"id":id}, function (result) {
		if (result.result == 1) {
			var divs = $(obj).find('div.add_img_to_fav');
			divs.eq(0).html ("-&nbsp;");
			divs.eq(1).html ("<u>удалить из избранного</u>");
			$('.favorites_count').text(result.count);
		} else if (result.result == -1) {
			var divs = $(obj).find('div.add_img_to_fav');
			divs.eq(0).html ("+&nbsp;");
			divs.eq(1).html ("<u>добавить в избранное</u>");
			$('.favorites_count').text(result.count);
		} else {
			return false;
		}
	},"json");
}
