var bgs = new Array("img1","img2","img3","img4","img5","img6","img7","img8","img9");
function randOrd(){
	return (Math.round(Math.random())-0.5); 
}
bgs.sort( randOrd );

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
var homePage = 0;
var mouseOver = 0;
if (sPage=='') {
	homePage = 1;
}

$(document).ready(function() {
	initNav();
	loadFirst();
	initTooltips();
	navHeight();
});

function checkExplorer()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    return true;
  } else {
	return false;
  }
}

function getIEversion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  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;
}

jQuery.fn.onImagesLoaded = function(_cb) { 
  return this.each(function() {
 
    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };
 
    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
          }
        }; // _checki \\
 
        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};

$(function() {
	$window = $(window);
	/*
	if (homePage) {
		$window.scroll(function() {
			$img = $("#bg");
			var h = $window.scrollTop();
			var w = $window.scrollLeft();
			$img.css("top","-"+h+"px");
			$img.css("left","-"+w+"px");
		});
	} else {
	*/
		$window.scroll(function() {
			if (homePage) {
				// nothing
				var newHeight=$("#extrasWrap").height();
				var ieHeight = 0;
				//$('#tester').html(newHeight);
					$("html").css("height",""+newHeight+"px !important");
					$("body").css("height",""+newHeight+"px !important");
					$("html").css("max-height",""+newHeight+"px !important");
					$("body").css("max-height",""+newHeight+"px !important");
				$("#extras").css("height",""+newHeight+"px !important");
				$("#extras").css("min-height",""+newHeight+"px !important");
				$("#extras").css("max-height",""+newHeight+"px !important");
					$("#nav").css("height",""+newHeight+"px !important");
					$("#nav").css("min-height",""+newHeight+"px !important");
					$("#nav").css("max-height",""+newHeight+"px !important");
					$("#wrapper").css("height",""+newHeight+"px !important");
					$("#wrapper").css(min-"height",""+newHeight+"px !important");
					$("#wrapper").css("min-height",""+newHeight+"px !important");
				$("#extrasWrap").css("height",""+newHeight+"px !important");
				$("#extrasWrap").css("min-height",""+newHeight+"px !important");
				$("#extrasWrap").css("max-height",""+newHeight+"px !important");
				//$("#tester").html($("html").height());
			}
			$wrapper = $("#wrapper");
			$nav = $("#extras");
			var wHeight = $window.height();
			var nHeight = $nav.height();

			//var h = $window.scrollTop();
			if ((BrowserDetect.browser == 'Firefox')) {
				//nHeight+=50;
				nHeight = $("#extrasWrap").height();
			}
			if (nHeight > wHeight) {
			var perc = ($window.scrollTop())/($(document).height()-$window.height());
				var h = (nHeight-wHeight)*perc;
			} else {
				var h = 0;	
			}
			var w = $window.scrollLeft();
			$nav.css("top","-"+h+"px");
			if (homePage) {
				// nothing
			} else {
				$nav.css("right",""+w+"px");
			}
		});
		
		if (checkExplorer()) {
			$window.resize(function() {
				$wrapper = $("#wrapper");
				$nav = $("#extras");
				var wHeight = $window.height();
				//var h = $window.scrollTop();
				if ($nav.height() > wHeight) {
				var perc = ($window.scrollTop())/($(document).height()-$window.height());
					var h = ($nav.height()-wHeight)*perc;
				} else {
					var h = 0;	
				}
				var w = $window.scrollLeft();
				$nav.css("top","-"+h+"px");
				$nav.css("right",w+"px");				
			});
		}
		
	//}
});

