var currentNewsPosition = 0,
	newsItems,
	newsItemsLength,
	newsNavCircles,
	newsCirclesLength,
	currentPhotoPosition = 0,
	photosScrollerCount = 0,
	photoWidth = 580;

var currentVideoPreviewPosition = 0,
	videoPreviewItems,
	videoPreviewItemsLength,
	videoPreviewNavCircles,
	videoPreviewCirclesLength;

var photoScrollers = [];

var galleryPhotoPreviewScrollers = [];

$(document).ready(function(){


	$('#menuItem_feedback').click(function(){
	    $('#overlay').stop().show().animate({opacity: 0.5}, 1000).click(function(){
	        closePopup();
		});
		
	    $('.popupClose').click(function(){
	        closePopup();
		});
		
		$('#vk_comments').stop().fadeIn(1000);
		
		return false;
	})

	
	$('.videoPreviewImg').click(playVideo);
	
	$('#photoGalleryHeader .galleryHeaderLink').click(function(){
		$('#photoGalleryHeader .galleryHeaderLink').removeClass('galleryHeaderLinkActive');
		$(this).addClass('galleryHeaderLinkActive');
		
		
		$('.galleryPhotoContentYear').hide();
		
		var id = this.id.replace('galleryHeaderLinkPhoto_','');
		
		$('#galleryPhotoContentYear_'+id).show();
		
		$($('#galleryPhotoContentYear_'+id).find('.galleryAlbumsList span')[0]).click();
	});
	$($('#photoGalleryHeader .galleryHeaderLink')[0]).click();
	
	$('#photoPanoramaGalleryHeader .galleryHeaderLink').click(function(){
		$('#photoPanoramaGalleryHeader .galleryHeaderLink').removeClass('galleryHeaderLinkActive');
		$(this).addClass('galleryHeaderLinkActive');
		
		var id = this.id.replace('galleryHeaderLinkPanorama_','');
		
		$('#panoramaSWF').html('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="940" height="600"> \
			<param name="movie" value="/swf/panorama/'+id+'.swf"><param name="allowFullScreen" value="true"><param name="quality" value="high"> \
 			<embed src="/swf/panorama/'+id+'.swf" quality="high" allowFullScreen=true pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="940" height="600"></embed></object>');
		
		//new swfobject.embedSWF("/swf/panorama/"+id+".swf", "panoramaSWF", "940", "600", "9.0.115", {allowFullScreen: "true"}, {allowFullScreen: "true"});
	});
	//$($('#photoPanoramaGalleryHeader .galleryHeaderLink')[0]).click();
	
	
	
	$('.galleryAlbumsList span').click(function(){
		$('.galleryAlbumsList span').removeClass('active');
		$(this).addClass('active');
		
		
		$('.galleryPhotoAlbum').hide();
		
		var id = this.id.replace('galleryAlbumLink_','');
		
		$('#galleryPhotoAlbum_'+id).show(); 
	});
	
	$($('.galleryAlbumsList span')[0]).click();
	
	$('.firstItem').mouseover(function(){
		$(this).addClass('firstItemHovered');	
	}).mouseout(function(){
		$(this).removeClass('firstItemHovered');	
	});
	
	$('.imagesScroller').each(function(index,element){
		
		photoScrollers[index] = {};
		
		photoScrollers[index].element = $(this);
		photoScrollers[index].photosScrollerCount = photoScrollers[index].element.find('.imagesScrollerItem').length;
		photoScrollers[index].currentPosition = 0;

		photoScrollers[index].element.find('.scrollLeft').click(function(){
			switchPhoto(index, photoScrollers[index].currentPosition-1);
		});
		
		photoScrollers[index].element.find('.scrollRight').click(function(){
			switchPhoto(index, photoScrollers[index].currentPosition+1);
		});

		photoScrollers[index].element.find('.scrollCounts').html(1+"/"+photoScrollers[index].photosScrollerCount);
		
		photoScrollers[index].element.find('.imgScrollerDescription').html($(photoScrollers[index].element.find('.imagesScrollerItem')[0]).attr('titletext'))

		//switchPhoto(index,0);
	});
	
	$('.galleryPhotoAlbum').each(function(index,element){
		
		galleryPhotoPreviewScrollers[index] = {};
		
		galleryPhotoPreviewScrollers[index].element = $(this);
		galleryPhotoPreviewScrollers[index].photosScrollerCount = galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoScrollerItem').length;
		galleryPhotoPreviewScrollers[index].currentPosition = 0;

		galleryPhotoPreviewScrollers[index].element.find('.scrollLeft').click(function(){
			switchGalleryPhotoPreview(index, galleryPhotoPreviewScrollers[index].currentPosition-1);
		});
		
		galleryPhotoPreviewScrollers[index].element.find('.scrollRight').click(function(){
			switchGalleryPhotoPreview(index, galleryPhotoPreviewScrollers[index].currentPosition+1);
		});

		galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoPreviewScrollCount').html(1+"/"+galleryPhotoPreviewScrollers[index].photosScrollerCount);
		
		galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoPreviewScrollDescription').html(galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoScrollerItem')[0].title)
		
		//galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoMain').css('backgroundImage','/gallery/'+galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoScrollerItem')[0].id.replace('galleryPhotoScrollerItem_','')+'.jpg');
		
		galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoMain').css('background-image','url("/gallery/'+galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoScrollerItem')[0].id.replace('galleryPhotoScrollerItem_','')+'.jpg")');
		
		//$('#debug').append(galleryPhotoPreviewScrollers[index].element.find('.galleryPhotoMain').attr('id')+" ")

		//switchPhoto(index,0);
	});

	$('#schemeLink3600').click(function(){
		$(this).addClass('active');
		$('#schemeLink1500').removeClass('active');
		
		$('#schemeImg').attr('src','/i/scheme/3600.png');
	});
	
	$('#schemeLink1500').click(function(){
		$(this).addClass('active');
		$('#schemeLink3600').removeClass('active');
		
		$('#schemeImg').attr('src','/i/scheme/1500.png');
	});
	

	$('.closeOrange').click(function(){
        $('#raspsFull').slideUp(1000);
        $('#raspsLink').slideDown(1000);
	})
	
	$('#raspsLink').click(function(){
        $('#raspsFull').slideDown(1000);
        $('#raspsLink').slideUp(1000);
	})

	$('#schemeIcon').click(function(){
	    $('#overlay').stop().show().animate({opacity: 0.5}, 1000).click(function(){
	        closePopup();
		});
		
	    $('.popupClose').click(function(){
	        closePopup();
		});
		
		$('#schemesPopup').stop().fadeIn(1000);
	});
	
	$('#regButton').click(function(){
	    $('#overlay').stop().show().animate({opacity: 0.5}, 1000).click(function(){
	        closePopup();
		});
		
	    $('.popupClose').click(function(){
	        closePopup();
		});
		
		$('#statisticsPopup').stop().fadeIn(1000);
		
		return false;
	});
	
	
	newsItems = $('.newsItem');
	newsItemsLength = newsItems.length;
	newsCirclesLength = Math.round(newsItems.length / 4);

	if (newsCirclesLength < newsItems.length / 4)
		newsCirclesLength++;
	
	for (var i=0; i<newsCirclesLength; i++)
		$('#newsNav').append('<span class="newsNavCircle">'+(i+1)+'</span>');

	if (i==1){
		$('#newsArrowLeft').addClass('inActive');
		$('#newsArrowRight').addClass('inActive');
	}
	
	newsNavCircles = $('.newsNavCircle');

	newsNavCircles.each(function(index,el){
		$(el).click(function(){switchNews(index)});
	})

	$('#newsArrowLeft').click(function(){switchNews(currentNewsPosition-1)});
	$('#newsArrowRight').click(function(){switchNews(currentNewsPosition+1)});

	switchNews(0);

	$('#news').css({
		overflow: 'hidden'
	});
	
	
	
	
	videoPreviewItems = $('.videoPreviewScrollerItem');
	videoPreviewItemsLength = videoPreviewItems.length;
	
	if (videoPreviewItemsLength > 0){
		videoPreviewCirclesLength = Math.round(videoPreviewItems.length);
		
		if (videoPreviewCirclesLength < videoPreviewItems.length)
			videoPreviewCirclesLength++;
		
		if (videoPreviewCirclesLength>1){
			for (var i=0; i<videoPreviewCirclesLength; i++)
				$('#videoPreviewScrollerNavigation').append('<span class="newsNavCircle">'+(i+1)+'</span>');
		}
	
		/*
		if (i==1){
			$('#newsArrowLeft').addClass('inActive');
			$('#newsArrowRight').addClass('inActive');
		}
		$('#newsArrowLeft').click(function(){switchNews(currentNewsPosition-1)});
		$('#newsArrowRight').click(function(){switchNews(currentNewsPosition+1)});
		*/
		//$('#debug').append(videoPreviewItemsLength+" ")
		videoPreviewNavCircles = $('.newsNavCircle');
	
		videoPreviewNavCircles.each(function(index,el){
			$(el).click(function(){switchVideoPreview(index)});
		})
	
		switchVideoPreview(0);
	}
})


