jQuery(function($) {
	

	$('.toggle-plus-signs').hover(
		function() {
			$(this).parent().find('.tooltip-body').fadeIn();
		},
		function() {
			$(this).parent().find('.tooltip-body').fadeOut();
		}
	);
	
	$('body').live('click', function(e) {
		if (!$(e.target).parents('.service-popup').length) {
			$('.service-popup:visible').fadeOut();
		}
	});
	
	jQuery.fn.center = function(loaded) {
	    var objs = this;
	    body_width = parseInt($(window).width());
	    body_height = parseInt($(window).height());
	    
	    objs.each(function() {
	        var obj = $(this)
	        var block_width = parseInt(obj.width());
	        var block_height = parseInt(obj.height());
	        
	        var left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
	        if (body_width < block_width) { left_position = 0 };
	        var top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
	        if (body_height < block_height) { top_position = 0 };
	        
	        if(!loaded) {
	        
	            obj.css({'position': 'absolute'});
	            obj.css({'left': left_position, 'top': top_position});
	            obj.center(!loaded);
	            $(window).bind('resize', function() { 
	                obj.center(!loaded);
	            });
	            $(window).bind('scroll', function() { 
	                obj.center(!loaded);
	            });
	            
	        } else {
	            obj.stop();
	            obj.css({'position': 'absolute'});
	            obj.animate({'left': left_position, 'top': top_position}, 200, 'linear');
	        }
	    });
	}

	$('#client-login li:last, #navigation > ul > li:last, .data .socials li:last').addClass('last');
	
	var servicesSlider, sliderSpeed = 800;
	
	$("#client-login-link a").click(function() {
		var $t = $(this);
		if( $(this).is(".open") )
		{
			$("#client-login").stop().animate({
				marginTop: -53
			}, "slow", function() {
				$t.removeClass("open");
			});
		}
		else
		{
			$("#client-login").stop().animate({
				marginTop: 0
			}, "slow", function() {
				$t.addClass("open");
			});			
		}
		
		return false;		
	}); 
	
	
	$("#navigation li").find("ul").wrap("<div class='dd' />");	
	
	$("#navigation li").each(function() {
		$(this).find(".dd").each(function() {
			var dd = $(this);
			dd.wrapInner("<div class='dd-m' />");
			dd.prepend("<div class='dd-t' />");
			dd.children(".dd-t").append("<span />");
			dd.append("<div class='dd-b' />");
			dd.children(".dd-b").append("<span />");
		});
		
	});
	$("#navigation > ul > li").each(function() {
		$(this).find(".dd:eq(0)").addClass("main-dd").prepend("<div class='dd-arr'><span></span></div>");
	});
	
	$("#navigation li").hover(function() {
		var $dd = $(".dd:eq(0)", this);
		
		if( !$dd.size() ) return;
		
		$("a:eq(0)", this).addClass("hover");
		$dd.stop(true, true).fadeIn();
		
		if( $(this).parents(".dd").size() )
		{
			var newLeft;
			var shellWidth = $(this).parents(".shell").width();
			var shellOffset = $(this).parents(".shell").offset().left;
			var thisOffset = $(this).offset().left;
			var thisWidth = $(this).width();
			var ddWidth = $dd.width();
			
			if( thisOffset + thisWidth + ddWidth < shellOffset + shellWidth )
				newLeft = $(this).width()
			else
				newLeft = -$dd.outerWidth();
			
			$dd.css({
				left: newLeft
			})
		}
		else
		{
			$dd.css({
				left: ( $(this).width() - $dd.width() )/2
			});
		}
	}, function() {
		$("a:eq(0)", this).removeClass("hover");
		$(".dd:eq(0)", this).stop(true, true).fadeOut();
	});
	
	$(".field").focus(function() {
		var $t = $(this);
		
		if( $t.is(".field-mask") )
		{
			$(this).siblings(".field").show().focus();
			$(this).hide();
			return;
		}
		
		if( $t.attr("title") == $t.val() ) $t.val("");
	}).blur(function() {
		var $t = $(this);
		
		if( $t.is(".field-pass") && $t.val() == "" )
		{
			$t.siblings(".field").show();
			$t.hide();
		}
		
		if( $t.val() == "" ) $t.val( $t.attr("title") );
	});
	
	$("#login .arr").click(function() {
		var $t = $(this);
		var speed = "slow";
		
		if( $(this).is(".arr-open") )
		{
			$("#login .arr-open").fadeOut(speed);
			$("#login .arr-close").fadeIn(speed);
			$("#login").animate({
				right: 0
			}, speed);
		}
		else
		{
			$("#login .arr-open").fadeIn(speed);
			$("#login .arr-close").fadeOut(speed);
			$("#login").animate({
				right: -$("#login form").outerWidth()	
			}, speed);
		}
		
		return false;
	});
	
	$(".leaf").each(function() {
		var newAlt = "";
		
		var initLeft = $(this).position().left;
		var initTop = $(this).position().top;
		
		newAlt = "l:"+ initLeft + ";t:" + initTop;
		
		$(this).attr("alt", newAlt);
	});
	
	function hideTooltips(element)
	{
		element.siblings(".leaf").find(".tooltip-body").fadeOut();
		element.siblings(".bubble").find(".data").animate({
			width:0
		}, "fast");
	}
	
	if( $("body").is("#home") )
	{
		var mouse = function(){
			this.x = 0;
			this.y = 0;
		}
		if (!document.all) document.captureEvents(Event.MOUSEMOVE);
			
		
		// Those koeficients set the movement. k1 is global, k2 for the distance
		var k1 = -0.001;
		var k2 = 0.7;
		$(document).mousemove(function getMouseXY(e) {
			return false;
			if (document.all) {
				mouse.x = event.clientX + document.body.scrollLeft;
				mouse.y = event.clientY + document.body.scrollTop;
			} else {  
				mouse.x = e.pageX;
				mouse.y = e.pageY;
			}  
			
			
			if (mouse.x < 0){mouse.x = 0};
			if (mouse.y < 0){mouse.y = 0};
			
			var tree = $("#tree")
			var screenX = tree.offset().left + tree.width()/2;
			var screenY = tree.offset().top + tree.height()/2;
			
			$(".leaf").each(function() {
				var leafPos = $(this).attr("alt");
				leafPos = {
					x: parseInt(leafPos.split(";")[0].split(":")[1]),
					y: parseInt(leafPos.split(";")[1].split(":")[1])
				};
				
				var distanceX = mouse.x - screenX;
				var distanceY = mouse.y - screenY;
				
				var d = Math.pow( Math.abs(leafPos.x + $(this).parent().offset().left - mouse.x) ,2);
				d +=    Math.pow( Math.abs(leafPos.x + $(this).parent().offset().top - mouse.x ) ,2);
				d = Math.sqrt( d );
				d = Math.pow(d, k2);
				
				var ratioX = $(this).offset().left - Math.abs(distanceX);
				var ratioY = $(this).offset().top - Math.abs(distanceY);
				
				$(this).css({
					left: leafPos.x + k1 * distanceX * d,
					top: leafPos.y + k1 * distanceY * d
				});
			});
		
			return true;
		});
		
		$(".tooltip .plus").hover(
			function() {
				$(".tooltip-body", $(this).parent()).fadeToggle();
				hideTooltips($(this).parents(".leaf"));
			},
			function() {
				$(".tooltip-body", $(this).parent()).fadeToggle();
			}
		);
	}
	// EOF Tooltips and so on
	
	// Homepage elements
	(function() {
		if ( $('#tree .leaf').length == 0 ) {
			return;
		};
		
		var treeOffset = $('#tree .shell').offset(),
			accRatio = 0.06;
		$(window).resize(function() {
			treeOffset = $('#tree .shell').offset();
		});
		
		var elements = $('#tree .shell > .leaf');
		elements.each(function() {
			var th = $(this);
			var objOrigPos = {t: parseInt(th.css('top')), l: parseInt(th.css('left')), d: th.attr('data-speed')};
			th.data('objOrigPos', objOrigPos)
		});
		$(document).mousemove(function(e) {
			var diffOffset = {t: e.pageY - treeOffset.top, l: e.pageX - treeOffset.left};
			elements.each(function() {
				var origPos = $(this).data('objOrigPos'),
					diffY = (origPos.t-diffOffset.t)*accRatio,
					diffX = (origPos.l-diffOffset.l)*accRatio;
				$(this).css({top: origPos.t + origPos.d*diffY, left: origPos.l + origPos.d*diffX})
				
			});
		});
		
	})();
	// End Homepage elements
	
	function createSlider($s) {
		$s.jcarousel({
			visible: 6,
			start:1,
			scroll:1,
			wrap: "circular",
			animation: sliderSpeed,
			itemVisibleOutCallback: {
				onBeforeAnimation: function(carousel, item, idx, state) {
					$(".service").fadeOut();
					
					$(".slide-inner p", item).fadeOut(50);
					if($.browser.msie && $.browser.version.substr(0,1) == 6 ) 
						$(".slide-inner .circle", item).animate({
							width: 0,
							height: 0
						}, 300);
					$(".slide-inner", item).animate({ 
						width:0,
						top: $(".slide-inner", item).height()/2,
						left: $(".slide-inner", item).width()/2
					}, 300);
				}
			},
			itemVisibleInCallback: {
				onBeforeAnimation: function(carousel, item, idx, state) {
					$(".slide-inner", item).css({ 
						top: 72,
						left: 72,
						width: 0
					 });
					$(".slide-inner p", item).hide();
					
					setTimeout(function() {
						if($.browser.msie && $.browser.version.substr(0,1) == 6 )
							$(".slide-inner .circle", item).animate({
								width:145,
								height:145
							}, 200);
							
						$(".slide-inner", item).animate({ 
							width:145,
							top: 0,
							left: 0
						}, 200, function() {
							$(".slide-inner p", item).fadeIn(100);
						});
					}, 200);
				}
			},
			initCallback: function(carousel) {
				if($.browser.msie && $.browser.version.substr(0,1) == 6 ) { 
					$(carousel.container).find("li").each(function() {
						$(this).find("img:eq(0)").replaceWith("<span class='circle' />");
					});
				}
				
				servicesSlider = carousel;
				carouselInitiated = true;
			}
		});	
	}
	
	$('.bubble').hover(
		function() {
			var bubble = $(this);
			bubble.find(".data").each(function() {
				hideTooltips(bubble);
				var newW = 19 + $(this).find(".data-m").outerWidth();
				$(this).find(".data-inner").css({ width:newW })
				$(this).stop(true, true).animate({
					width: newW
				}, "fast");	
			});
		},
		function() {
			var bubble = $(this);
			bubble.find(".data").each(function() {
				hideTooltips(bubble);
				$(this).stop(true, true).animate({
					width: 0
				}, "fast");	
			});
		}
	);
	
	$('.toggle-plus-signs').click(function() {
		if (parseInt($('.leaf .plus:eq(0)').css('opacity')) > 0) {
			$('.leaf .plus').stop().fadeTo("fast", 0);
		} else {
			$('.leaf .plus').stop().fadeTo("fast", 1);
		}
		return false;
	});
	
	var carouselInitiated = false;
	$("#tree .services .plus").click(function() {
		hideTooltips($("#tree .services"));
				
		var $s = $("#tree .slider");
		
		if( $s.is(":visible") )
		{
			$s.find(".slide-inner p").fadeOut(50);
			$s.find(".slide-inner").stop(true,true).animate({
				top: 72,
				left: 72,
				width: 0
			}, "fast", function() {
				$s.hide();
			});
		}
		else
		{
			$s.show();
			if(carouselInitiated)
			{
				$s.find(".slide-inner").stop(true,true).animate({
					top: 0,
					left: 0,
					width: 145
				}, "fast", function() {
					$s.find(".slide-inner p").fadeIn(100);
				});
			}
			else
			createSlider($s);
		}
		
		return false;
	});
	
	$("#services, .services").each(function() {
		$(".slide-inner a", this).append("<span class='hover'><em>&nbsp;</em></span>");
		if ($(this).is('#services')) {
			createSlider($(".slider", this));	
		}
		
		$(".slide-inner a", this).hover(function() {
			$(".hover", this).stop(true,true).fadeIn("fast");
			
		}, function() {
			
			$(".hover", this).stop(true,true).fadeOut("fast");
			
		}).live('click', function() {
			
			var $t = $(this);
			var href = $t.attr('href');
			if ($(href).is(':visible')) {
				return false;
			}
						
			$('.service:visible').fadeOut();
			
			var itemPos = ( $t.offset().left - $t.parents(".jcarousel-clip").offset().left ) / $t.width() + 1;
			var left;
			
			if(itemPos<4){
				left = $t.offset().left - $(".shell").offset().left + $t.width() - 20;
				$(href).removeClass("service-right-arr");
			} else {
				left = $t.offset().left - $(".shell").offset().left - $(href).width();
				$(href).addClass("service-right-arr");
			}
			
			$( href ).fadeIn().css({
				left: left,
				top: $t.offset().top - $t.parents(".shell").offset().top - $(href).height()/2 + $t.outerHeight()/2
			}).find(".service-inner").jScrollPane();
			
			var curr_scroll = $(window).scrollTop();
			window.location.hash = "#" + href.split("#")[1];
			$(window).scrollTop(curr_scroll);
			
			return false;
		});
		
		$(".service-close").click(function() {
			$(".service:visible").fadeOut();
			return false;
		});
		
		if( window.location.hash != "" ) {
			var a = $("#main a[href*="+window.location.hash+"]");
			if(a.size()) {
				var idx = a.parents("li").index();
				if (idx >= 6) {
					idx = idx % 6;
				}
				
				servicesSlider.scroll( idx );
				setTimeout(function() {
					a.parents("li").parent().find('li:eq(' + (idx+1) + ') a:eq(0)').trigger('click');
				}, sliderSpeed+300);
			} else {
				if (window.location.hash = '#form-login') {
					$('#login .arr-open span').trigger('click');
				}
			}
		}
	});
	
	$('.services-title').click(function() {
		$(this).parents('.services:eq(0)').find('.plus:eq(0)').trigger('click');
		return false;
	});
	
	$(window).load(function() {
		$('#tree .services .plus').trigger('click');
		$('.page-template-page-services-php #navigation .sub-menu a[href*="services/#"]').click(function() {
			var hashpart = $(this).attr('href').replace(/[^#]*#/, '#');
			$('#main a[href="' + hashpart + '"]').trigger('click');
			return false;
		});
	});
	
	$('#client-login .menu a').attr('target','_blank');
	
});

function htmlLoaded() {
	if(jQuery.browser.msie && jQuery.browser.version.substr(0,1) == 6 ) { 
		DD_belatedPNG.fix('#logo a, #header .shell, #client-login-link a, .leaf .bg, .leaf .plus, .bubble .body, .bubble .arr, .bubble .data-l, .bubble .data-m, .bubble .data-r, #footer img, #footer .logo, .services .plus, #login form, #footer .bottom, .gform_body .field-box');
	}
}
