 /*

  JQuery goodies for Cobb Computing LLC.
   Navigation animation, and effects.

 */

$(document).ready(function() {

// Some global variables declared
	
  websitehostname = "cobbcomp.com"; // set this to be your hostname without the www
  animDelayAmount = 300; // how much delay
  animDuration = 500; //default animation speed
  currenthref = location.href; //current website page
  iev = getInternetExplorerVersion(); // version number of IE (or -1)
  heightanimamount = "189px"; // Adjustments for IE6/7/8 for the sliding animation
  if (iev == 6) heightanimamount = "232px";
  topanimadjustment ="-148px";
  if (iev == 6 || iev == 7) topanimadjustment = "-178px";
  marginanimamount = "0 auto";
  if (iev == 6 || iev == 7) marginanimamount = "30px auto 0 auto";
  widthanimamount = "auto";
  if (iev == 8) widthanimamount = "570px";

// Top navigation slider setup here (nav follows mouse movement at top)

  $nava = $(".navactive");
  $navawrap = $("#navwrap");
  originalleft = ($nava.position().left - (($navawrap.width()-960)/2)) ; // corrects auto margin centering in parent

// Set up our first slide for the autoplaying content slider

  $firstslide = $("#staff1");
  var autoplayslidertimer = 9000;

// This next section intercepts all link clicks so we can see what is clicked and choose animations (if any) to trigger.
// Thanks to seb for the 'live' listner function -- http://goo.gl/eqG9X
 
$("a").live("click", function(){  // ...everytime a link is clicked...

  clickedhref = $(this).attr("href");
  animDelay = 0; // 1=yes, 0=no tells if animation triggers or not, so reset it here
  currenthost = $(this).attr("host"); // no animation on external links so save the clicked link's host
  if (!currenthost || currenthost=='localhost' || currenthost.match("192.168")) {currenthost = websitehostname;} // this is for testing the animation locally, comment out on real host?


// Top sliding animation testing

// if navigating to the about/index page from another page on the site
// No animation on footer links due to the refreshing jumping as the height is resized looks too choppy

 if ((clickedhref == "index.html") && ( !currenthref.match("index.html") ) && (currenthref.match("html" ) && ($(this).parent().parent().attr('id') != "nav2" ) ) ) {  animDelay = 1; movedownmaincontent(); }
 
// if navigating away from the about/index page but still on the site 

 if ((clickedhref.substring(0,1) != "#") && (clickedhref != "index.html" ) && (currenthost.match(websitehostname) &&
     (currenthref.match("index.html") || (!currenthref.match("html" )) ) ) && ($(this).parent().parent().attr('id') != "nav2" ) )  { animDelay = 1; moveupmaincontent(); }
     
/*
  alert(clickedhref+' was the clicked link.\n'
  +currenthref+' is current location.\n'
  +animDuration+' * '+animDelay+' is the delay.\n'
  +currenthost+' is the clicked link\'s host.\n'
  +iev+' is the internet explorer version.\n'
  +$(this).parent().parent().attr('id')+' is parents parents id value.');  
*/


// prevent user navigation away until animation's finished, if animDelay is zero then no wait for animation 

  setTimeout(function () { window.location = clickedhref;  }, animDelay*(animDuration+animDelayAmount));   return false;


}); //end live link clicking detection

	
// About page hide sliding area

function moveupmaincontent() { // navigating away from the about/index page but still on the site
$("#aboutpage  #header").animate({    height: heightanimamount       }, animDuration ) //modified to 232px for IE6, 189px default
  $("#aboutpage #logosub, #aboutpage #logotag").fadeOut("fast", function() {
	$("#aboutpage #logosub").css({'fontSize' : '3.0em', 'width' : '360px', 'top' : topanimadjustment, 'right' : '40px', 'position' : 'absolute'})
 	// topanimadjustment modified for IE6/7 -178px, -148px ie8
  	$("#aboutpage #logosub").fadeIn( "fast" ) ;
  });
}
// About page show sliding area

function movedownmaincontent() { // navigating to the about/index page from another page on the site
$("#header").animate({    height: "392px"       }, animDuration ) 
  $("#logosub, #aboutpage #logotag").fadeOut(animDuration, function() {
  $("#logosub").css({ 'fontSize' : '4.8em', 'position' : 'static', 'margin' : marginanimamount, 'width' : widthanimamount })
 	// marginanimamount added for IE6/7, 0 auto for ie8
 	// widthanimamount auto for 6/7, 570px for ie8
 	$("#logotag").css( "display", "block" )
 	$("#logosub, #logotag").fadeIn( animDuration )
 });
}

function getInternetExplorerVersion() {

    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
	
 
// Contact form focus-highlighting

	$("#contactform input, #contactform textarea").focus(function() {
		$(this).parent().addClass("contactfocus");
		$(this).parent().removeClass("contacthover");
	});

	$("#contactform input, #contactform textarea").blur(function() {
		$(this).parent().removeClass("contactfocus")
	});

	$("input.submitbutton").click(function() {
		$("input.submitbutton").parent().removeClass("contactfocus")
	});

// Contact form hover-highlighting

	$("#contactform input, #contactform textarea").hover(
	function() {		
		$(this).parent().not( $(this).parent(".contactfocus")).addClass("contacthover");
	},
	function() {
		$(this).parent().removeClass("contacthover");
	});

	$("input.submitbutton, #contactform input, #contactform textarea").click(function() {
		$("input.submitbutton").parent().removeClass("contacthover")
	});
	

// Top navigation slider animation, thanks to css-tricks.com for the examples
	
	$("#navwrap").hover(
	function() { // onmouseover navwrap
	  	$("#nav").find("a").hover(
  		function() { // onmouseover links
  			$el = $(this); 	 
  	        	leftpos = ( $el.position().left - (($navawrap.width()-960)/2))  ; // corrects auto margin centering in parent
  			//alert ("$el.position().left = "+$el.position().left+"\n"+"(($navawrap.width()-960)/2)) = "+(($navawrap.width()-960)/2)+"\nFinal result ="+leftpos);
  			$nava.stop().animate( {left: leftpos}, 250 )
		}); //end hovering over 'a' links
	},
	function() { // onmouseout from navwrap
  		$nava.stop().animate( {left: originalleft}, 250 );
	}); //end hoving over navwrap

// Content slider section (with in slide navigation and autoplay and goto slide features)

	keepshowingslidernextorprev = 0;

// Thanks to Kevin Liew ( http://tiny.cc/b5mkf ) for the tutorial on an autoplaying jquery slider which this next section is based off of	
// This is the main function which advances the slide
	
	function contentsliderchange(direction) {  //1==foward/next, 0==backward/prev
		
		$currentslide = $(".currentslide").length ? $(".currentslide") : $firstslide; // $firstslide defined further up
		currentleftslider = $("#nocontentsliderscrolling").position().left; // position of viewing slide window
				
		if ( $currentslide.next().length ) {
	
			$('.rightslidernext').css(  "background-image", "url('images/slidercontentnext.png')" 	);
			$('.leftsliderprev').css(  "background-image", "url('')" 	);	
		} else {
			$('.leftsliderprev').css(  "background-image", "url('images/slidercontentprev.png')" 	);
			$('.rightslidernext').css(  "background-image", "url('')" 	);	 
		}
		
		
		//autoplay will fade the navigation controls unless you're manually clicking them
		$('.rightslidernext, .leftsliderprev').show();
		if ( !(keepshowingslidernextorprev) ) { $('.rightslidernext, .leftsliderprev').fadeOut(3000); keepshowingslidernextorprev = 0; }

		if (direction) { //is next (foward/advance)		
			//if firstslide = rightslidernext goto first slide		
			$nextslide = ($currentslide.next().length) ? $currentslide.next() : $('#contentsliderwrap li:first');
			slideamount = ($currentslide.next().length) ? -700 : ($('#contentsliderwrap li').length-1) * 700  ;				
		} else { // if direction is prev (backwards)
			//if lastslide = leftsliderprev goto last slide
			$nextslide = ($currentslide.prev().length) ? $currentslide.prev() : $('#contentsliderwrap li:last');
			slideamount = ($currentslide.prev().length) ? 700 : ($('#contentsliderwrap li').length-1) * -700  ;
		}		

		// slide to next content slide here	
		$("#nocontentsliderscrolling").animate( {left: currentleftslider+slideamount}, 550 );

		$currentslide.removeClass("currentslide");
		$nextslide.addClass("currentslide");
		
		
	}; //end contentsliderchange	

	

	// this section lets you go to a specific slide referenced by the slider navigation
	$('#slidercontentnav li a').click ( function () {
		clearInterval(run);
		currentleftslider = (($($(this).attr("href")).index()) * -700); 
		$("#nocontentsliderscrolling").animate( {left: currentleftslider}, 550 );
		$currentslide = $('#contentsliderwrap').children().eq($($(this).attr("href")).index());
		$(".currentslide").removeClass("currentslide");
		$currentslide.addClass("currentslide");
		return false;
		});
		

	//slider 'in-slide' navigation controls	left and right
	$('.rightslidernext').click ( function () {
		clearInterval(run);
		keepshowingslidernextorprev = 1;
		contentsliderchange(1);
		return false;  }); // clicking next
	$('.leftsliderprev').click  ( function () { 
		clearInterval(run);
		keepshowingslidernextorprev = 1;
		contentsliderchange(0);
		return false;  }); // clicking prev


	//When hovering over the slide area, show the navigation if hovering over the sides and also resume the playback on mouseout
      	$('#contentslidermain').hover(     
        	function() { clearInterval(run); 
        		$('.rightslidernext, .leftsliderprev').css(  "background-image", "url('')" );
        		$('.rightslidernext, .leftsliderprev').fadeIn(0);
        		$('.rightslidernext, .leftsliderprev').show();
        		$('.rightslidernext').hover(
        					function() { $('.rightslidernext').css(  "background-image", "url('images/slidercontentnext.png')" );
        						      }, 
						function() { $('.rightslidernext').css(  "background-image", "url('')" ); });
			$('.leftsliderprev').hover(
        					function() { $('.leftsliderprev').css(  "background-image", "url('images/slidercontentprev.png')" );
        						     }, 
						function() { $('.leftsliderprev').css(  "background-image", "url('')" ); });
			},			
        	function() { keepshowingslidernextorprev = 0; run = setInterval( function() { contentsliderchange(1); }, autoplayslidertimer);
        		 } 
    	);  
    	
    	
	//this section will fix the slider window and current slide offset if you arrive from an anchor tag link.			
	if ( currenthref.match("#") ) { 
		anchorlink = unescape(self.document.location.hash);
		indexed = $('#contentsliderwrap').children().eq($($($('a[href="'+anchorlink+'"]')).attr("href")).index()).index();
		//above two lines give the offset of the li slide in #contentsliderwrap (starting at 0) based off the url hash (anchor)
		//yes that was kinda crazy but it works :P
		$currentslide = $('#contentsliderwrap').children().eq(indexed);			
		$currentslide.addClass("currentslide");
		currentleftslider = $("#nocontentsliderscrolling").position().left;// ( (indexed) * -700);
		$("#nocontentsliderscrolling").css("left", currentleftslider);
		};
			
	// Set up the auto play for the content slider
	run = setInterval(function() { contentsliderchange(1);	}, autoplayslidertimer )
                
}); //  end document.ready



