/**
 *
 */
function align_center (cls)
{
	$(cls).removeAttr('style');
	var width = $(cls).width();

	if (width > 1280) 
	{
		width = 1280;
	}

	$(cls).css('left','50%');
	$(cls).css('margin-left',-width/2+'px');
	$(cls).width(width);
}

function load_gallery(item){
		
	if (item) {
		$(item).ajaxStart(function(){
			var height = $('.gallery-block').height();
			$('.gallery-block .gallery-panel').height(height-33);
			
			var outer = $('.gallery-block').parent();
			
			if (!outer.hasClass('gallery-wraper'))
			{
				$('.gallery-block').wrap($('<div class="gallery-wraper">'));
				$('.gallery-wrapper').height(height+100);
			}
			else
			{
				$('.gallery-wrapper').height(height);
			}
			$('.gallery-panel ul').hide();
			$('.gallery-block').find('.loader').show();
		});
		
		$(item).ajaxStop(function(){
			$('.gallery-wrapper').removeAttr('style');
			$('.gallery-panel ul').show();
			$('.gallery-block').find('.loader').hide();
		});
		
		$.post($(item).attr('href'), function(data){
			
			
			$('.gallery-block').replaceWith(data);
			
			$('.gallery-block').fadeIn(150, function(){
			$('.gallery-block .next-arrow').click(function(){
				load_gallery(this);
				return false;
			});
			$('.gallery-block .prev-arrow').click(function(){
				load_gallery(this);
				return false;
			});
			$('.gallery-block .gallery-nav a').click(function(){
				load_gallery(this);
				return false;
			});
			
			$('.thickbox').fancybox({ 
				'zoomSpeedIn': 200, 
				'zoomSpeedOut': 200,
				'frameWidth': 100,
				'frameHeight': 100,
				'overlayShow': true,
				'hideOnContentClick' :false,
				'centerOnScroll' : false,
				'autoScale': false
			});
			
			});
		});
		
		return false;
	}
}