function FullScreenBackground(theItem){
	var winWidth=$(window).width();
	var winHeight=$(window).height();
	if (winHeight < 700) {
		winHeight = 700;
	}
	if (winWidth < 960) {
		winWidth = 960;
	}
	var imageWidth=$(theItem).width();
	var imageHeight=$(theItem).height();
	var picHeight = imageHeight / imageWidth;
	var picWidth = imageWidth / imageHeight;
	if ((winHeight / winWidth) < picHeight) {
	$(theItem).css("width",winWidth);
	$(theItem).css("height",picHeight*winWidth);
	} else {
	$(theItem).css("height",winHeight);
	$(theItem).css("width",picWidth*winHeight);
	};
	$(theItem).css("margin-left", 0 );
	$(theItem).css("margin-top", (winHeight - $(theItem).height())/2 );
}
window.onload = function () {
	FullScreenBackground('.bgimg');
}
$(window).resize(function() {
	FullScreenBackground('.bgimg');
	navHeight();
});

function loadFirst() {
	
	var myImg = bgs[0];

	var html = '<img src="/images/backgrounds/'+myImg+'.jpg" class="bgimg" style="z-index: 1;" id="img0" />';
	$("#bg").html(html);
	$("#img0").css("opacity","0");
	$('.bgimg').onImagesLoaded(function(_this){

		FullScreenBackground('.bgimg');
		
		$("#img0").stop().animate({
			opacity: 1
		}, 1000, function() { 
			if (homePage) {
				if (!mouseOver) {
					timer = setTimeout (hideNav, navTime); 
				}
				rotation = setTimeout(rotator, rotateTime); 
			}
			if (sPage=='photos') {
				initIso();
			}
		});

	});
		
}

function initNav() {
	$(function() {	
		if (homePage) {
			// begin tab/sliding functions	
			$("#extrasTab").hover(function () {
				//hideExtrasTab();
				setTimeout (hideNavTab, 100);
				setTimeout (showNav, 300);
			});
	
			$("#navTab").hover(function () {
				hideNavTab();
				//setTimeout (hideExtrasTab, 100);
				setTimeout (showNav, 300);
			});
			
			$("#extras").mouseover(function () {
				mouseOver = 1;
				clearTimeout(timer);
			});
			$("#nav").mouseover(function () {
				mouseOver = 1;
				clearTimeout(timer);
			});
			$("#extras").mouseout(function () {
				mouseOver = 0;
				timer = setTimeout (hideNav, navTime);
			});
			$("#nav").mouseout(function () {
				mouseOver = 1;
				timer = setTimeout (hideNav, navTime);
			});
			// end tab/sliding functions
		}
		
		// begin main nav animation
		
		$("#mainnav a").hover(function() {
			
			$(this).stop().animate({
				paddingLeft: "20px",
				backgroundPosition: "1px 6px"
				}, 300, "easeOutBack");
			},
			
			// ON MOUSE OUT
			function() {
				
				$(this).stop().animate({
					paddingLeft: "0px",
					backgroundPosition: "-12px 6px"
				}, 300);
			
		});
		
		// end main nav animation
				
		$(".icons a img").css("opacity",".25");
		$(".icons a img").hover(function () {
		
			// SET OPACITY TO 100%
			$(this).stop().animate({
				opacity: .8
				}, 300);
			},
			
			// ON MOUSE OUT
			function () {
			
				// SET OPACITY BACK TO 50%
				$(this).stop().animate({
				opacity: .25
				}, 300);
				
		});
		
		$("#iGallery").rotate({ 
		   bind: 
			 { 
				mouseover : function() { 
					$(this).rotate({animateTo:90})
				},
				mouseout : function() { 
					$(this).rotate({animateTo:0})
				}
			 } 
		   
		});
		
		// vertical center icons
		$('.icons').onImagesLoaded(function(_this){
		
			// vertical center each icon
			$(".smallIcon").each(function(i){
				var ah = $(this).height();
				var ph = 42;
				var mh = Math.ceil((ph-ah) / 2);
				$(this).parent().css('margin-top', mh);
			});

		});
		
		// swap grey for acordion links
		$(".accHeader").mouseover(function() {
			$(this).css("background-image","url(/images/grey2.png)");
		});
		$(".accHeader").mouseout(function() {
			$(this).css("background-image","url(/images/grey1.png)");
		});
		
	});
}

