﻿$(document).ready(init);
$(window).bind('resize', onResize);

function init() {
	onResize();
	
	//main Page Animation
	animInit();
	
	initPhotoVideo();
	initArticlesList();
	initBlockSlider();
	
	initInputFields();
	
	initPageSubmenu();
	
	initClientsList();
	
	initSiteMap();
	
	initPopupPhoto();
	
	initLightBox();
	
	$('.left-menu li p').click(function(){
		$(this).parents('li').children('ul').toggle();
	});
	$('.left-menu li ul span').parents('ul').show();
	
	$('.block .block-submit').click(function(){
		$(this).parents('form').submit();
		return false;
	});
	
	/*!!!test main page skins *
	if ($('body').hasClass('skin01')) {
		var skin = 1;
		$('body').click(function(){
			$('body').removeClass('skin0'+skin);
			skin++;
			if (skin == 4) skin = 1;
			$('body').addClass('skin0'+skin);
			animResize();
			var cLeft = parseInt($('.anim').width()-10)/2;
			if ($('.anim-center-hover').length > 0) $('.anim-center-hover').css({left: cLeft+'px'});
		});
	}
	/**/
}

function onResize() {
	animResize();
	resizeArticlesList();
}

function resizeArticlesList() {
	if ($('.block-articles-list').length > 0 && $('.block-articles-right').width() < 632) {
		$($('.block-articles-list li').get(3)).hide();
	} else {
		$($('.block-articles-list li').get(3)).show();
	}
}

//var animSpeed1 = 1000, animSpeed2 = 1000, animLeft = false;
var animSpeed1 = 500, animSpeed2 = 500, animLeft = false;

function animInit() {
	/*if ($.browser.safari) animSpeed2 = 1020;
	if ($.browser.opera) animSpeed2 = 1080;
	
	if ($('body').hasClass('skin02')) {
		animSpeed1 = animSpeed1+1000;
		animSpeed2 = animSpeed2+1000;
	}*/
	if ($.browser.safari) animSpeed2 = 515;
	if ($.browser.opera) animSpeed2 = 540;
	
	//var cLeft = parseInt($('.anim').width()-10)/2;
	$('.anim')
		.append($('<div>').addClass('anim-left-hover'))
		.append($('<div>').addClass('anim-right-hover'))
		//.append($('<div>').addClass('anim-center-hover'))
		;
	
	//$('.anim-left-hover').hover(animHover, animOut);
	//$('.anim-right-hover').hover(animHover, animOut);
	//$('.anim-center-hover').hover(animRestore, false);
	$('.anim').mousemove(animMove);
	//$('.anim').hover(false, animRestore);
	$('.header').hover(animRestore, false);
	$('.footer').hover(animRestore, false);
	$('.container').hover(false, animRestore);
}

