$(function() {

	resizeBlocks();
	createBlack();
	
	$(window).resize(function() {
		resizeBlocks();
		$(document).pngFix();
	});
	changeMenu();		
	changeFadeMenu();
	doOpacity();
	$(document).pngFix(); 
	checkLoadMore();
	$(window).scroll(function() {checkLoadMore();})
	
	verticalSlider();
	updateStatistic();
	updateGallery();
	resizeBlocks();
	$(document).pngFix();
	ShareGallery();
	IndexMans();
});

function IndexMans()
{
	var i=0;
	$(".footballitem").each(function() {if (i++ > 0) $(this).css({"opacity":0,"zIndex":100}); else $(this).css("zIndex",101);});
	if (i <= 0) return;
	var t = 5000;
	setTimeout("ShowNextGammer(0,1,"+i+","+t+");",t);	
}

function ShowNextGammer(myCurrent,i, myCount, myTime)
{
	var t = 500;
	if (i >= myCount) i = 0;
	$($(".footballitem")[myCurrent]).animate({"opacity":0},t, function() {$(this).css("zIndex",100)});
	$($(".footballitem")[i]).animate({"opacity":1},t, function() {$(this).css("zIndex",101); setTimeout("ShowNextGammer("+i+","+(i+1)+","+myCount+","+myTime+");",myTime);	});
}

function ShareGallery()
{
	if (! $("#SlideGallery").size()) return;
	workObjects['slide'] = new mySlider(document.getElementById("SlideGallery"),'slide',true,5000);
}


var newSlider = function(myClass) {
	this.num = 0;
	this.myClass = myClass;
	this.intall = function() {
		var myStep = 0; var mySize = 0; var maxSize = 0; var startWidth = 0;
		
		$(this.myClass).each(function() {
			if (!$('li .item',this).size()) return(false);
			startWidth = $(this).width()
			mySize = $('li .item',this).size();
			
			with($($('li .item',this)[0])) {
				myStep = parseInt(width()) + (isNaN(css('marginLeft'))? 0 : parseInt(css('marginLeft'))) + (isNaN(css('marginRight')) ? 0 : parseInt(css('marginRight')))+2;
			}
			
			maxSize =  myStep * $('li .item',this).size();
			$('ul',this).each(function() {$(this).width(maxSize+100);myBlock = this;});
			maxSize = Math.ceil((maxSize -startWidth)/myStep);
			
		});
		
		this.mySize = mySize;
		this.maxSize = maxSize;
		this.myStep = myStep;
		this.myBlock = myBlock;
	
	}
	
	this.moveLeft = function() {
		if (this.num < 1) return(false);
		this.num--;
		this.move();
	}
	
	this.moveRight = function() {
		if (this.num >= this.maxSize) return(false);
		this.num++;
		this.move();
	}
	
	this.move = function() {
		var myLeft = -(this.num * this.myStep);
		$(this.myBlock).animate({marginLeft:myLeft},{duration:800,queue:false});
	}
	
	this.intall();
	
	
	
}

function updateStatistic() {
	$('.statistic tr:odd').each(function() {$(this).attr('class','dark')})
}

function updateGallery() {
	$('.profileGallery img').each(function() {
		$(this).bind('click',function() {
			
			var myBlock = $('.clientProfile').size() ? $('.clientProfile')[0]: false;
			if (!myBlock) return(false);
			var myUrl = ($(this).attr('class') == 'video' ?'getVideo.htm?video=' : 'getGallery.htm?photo=')+parseInt($(this).attr('rel'));
			
			$('.floatGallery').remove();
			var myContent = $('<div class="floatGallery"><a class="close" href="#" title="Закрыть окно"><img width="58" height="16" src="img/close.gif" alt="Закрыть" /></a><div class="loading">Загрузка...</div></div>')[0];
			$(myBlock).append(myContent);
			
			$(myContent)
				.hide(1,function() {$(this).show();});
			
			
			
			$('a.close',myContent).each(function() {
				$(this).bind('click',function() {
					$(myContent).hide('scale',function() {$(myContent).remove()});
					return(false);
				});
			});
			
			
			
			$.post(myUrl,{'show':1},function(myContent) {
				var newContent =  $('.floatGallery').size() ? $('.floatGallery')[0]: false;
				if (!newContent) return(false);
				newContent.innerHTML = myContent;
				$('a.close',newContent).each(function() {
					$(this).bind('click',function() {
						$(newContent).hide('scale',function() {$(newContent).remove()});
						return(false);
					});
				});
				var mySilider = new newSlider('.smallPreview');
				
				
				$('button.stepBack').each(function() {
					$(this).bind('click',function() {
						mySilider.moveLeft();
						return(false);
					});
				});
				
				$('button.stepForward').each(function() {
					$(this).bind('click',function() {
						mySilider.moveRight();
						return(false);
					});
				});
				
				$('img.openPhoto').each(function() {
					$(this).bind('click',function() {
						var myId = $(this).attr("rel");
						if (myId) {
							$('#bigObject')[0].innerHTML = '<img src="myPics/photo_'+myId+'?640_388" alt="" />'; 
						}
						return(false);
					});
				});
				
				$('img.openVideo').each(function() {
					$(this).bind('click',function() {
						var myId = $(this).attr("rel");
						if (myId) {
							$('#bigObject')[0].innerHTML = '<iframe style="position:relative; margin:auto" title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/'+myId+'" frameborder="0" allowfullscreen></iframe>'; 
						}
						return(false);
					});
				});
			})
			
		});
	});
}