function initTooltips() {
$(function() {
	var $tooltip = $('#tooltip');
	$tooltip.css("opacity","0");
	$("a[rel]").hover(function() {
		
		document.getElementById('tipText').innerHTML = this.rel;
		
		x = $(this).offset().left;
		x += ($(this).width())/2;
		x-= (document.getElementById('tooltip').offsetWidth)/2;
		y = $(this).offset().top;
		y-= (document.getElementById('tooltip').offsetHeight);
		y-= 5;
		endY = y-15;
		
		$tooltip.css("top",y+'px');
		$tooltip.css("left",x+'px');
		$tooltip.stop().animate({
			opacity: 1.0
		}, 300);
	   
	},
	
	// ON MOUSE OUT
	function () {
		// SET OPACITY BACK TO 50%
		$tooltip.stop().animate({
			opacity: 0,
			top: endY
		}, 300, function(){
					
					$tooltip.css("top",'-100%');
					$tooltip.css("left",'-100%');
					
				});
	});
});
}

$(function() {
	$(".announcement .label").each(function(i){
		$label = $(this);
		$(this).onImagesLoaded(function(_this){
			var h1 = $label.parent().height();
			var h2 = $label.height();
			if (h1 > h2) {
				var mover = (h1-h2)/2;
				$label.css("margin-top",""+mover+"px");	
			}
		});
	});
});

function navHeight() {
	$(function() {
		$("#extras").onImagesLoaded(function(_this){
			var navHeight = $("#extrasWrap").height();
			var winHeight = $(window).height();
			if (navHeight > winHeight) {
				//navHeight += 20;
				$("#extras").css("min-height",""+navHeight+"px !important");
				$("#wrapper").css("min-height",""+navHeight+"px !important");
				$("#secWrap").css("min-height",""+navHeight+"px !important");
				$(".content").css("min-height",""+navHeight+"px !important");
				if (homePage) {
					$("#nav").css("min-height",""+navHeight+"px !important");
					$("#nav").css("height",""+navHeight+"px !important");
				}
			} else {
				$("#extrasWrap").css("height","100%");
			}
			
			var bodyHeight = $("#secWrap .content").height();
			if (bodyHeight < navHeight) {
				navHeight+=20;
				$("#secWrap .content").css("height",""+navHeight+"px !important");
			}
			
		});
	});
}

function createObjectReply() {
	var request_type;
	var browser = navigator.appName;
	
	if(browser == "Microsoft Internet Explorer"){
		request_type = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request_type = new XMLHttpRequest();
	}

	return request_type;
}

var http2 = createObjectReply();


function goComment() {
	
	var myQS = $('#commentForm').serialize();
	
	nocache = Math.random();
	http2.open('post', '/scripts/comment_submit.php?nocache='+nocache+'&'+myQS);
	http2.onreadystatechange = endDiscussion;
	http2.send(null);
			
	function endDiscussion() {

		if(http2.readyState == 4){ 		
			var finish = http2.responseText;
			if (finish == 'totallyFinished') {

				alert('Thank you! Your comment has been submitted for review.');
				window.location.reload(true);

			} else {
				alert(finish);
			}
		}
	}

}

// share box functions
$(function() {
	$(".referBtn").click(function() {
		$("#blackoverlay").css("display","block");
		$("#blackoverlay").css("opacity","0.75");
		
		var $elem = $("#share");
		var myWidth = ($elem.width()/2);
		var myHeight = $elem.height();
		$elem.css("display","block");
		/*
		$elem.css("margin-left","-"+myWidth+"px");
		$elem.css("top","-"+myHeight+"px");
		$elem.stop().animate({
			top: 100
		}, 300);
		*/
		return false;
	});
	$("#share .closer").click(function() {
		$("#blackoverlay").css("display","none");
		$("#blackoverlay").css("opacity","0");
		$("#share").css("display","none");
		return false;
	});
});

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