function animResize() {
	if ($('.anim').length > 0) {
		/*var h = $(window).height();
		var minH = parseInt($('.anim').css('minHeight')) + $('.footer').height()*2;
		if (h < minH) h = minH;
		$('.anim').css({height: h+'px'});*/
		
		var winH = $(window).height();
		var animH = $('.anim').height();
		var minH = animH;// + 100;
		if (winH < minH) winH = minH;
		var pad = parseInt((winH - animH)/2);
		$('.anim-container').css({height: (winH-pad)+'px', paddingTop: pad+'px'});
	}
}
/*function animHover(e) {
	if (e.target.className.indexOf('left') != -1) {
		$('.anim-left').stop().css({zIndex: 3}).animate({width: '100%'}, animSpeed1);
		$('.anim-right').stop().css({zIndex: 2}).animate({width: '0'}, animSpeed2);
		animLeft = true;
	} else if (e.target.className.indexOf('right') != -1) {
		$('.anim-right').stop().css({zIndex: 3}).animate({width: '100%'}, animSpeed1);
		$('.anim-left').stop().css({zIndex: 2}).animate({width: '0'}, animSpeed2);
		animLeft = false;
	}
	//e.stopPropagation();
}*/
var animFlag = true, animCanMove = false;
function animMove(e) {
	
	var w = $('.anim').width();
	var x = parseInt( e.pageX - (($('.container').width() - w) / 2) );
	
	animLeft = (x < w/2);
	
	if (animFlag) {
		if (animLeft) {
			$('.anim-left').stop().css({zIndex: 3}).animate({width: (x)+'px'}, animSpeed1, function(){animCanMove = true;});
			$('.anim-right').stop().css({zIndex: 2}).animate({width: (w-x)+'px'}, animSpeed2, function(){animCanMove = true;});
		} else {
			$('.anim-right').stop().css({zIndex: 3}).animate({width: (w-x)+'px'}, animSpeed1, function(){animCanMove = true;});
			$('.anim-left').stop().css({zIndex: 2}).animate({width: (x)+'px'}, animSpeed2, function(){animCanMove = true;});
		}
		animFlag = false;
	} else if (animCanMove) {
		/*//shark head!
		if ($('body').hasClass('skin02') && x > w-280) {
			$('.anim-left').css({width: (w)+'px'});
			//if ($.browser.msie) $('.anim-right').hide(); //.css({width: '0px'});
			//else $('.anim-right').fadeOut(); //.css({width: '0px'});
			$('.anim-right').hide();
		} else {*/
			$('.anim-left').css({width: (x)+'px'});
			$('.anim-right').show().css({width: (w-x)+'px'});
		//}
	}
	
	//e.stopPropagation();
}
/**/
function animOut(e) {
	/*if (e.target.className.indexOf('left') != -1) {
		$('.anim-right').stop().css({zIndex: 3}).animate({width: '50%'}, animSpeed1);
		$('.anim-left').stop().css({zIndex: 2}).animate({width: '50%'}, animSpeed2);
	} else if (e.target.className.indexOf('right') != -1) {
		$('.anim-left').stop().css({zIndex: 3}).animate({width: '50%'}, animSpeed1);
		$('.anim-right').stop().css({zIndex: 2}).animate({width: '50%'}, animSpeed2);
	}*/
	//e.stopPropagation();
}
function animRestore(e) {
	if (animLeft) {
		$('.anim-right').stop().css({zIndex: 3}).animate({width: '50%'}, animSpeed1);
		$('.anim-left').stop().css({zIndex: 2}).animate({width: '50%'}, animSpeed2);
	} else {
		$('.anim-left').stop().css({zIndex: 3}).animate({width: '50%'}, animSpeed1);
		$('.anim-right').stop().css({zIndex: 2}).animate({width: '50%'}, animSpeed2);
	}
	//$('.anim-left').stop().css({zIndex: 2}).animate({width: '50%'}, 500);
	//$('.anim-right').stop().css({zIndex: 3}).animate({width: '50%'}, 510);
	e.stopPropagation();
	animFlag = true;
	animCanMove = false;
}



function initLightBox() {
	if ($('.photo').length > 0) $('.photo a').lightBox();
}


function initPhotoVideo() {
	$('.block-photo-list li').hover(onBlockPhotoListHover, onBlockPhotoListOut);
	//$('.block-video-list li').hover(onBlockVideoListHover, onBlockVideoListOut);
	
	$('.block-photo-list li').click(onBlockPhotoClick);
	$('.block-video-list li').click(onBlockVideoClick);
	
	if ($('.block-video-list').length > 0) $('.block-video-list li:first').click();
}

var popupPhotoCurr = 0, popupPhotoTotal = 0;
function initPopupPhoto() {
	if ($('.popup-photo').length > 0) {
		$('.popup-photo-close').click(function() {
			$('.photo-gallery').css({height: 'auto'});
			$('.popup-photo').hide();
		});
		$('.popup-photo-prev').click(function() {
			if (popupPhotoCurr > 0) {
				popupPhotoCurr--;
				$($('.block-photo-list li').get(popupPhotoCurr)).click();
			}
			checkPopupPhotoArrows();
			return false;
		});
		$('.popup-photo-next').click(function() {
			if (popupPhotoCurr < popupPhotoTotal-1) {
				popupPhotoCurr++;
				$($('.block-photo-list li').get(popupPhotoCurr)).click();
			}
			checkPopupPhotoArrows();
			return false;
		});
		popupPhotoTotal = $('.photo-gallery ul li').length;
		checkPopupPhotoArrows();
	}
}

