$(function(){
	$('ul.mainmenu li').hover(
		function(){
			$('ul.plates li').removeClass('hover');
			$('ul.plates li.'+$(this).attr('class')).addClass('hover');
		},
		function(){
			$('ul.plates li.'+$(this).attr('class')).removeClass('hover');
		}
	);

	$("a.group").fancybox({'cyclic':true});
	$("a.vgroup").fancybox({'cyclic':true,'showNavArrows':false});
	menuAnim();
	startPhotos();
//	startVideos();
	/*** search checkboxes **/
    $('#modules_all').click(function(){
        $('.search-modules input').attr('checked',$('#modules_all').attr('checked'));
    });
    
    $('.search-modules input').click(function(){
        if ($(this).attr('id') != 'modules_all')
        {
            if ($(this).attr('checked') == false)
            {
                $('#modules_all').attr('checked',false);
            }
        } 
    });
});

/*******************************************************************\
****************************   SNOW   *******************************
\*******************************************************************/

function startSnow(){
	for (var i = 0; i < 8; i++){
		addSnowflake(i);
	}
	for (var i = 0; i < 4; i++){
		addSnowflake2(i);
	}
	for (var i = 0; i < 20; i++){
		addSnowflake3(i);
	}
}

function addSnowflake(number){
	setTimeout(
		function(){
			$('div.main').prepend('<img class="snow-diagonal" id="snow'+number+'" src="/contents/templates/default/images/snow1.png" />');
			animateSnowflake('snow'+number,2500);
		},
		500*number
	);
}

function addSnowflake2(number){
	setTimeout(
		function(){
			$('div.main').prepend('<img class="snow-diagonal" id="snow2_'+number+'" src="/contents/templates/default/images/snow2.png" />');
			animateSnowflake('snow2_'+number,1500);
		},
		500*number
	);
}

function addSnowflake3(number){
	setTimeout(
		function(){
			$('div.main').prepend('<img class="snow-diagonal" id="snow3_'+number+'" src="/contents/templates/default/images/snow3.png" />');
			animateSnowflake('snow3_'+number,4000);
		},
		500*number
	);
}

function animateSnowflake(objectID,speed){
	$('#'+objectID).css({'top': '-30px', 'left': Math.random()*($('body').width()-50)+'px'});
	$('#'+objectID).animate(
		{'top': parseInt($('#'+objectID).css('top'))-(-500)+'px', 'left': parseInt($('#'+objectID).css('left'))-300+'px'},
		speed-Math.round(Math.random()*speed/2),
		'linear',
		function(){
			animateSnowflake(objectID,speed);
		}
	);
}

function animateSnowflake2(objectID,speed){
	$('#'+objectID).css({'top': '-30px', 'left': Math.random()*($('body').width()-50)+'px'});
	$('#'+objectID).animate(
		{'top': parseInt($('#'+objectID).css('top'))-(-500)+'px', 'left': parseInt($('#'+objectID).css('left'))-300+'px'},
		speed-Math.round(Math.random()*speed/2),
		'linear',
		function(){
			animateSnowflake2(objectID,speed);
		}
	);
}

/*******************************************************************\
*************************   HEADER ANIM   ***************************
\*******************************************************************/

function menuAnim(){
	var mmWidth = $('.mainmenu').width();
	var bodyWidth = $('.main').width();
	var bgStart = $('.main').css('background-position').split(' ');
	bgStart = parseInt(bgStart[0]);
	$('.plates,.mainmenu').css({'margin-left': Math.round((bodyWidth - mmWidth)/2)+'px', 'width': mmWidth+'px'});
	$('.main').mousemove(function(e){
		var xx = e.pageX - document.getElementsByClassName('main')[0].offsetLeft;
		var mmLeft = - mmWidth*(1 - xx/bodyWidth) + bodyWidth - xx;
		$('.plates,.mainmenu').css({'margin-left': Math.round(mmLeft)+'px'});
		$('.branches').css({'background-position': Math.round((mmLeft*2-400))+'px'});
		$('.main').css({'background-position': Math.round((mmLeft*0.15+bgStart))+'px 0px'});
		$('.tree').css({'margin-left': Math.round((mmLeft*0.07))+'px'});
	});
}

function startPhotos(){
	var currentImage = 4;
	var oldImage = 1;
	setInterval(function(){
		if (currentImage == 1){
			oldImage = 1;
			$('#rp0 img,#rp1 img,#rp2 img,#rp3 img,#rv0 img,#rv1 img,#rv2 img,#rv3 img').animate(
				{'opacity': 'show'},
				2000,
				'swing',
				function(){
					currentImage = 4;
				}
			);
		}else{
			oldImage = currentImage;
			$('#rp0 img:nth-child('+currentImage+'),#rp1 img:nth-child('+currentImage+'),#rp2 img:nth-child('+currentImage+'),#rp3 img:nth-child('+currentImage+'),#rv0 img:nth-child('+currentImage+'),#rv1 img:nth-child('+currentImage+'),#rv2 img:nth-child('+currentImage+'),#rv3 img:nth-child('+currentImage+')').animate(
				{'opacity': 'hide'},
				2000,
				'swing',
				function(){
					currentImage = oldImage-1;
					currentImage = currentImage < 1 ? 4 : currentImage;
				}
			);
		}
	},
	5000
	);
}

function startVideos(){
	setInterval(function(){
		$('.videos-last').animate(
			{'opacity': 'hide'},
			2000,
			'swing',
			function() {
				$('.videos-last').load('/'+lang+'/video/ajax-right-block/','',function(){ $('.videos-last').animate({'opacity': 'show'},	2000); });
			}
		);},
		5000
	);
}
