var iexploder = -1;

jQuery.each(jQuery.browser, function(i, val) {
  if(i=="msie" && jQuery.browser.version.substr(0,3)< 7)
     iexploder = 1;
});



$(window).bind("load", function() {
	  var imageCount = $("#imagecache img.bgimages").length; 
	  var iespace=0;
	  var nextstate=0;
	  var currentimg = 0; // sets the default image;
      var textcount = $("#titlecache div");
	  var loop =1;
	   
	  jQuery.fn.runslideshow = function() { 
				$(".homepage").fadeOut();
				if(currentimg >= imageCount) {
				   $('img#bg0').fadeIn( function() {
				          $(".homepage").html(textcount['0']);
				          $('#imagecache img.bgimages').css('display', 'block');
						  currentimg=0;
				   });
				}
				$('img#bg'+currentimg).fadeOut(1500, function() {
				if(textcount[currentimg]) {
					$(".homepage").html(textcount[currentimg]);
				}
				else {
				$(".homepage").html(textcount[0]);
				}
				$(".homepage").fadeIn();
				});
				currentimg++;
				
      };
	  	  

	if(imageCount > 1) {
	     $('img#bg0').fadeIn(500, function() {
				          $('#imagecache img.bgimages').css('display', 'block');
						  $('#imagecache').css('background-image', 'url('+$('img#bg0').attr('src')+')');
						  $(".homepage").html(textcount['0']);
		  });
	     setInterval("$('#imagecache').runslideshow()", 6000);
	  }
	  else {
	     $('.homepage, img#bg0').fadeIn(500);
	  }
      $('#mgfanim').fadeIn(500);
	         
	});

function initialise(){
	$('#googlemaps').fadeIn();
       if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("googlemaps"));
		
		// Create our "tiny" marker icon
		var starIcon = new GIcon();
		starIcon.image = "http://www.webbhenderson.com/images/webb-marker.png";
		starIcon.iconSize = new GSize(20, 20);
		starIcon.iconAnchor = new GPoint(10, 10);
		starIcon.infoWindowAnchor = new GPoint(5, 1);
		
		// Set up our GMarkerOptions object
		markerOptions = {
		icon: starIcon
		};

		map.setCenter(new GLatLng(-36.84334, 174.760983), 16);
		latlng = new GLatLng(-36.84334, 174.760983);
		map.addOverlay(new GMarker(latlng, markerOptions));
	}

}

function initialiseau(){
	$('#googlemapsau').fadeIn();
       if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("googlemapsau"));
		
		// Create our "tiny" marker icon
		var starIcon = new GIcon();
		starIcon.image = "http://www.webbhenderson.com/images/webb-marker.png";
		starIcon.iconSize = new GSize(20, 20);
		starIcon.iconAnchor = new GPoint(10, 10);
		starIcon.infoWindowAnchor = new GPoint(5, 1);
		
		// Set up our GMarkerOptions object
		markerOptions = {
		icon: starIcon
		};

		map.setCenter(new GLatLng(-33.865765, 151.207562), 19);
		latlng = new GLatLng(-33.865765, 151.207562);
		map.addOverlay(new GMarker(latlng, markerOptions));
	}

}



$(document).ready( function() {  
	  $("#footer a").livequery('click', function(event) {
		    event.preventDefault();
			$('#googlemaps, #googlemapsau, #contact').fadeOut();
			pagename = $(this).attr('href');
			$(".landingpage").fadeOut(500);
			$('#inpage li').css('background-color', '#635a52');
			$("#popoverwhite #content").load(pagename +" #content > *", function() {
			$("#popoverwhite").fadeIn(1000);
		});
	  });
	  

	  $("#inpage li a").livequery('click', function(event) {
			//if(iexploder < 0 ){
			event.preventDefault();
			$("#nextarrow").hide();
			$(".landingpage").fadeOut();
			nextstate = $("#inpage li a").index(this);
			$("#inpage li ul").fadeOut(500);
			$(this).parents('li').children('ul').fadeIn(500);
			$('#inpage li').css('background', '#635a52');
			$(this).parents('li').css('background', '#7e2f08');
			pagename = $(this).attr('href');
			$('#googlemaps, #contact').fadeOut();
			$("#popoverwhite #content").load(pagename + ' #content > *', function() {
				if(pagename == 'http://www.webbhenderson.com/index.php/contact-form' || pagename == 'http://www.webbhenderson.com/contact-form.html') {
			     $('#contact').fadeIn();
			 $('#googlemaps, #googlemapsau').fadeOut();

			  }
			  if(pagename== 'http://www.webbhenderson.com/index.php/address-and-directions' || pagename== 'http://www.webbhenderson.com/address-and-directions.html') {
			         $('#contact').fadeOut();
				 $('#googlemaps, #googlemapsau').fadeIn(function() {
				initialise(); initialiseau(); });
			  }
			$("#popoverwhite").fadeIn(1000, function() {
				$("#nextarrow").fadeIn (500);
			});
		              $('#leftcol').css('height', $('#content').height()+80);
		});
		//}
	  });

	  $('#popoverwhite #closepopover, #sidenav h2').livequery('click', function(event) {
          	if($('#content').length > 0) {
			     event.preventDefault();
				 $("#nextarrow").fadeOut(500, function() {
					$('#popoverwhite').fadeOut(1000, function() {
						$("#inpage li ul").fadeOut(500);
						$(".landingpage").fadeIn(500);
				 });
				 });

			}
	  });
      
	  $('#nextarrow').livequery('click', function(event) {
			event.preventDefault();
			//alert(nextstate);
			var thismenu = $('#inpage li').siblings().children('a');
			if(nextstate >= thismenu.length) {
			  nextstate=0;
			}
			if($(thismenu[nextstate]).attr('href')) {
			pagename = $(thismenu[nextstate]).attr('href');
			$('#googlemaps, #googlemapsau, #contact').fadeOut();
			$("#popoverwhite #content").fadeOut();
			  if(pagename == 'http://www.webbhenderson.com/index.php/contact-form' || pagename == 'http://www.webbhenderson.com/contact-form.html') {
			     $('#contact').fadeIn();
			 $('#googlemaps, #googlemapsau').fadeOut();
			  }
			  if(pagename== 'http://www.webbhenderson.com/index.php/address-and-directions' || pagename== 'http://www.webbhenderson.com/address-and-directions.html') {
			     	$('#contact').fadeOut();
				 $('#googlemaps, #googlemapsau').fadeIn(function() {
				initialise();});
			  }
			$("#popoverwhite #content").load(pagename+' #content > *', function() {
			$("#inpage li ul").fadeOut(500);
			$("#inpage li").css('background-color', '#635a52');
			$(thismenu[nextstate]).parents('li').children('ul').fadeIn(500);
			$(thismenu[nextstate]).parents('li').css('background', '#7e2f08');
			$("#inpage ul li").css('background', 'none');
			$("#content").fadeIn(500);
			nextstate++;
			//alert(nextstate);	    
			});
			$('#leftcol').css('padding-bottom', $('#content').height()+80);
			}
	});
$(document).pngFix();
 });