







$(document).ready(function(){
	
	
	$('a[rel="targetblank"]').attr('target', '_blank');
	
	
	$('.slide').width($('.slide .oneSlide').size() * $('.slide .oneSlide').width());
	
	//var active = $('#tabs li').index($('#tabs li.active'));
	//$('.slide').css('left', -1*$('.slide .tbl:eq('+active+')').get(0).offsetLeft + 'px');
	
	$('#tabs li').click(function(){
		
		if ($('.slide').size() <= 0) {	// Если мы не на главной странице, то слоёв нет
			return true;
		}
		
		if (!$('.slide').is(':animated')) {
			$('#tabs li').removeClass('active');
			$(this).addClass('active');
			
			var index = $('#tabs li').index($(this));
			var newHeight = $('.slide .oneSlide:eq('+index+')').height();
			var minHeight = parseInt($('.slide-wrap').css('min-height'));
			$('.slide-wrap').animate({height:Math.max(newHeight, minHeight)}, 600, 'circEaseInOut');
			//alert($('.slide .tbl:eq('+index+')').height());
			var tleft = $('.slide .oneSlide:eq('+index+')').get(0).offsetLeft;
			
			$('.slide').animate({
				'left': -1*tleft + 'px'
			}, 800, 'backEaseInOut');
		}
		
		return true;
		
	});
	
	
	if ($('.slide').size() > 0) {
		var openTab = getAncherParametr('openTab');
		if (openTab == '') {
			$('#tabs li:eq(0)').click();
		} else {
			// alert(openTab);
			$('#tabs li:eq('+ openTab +')').click();
		}
	}
	
	
	
	
	galleryStartPosition = getAncherParametr('gallery') || 1;
	
	$('div.mainText table.gallery ul').jcarousel({
		buttonNextHTML	: null,
		buttonPrevHTML	: null,
		scroll			: 1,
		auto			: 5,
		start			: galleryStartPosition,
		easing			: 'circEaseInOut',
		
		itemVisibleInCallback : {
			onBeforeAnimation : myCarousel.startMove,
			onAfterAnimation : myCarousel.endMove
		},
		
		initCallback	: myCarousel.initCallback
	});
	
	
	$(".mainText a[href^='upload/tiny'] img").each(function() {
		$(this).parent().attr('rel', 'lightbox-galery');
	})

// FancyBox
	$('.mainText').find('a[href$=".jpg"], a[href$=".JPG"], a[href$=".jpeg"], a[href$=".JPEG"], a[href$=".png"], a[href$=".PNG"], a[href$=".gif"], a[href$=".GIF"]').each(function() {
		$(this).addClass('slideshow').attr('rel', 'fancybox');
	});
	
	
	$(".mainText a[href*='youtube.com/watch']").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 480,
				'height'		: 385,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'				: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		return false;
	});
	
	$('a.slideshow').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	500, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'autoScale'		:	false,
		'titlePosition'	:	'inside',
		'titleFormat'	:	fancyFormatTitle,
		'showNavArrows'	:	true,
		'padding'		:	1,
		'overlayOpacity':	.3,
		'overlayColor'	:	'#000000',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack'/*,
		'onComplete'	:	function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").show();
			}, function() {
				$("#fancybox-title").hide();
			});
		}*/

	});
	
});



function fancyFormatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id="tip7-title">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + 'Изображение ' + (currentIndex + 1) + ' из ' + currentArray.length + '</div>';
}
/**
 * Класс для обработки событий в галерее
 * 
 */
var myCarousel = {
	
	duration : 1000,
	
	tbl : null,
	
	windowLocation : window.location.href.replace(/\#.+$/, ''),
	
	needAnchorAdd : false,
	
	
	initCallback : function(carousel) {
		
		carousel.options.animation = myCarousel.duration;
		
		myCarousel.tbl = carousel.container.closest('table');
		
		
		if (carousel.options.start == 1) {
			$('td.arrow.left', myCarousel.tbl).addClass('unactive');	// Сходу ставим левую кнопку не активной
		}
		
		
		$('td.main', myCarousel.tbl).click(myCarousel.mainClick);	// При клике на яйчейку таблицы будем смотреть: является ли она ссылкой?
		
		$('td.arrow.left', myCarousel.tbl).bind('click', function() {
			carousel.prev();
			carousel.options.auto = 0;
		});
		
		$('td.arrow.right', myCarousel.tbl).bind('click', function() {
			carousel.next();
			carousel.options.auto = 0;
		})
		
	},
	
	startMove : function(carousel, li, num, status) {
		
		if (num == 2) {
			$('td.arrow.left', myCarousel.tbl).removeClass('unactive');
		}
		
		var size = carousel.size();
		
		if (num == size - 1) {
			$('td.arrow.right', myCarousel.tbl).removeClass('unactive');
		}
		
		
		
		var halfDuration = myCarousel.duration / 2;
		var alt		= $('img', li).attr('alt');
		var link	= $('a', li);
		var div		= $('td.main div.nameHint a.alt-name', myCarousel.tbl);
		
		if (status == 'init') { // Если эта ф-ция была вызвана в момент загрузки страницы, то сразу же отображаем надпись
			div.find('span').text(alt);
		} else {
			div.fadeOut(halfDuration, function() {
				$(this).find('span').text(alt).parent().fadeIn(halfDuration);
			});
		}
		
		
		var tdMain = $('td.main', myCarousel.tbl);
		if (link.size() > 0) {	// Если внутри текущего слайда есть ссылка.
			div.attr('href', link.attr('href'));
			tdMain.addClass('islink');
		} else {
			div.removeAttr('href');
			tdMain.removeClass('islink');
		}
		
		
		
		
	},
	
	endMove : function(carousel, li, num, status) {
		
		
		if (num == 1) {	// Если в самом начале
			$('td.arrow.left', myCarousel.tbl).addClass('unactive');
		}
		
		var size = carousel.size();
		
		if (num == size) {	// Если в самом начале
			$('td.arrow.right', myCarousel.tbl).addClass('unactive');
		}
		
		
		if (myCarousel.needAnchorAdd) {
			window.location.href = myCarousel.windowLocation + '#gallery=' + num;
		}
		
		
	}
	
	
	
};


function getMile(prefix, postfix)
{
	
	var mail = prefix + '@' + postfix
	document.write('<a href="mailto:'+ mail +'">'+ mail +'</a>');
	
}

function getUrlParametr( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function getAncherParametr(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[#]"+name+"=([^&]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}









/**
 * Аналог для print_r()
 * 
 * @param	mixed	variable	Переменная любого типа
 */
function pr(variable) {
	if ($.dump) {
		if (typeof(myAlert) != 'undefined') {
			myAlert('<pre>' + $.dump(variable) + '</pre>');
		} else {
			alert($.dump(variable));
		}
	} else {
		myAlert('Не подключено расширение jQuery.dump');
	}
}

function pr2(variable, x, y) {
	
	
	
	if ( ! $.dump) {
		alert('Не подключено расширение jQuery.dump');
	}
	
	
	if ($('#dump').size() <= 0) {
		x = x || 200;
		y = y || 200;
		$('body').append('<pre style="min-width: 150px; position:absolute; top:'+ y +'px; left:'+ x +'px; border:1px solid #000000; background-color: #FFFFFF; padding:5px;" id="dump"></pre>');
	}
	
	
	
	var t = $.dump(variable);
	t = t.replace(/	/g, '    ');
	
	
	$('#dump').text(t);
}
