var MySite = 
{
	init:function($)
	{
		this.fixMargin($);
		$(window).resize(function(){
			MySite.fixMargin($);
		});
		this.scrollBar($);
		this.menu($);
		this.lightBox($);
		this.imgFX($);
		this.teamHover($);
		this.teamSlider($);
		this.toolTip($);
	},
	
	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]").lightBox({
	        	imageLoading: 'mysite/javascript/jquery-lightbox/images/lightbox-ico-loading.gif', // (string) Path and the name of the loading icon
	        	imageBtnPrev: 'mysite/javascript/jquery-lightbox/images/lightbox-btn-prev.gif', // (string) Path and the name of the prev button image
	        	imageBtnNext: 'mysite/javascript/jquery-lightbox/images/lightbox-btn-next.gif', // (string) Path and the name of the next button image
	        	imageBtnClose: 'mysite/javascript/jquery-lightbox/images/lightbox-btn-close.gif', // (string) Path and the name of the close btn
	        	imageBlank: 'mysite/javascript/jquery-lightbox/images/lightbox-blank.gif'
    		});
		}
	},
	
	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();
			});
		}
	}
}

jQuery.noConflict();
jQuery(function($) 
{
	MySite.init($);
});