/* EFFECTS*/
$(document).ready(function(){
/* tooltipp */
	$('a[rel=tooltip]').hover(function(e) {
		var tip = $(this).attr('title');	
		$(this).attr('title','');
		$(this).append('<div id="tooltip"><div class="tipBody">' + tip + '</div></div>');		
		$('#tooltip').fadeIn('500');
	}).mousemove(function(e) {
		var parentpos = $(this).offset();
		$('#tooltip').css('top', e.pageY - parentpos.top + 18);
		$('#tooltip').css('left', e.pageX - parentpos.left - 45);
	}).mouseleave(function() {
		$(this).attr('title',$('.tipBody').html());
		$(this).children('div#tooltip').remove();
	});
/* reset */
    $('body').fadeIn(1000);
    $('#header').fadeIn(1500);
    $('img.a').css({"opacity": "0.25"}, 0);
    $('img.b').css({"opacity": "0"}, 0);
    $('.referenceInfo').css({ "top": "180px", "opacity": "0" }, 0);
    $('.big .referenceInfo').css({ "top": "380px", "opacity": "0" }, 0);
    $('.huContent').css({"opacity": "0"}, 0);
/* references */
  $('.reference').hover(function(){
    $('img.a',this).stop().animate({"opacity":"0"}, "fast");
    $('img.b',this).stop().animate({"opacity":"1"}, "fast");
    $('.referenceInfo',this).css({"top":"180px", "width":"320px"});
    $('.referenceInfo',this).stop().animate({"top":"220px", "opacity":"1"},{queue:false,duration:200});
	}, function() {
    $('img.a',this).stop().animate({"opacity":"0.25"}, "slow");
    $('img.b',this).stop().animate({"opacity":"0"}, "slow");
    $('.referenceInfo',this).stop().animate({"width":"0px", "opacity":"0"},{queue:false,duration:200});
	});
	$('.big').hover(function(){
    $('.referenceInfo',this).css({"width":"640px"});
    $('.referenceInfo',this).animate({"top":"440px", "opacity":"1"},{queue:false,duration:200});
	}, function() {
    $('.referenceInfo',this).stop().animate({"width":"0px", "opacity":"0"},{queue:false,duration:200});
	});
	$('.wide').hover(function(){
    $('.referenceInfo',this).css({"width":"640px"});
	});
/* logo */
	$('#logo').hover(function(){
		$('span',this).animate({backgroundPosition:'0px -50px'},{queue:false,duration:200});
		$('span>span',this).animate({backgroundPosition:'-150px 0px'},{queue:false,duration:200});
	}, function() {
        $('span',this).animate({backgroundPosition:'0px 0px'},{queue:false,duration:200});
		$('span>span',this).animate({backgroundPosition:'-150px -50px'},{queue:false,duration:200});
	});
/* menu */
	$('a.mW').hover(function(){
		$(this).animate({backgroundPosition:'0px -50px'},{queue:false,duration:200});
	}, function() {
            $(this).animate({backgroundPosition:'0px 0px'},{queue:false,duration:200});
	});
	$('a.mA').hover(function(){
		$(this).animate({backgroundPosition:'-95px -50px'},{queue:false,duration:200});
	}, function() {
            $(this).animate({backgroundPosition:'-95px 0px'},{queue:false,duration:200});
	});
	$('a.mC').hover(function(){
		$(this).animate({backgroundPosition:'-198px -50px'},{queue:false,duration:200});
	}, function() {
			$(this).animate({backgroundPosition:'-198px 0px'},{queue:false,duration:200});
	});
/* home menu */
	$('a.hW').hover(function(){
		$(this).animate({backgroundPosition:'-0px -70px'},{queue:false,duration:200});
	}, function() {
			$(this).animate({backgroundPosition:'-0px 0px'},{queue:false,duration:200});
	});
	$('a.hA').hover(function(){
		$(this).animate({backgroundPosition:'-192px -70px'},{queue:false,duration:200});
	}, function() {
			$(this).animate({backgroundPosition:'-192px 0px'},{queue:false,duration:200});
	});
	$('a.hC').hover(function(){
		$(this).animate({backgroundPosition:'-403px -70px'},{queue:false,duration:200});
	}, function() {
			$(this).animate({backgroundPosition:'-403px 0px'},{queue:false,duration:200});
	});
/* aboutTextEnToHu */
	$('.aboutText').hover(function(){
		$('.huContent',this).stop().animate({"opacity": "1"}, "fast");
		$('.enContent',this).stop().animate({"opacity": "0"}, "fast");
	}, function() {
            $('.huContent',this).stop().animate({"opacity": "0"}, "fast");
            $('.enContent',this).stop().animate({"opacity": "1"}, "fast");
	});
	$('#aboutBelieve').hover(function(){
		$('h3',this).animate({backgroundPosition:'0px -50px'},{queue:false,duration:200});
	}, function() {
			$('h3',this).animate({backgroundPosition:'0px 0px'},{queue:false,duration:200});
	});
	$('#aboutUnderstand').hover(function(){
		$('h3',this).animate({backgroundPosition:'-235px -50px'},{queue:false,duration:200});
	}, function() {
			$('h3',this).animate({backgroundPosition:'-235px 0px'},{queue:false,duration:200});
	});
	$('#aboutConsider').hover(function(){
		$('h3',this).animate({backgroundPosition:'-560px -50px'},{queue:false,duration:200});
	}, function() {
			$('h3',this).animate({backgroundPosition:'-560px 0px'},{queue:false,duration:200});
	});
  
	$('#whatWeDo a').hover(function(){
		$('#whatWeDoOff').stop().animate({"opacity": "0"}, "slow");
	}, function() {
  		$('#whatWeDoOff').stop().animate({"opacity": "1"}, "slow");
	});
  
// parallax
	var $window = $(window);
	var $homeBG = $('#home');
	var $workBG = $('#work');
	var $aboutBG = $('#about');
	var $contactBG = $('#contact');
	var chameleon = $("#home .chameleon");
	var bubbles = $("#home .bubbles");
	var bodyBg = $("body");
	var aboutSmokeSmall = $("#about .smokes");
	var aboutSmokeBig = $("#about .smokel");
	var contactNoise = $("#contact .noise");
	var dog = $('.dog');
	var windowHeight = $window.height(); //get the height of the window
//apply the class "inview" to a section that is in the viewport
	$('#home, #work, #about, #contact').bind('inview', function (event, visible) {
			if (visible == true) {
                $(this).addClass("inview");
			} else {
                $(this).removeClass("inview");
			}
		});
//function that places the navigation in the center of the window
	function RepositionNav(){
		var windowHeight = $window.height(); //get the height of the window
		var navHeight = $('.akarmi').height() / 2;
		var windowCenter = (windowHeight / 2); 
		var newtop = windowCenter - navHeight;
		$('.akarmi').css({"top": newtop}); //set the new top position of the navigation list
	}
//function that is called for every pixel the user scrolls. Determines the position of the background
	/*arguments: 
		x = horizontal position of background
		windowHeight = height of the viewport
		pos = position of the scrollbar
		adjuster = adjust the position of the background
		inertia = how fast the background moves in relation to scrolling
	*/
	function newPos(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	function newPosBottom(x, maxPos, pos, maxTop, inertia){
		return x + "% " + (((maxPos - pos) * inertia)+maxTop)  + "px";
	}
//function to be called whenever the window is scrolled or resized
	function Move(){ 
		var pos = $window.scrollTop(); //position of the scrollbar
		windowHeight = $window.height();
		var bodyHeight = $('body').height();
		if($homeBG.hasClass("inview")){
      $homeBG.animate({'backgroundPosition': newPos(50, 0, pos, 0, 0.1)},{queue:false,duration:300});
      chameleon.animate({'backgroundPosition': newPos(50, 0, pos, 0, 0.3)},{queue:false,duration:300});
      bubbles.animate({'backgroundPosition': newPos(50, 0, pos, 0, 1.2)},{queue:false,duration:200});
      $('#menu').addClass('inAnimation').animate({"left": "0", "width": "450px"},{queue:false,duration:300,complete:function(){$(this).removeClass('inAnimation')}});
		} else {
      $('#menu').addClass('inAnimation').animate({"left": "450px", "width": "510px"},{queue:false,duration:300,complete:function(){$(this).removeClass('inAnimation')}});
        }
		if($workBG.hasClass("inview")){
      bodyBg.animate({'backgroundPosition': newPos(50, 0, pos, 0, -0.6)},{queue:false,duration:400});
		}
		if($aboutBG.hasClass("inview")){
      aboutSmokeSmall.animate({'backgroundPosition': newPos(50, 0, pos, 0, -0.6)},{queue:false,duration:200});
      aboutSmokeBig.animate({'backgroundPosition': newPos(50, 0, pos, 0, -0.2)},{queue:false,duration:800});
			$('#menu').animate({backgroundPosition:'0px -50px'},{queue:false,duration:50});
		} else { 
			$('#menu').animate({backgroundPosition:'0px 0px'},{queue:false,duration:50});
        }
		if($contactBG.hasClass("inview")){
			var maxDogBottom = bodyHeight-windowHeight;
			var maxDogBottomTop = windowHeight - 700;
			dog.animate({'backgroundPosition': newPosBottom(50, maxDogBottom, pos, maxDogBottomTop, 0.5)},{queue:false,duration:200});
      contactNoise.animate({'backgroundPosition': newPos(50, 0, pos, 0, 10.0)},{queue:false,duration:1000});
		}
  }

	RepositionNav(); //Reposition the Navigation to center it in the window when the script loads
	$window.resize(function(){ //if the user resizes the window...
		Move(); //move the background images in relation to the movement of the scrollbar
		RepositionNav(); //reposition the navigation list so it remains vertically central
	});		
	$window.bind('scroll', function(){ //when the user is scrolling...
		Move(); //move the background images in relation to the movement of the scrollbar
	});
});

/* SCROLL */
$(function(){
    $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1000);
                return false;
            }
        }
    });
});

