$(document).ready(function(){
	
	// точка списка
	//$('aside ul li').hover(
	//	function(){
	//		$(this).addClass('hovered');
	//	}, function(){
	//		$(this).removeClass('hovered');
	//	}
	//);
	
	// :)
	if($.browser.msie && $.browser.version<7) document.location.href = 'http://opera.com/';
	
	// кнопки голосования
	$('.facebook > div > a')
		.css('opacity', 0.7)
		.live({
			mouseenter:
				function() {
					$(this).animate({opacity: 1}, 200)
				},
			mouseleave:
				function(){
					$(this).animate({opacity: 0.7}, 200)
				}
			})
		
	// загрузка списка работ для титульной
	/*var linkLoad = location.href.split('#');*/
	jQuery.getScroll(3, 319, '#gallery', false);
	if($('#gallery').length>0 && $('#project').length<1) {
                getData((/*linkLoad[1]||*/$('#gallery > div').length<1)?0:3, (jQuery.onPage)*jQuery.imageRow);
	}
	
	// сортировка по типам работ
	$(document).on('click', '#gallery div figcaption > a, #sort ol li > a', function() {
		jQuery.category = parseInt($(this).attr('rel'));
		getWorks(false);
	})
	
	// функции плавного скроллинга статей
	$('#artnav > a').click(function() {
		var id = $(this).attr('rel');
		$('#artnav > a').animate({'opacity': 'hide'});
		$('#ajax').animate({'opacity':'show'});
		if($('#floater .num'+id).length>0) {
			$('#floater > .show').removeClass('show');
			$('#floater .num'+id).addClass('show');
			if($(this).hasClass('left'))
				showArt('right');
			else
				showArt('left');
			changeTop(id);
		} else {
			$(this).addClass('active');
			$.get(
				'/components/content/ajax/article.php?id='+id+rand(),
				function(data){
					var active = $('#artnav > .active');
					if(data) {
						var floater = $('#floater');
						if(active.hasClass('left')) {
							floater
								.css({left: '-=740'})
								.children('.show')
									.before(data)
									.removeClass('show');
							showArt('right');
						} else {
							floater
								.children('.show')
									.after(data)
									.removeClass('show');
							showArt('left');
						}
					}
					changeTop(active.attr('rel'));
					
					var title = $('body > title');
					var sitename = title.text().split(' - ');
					title.text($('#floater .show h1:first').text()+' - '+sitename);
				},
				'html'
			)
		}
		return false;
	});
	showArt = function(direction){
		var div = $('#floater');
		if(direction=='left')
			div.animate({left: '-=740'}, function(){changeHeight()});
		else
			div.animate({left: '+=740'}, function(){changeHeight()});
		
	}
	changeHeight = function(){
		$('#page').animate({height: $('#floater .show').height()+'px'})
	}
	changeTop = function(id){
		$.getJSON(
			'/components/content/ajax/links.php?id='+id+rand(),
			function(data) {
				if(data) {
					var active = $('#artnav > .active');
					if(active.hasClass('left')) var a = 'left'; else var a = 'right';
					if(!$.browser.msie) window.history.pushState({}, data.current.title, '/'+data.current.seolink+'.html');
					active.removeClass('active');
					if(data.prev)
						$('#artnav .left')
							.text(data.prev.title)
							.attr('href', '/'+data.prev.seolink+'.html')
							.attr('rel', data.prev.id)
							.animate({'opacity': 'show'})
					if(data.next)
						$('#artnav .right')
							.text(data.next.title)
							.attr('href', '/'+data.next.seolink+'.html')
							.attr('rel', data.next.id)
							.animate({'opacity': 'show'})
					$('.facebook > div > a')
						.css('opacity', 0.7)
				}
				$('#ajax').animate({'opacity':'hide'});
			}
		)
	}
	
	// голосование для страниц работ и статей
	//if($('#page').length>0 || $('#project').length>0) {
		$('.up, .down').live('click', function() {
			if($('> .voted', $(this).parent()).length<1) { // клики до получения ответа ajax
				var id = $(this).attr('rel');
				if($('#page').length>0)
					var type = 'page';
				else var type = 'work';
				$(this).addClass('voted');
				$.get(
					'/modules/mod_rating/ajax/rate.php?type='+type+'&id='+id+'&do='+$(this).attr('class').replace('voted', '').replace(' ', '')+rand(),
					function(data) {
						if(data) {
							var current = $('.facebook .voted');
							var a = current.attr('class').replace('voted', '').replace(' ', '');
							var span = $('.value_'+a+' > span', current.parent());
							current.removeClass('voted');
							switch(parseInt(data)) {
								case 0: // уже голосовали, уже установлен рейтинг
									break;
								case 2: // уже голосовали за другую карму, плюсуем здесь, отнимаем там
									var up = parseInt($('.value_up > span', current.parent()).text());
									var down = parseInt($('.value_down > span', current.parent()).text());
									if(a!='up') $('.value_up > span', current.parent()).text(
												(up<1?0:(up-1))
											);
									else $('.value_down > span', current.parent()).text(
												(down<1?0:(down-1))
											);
								case 1: // ещё не голосовали, плюсуем
									span.text((parseInt(span.text())+1));
									break;
							}
						}
					}
				)
			}
		})
	//}
	
	// едущий логотип
	//$('#topper').css('opacity', 0);
	//$(window).scroll(function() {
	//	if($(document).scrollTop() >= 260 ) {
	//		$('#topper').show().animate({opacity: 'show'})
	//	} else {
	//		$('#topper').animate({opacity: 'hide'}, function(){$(this).hide()})
	//	}
	//});
	
});

// works list
function getWorks(cats) {
	jQuery.without = null;
	jQuery.onUpdate = true;
	if(typeof(jQuery.slider) == 'object') jQuery.slider.stop();
	$('#slider, #description, #artnav, #page, #project, #index_face').slideUp();
	jQuery.fullInfo = true;
	if(cats) jQuery.category = null;
	if($('#gallery').length>0) {
		$('#gallery').animate({opacity: 'hide'}, function(){
			loadWorks();
			$(this)
				.empty()
				.animate({opacity: 'show'});
		})
	} else {
		$('#content').append($('<div />').attr('id', 'gallery').css('opacity', 0));
		loadWorks();
		$('#gallery')
			.animate({opacity: 1}, 700);
	}
}
function loadWorks() {
	getData(0, (jQuery.onPage+2)*jQuery.imageRow);
	$('html, body')
			.stop()
			.animate({scrollTop: 0}, 400);
}

// next slide
function switcher() {
	jQuery.slider.next();
	return false;
}

// anti-cache
function rand() {
        min = 10000;
        max = 100000;
	if( max )
	    var ran =  Math.floor(Math.random() * (max - min + 1)) + min;
	else
	    var ran = Math.floor(Math.random() * (min + 1));
        return '&rand='+ran;
}
