var SEARCH_DEFAULT = 'Поиск по сайту';



$(document).ready(function() {
	$("a.gallery").fancybox(
	{						
		"padding" : 1,
		"imageScale" : false,
		"zoomOpacity" : true,
		"zoomSpeedIn" : 800,
		"zoomSpeedOut" : 800,
		"zoomSpeedChange" : 1000,
		"frameWidth" : 700,
		"frameHeight" : 600,
		"overlayShow" : true,
		"overlayOpacity" : 0.1,
		"hideOnContentClick" :true,
		"centerOnScroll" : true
	});

	$('#photos-hdr .slide').css({float: 'none', position: 'absolute'}).append('<span class="control play">&nbsp;</span>');
		
	$('#photos-hdr .slide').not(':eq(1)').find('.control').hide();
	$('#photos-hdr .slide:eq(1)')
		.prev().css({marginTop: '219px', borderWidth: '3px', left: 0, top: 0}).end()
		.css({borderWidth: '3px', left: '77px', top: '46px'})
		.nextAll('.slide').each(function(i) {
			$(this).css({top: 0 - 71*i, borderWidth: '3px', left: 332 + 95*i});
		});
	$('#photos-hdr .slide:eq(1)').append('<span class="caption">' + $('#photos-hdr .slide:eq(1)').find('img').attr('alt') + '</span>').children().show();
	
	$('#photos-hdr .slide').click(function() {
		if($(this).next().css('left') == undefined){
			next = $($('#header .slide')[0]);
		}else{
			next = $(this).next();
		}
		
		if(next.next().css('left') == undefined){
			nextnext = $($('#header .slide')[0]);
		}else{
			nextnext = next.next();
		}
		
		if($(this).prev().css('left') == undefined){
			elems = $('#header .slide')
			prev = $(elems[elems.length-1]);
		}else{
			prev = $(this).prev();
		}	
		
		if(prev.prev().css('left') == undefined){
			elems = $('#header .slide')
			prevprev = $(elems[elems.length-1]);
		}else{
			prevprev = prev.prev();
		}
		
		if	($(this).css('left') == '332px') {
		
			next.css('left', '427px');
			next.css('top', '-71px');
			next.css('margin-top', '0px');
		
			//$(this).find('img').attr('src', function() { return this.src.replace('preview/preview-', 'slide-'); });
			prev.find('img').attr('src', function() { return this.src.replace('slide-', 'preview/preview-'); });
			prev.animate({marginTop: '173px', borderWidth: '3px', left: '-=77px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '89px', height: '65px'}, {duration: 500, easing: 'jswing'});
			prevprev.animate({left: '-=95px', opacity: '0', marginTop: '+=71px' }, {duration: 500, easing: 'jswing'});
			$(this).animate({top: '46px', borderWidth: '3px', left: '-=255px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '267px', height: '190px'}, {duration: 500, easing: 'jswing'});
			next.animate({left: '332px', opacity: '1', top: '0px'}, {duration: 500, easing: 'jswing'}).show();
			//nextnext.animate({left: '-=95px', top:'-71px'}, {duration: 500, easing: 'jswing'}).hide();
			$(this).append('<span class="caption" style="display: none">' + $(this).find('img').attr('alt') + '</span>')
			prev.find('.caption').remove();
			prev.find('.control').hide();
			$(this).find('.caption').fadeIn();
			$(this).find('.control').fadeIn();

		};
		
		if	($(this).css('left') == '0px') {
			prev.css('left', '-95px');
			prev.css('top', '0px');
			prev.css('margin-top', '290px');			
		
			//$(this).find('img').attr('src', function() { return this.src.replace('preview/preview-', 'slide-'); });
			next.find('img').attr('src', function() { return this.src.replace('slide-', 'preview/preview-'); });
			next.animate({marginTop: '0px', top: '0px', borderWidth: '3px', left: '+=255px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '89px', height: '65px'}, {duration: 500, easing: 'jswing'});
			nextnext.animate({left: '+=95px', opacity: '0', top: '-71px' }, {duration: 500, easing: 'jswing'});
			$(this).animate({top: '46px', borderWidth: '3px', left: '+=77px', marginTop: '0px'}, {duration: 500, easing: 'jswing'}).find('img').animate({width: '267px', height: '190px'}, {duration: 500, easing: 'jswing'});
			prev.animate({left: '+=95px', opacity: '1', marginTop: '-=71px'}, {duration: 500, easing: 'jswing'}).end();
			//prevprev.animate({left: '+=95px', top:'-=71px'}, {duration: 500, easing: 'jswing'});
			$(this).append('<span class="caption">' + $(this).find('img').attr('alt') + '</span>')
			next.find('.caption').remove();
			next.find('.control').hide();			
			
			$(this).find('.caption').css({display: 'none'}).fadeIn();
			$(this).find('.control').fadeIn();

		};
		return
	});
	$('#photos-hdr .control').click(function() {
		if ($(this).hasClass('play'))	{
			interval = setInterval(rightToLeft, 2000);
			$('#photos-hdr .control').removeClass('play').addClass('pause');
			$.cookie('play', '1', {path: '/'});
		}
		else if ($(this).hasClass('pause'))	{
			clearInterval(interval);
			$('#photos-hdr .control').removeClass('pause').addClass('play');
			$.cookie('play', '0', {path: '/'});
		}
	});
	$('#photos-hdr img').not(':eq(1)').each(function(i) {
		$(this).css({width: '89px', height: '65px'});
	});
	
	$("input#search").focus(function(){
		if(this.value=='Поиск по сайту') this.value = '';
	})
	
	$("input#search").blur(function(){
		if(this.value=='') this.value = 'Поиск по сайту';
	})
	
	if($.cookie('play')=='1')$('#header .control:eq(1)').click()
});

var clickableElement = 2;

function rightToLeft()	{
	if	($('#photos-hdr .slide:last').css('left') == '200px')
		clickableElement = 0;
	else if ($('#photos-hdr .slide:first').css('left') == '0px')
		clickableElement = 2;

	$('#photos-hdr .slide:visible:eq(' + clickableElement + ')').trigger('click');
}