$(document).ready(function(){
	

	/*align_center('.page');
	
	$(window).resize(function(){
		$(this).oneTime(100,function(){
			align_center('.page');
		});
	});*/
	
	$('.thickbox-pano').fancybox({ 
		'zoomSpeedIn': 200, 
		'zoomSpeedOut': 200,
		'width': 640,
		'height': 480,
		'overlayShow': true,
		'hideOnContentClick' :false,
		'centerOnScroll' : false,
		'autoScale': false,
		'swf': {wmode: 'transparent', scale : 'noscale', allowfullscreen: 'true', allowscriptaccess: 'sameDomain'} 
	});

	$('.before-footer').each(function(){
		var height = $(this).height();
		var line_height = $('.bottom-menu').height()+parseInt($('.bottom-menu').find('.inner').css('padding-bottom'))+parseInt($('.bottom-menu').find('.inner').css('padding-top'))+20;
		if (line_height > height) 
			$(this).height(line_height);
		else
			$(this).height(height);
	});
	
	var photoflip_controls = true;

	$('.photoflip li span img').each(function(){
		
		// With IE 7 workarond
		var img = this;
		img.complete_in = false;
		
		function onload() 
		{
			var im = $(img).closest('li').next().find('span');
			
			if (!img.complete_in) {
				$(img).closest('li').find('.loader-shadow').fadeOut(100);
				img.complete_in = true;
				im.find('img').attr('src', im.find('input').val());
				im = $(img).closest('li').prev().find('span');
				im.find('img').attr('src', im.find('input').val());
			}
		}
		
		if($.browser.msie && ($.browser.version.substr(0,1) == 7)){
		    function testImg(){
		        if(img.complete != null && img.complete == true){ 
		        	onload();
		            return;
		        }
		        setTimeout(testImg, 1000);
		    }
		    setTimeout(testImg, 1000);
		}
		
		$(this).load(function(){onload();}); 
	});
	
	$('.photoflip li').each(function(){
		this.lpos = $(this).position().left; 
	});
	
	$('.photoflip li').hover(function(){
		var parent = $(this);
		if (!photoflip_controls)
			return;
		parent.find('.left-arrow').animate({opacity: 1}, {duration: 1500, queue: false});
		parent.find('.right-arrow').animate({opacity: 1}, {duration: 1500, queue: false});
		parent.find('.description').animate({opacity: 1}, {duration: 1500, queue: false});
	});

	$('.photoflip li').mouseleave(function(){
		var parent = $(this);
		if (!photoflip_controls) {
			photoflip_controls = true;
			return false;
		}
		parent.find('.left-arrow').animate({opacity: 0}, {duration: 1500, queue: false});
		parent.find('.right-arrow').animate({opacity: 0}, {duration: 1500, queue: false});
		parent.find('.description').animate({opacity: 0}, {duration: 1500, queue: false});
	});

	$('.photoflip').mouseleave(function(){
		photoflip_controls = true;
	});
	
	$('.photoflip .right-arrow').click(function(){
		var parent = $(this).closest('li');  
		photoflip_controls = false;
		
		$('.photoflip li').each(function(){
			$(this).css({position: 'absolute', left: this.lpos});
			this.lpos -= 1010;
			$(this).animate({left: this.lpos});
		});
		
		parent.find('i.shadow').fadeIn(1000);
		parent.find('.left-arrow').fadeOut(500);
		parent.find('.right-arrow').fadeOut(500);
		parent.find('.description').fadeOut(500);
		
		parent.next().find('.left-arrow').fadeIn(1500);
		parent.next().find('.right-arrow').fadeIn(1500);
		parent.next().find('.description').fadeIn(1500, function(){
			//photoflip_controls = true;
		});
		
		parent.next().find('i.shadow').fadeOut(1000, function(){
			var first = $('.photoflip li:first');
			var last = $('.photoflip li:last');
			first.appendTo('.photoflip ul');
			first.get(0).lpos = last.get(0).lpos + 1010;
			first.css({left: first.get(0).lpos});
			
		});
		
		return false;
	});

	$('.photoflip .left-arrow').click(function(){
		var parent = $(this).closest('li');  
		photoflip_controls = false;

		var first = $('.photoflip li:first');
		var last = $('.photoflip li:last');
		last.prependTo('.photoflip ul');
		last.get(0).lpos = first.get(0).lpos - 1010;
		last.css({left: last.get(0).lpos});
		
		$('.photoflip li').each(function(){
			$(this).css({position: 'absolute', left: this.lpos});
			this.lpos += 1010;
			$(this).animate({left: this.lpos});
		});

		parent.find('i.shadow').fadeIn(1000);
		parent.find('.left-arrow').fadeOut(500);
		parent.find('.right-arrow').fadeOut(500);
		parent.find('.description').fadeOut(500);

		parent.prev().find('.left-arrow').fadeIn(1500);
		parent.prev().find('.right-arrow').fadeIn(1500);
		parent.prev().find('.description').fadeIn(1500);
		parent.prev().find('i.shadow').fadeOut(1000);
		
		return false;
	});
	
	$('.thickbox').fancybox({ 
		'zoomSpeedIn': 200, 
		'zoomSpeedOut': 200,
		'frameWidth': 100,
		'frameHeight': 100,
		'overlayShow': true,
		'hideOnContentClick' :false,
		'centerOnScroll' : false,
		'autoScale': false
	});
	
	$('.gallery-block .next-arrow').click(function(){
		load_gallery(this);
		return false;
	});
	
	$('.gallery-block .prev-arrow').click(function(){
		load_gallery(this);
		return false;
	});
	
	$('.gallery-block .gallery-nav a').click(function(){
		load_gallery(this);
		return false;
	});
	
	$('.map-toggle a').click(function(){
		var rel = $(this).attr('rel');
		
		if (rel == 'map-scheme')
		{
			//$('.maps').find('#map-google').fadeOut(500);
			$('.maps').find('#map-scheme').fadeIn(500);
			$('.map-toggle').find('.maps-yandex').removeClass('active');
			$('.map-toggle').find('.map-scheme').addClass('active');
		}
		else
		{
			$('.maps').find('#map-scheme').fadeOut(500);
			$('.map-toggle').find('.maps-yandex').addClass('active');
			$('.map-toggle').find('.map-scheme').removeClass('active');
		}
		
		return false;
		
	})
	
});

