jQuery.noConflict();
jQuery(function($) 
{
	var frontend = 
	{
		init:function()
		{
			this.fixMargin();
			$(window).resize(function(){
				frontend.fixMargin();
			});
			this.scrollBar();
			this.menu();
			this.lightBox();
			this.imgFX();
			this.teamHover();
			this.teamSlider();
			this.toolTip();
			this.langSwitcher();
		},
		
		fixMargin:function()
		{
			var winHeight = $(window).height();
			var $outerContainer = $('.container').eq(0);
			if(winHeight <= 600)
			{
				$outerContainer.css(
				{
					margin:0
				});	
			}
			else
			{
				var marginTopX = (winHeight - 600) / 2;
				$outerContainer.css(
				{
					marginTop : marginTopX + "px"
				});				
			}
		},
		
		scrollBar:function()
		{
			if($('.TeamPage').length ==  0)$('.container .container').jScrollPane({scrollbarWidth: 19});
			if($('.jScrollPaneDrag').length > 0 && $('.jScrollPaneDrag').height() < 30) $('.jScrollPaneDrag').css('background-image', 'none');
		},
		
		menu:function()
		{
			$('div#Main ul ul').hide();
			$('li.current, li.section ').parent().show();
			
			$('div#Main a').click(function(){
				if($(this).next().length && $(this).next().get(0).tagName.toLowerCase() == "ul")
				{
					$(this).next().slideToggle('slow');
					$(this).parent().siblings().children("ul").slideUp("slow");
	
					return false;
				}
			});
				
		},
		
		lightBox:function()
		{
			if($('a[rel*=lightbox]').length)
			{
				$("a[rel*=lightbox]").prettyPhoto();
			}
		},
		
		imgFX:function()
		{
			$('.Page .aside img').css('opacity', .5).hover(function(){$(this).css('opacity', 1);}, function(){$(this).css('opacity', .5);});
		},
		
		teamHover:function()
		{
			
	        if ($('#TeamMembers').length > 0) {
	            var oldsrc;
	            $('#TeamMembers li.hover img').hover(function(){
					
	                oldsrc = $(this).attr('src');
	                var img = oldsrc.split('.');
	                var newsrc = img[0] + "-hover." + img[1];
	                $(this).attr('src', newsrc);
	            }, function(){
	                $(this).attr('src', oldsrc);
	            });
	        }
	    },
		
		teamSlider:function()
		{
			if($('#TeamMembers').length > 0){
				$('#TeamMembers').wrap('<div id="ScrollWrap">');
				
				function addSlide(){
					if ($('#TeamMembers').width() > $('#ScrollWrap').width()) {
						if ($('#Slider').length == 0) {
							$('<div id="Slider">').insertAfter("#ScrollWrap");
							$('#Slider').slider({
								slide: handleSlide
							});
						}
					}
					else{
						if ($('#Slider').length > 0)
						{
							$('#Slider').remove();
						}
					}
				}
				addSlide();
				$(window).resize(function(){ addSlide();});
			}
			
			
			
			function handleSlide(e, ui){
				var maxScroll = $("#ScrollWrap").attr("scrollWidth") - $("#ScrollWrap").width(); 
				$("#ScrollWrap").attr({scrollLeft: ui.value * (maxScroll / 100) });
			}
		},
		
		toolTip:function()
		{
			if($('#TeamMembers').length > 0){
				$('#TeamMembers img').mousemove(function(e){
					var t = $(this).attr('alt');
					if($('#ToolTip').length == 0)$('<div id="ToolTip">').appendTo('body').css('position', 'absolute');
					
					$('#ToolTip').text(t).css({
						left: (e.pageX+15) + "px",
						top: (e.pageY) +"px"
						});
				});
				$('#TeamMembers img').mouseout(function(){
					if($('#ToolTip').length > 0)$('#ToolTip').remove();
				});
			}
		},
		
		langSwitcher:function()
		{
			if(!$.support.opacity)
			{
				if($('ul.dropdown').length)
				{
					$('ul.dropdown li').hover(
						function(){ $(this).find('ul').css('display', 'block')},
						function(){ $(this).find('ul').css('display', 'none')}
					);
				}
			}
		}
	}
	frontend.init();
});