function switchNews(where) {

	var scrollLeftStart = $('#news').scrollLeft();

	if (where < 0)
		where = 0;

	if (where + 1 >= newsCirclesLength)
		where = newsCirclesLength - 1;

	currentNewsPosition = where;

	newsNavCircles.removeClass('newsNavCircleCurrent');
	
	$(newsNavCircles[currentNewsPosition]).addClass('newsNavCircleCurrent');
	
	

	var scrollLeftEnd = currentNewsPosition * (235 * 4);
	

	if (scrollLeftEnd > $('#newsTable').width() - (235 * 4 )){
		scrollLeftEnd = $('#newsTable').width() - (235 * 4);
	}


	var scrollLeftPart = (scrollLeftEnd - scrollLeftStart) / 40;

	var timer = setInterval(function(){
		$('#news').scrollLeft($('#news').scrollLeft() + scrollLeftPart);

		if (scrollLeftPart > 0){
			if ($('#news').scrollLeft() >= scrollLeftEnd){
				clearInterval(timer);
				$('#news').scrollLeft(scrollLeftEnd);
			}
		}else{
			if ($('#news').scrollLeft() <= scrollLeftEnd){
				clearInterval(timer);
				$('#news').scrollLeft(scrollLeftEnd);
			}
		}

	},10);

	setTimeout(function(){
		clearInterval(timer);
		$('#news').scrollLeft(scrollLeftEnd);
		//$('#debug').html(scrollLeftStart + " = " +scrollLeftEnd + " = "+scrollLeftPart + " = "+currentNewsPosition)
	},1000)
	
	$('#newsArrowRight').removeClass('inActive');
	$('#newsArrowLeft').removeClass('inActive');
	
	if (currentNewsPosition == 0){
		$('#newsArrowLeft').addClass('inActive');
	}
	
	if (currentNewsPosition == newsCirclesLength - 1){
		$('#newsArrowRight').addClass('inActive');
	}
	//$('#debug').html(scrollLeftStart + " = " +scrollLeftEnd + " = "+scrollLeftPart + " = "+currentNewsPosition)
	
}

