$(document).ready(function(){
	$("#Navigation ul li").hover(function(){
		$(this).children('ul').show();
	},function(){
		$(this).children('ul').hide();
	})
})

$.fn.iotaRealMoving = function(){

	RM = new Object();
	RT = new Object();
	
	RM.w = $("#MainPhoto").width();
	RM.h = $("#MainPhoto").height();
	RM.iw = $("#MainPhoto .photo img").width();
	RM.ih = $("#MainPhoto .photo img").height();
	
	RM.ow =  RM.w -RM.iw;
	RM.oh =  RM.h - RM.ih;

	RM.dw = ($("#MainPhoto").offset().left);
	RM.dh = ($("#MainPhoto").offset().top);
	
	RT.w = parseFloat($('#MainPhoto .text-overlay').css('left'));
	RT.h = parseFloat($('#MainPhoto .text-overlay').css('top'));

	RT.iw = parseFloat($('#MainPhoto .text-overlay').css('width'));
	RT.ih = parseFloat($('#MainPhoto .text-overlay').css('height'));

	RT.w2 = parseFloat($('#MainPhoto .overlay2').css('left'));
	RT.h2 = parseFloat($('#MainPhoto .overlay2').css('top'));
	
	RT.iw2 = parseFloat($('#MainPhoto .overlay2').css('width'));
	RT.ih2 = parseFloat($('#MainPhoto .overlay2').css('height'));
	
//	setTimeLimit(changePhoto(PhotosArr[1]);
	photoIndexCount = 0;
	myInterval = setInterval(function() {
		photoIndexCount = ++photoIndexCount<4?photoIndexCount:0
		changePhoto(PhotosArr[photoIndexCount]);
	}, 3500);

	$('html').mousemove(function(event){
		
		$(window).width();
		$(window).height();
		
		RM.mw = event.pageX;
		RM.mh = event.pageY;
		
		RM.cw = (RM.mw * RM.ow)/$(window).width();
		RM.ch = (RM.mh * RM.oh)/$("body").attr("scrollHeight");

		RT.cw = RT.w-((RM.mw-RM.dw) * RT.iw)/($(window).width()*5);
		RT.ch = RT.h+((RM.mh-RM.dw) * RT.ih)/($(window).height()*5);
		
		RT.cw2 = RT.w2-((RM.mw-RM.dw) * RT.iw2)/($(window).width()*4);
		RT.ch2 = RT.h2+((RM.mh-RM.dw) * RT.ih2)/($(window).height()*4);
		
//		RM.ch = 
		$('#MainPhoto .photo').css({
			top: RM.ch+'px',
			left: RM.cw+'px'
		});
		
//		$("#TEXTIN").html((RT.cw-540))
		
		$('#MainPhoto .text-overlay').css({
			top: RT.ch+'px',
			left: RT.cw+'px',
			'webkit-transform': 'rotateY('+(RT.cw-540)+'deg)'
		});

		$('#MainPhoto .overlay2').css({
			top: RT.ch2+'px',
			left: RT.cw2+'px',
			'webkit-transform': 'rotateY('+(RT.cw-540)+'deg)'
		});
	});
}

$.fn.iotaHover = function(){
	$(this).hover(function(){
		$(this).find(".image img")
			.stop()
			.animate({
				opacity: 0
				},
				800
			);

		$(this).find(".descr")
			.stop()
			.animate({
				top: -70
				},
				300
			);
	},function(){
		$(this).find(".image img")
			.stop()
			.animate({
				opacity: 1
			},
			600
			);
		
		$(this).find(".descr")
			.stop()
			.animate({
				top: 0
			},
			300
			);
	});
}

function changePhoto(photoSrc){
	var ImageGallery = new Image();
	ImageGallery.src = photoSrc;
	$(ImageGallery).load(function(){
		$("#MainPhoto").children('p.photo')
			.css("background","url('"+ImageGallery.src+"') top left no-repeat")
			.find("img")
			.animate({
				opacity: 0
			},
			2000,
			function(){
				$(this)
					.attr("src",ImageGallery.src)
					.attr("width",ImageGallery.width)
					.attr("height",ImageGallery.height)
					.css("opacity",1);
				}
			);
		
	});		
}