function checkPopupPhotoArrows() {
	if (popupPhotoCurr == 0) $('.popup-photo-prev').hide(); else $('.popup-photo-prev').show();
	if (popupPhotoCurr == popupPhotoTotal-1) $('.popup-photo-next').hide(); else $('.popup-photo-next').show();
	
	$('.photo-gallery').css({height: 'auto'});
	if (($('.popup-photo img').height()+100) > $('.photo-gallery').height()) $('.photo-gallery').css({height: ($('.popup-photo img').height()+100)+'px'});
	//else $('.photo-gallery').css({height: 'auto'});
}

function onBlockPhotoListHover() {
	if (!$(this).hasClass('more')) {
		$('.block-photo-list li').css({zIndex: 4});
		$(this).css({zIndex: 5});
		var hover = $('.block-photo-list .hover');
		hover.empty();
		hover.append($(this).find('img').clone().css({opacity: 1}));
		var title = $(this).find('a').attr('title');
		if (title != '') {
			hover.append($('<p>').html(title));
		}
		$(this).append(hover.show());
	}
}
function onBlockPhotoListOut() {
	$('.block-photo-list .hover').hide();
}

/*function onBlockVideoListHover() {
	if ($(this).parents('.video-gallery').length > 0) {
		if (!$(this).hasClass('more')) {
			$('.block-video-list li').css({zIndex: 4});
			$(this).css({zIndex: 5});
			var hover = $('.block-video-list .hover');
			hover.empty();
			hover.append($(this).find('img').clone().css({opacity: 1}));
			hover.append($('<i>'));
			var title = $(this).find('a').attr('title');
			if (title != '') {
				hover.append($('<p>').html(title));
			}
			$(this).append(hover.show());
		}
	}
}
function onBlockVideoListOut() {
	if ($(this).parents('.video-gallery').length > 0) {
		$('.block-photo-list .hover').hide();
	}
}*/

var newPhotoSrc;
function onBlockPhotoClick() {
	if ($(this).parents('.photo-gallery').length > 0) {
		popupPhotoCurr = $(this).index();
		$('.popup-photo img').attr({src: $(this).find('a').attr('href')});
		newPhotoSrc = $(this).find('a').attr('href');
		im = new Image();
		im.onload = function() {
			if (newPhotoSrc == this.src) {
				$('.photo-gallery').css({height: 'auto'});
				if ((this.height+100) > $('.photo-gallery').height()) $('.photo-gallery').css({height: (this.height+100)+'px'});
			}
		}
		im.src = newPhotoSrc;
		$('.popup-photo-text p').html($(this).find('a').attr('title'));
		$('.popup-photo .popup-photo-total').html(popupPhotoTotal);
		$('.popup-photo .popup-photo-curr').html(popupPhotoCurr+1);
		$('.popup-photo').show();
		checkPopupPhotoArrows();
	} else {
		if (!$(this).hasClass('more')) {
			$('.block-photo-img .block-photo-text p').html($(this).find('a').attr('title'));
			if ($(this).find('a').attr('title') == '') $('.block-photo-img .block-photo-text').css({display: 'none'});
			else $('.block-photo-img .block-photo-text').css({display: 'block'});
			$('.block-photo-img img').attr({src: $(this).find('a').attr('href')});
			$('.block-photo-list li').removeClass('c');
			$(this).addClass('c');
		}
	}
}