function verticalSlider() {
	$('.vslider').each(function() {
		
		var vslider = this;
		
		var bigAllSize = 0; var bigSize = 0; var currentTop  = 0; var maxTop = 0; var myStep = 0; var currentNum = 0; var maxNum = 0;
		
		var myUl = false;
		
		if (!$('ul',this).size()) return(false);
		myUl = $('ul',this)[0];
		
		if (!$('.item',this).size()) return(false);
		
		var myItem = $('.item',this)[0];
		
		//with($($('.item',this)[0])) {myStep = parseInt(height()) + parseInt(css('marginTop')) + parseInt(css('marginBottom'))};
		myStep = parseInt($(myItem).height()) + (!isNaN($(myItem).css('marginTop')) ? parseInt($(myItem).css('marginTop')) : 0) + (!isNaN($(myItem).css('marginBottom')) ? parseInt($(myItem).css('marginBottom')) : 0);
		
		bigAllSize = myStep * parseInt($('.item',this).size());
		
		
		if (!$('.hslider',this).size()) return(false);
		bigSize = $($('.hslider',this)[0]).height();
		maxTop = bigAllSize - bigSize;
		maxNum = $('.item',this).size() - parseInt(bigSize/myStep);
		//alert(bigSize);
		
		$('.buttons img',this).each(function() {
			$(this)
				.css('opacity',0.8)
				.mouseenter(function() {$(this).animate({opacity:1},{duration:800,queue:false});})	
				.mouseleave(function() {$(this).animate({opacity:0.8},{duration:800,queue:false});});
			if ($(this).attr('rel') == 'down') {
				$(this).bind('click',function() {	
					if (currentNum >= maxNum) return(false);				
					$(myUl).stop();
					currentTop = (++currentNum * myStep);
					currentTop = (currentTop > maxTop) ? maxTop : currentTop;					
					$(myUl).animate({marginTop: -currentTop},800);
					
				
					return(false);
				})
			}
			if ($(this).attr('rel') == 'up') {
				
				$(this).bind('click',function() {
					//alert(currentNum + ' и ' + maxNum);
					if (currentNum <= 0) return(false);
					$(myUl).stop();
					currentTop = (--currentNum * myStep);
					currentTop = (currentTop > maxTop) ? maxTop : currentTop;
					$(myUl).animate({marginTop: -currentTop},800);
					return(false);
				})
				.bind('dblclick',function() {return(false)});
			}
		});
		
		
		
		
	});
}


function doOpacity() {
	$('.opacity:has(.fon)').each(function() {
		$(this).mouseenter(function() {
			$('.fon',this).each(function() {
				$(this).animate({opacity:1},{duration:800,queue:false});
			});
		});
		$(this).mouseleave(function() {
			$('.fon',this).each(function() {
				$(this).animate({opacity:0.7},{duration:800,queue:false});
			});
		});
	});
	
	
	$('.opacity').each(function() 
	{
		var myHref = $('a',this).size() ? $($('a',this)[0]).attr('href'):false;
		if (! myHref) return;
		$(this)
		.css('cursor','pointer')
		.bind('click',function() 
		{
			location.href = myHref;
			return(false);
		});
	});
}


function changeFadeMenu() {
	var myUL = false; var myCurrentLevel = false;
	$('ul.fade').each(function() {
		
		var myLeft; var myTop;
		myLeft = $(this).offset().left;
		myTop = $(this).offset().top;
		
		$(this)
			.appendTo('.body')
			.css({
			'position': 'absolute',
			'zIndex' : 100,
			'left' : myLeft,
			'top' : myTop	
			})
			.mouseleave(function() {
				$('.level2',this).each(function() {	$(this).stop();	});				
				$('a.fade-sel',this).each(function() {
					///if (!$(this).attr("id")) {
						$('.level2',this.parentNode).each(function() {$(this).fadeOut()});
						$(this).attr('class','');
					//}
				});
				$('#fadesel',this).each(function() {
					//alert("OK");
					$(this).attr('class','fade-sel');
					/*$('.level2',this.parentNode).each(function() {
							$(this).fadeIn()
					});*/
				});
				
			});

		
		
		myUL = this;
		$('.level1',this).each(function() {
			
		//	if (! $('a.fade-sel',this).size()) 
				$('.level2',this).each(function() {$(this).hide()});
				
			$(this)
			
			.bind('mouseover',function() {
				$('.level2',this).each(function() {$(this).css('opacity',1);});	
			})
			.mouseenter(function() {
				
				var myLevel = this;
				// ������� �� ������� ��� �� ������� ������
				
				
				
				//if($('a.fade-sel',this).size()) return(false);
				
				$('.level2',myUL).each(function() {
					$(this).stop();	
					$(this).fadeOut(1);
				});
				
				
				
				$('a.fade-sel',myUL).each(
					function() {
						var myUrl = this;
						$('.level2',this.parentNode).each(function() {$(this).fadeOut()});	
						$(this).attr('class','');					
					}
				);
				
				// �������� �������� ������
				$('.level2',this).each(function() {
					$(this).fadeIn(function() {});	
				});
				
				$('a:first',myLevel).each(function() {
							$(this).attr('class','fade-sel');	
				});	
				
			});	
			
		});		
	});
}


