$(document).ready(function() {
//navigation on states
	var nav = window.location.href;
	if (nav) {
		$('#navigation li a[href=' + nav + ']').addClass('on');
	}
	if (/\/services\//.test(window.location)) {
    	$('#navigation li a[href*=services]').addClass('on');
	} else if (/\/industries\//.test(window.location)) {
    	$('#navigation li a[href*=industries]').addClass('on');
	}
	
//on load
	$('#landing #background img:first').fadeIn(300);
	$('#tooltip li:first').addClass('on');

	var homeSpeed = 6000;
	var int=self.setInterval( "slideSwitch()", homeSpeed );
	
//tooltip navigation
	$('#tooltip li').click(function(){
		int=window.clearInterval(int);
		int=self.setInterval( "slideSwitch()", homeSpeed );
		var h3Text = $(this).children('a').attr('title');
		var pText = $(this).children('a').attr('name');
		var bgImg = $(this).index();
		$('#tooltip h3').text(h3Text);
		$('#tooltip p').text(pText);
		$('#tooltip li').removeClass('on');
		$(this).addClass('on');
		$('#background img:visible').fadeOut(500);
		$('#background').find('img').eq(bgImg).fadeIn(500);
	});
	
//photo modal
	$('#photos a').click(function(e) {
		e.preventDefault();
		var imgSrc = $(this).attr('href');
		//var pText =  $(this).children('img').attr('alt');
		var modal = $('#modal');
		$('#modal img').attr('src', imgSrc);
		//$('#modal p.caption').text(pText);
		var maskHeight = $(document).height();
		var maskWidth = $(document).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});	
		$('#mask').fadeTo(600,0.6);	
		var winH = $(window).height();
		var winW = $(window).width();
		$(modal).css('left', winW/2-$(modal).width()/2);
		$(modal).delay(200).fadeIn(500); 	
	});
	$('#mask, #modal p.close').click(function (e) {
		e.preventDefault();
		$('#mask, #modal').hide();
	});		

});

(function($){
	var origDiv = '#interior #background';
	var origImg = '#interior #background img';
	
	//resize the image on brower load
	$(document).ready(function() {
		$(origDiv).resizeImg();
	});
	//resize the image on browser resize
	$(window).bind("resize", function() {
		$(origDiv).resizeImg();
	});
    
    //Adjust image size
    $.fn.resizeImg = function() {
        //define original width and height of the image
        var imgWidth = $(origImg).attr('width');  
        var imgHeight = $(origImg).attr('height');
        //define image ratio
        var ratio = imgHeight/imgWidth;
        //get browser dimensions
        var winWidth = $(window).width() - 210;
        var winHeight = $(window).height();
		var winRatio = winHeight/winWidth;
        //resize the image
        if (winRatio > ratio) {
            $(origDiv).height(winHeight);
            $(origDiv).width(winHeight / ratio);
            $(origImg).height(winHeight);
            $(origImg).width(winHeight / ratio);
        } else {
            $(origDiv).width(winWidth);
            $(origDiv).height(winWidth * ratio);
            $(origImg).width(winWidth);
            $(origImg).height(winWidth * ratio);
        }

    };
})(jQuery);

function slideSwitch() {
    var $active = $('#tooltip li.on');
    if ( $active.length == 0 ) $active = $('#tooltip li:last');
    var $next =  $active.next().length ? $active.next()
        : $('#tooltip li:first');
    $active.removeClass('on');
    $next.addClass('on');
	
	var h3Text = $('#tooltip li.on a').attr('title');
	var pText = $('#tooltip li.on a').attr('name');
	var bgImg = $('#tooltip li.on').index();
	$('#tooltip h3').text(h3Text);
	$('#tooltip p').text(pText);
	$('body.page-parent #background img:visible').fadeOut(500);
	$('body.page-parent #background').find('img').eq(bgImg).fadeIn(500);
	
}