function onBlockVideoClick() {
	if ($(this).parents('.video-gallery').length > 0) {
		//$('.video-player img').attr({src: $(this).find('a').attr('href')});
		$('#video-player').empty();
		var flashvars = {"comment":"", "m":"video", "file":$(this).find('a').attr('href')};
		var params = {bgcolor:"#ffffff",  allowFullScreen:"true", allowScriptAccess:"always",id:"video-player"}; 
		new swfobject.embedSWF(BASE_URL+"uppod.swf", "video-player", "600", "400", "9.0.115.0", false, flashvars, params);
		if (!$(this).hasClass('more')) {
			$('.block-video-list li').css({zIndex: 4});
			$(this).css({zIndex: 5});
			var hover = $('.block-video-list .hover');
			hover.empty();
			hover.append($(this).find('img').clone().css({opacity: 1}));
			hover.append($('<i>'));
			var title = $(this).find('a').attr('title');
			if (title != '') {
				hover.append($('<p>').html(title));
			}
			$(this).append(hover.show());
		}
		return false;
	} else {
		if (!$(this).hasClass('more')) {
			//$('.block-video-player img').attr({src: $(this).find('a').attr('href')});
			$('#video-player').empty();
			var flashvars = {"comment":"", "m":"video", "file":$(this).find('a').attr('href')};
			var params = {bgcolor:"#ffffff",  allowFullScreen:"true", allowScriptAccess:"always",id:"video-player"}; 
			new swfobject.embedSWF(BASE_URL+"uppod.swf", "video-player", "600", "400", "9.0.115.0", false, flashvars, params);
			$('.block-video-list li').removeClass('c');
			$(this).addClass('c');
			
			return false;
		}
	}
}

function initArticlesList() {
	$('.block-articles-list li').hover(onBlockArticlesItemHover, onBlockArticlesItemOut);
}

function onBlockArticlesItemHover() {
	$(this).addClass('block-articles-hover');
}
function onBlockArticlesItemOut() {
	$(this).removeClass('block-articles-hover');
}


function initBlockSlider() {
	if ($('.block-slider #slider').length > 0) {
		$('#slider').slider({
			value:	0,
			min:	0,
			max:	$('.block-bulletin-list').width() - $('.block-bulletin-container').width(),
			step:	5,
			slide: function(event, ui) {
   				 //$('.items img').css({width: ui.value+'px', height: ui.value+'px'});
				 $('.block-bulletin-list').css({marginLeft: '-'+ui.value+'px'});
  			}
		});
	}
}

function initPageSubmenu() {
	if ($('.page-category').length > 0) {
		$('.page-category h4').click(function() {
			$(this).parents('.page-category').toggleClass('page-category-opened');
			return false;
		});
	}
}

function initClientsList() {
	if ($('.clients-category').length > 0) {
		$('.clients-category h4').click(function() {
			$(this).parents('.clients-category').toggleClass('clients-category-opened');
			return false;
		});
	}
}

function initSiteMap() {
	$('.sitemap-link').click(onClickSiteMapLink);
	$('.popup-close').click(function() {
		$('.popup-win').hide();
		$('.popup-close').hide();
		$('.popup-bg').hide();
	});
}
function onClickSiteMapLink() {
	$('.popup-bg').css({opacity: 0.7}).show();
	$('.popup-close').show();
	$('.sitemap').show();
	return false;
}


function initInputFields() {
	inputHelper($('#site-search'), inputSiteSearch);
	//inputHelper($('#press-search'), 'Поиск в пресс-центре');
	inputHelper($('#block-login'), inputBlockLogin);
	inputHelper($('#block-password'), inputBlockPassword);
}

function inputIn(obj, text) {
	if ($(obj).val() == text) {
		if (text != 'http://') $(obj).val('');
		$(obj).removeClass('empty');
		var but = '#'+$(obj).attr('id')+'-button';
		if ($(but).length > 0) $(but).attr('disabled', false);
   }
}
function inputUp(obj, text) {
	var but = '#'+$(obj).attr('id')+'-button';
	if ($(but).length > 0) $(but).attr('disabled', ($(obj).val() == '' || $(obj).val() == text));
}
function inputOut (obj, text) {
	if ($(obj).val() == '' || $(obj).val() == text) {
		$(obj).val(text).addClass('empty');
		var but = '#'+$(obj).attr('id')+'-button';
		if ($(but).length > 0) $(but).attr('disabled', true);
	}
}	
function inputHelper(obj, text) {
	$(obj)
		.bind ('focus', function () {
			inputIn (this, text);
		})
		.bind ('keyup', function () {
			inputUp (this, text);
		})
		.bind ('blur', function () {
			inputOut (this, text);
		});
	inputOut(obj, text);
}