function changeMenu() {
	$('ul.change').each(function() {
		$('a',this).each(function() {
			$(this).bind('mouseover',
				function() {
					if ($(this).attr('class') != 'sel') {
						$('a',this.parentNode.parentNode).each(function() {$(this).attr('class','')});
						$(this).attr('class','sel')
					} 
				});	
		});
		
		$(this).mouseleave(function() {
			$('a',this).each(function() {$(this).attr('class','')});
			$('a#sel',this).each(function() {$(this).attr('class','sel')});
		});	
	});	
}

function resizeBlocks() {
	
	if ($(window).width() < 1000) $('body').width(1000);
    else $('body').width($(window).width());
	
	var padding=17; var mustWidth = 360; var maxWidth =  955; var minHeight = 400; var maxWidth2 =  500;
	var leftBlock = $('.leftCenterBlock').size() ? 	$('.leftCenterBlock')[0] : false;
	var rightBlock = $('.rightCenterBlock').size() ? $('.rightCenterBlock')[0] : false;
	var rightBlock2 =  $('.smallLeftBlock').size() ? $('.smallLeftBlock')[0] : false;
	var myBlocks = $('.blocks').size() ? $('.blocks')[0] : false;
	
		
	if (!leftBlock ) {
		createBlack();
		return(false);
	}
	
	//if ($(leftBlock).height() < minHeight ) $(leftBlock).height(minHeight);	
	var myNewWidth = $(myBlocks).width() - ((rightBlock) ? mustWidth : 0) - ((rightBlock2) ? $(rightBlock2).width() + 10 : 0) - padding;
    if (myNewWidth < 665 && rightBlock2) myNewWidth = 665;
    
    
    
    if (!rightBlock2 || !rightBlock) maxWidth = 965;
	
	$(leftBlock).width((myNewWidth > maxWidth) ? maxWidth : myNewWidth);
	if (rightBlock) {
		if ($(rightBlock).height() < 660)  $(rightBlock).height(660);
		myNewWidth = $(myBlocks).width() - $(leftBlock).width() - padding;
		myNewWidth = (myNewWidth > maxWidth2) ? maxWidth2 : myNewWidth;
		myNewWidth = (mustWidth > myNewWidth) ? mustWidth : myNewWidth;
		$(rightBlock)
			.css("paddingLeft",padding)
			.width(myNewWidth);	
	}
	
	//Высота блока
	
	
	$('.clientProfile').each(function() {
		$(this).css('height','auto');
			if ($(this).height() < 605) {
				$(this).css('height',605);
				$(leftBlock).css('height',605);
			}
	});
	
	
	
	createBlack();
}


function createBlack() {
	$('.fon').each(function() {
		$(this)
			.css({
			'background' : 'black',
			'opacity' : 0.7,
			'position' : 'absolute',
			'top' : 0,
			'left' : 0,
			'width':$(this.parentNode).width(),
			'height':$(this.parentNode).height()
			});	
	});	
}


function checkLoadMore() {
	if (! $('.updateBlock:has(a)').size()) return(false);
	var myBlock = $('.updateBlock')[0];
	var myHref  = $($('.updateBlock a')[0]).attr('href');
	
	var scrollTop = $(window).height() + $(document).scrollTop();
	if ($(myBlock).offset().top  > scrollTop) return(false); 
	
	
	var myHref  = $($('.updateBlock a')[0]).attr('href');
	var myCount = parseInt($($('.updateBlock a')[0]).attr('rel'));
	$(myBlock).html('<div class="notFound"><span>Загрузка...</span></div>')
	$.post(myHref+"?count="+myCount,{'load':'true'},function(myContent) {
		if (myContent) {
			$('ul',myBlock.parentNode).append(myContent);
			createBlack();
			doOpacity();
			myBlock.innerHTML = '<a rel="'+(myCount+6)+'" href="'+myHref+'" title="Еще"><span>Еще</span></a>';
		}
		else myBlock.innerHTML = '&nbsp;';
	});
	
}