/* BACKGROUND POSITION ANIM */
(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			start = toArray(start);
			fx.start = [start[0],start[2]];
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
	};
})(jQuery);

/*FORM CHECKING & AJAX */
function formCheck() {
	var retfalse = false;
	// név ellenőrzése
	if ($('#name').val() == "") {
		$('#nameError').fadeIn('normal');
		retfalse = true;
	} else {
		$('#nameError').hide();
	}
	// email ellenőrzése
	if ($('#email').val() == "" || !$('#email').val().match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
		$('#mailError').fadeIn('normal');
		retfalse = true;
	} else {
		$('#mailError').hide();
	}
	// üzenet ellenőrzése
	if ($('#message').val() == "") {
		$('#messageError').fadeIn('normal');
		retfalse = true;
	} else {
		$('#messageError').hide();
	}
	if (retfalse) return false;
		$('#emailSent').fadeIn('normal');
	$.post(
			"sender.php",
			{sendMessage: 'sendMessage' , name : $('#name').val() , email : $('#email').val() , message : $('#message').val()},
			function (data) {
				if (data.status == "ok") {
                    $('#name').val("");
                    $('#email').val("");
                    $('#message').val("");
                    $('#emailSent').delay(4000).fadeOut('normal');
				}
			},
			"json"
	);
	return false;
}