function switchVideoPreview(where) {

	var scrollLeftStart = $('#videoPreviewScroller').scrollLeft();

	if (where < 0)
		where = 0;

	if (where + 1 >= videoPreviewCirclesLength)
		where = videoPreviewCirclesLength - 1;

	currentVideoPreviewPosition = where;

	videoPreviewNavCircles.removeClass('newsNavCircleCurrent');

	$(videoPreviewNavCircles[currentVideoPreviewPosition]).addClass('newsNavCircleCurrent');

	var scrollLeftEnd = currentVideoPreviewPosition * 460;
	

	if (scrollLeftEnd > (videoPreviewCirclesLength-1)  * 460){
		scrollLeftEnd = (videoPreviewCirclesLength-1) * 460;
	}
	
	
	
	var scrollLeftPart = (scrollLeftEnd - scrollLeftStart) / 40;

	

	var timer = setInterval(function(){
		$('#videoPreviewScroller').scrollLeft($('#videoPreviewScroller').scrollLeft() + scrollLeftPart);
		
		if (scrollLeftPart > 0){
			if ($('#videoPreviewScroller').scrollLeft() >= scrollLeftEnd){
				clearInterval(timer);
				$('#videoPreviewScroller').scrollLeft(scrollLeftEnd);
			}
		}else{
			if ($('#videoPreviewScroller').scrollLeft() <= scrollLeftEnd){
				clearInterval(timer);
				$('#videoPreviewScroller').scrollLeft(scrollLeftEnd);
			}
		}

	},10);

	setTimeout(function(){
		clearInterval(timer);
		$('#videoPreviewScroller').scrollLeft(scrollLeftEnd);
		//$('#debug').html(scrollLeftStart + " = " +scrollLeftEnd + " = "+scrollLeftPart + " = "+currentNewsPosition)
	},1000)
	

}

