var isAjax = 0;
$(document).ready(function(){
 	$(function() {
		// if the function argument is given to overlay,
		// it is assumed to be the onBeforeLoad event listener
		$("a[rel]").overlay({
			mask: 'black',
			effect: 'apple',
			onBeforeLoad: function() {
				// grab wrapper element inside content
				var wrap = this.getOverlay().find(".contentWrap");
				// load the page specified in the trigger
				wrap.load(this.getTrigger().attr("href"));
			},
			onLoad: function() {
				
					//alert('cvb');
			},
			onClose: function() {
			//alert('cvbcvb');
			$('.contentWrap').text('');
			$('#calendar_cont').css('display','none');
			}
		});
	});
//	Drupal.attachBehaviors();
	//сдвиг элемента портфолио при наведении
	$(".scrollable-portfolio .field-item img").hover(
	function() {
		if (!$(this).hasClass('active')){
			 $(this).css({
				'width' : 96,
				'height' : 65,
				'background' : 'url("/themes/baltic/images/photo_back1_hov.png") no-repeat scroll left top transparent'
			 });
			 $(this).parent().css({
				'margin-left': -3,
				'margin-right' : 13
			 })
		 }
	},
	function() {
		if (!$(this).hasClass('active')){
			$(this).css({
				'width' : 91,
				'height' : 61,
				'background' : 'url("/themes/baltic/images/photo_back1.png") no-repeat scroll left top transparent'
			 });
			 $(this).parent().css({
				'margin-left': 0,
				'margin-right' : 10
			 })
		 }
	});
	//перемещение блока контента
	$("#cont_open").click(function() {
		$('.openconentleft').animate(
		{
			left: '0'
		},
		300, function() {
			$("#cont_close").css('display','block');
			$("#cont_open").css('display','none');
			
		});
	});
	$("#cont_close").click(function() {
		$('.openconentleft').animate(
		{
			left: '-552'
		},
		300, function() {
			$("#cont_close").css('display','none');
			$("#cont_open").css('display','block');
		});
	});
		$("#cont_close_right").click(function() {
		$('.opencontentright').animate(
		{
			left: '540'
		},
		300, function() {
			$("#cont_close_right").css('display','none');
			$("#cont_open_right").css('display','block');
		});
	});
	$("#cont_open_right").click(function() {
		$('.opencontentright').animate(
		{
			left: '0'
		},
		300, function() {
			$("#cont_close_right").css('display','block');
			$("#cont_open_right").css('display','none');
		});
	});
	$(function() {
		if ($(".scrollable-portfolio").height()){
			$(".scrollable-portfolio").scrollable(); 
		}
	});
	$(".view-photogallery .items img").click(function() {
		// see if same thumb is being clicked
		if ($(this).hasClass("active")) {return;}
		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		var url = $(this).attr("src").replace("/118x81/", "/bigs/");
		// the large image from www.flickr.com
		var img = new Image();
		// call this function after it's loaded
		img.onload = function() {
			// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#body-background").hide();
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
			// change the image
			wrap.find("img").attr("src", url);
		};
		// begin loading the image from www.flickr.com
		img.src = url;
		// activate item
		$(".view-photogallery .items img").removeClass("active");
		$(this).addClass("active");
		// when page loads simulate a "click" on the first image
	});
	$(".view-portfolio-content .items img").click(function() {
		//alert ('cvbcvb');
		// see if same thumb is being clicked
		if ($(this).hasClass("active")) {return;}
		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		var url = $(this).attr("src").replace("/91x61/", "/423x320/");
		// the large image from www.flickr.com
		var img = new Image();
		// call this function after it's loaded
		img.src = url;
		img.onload = function() {
			// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#port_big_image").hide();
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
			// change the image
			wrap.find("img").attr("src", url);
		};
		// begin loading the image
		$(".view-portfolio-content .items img").removeClass("active");
		$(".view-portfolio-content .items img").css({
			'width' : 91,
			'height' : 61,
			'background' : 'url("/themes/baltic/images/photo_back1.png") no-repeat scroll left top transparent'
		 });
		 $(".view-portfolio-content .items field-item").parent().css({
			'margin-left': 0,
			'margin-right' : 10
		 });
		$(this).addClass("active");
		$(this).css({
			'width' : 96,
			'height' : 65,
			'background' : 'url("/themes/baltic/images/photo_back1_hov.png") no-repeat scroll left top transparent'
		 });
		 $(this).parent().css({
			'margin-left': -3,
			'margin-right' : 13
		 })
	});
			$("#body-background").ezBgResize();
			$("div.scriptdiv").hover(
				function(){
					$(".prevnext").css('display', 'none');
					$('.transport_combain').css('z-index', '99');
					$("#buttonbigimg").hide('show');
					$("#buttonslideshow").hide('show');
					$("div.toping").stop().animate(
						{
							height:"250px"
						},
						200,
						function(){
							$("div.up_menu ul ul li").stop().show();
							$("div.up_menu ul ul li").css('opacity','1');
						});
					$("div.scriptdiv").css('display','none');
				},function(){});
			$("div.toping").hover(
				function(){
					$(this).addClass("mouse-in");
				},function(){
					setTimeout(MenuClose, 500);
					$(this).removeClass("mouse-in");
					$("div.scriptdiv").css('display','block');
				});
			function MenuClose(){
				if($("div.toping").hasClass("mouse-in")){
					return;
				}
				$("div.up_menu ul ul li:not(.active-trail)").stop().hide();
					$("div.toping").stop().animate(
						{
							height:"140px"
						},
						200);
						SubItems();
						$(".prevnext").css('display', 'block');
						$("#buttonbigimg").show('show');
						$("#buttonslideshow").show('show');
						$('.transport_combain').css('z-index', '101');
			}
				$("#buttonbigimg").click(
				function(){
                    $('.toping_upmenu').hide('slow');
                    $('.prevnext').hide('slow');
					$('.right-port').hide('slow');
					$('#buttonbigimg').css('display','none');
					$('#buttonslideshow').animate({opacity: 0}, 500);
					$('.center').hide('slow');
					$('.footer').hide('slow');
					$('#body-background').addClass('get_menu_back');
                  })
				
				$('#body-background').click(function(){
					if ($(this).hasClass('get_menu_back')){
						$('.toping_upmenu').show('slow');
						$('.prevnext').show('slow');
						$('.right-port').show('slow');
						$('#buttonbigimg').css('display','block');
						$('#buttonslideshow').animate({opacity: 1}, 500);
						$('.center').show('slow');
						$('.footer').show('slow');
						$(this).removeClass('get_menu_back');
					}
			 });
			var start = 0;
			var top = 130;
			var elem = 110;
			var n = ($(window).height() - top)/elem;
			var tab = $(".right_center .img-tab");
			for(var i = 0; i < Math.min(n - 1,tab.length) ; i++){
				$(tab[i]).show();
			}
			$(".right_center .right-port-left img").click(function(){
				var n = parseInt(($(window)[0].innerHeight - top)/elem);
				start -= n;
				start = (start < 0) ? 0 : start;
				start = (start >= $(".img-tab").length) ? 0 : start;
				showImage();
			});
			$(".right_center .right-port-right img").click(function(){
				var n = parseInt(($(window)[0].innerHeight - top)/elem);
				start += n;
				start = (start < 0) ? 0 : start;
				start = (start >= $(".img-tab").length) ? start - n : start;
				showImage();
			});
			$(window).resize(function(){
				var n = ($(window)[0].innerHeight - top)/elem;
				var tab = $(".img-tab");
				for(var i = 0; i < tab.length ; i++){
					if(i < n - 1){
						$(tab[i]).show();
					}
					else{
						$(tab[i]).hide();
					}
				}
			});
			function showImage(){
				var n = ($(window)[0].innerHeight - top)/elem;
				var tab = $(".img-tab");
				for(var i = 0; i < tab.length ; i++){
					if(i < start + n - 1 && i >= start){
						$(tab[i]).show();
					}
					else{
						$(tab[i]).hide();
					}
				}
			}
			right_hover_init();
			function right_hover_init(){
				$(".right_center .img-wrap").hover(
					function(){
					$(".right_center .text-tab").stop().hide();
					$(".right_center .img-wrap").attr("id","");
					$(".right_center .img-tab").attr("id","");
					$(".right_center .img-wrap-1").attr("id","");
					$(this).attr("id","img-wrap-active");
					$(this).attr("id","img-tab-active");
					//$(this).parents(".img-wrap-1").attr("id","img-wrap-1-active");
					$(this).find(".text-tab").show();
					
					},
					function(){
					});
					$(".right_center .img-wrap").hover(function(){
					},
					function(){
						$(this).find(".text-tab").stop().hide();
						$(this).attr("id","");
						$(this).find(".img-tab").attr("id","");
						$(this).find(".img-wrap-1").attr("id","");
					})
			} 
		$(".toping_upmenu a[href], .lang a[href], .bottom_menu ul a[href], .spb, .moscow, .sochi, .transport_combain a[href], .right-port a[href], .prevnext a[href], .transport_combain a[href], .trans_cont_links a[href], #prev_month a, #next_month a").click(function() {
		$('.openconentleft').animate({left: -510}, 1000);
		$('.opencontentright').animate({left: 525}, 1000);
		$('.toping').animate({height: 5}, 1000);
		$('.footer').animate({bottom: -55}, 1000);
		$('.right-port').animate({right: -175}, 1000);
		$('.marg').animate({opacity: 0}, 1000, function(){
			$(this).html('');
		});
        $('body').css('overflow', 'hidden');
	});
	$('.cont_one').ajaxComplete(function(){
		$('#calendar_cont').css('display', 'block');
		div_height = 365 - $('.calenup_text h1').height() - $('.calenup_time').height() - $('.calen_value').height();
		$('.calendiv').css('height', div_height);
		$('.calen_body').css('height', div_height);
		$('.calen_body').jScrollPane({showArrows:true, scrollbarWidth:10, dragMaxHeight:127});
	});
	BindRightArrows();
	SubItems();
});
function SubItems(){
	upmenuItems = $('.up_menu .menu li:has(ul)');
            upmenuItems.children('ul').children('li').css('display','block').css('opacity','0.3');
            upmenuItems.children('ul').children('li.last').css('display','block').css('opacity','0.6');
            upmenuItems.children('ul').children('li.active-trail').parent().children('li').css('display','none');
            upmenuItems.children('ul').children('li.active-trail').css('display','block').css('opacity','1');
};
function CheckEsc(){
    $('body').keypress(function(event){
            alert(event.keyCode);
        });

}
function BindRightArrows(){
	$('.right-port .next').removeClass('disabled');
	$('.right-port .prev').removeClass('disabled');
	$('.right-port .prev').click(function(){
			item_pos = parseInt($('.right-port .items').css('top').replace('pt',''));
			item_pos += $('.items').children('div').height();
			if (item_pos >= 0){
				item_pos = 0;
			};
			$('.right-port .items').animate({
				top : item_pos
			});
	});
	$('.right-port .next').click(function(){
			check_pos = $('.right-port .scrollable').height() - $('.right-port .items').height();
			item_pos = parseInt($('.right-port .items').css('top').replace('pt',''));
			item_pos -= $('.items').children('div').height();
			if ( item_pos <= check_pos ){
				item_pos = check_pos;
			};
			$('.right-port .items').animate({
				top : item_pos
			});
	});
}
function BindContHovers(){
	$('.list_sity .images').hover(function(){
		if (!$(this).hasClass('.currImg')){
			$(this).animate({
				margRight : 2,
				margLeft : -10,
				width: 147,
				height: 160
			}, 500, function(){
				$(this).css('background','url("/themes/baltic/images/city_currimg.png") no-repeat scroll left top');
			});
			$(this).children('a').animate({height: 112},500);
			$(this).find('img').animate({
				width : 135,
				height: 95
			});
		}
	}, function(){
		if (!$(this).hasClass('.currImg')){
			$(this).animate({
				margRight : 12,
				margLeft : 0,
				width: 127,
				height: 141
			}, 500, function(){
				$(this).css('background','url("/themes/baltic/images/sity_contact.png") no-repeat scroll left top');
			});
			$(this).children('a').animate({height: 92},500);
			$(this).find('img').animate({
				width : 115,
				height: 78
			});
		}
	})
}