function switchGalleryPhotoPreview(scrollerIndex,where) {

	var scroller = galleryPhotoPreviewScrollers[scrollerIndex];

	scroller.currentPosition = where;
	
	if (scroller.currentPosition < 0)
		scroller.currentPosition = 0;

	if (scroller.currentPosition >= scroller.photosScrollerCount)
		scroller.currentPosition = scroller.photosScrollerCount - 1;
	
	if (scroller.currentPosition == 0)
		scroller.element.find('.scrollLeft').addClass("inActive");
	else
		scroller.element.find('.scrollLeft').removeClass("inActive");

	if (scroller.currentPosition == scroller.photosScrollerCount - 1)
		scroller.element.find('.scrollRight').addClass("inActive");
	else
		scroller.element.find('.scrollRight').removeClass("inActive");

	var scrollLeftStart = scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft();

	var scrollLeftEnd = scroller.currentPosition * 110;
	

	if (scrollLeftEnd > (scroller.photosScrollerCount-1)  * 110){
		scrollLeftEnd = (scroller.photosScrollerCount-1) * 110;
	}
	
	
	
	var scrollLeftPart = (scrollLeftEnd - scrollLeftStart) / 20;

	

	var timer = setInterval(function(){
		scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft(scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft() + scrollLeftPart);
		
		if (scrollLeftPart > 0){
			if (scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft() >= scrollLeftEnd){
				clearInterval(timer);
				scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft(scrollLeftEnd);
			}
		}else{
			if (scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft() <= scrollLeftEnd){
				clearInterval(timer);
				scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft(scrollLeftEnd);
			}
		}

	},10);

	setTimeout(function(){
		clearInterval(timer);
		scroller.element.find('.galleryPhotoPreviewScrollerContainer').scrollLeft(scrollLeftEnd);
		//$('#debug').html(scrollLeftStart + " = " +scrollLeftEnd + " = "+scrollLeftPart + " = "+currentNewsPosition)
	},500)
	
	scroller.element.find('.galleryPhotoPreviewScrollCount').html((scroller.currentPosition+1)+"/"+scroller.photosScrollerCount);
}

function loadGalleryPhoto(albumId,imageId){
	var i = document.createElement('img');
	i.src = "/gallery/'+imageId+'.jpg";
	
	var el = $('#galleryPhotoAlbum_'+albumId);
	
	el.find('.galleryPhotoPreviewScrollDescription').html($('#galleryPhotoScrollerItem_'+imageId).attr('title'))
	
	el.find('.galleryPhotoMain').animate({
		opacity: 0
	}, 400, function() {
		el.find('.galleryPhotoMain').css('background-image','url("/gallery/'+imageId+'.jpg")');
		setTimeout(function(){
			el.find('.galleryPhotoMain').animate({opacity: 1},400);
		},100);
	});
	
	
}


function closePopup(){
	$('.popup').stop().fadeOut(1000);
	$('#overlay').stop().animate({opacity: 0.0}, 1000, function(){
		$('#overlay').hide();
		$('.popup').hide();
	})
	$('#vk_comments').fadeOut();
}

var loadedNewsTimes = 0;
function loadMoreNews() {
	$('#moreNewsDiv span').hide();
	
	loadedNewsTimes++;
	$.get( "/ajax.php?action=loadMoreNews&times="+loadedNewsTimes, function(data, textStatus, XMLHttpRequest){
	    $('#moreNewsDiv').replaceWith(data);
	});
}

function switchPhoto(scrollerIndex,where){
	
	var scroller = photoScrollers[scrollerIndex];
	
	var scrollLeftStart = scroller.currentPosition;
	
	scroller.currentPosition = where;
	
	if (scroller.currentPosition < 0)
		scroller.currentPosition = 0;

	if (scroller.currentPosition >= scroller.photosScrollerCount)
		scroller.currentPosition = scroller.photosScrollerCount - 1;
	
	if (scroller.currentPosition == 0)
		scroller.element.find('.scrollLeft').addClass("inActive");
	else
		scroller.element.find('.scrollLeft').removeClass("inActive");

	if (scroller.currentPosition == scroller.photosScrollerCount - 1)
		scroller.element.find('.scrollRight').addClass("inActive");
	else
		scroller.element.find('.scrollRight').removeClass("inActive");
	
	if (scrollLeftStart <= scroller.currentPosition && !( scrollLeftStart == 0 && scroller.currentPosition == 0 )){
		if (scroller.element.find('.imagesScrollerItem')[scrollLeftStart] && !(scrollLeftStart == scroller.photosScrollerCount - 1))
			$(scroller.element.find('.imagesScrollerItem')[scrollLeftStart]).animate({width: 0})
	}else{
		if (scroller.element.find('.imagesScrollerItem')[scrollLeftStart-1])
			$(scroller.element.find('.imagesScrollerItem')[scrollLeftStart-1]).animate({width: photoWidth})
	}
	
	if (scroller.currentPosition == scroller.photosScrollerCount -1)
		$(scroller.element.find('.imagesScrollerItem')[scroller.currentPosition]).animate({width: photoWidth})
	
	//$('#debug').html(scrollLeftStart + " "+scroller.currentPosition)
	
	
	
	/*var scrollLeftEnd = scroller.currentPosition * photoWidth;
	
	if (scrollLeftEnd > (scroller.photosScrollerCount - 1) * photoWidth )
		scrollLeftEnd = (scroller.photosScrollerCount - 1) * photoWidth;
	
	var scrollLeftPart = (scrollLeftEnd - scrollLeftStart) / 20;
	
	var timer = setInterval(function(){
		scroller.element.find('.imagesScrollerContainer').scrollLeft(scroller.element.find('.imagesScrollerContainer').scrollLeft() + scrollLeftPart);
		
		if (scrollLeftPart < 0){
			if (scroller.element.find('.imagesScrollerContainer').scrollLeft() <= scrollLeftEnd){
				scroller.element.find('.imagesScrollerContainer').scrollLeft(scrollLeftEnd);
				clearInterval(timer);
			}
		}else{
			if (scroller.element.find('.imagesScrollerContainer').scrollLeft() >= scrollLeftEnd){
				scroller.element.find('.imagesScrollerContainer').scrollLeft(scrollLeftEnd);
				clearInterval(timer);
			}
		}
	},20);	
		
	setTimeout(function(){
		clearInterval(timer);
		scroller.element.find('.imagesScrollerContainer').scrollLeft(scrollLeftEnd);
	},700);
	*/
	
	scroller.element.find('.scrollCounts').html((scroller.currentPosition+1)+"/"+scroller.photosScrollerCount);
	
	if (scroller.element.find('.imagesScrollerItem')[scroller.currentPosition]){
		scroller.element.find('.imgScrollerDescription').html($(scroller.element.find('.imagesScrollerItem')[scroller.currentPosition]).attr('titletext'))
	}
}


function playVideo(event){
	if (this.id){
		var id = this.id.replace("galleryVideo_","");
		
		$('#videoDiv').html('<object width="460" height="283"><param name="movie" value="http://www.youtube.com/v/'+id+'?fs=1&amp;hl=ru_RU&amp;rel=0"></param>\
							<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>\
					<embed src="http://www.youtube.com/v/'+id+'?fs=1&amp;hl=ru_RU&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="460" height="283"></embed></object>');
		
		//uppodSend("videoDiv","file:/gallery/"+id,function(){});
	}
}
