// Jeff LaPlante Photography Global Website Functions

// Global variables:
		var gBehaviorsArray = [];
		
		function init(){
			reactToQueryString();
			resizePage();
		}
		
		function reactToQueryString(){
			var qs = window.location.search;
			qs = qs.substr(1);
			qsArray = new Array();
			qsArray = qs.split('&');
			
			switch(qsArray[0]){
				case "slideshows":
				case "slideshow":
					click_slideshow();
					if(qsArray[1]){
						startShow(qsArray[1]);
						appendBreadcrumb("" + qsArray[1]);
					}
				break;
				case "3DPhotoPlayer":
					click_3DPhotoPlayer();
				break;
				case "wedding":
					click_wedding();
				break;
				case "portrait":
					click_portrait();
				break;
				case "commercial":
					click_commercial();
				break;
				case "contact":
					click_contact();
				break;
				case "about":
					click_about();
				break;
				default:
					doDefaultLayout();	
			}
		}
		
		function test_fade(){
			set_photo('img/ashley_bright.jpg');
			displayOn("logo");
			displayOn("main");
			displayOn("nav");
			
			//Slideshow
			var intervalReturn=self.setInterval("changeSlide();",5000);			
		}
		
		function doDefaultLayout(){
			allOff();
			//setMainDivColor("#ffffff");
			setMainDivBorderStyle("0px solid #000");
			displayOn("logo");
			getElement('logo').style.top = getElement('logo').style.top + 30 + "px";
			displayOn("main");
			displayOn("splash");
			displayOn("contact-bar");
		}
		
		function click_wedding(){
			allOff();
			set_canvas_photo('img/slides/wedding_lead.jpg');
			//collapseLogo();
			setBreadcrumb("<a href='./'>home</a> . wedding");
			displayOn("main");
			displayOn("logo");
			displayOn("contact-bar");
			displayOn("breadcrumb");
			displayOn("menu_wedding");
			displayOn("canvas");
		}
		
		function click_portrait(){
			allOff();
			set_canvas_photo('img/slides/portrait_lead.jpg');
			//collapseLogo();
			setBreadcrumb("<a href='./'>home</a> . portrait");
			displayOn("main");
			displayOn("logo");
			displayOn("contact-bar");
			displayOn("breadcrumb");
			displayOn("menu_portrait");
			displayOn("canvas");
		}
		
		function click_commercial(){
			allOff();
			set_canvas_photo('img/slides/commercial_lead.jpg');
			//collapseLogo();
			setBreadcrumb("<a href='./'>home</a> . commercial");
			displayOn("main");
			displayOn("logo");
			displayOn("contact-bar");
			displayOn("breadcrumb");
			displayOn("menu_commercial");
			displayOn("canvas");
		}
		
		function click_3DPhotoPlayer(){
			allOff();
			setBreadcrumb("<a href='./'>home</a> . 3D Photo Player");
			displayOn("main");
			displayOn("logo");
			displayOn("contact-bar");
			displayOn("breadcrumb");
			displayOn("menu_home");
			
			displayOn("PhotoPlayer");
			
			window.frames['iFrameSlideshow'].location = './3DPhoto';
						
		}
		
		function click_slideshow(){
			allOff();
			setMainDivColor("#333333");
			setMainDivBorderStyle("0px solid #000");
			//collapseLogo();
			setBreadcrumb("<a href='./'>home</a> . slideshow: ");
			displayOn("main");
			displayOn("logo");
			displayOn("contact-bar");
			displayOn("breadcrumb");
			displayOn("menu_home");
			
			setOpacityZero("slideshowAperture");
																		
			// Fade In behavior For Main Image
			gBehaviorsArray["slideshowAperture"] = new Spry.Effect.AppearFade('slideshowAperture',{duration: 700,from: 0,to: 100, finish: 
	        			function(){
				        		gBehaviorsArray["slideshowAperture"] = null;
				        	}	
				        });		        
		}
		
		function click_about(){
			makeRed("about_url");
			allOff();
			//collapseLogo();
			setMainDivBorderStyle("0px solid #000");
			setOpacityZero("about");
			setBreadcrumb("<a href='./'>home</a> . about: ");
			
			displayOn("main");
			displayOn("logo");
			displayOn("menu_home");
			displayOn("contact-bar");
			displayOn("breadcrumb");
						
			// Fade In behavior
			gBehaviorsArray["about"] = new Spry.Effect.AppearFade('about',{duration: 700,from: 0,to: 100, finish: 
	        			function(){
	        				gBehaviorsArray["about"] = null; 
	        			}
					});
		}
		
		function click_contact(){
			makeRed("contact_url");
						
			allOff();
			//collapseLogo();
			setMainDivBorderStyle("0px solid #000");
			setOpacityZero("contact");
			setBreadcrumb("<a href='./'>home</a> . contact: ");
			
			displayOn("main");
			displayOn("logo");
			displayOn("menu_home");
		displayOn("contact-bar");
			displayOn("breadcrumb");
			
			// Fade In behavior
			gBehaviorsArray["contact"] = new Spry.Effect.AppearFade('contact', {duration: 700,from: 0,to: 100, finish: 
				function(){
					gBehaviorsArray["contact"] = null; 
				}
			});	
		}
		
		// Set Opacity to Zero for named element and turn it on
		function setOpacityZero(element){
			gBehaviorsArray[element] = new Spry.Effect.AppearFade(element,{duration: 100,from: 100,to: 0, finish: 
	        			function(){
	        				gBehaviorsArray[element] = null;
	        				displayOn(element); 
	        			}
	        		});
		}
		
		// Set Opacity to X for named element and turn it on
		function setOpacity(element, opacity){
			gBehaviorsArray[element] = new Spry.Effect.AppearFade(element,{duration: 100,from: 100,to: opacity, finish: 
	        			function(){
	        				gBehaviorsArray[element] = null;
	        				displayOn(element); 
	        			}
	        		});
		}

		
		function randomizeImage(){
			index = Math.floor(Math.random() * images.length);
			return images[index];
		}	
		
		// Cancel the animation behavior of the object with the given id.
		function CancelBehavior(id)
		{
		  if (gBehaviorsArray[id])
		  {
		    gBehaviorsArray[id].cancel();
		    gBehaviorsArray[id] = null;
		  }
		}
		//======================================================================
		
		function mainSlideshow(){
			var intervalReturn=self.setInterval("changeMainImage(1);",5000);
		}
		
		// Cycle through images on main page (sets background image on the main div)
		function changeMainImage(inc){
			if(inc == 0){
				photoIndex--;
				if(photoIndex < 0){photoIndex = 0;}
			}else{
				photoIndex++;
				if(photoIndex >= images.length){
					photoIndex = 0;
				}
			}
			
			getElement('mainImageCount').innerHTML = photoIndex;
			set_photo(images[photoIndex]);
		}
		
		function resizePage(){
			var winW = 1000;	// Default Window Width
			var winH = 650;		// Default Window Height
			var mainW = 900;	// Main Div Width
			var mainH = 685;	// Main Div Height
			var defaultMainH = 685;
			var defaultCanvasH = 600;
			var offsetW = 20;
			var offsetH = 20;
			
			if (window.innerWidth) {
			  winW = window.innerWidth;
			  winH = window.innerHeight;
			}else if (document.body.offsetWidth) {
			  winW = document.body.offsetWidth;
			  winH = document.body.offsetHeight;
		 	}
			
			// Get the current size of the main div
			displayOn('main');
			mainH = getElement('main').offsetHeight;
			mainW = getElement('main').offsetWidth;
			
			// Get the combined height of the logo and bottom nav elements
			var insideMainOffset = getElement('logo').offsetHeight + getElement('contact-bar').offsetHeight;
			
			// Offsets
			offsetW = (winW - mainW)/2;
			offsetH = (winH - mainH)/2;
			
			if(offsetW < 0) offsetW = 0;
			if(offsetH < 0) offsetH = 0;
			
			// If the viewport height is not large enough then: 
			// scale down the main div height
			// scale down the canvas div height
			// move up the nav elements on the bottom
			
			var overage = winH - mainH - 5;
			if(overage > 0) overage = 0;
			var adjustedMainH = mainH + overage;
			
			getElement('nesbit').innerHTML = 
			" Window Height: " + winH + 
			" MainH: " + mainH + 
			" mainW: " + mainW ;
		
			if(winH < mainH){
				getElement("main").style.height = adjustedMainH + 'px';
				getElement('canvas').style.height = ( adjustedMainH - insideMainOffset ) + 'px';
				if(getElement('canvas').style.display == "block"){
					getElement('contact-bar').style.top = 65 + getElement('canvas').offsetHeight + 'px';
					getElement('breadcrumb').style.top = 65 + getElement('canvas').offsetHeight + 'px';
				}
				else if(getElement('splash').style.display == 'block'){
					getElement('contact-bar').style.top = 105 + getElement('splash').offsetHeight + 'px';
					getElement('breadcrumb').style.top = 105 + getElement('splash').offsetHeight + 'px';
				}
			}else{
				document.getElementById('main').style.height = defaultMainH + 'px';
				getElement('canvas').style.height = defaultCanvasH + 'px';
				
			}
			
			// move main to keep it centered
			if(document.getElementById('main')){
				document.getElementById('main').style.top = offsetH + 5 + 'px';
				document.getElementById('main').style.left = offsetW + 'px';
			}
		}
		
		
		function makeRed(item){
			document.getElementById(item).style.color = "red";
		}
		
		function makeWhite(item){
			document.getElementById(item).style.color = "#ffffff";
		}
		
		function getElement(aID){
         	var rv = (document.getElementById) ? document.getElementById(aID) : document.all[aID];
         	//alert("id:" + aID + " value:" +  rv);
         	rv = document.getElementById(aID);
         	return rv;
       	}
		
		function allOff(){
			displayOff('slideshows');
			displayOff('slideshowAperture');
			displayOff('aboutJeff');
			displayOff('contact');
			displayOff('clients');
			displayOff('events');
			displayOff('canvas');
			displayOff('menu_wedding');
			displayOff('menu_portrait');
			displayOff('menu_commercial');
		}
		
		// Load slideshow iFrame with a new slideshow url
		function startShow(show){
			window.frames['iFrameSlideshow'].location = 'slideshows/' + show;
		}
		
		// Toggle display of DIV
		function toggleDisplay(me){
			if (me.style.display=="inline" || me.style.display=="block"){
				me.style.display="none";
			}
			else {
				me.style.display="block";
			}
		}
		
		// Toggle display of DIV
		function toggleFadeDisplay(me){
			if (me.style.display=="inline" || me.style.display=="block"){
				displayFadeOff(me);
			}
			else {
				displayFadeOn(me);
			}
		}
		
		// Turn on a DIV
		function displayOn(me){
			var elem = getElement(me);
			if(elem){
				elem.style.display="block";
			}
		}
		
		// Turn off a DIV
		function displayOff(me){
			var elem = getElement(me);
			if(elem){
				elem.style.display="none";
			}
		}
		
		// Fade Off an element
		function displayFadeOff(me, opacity){
			var elem = getElement(me);
			if(elem){
				gBehaviorsArray["fadeOffElement"] = new Spry.Effect.AppearFade(me, {duration: 300,from: opacity,to: 0, finish: 
		        			function(){
		        				gBehaviorsArray["fadeOffElement"] = null; 
								elem.style.display="none";
		        			}
				});
			}
		}
		
		// Fade On an element
		function displayFadeOn(elem, opacity){
			if(elem){
				setOpacityZero(elem);
				gBehaviorsArray["fadeOnElement"] = new Spry.Effect.AppearFade(elem, {duration: 500,from: 0,to: opacity, finish: 
		        			function(){
		        				gBehaviorsArray["fadeOnElement"] = null; 
							}
				});
			}
		}

		
		function getCoordX(event)
		{
			x=event.clientX
			return x;
		}
		
		function getCoordsY(event){
			y=event.clientY
			return y;
		}
	
		
		// Sets the background image on the main DIV
		function set_photo(img_path){
			gImageLoaded = 0;
			
			img = new Image();
			img.src = img_path;
			
			img.onload = function(){
				if(img_path){
					getElement("main").style.background='url('+ img_path +')';			
				}else{
					getElement("main").style.background='none';
				}	
			}
		}
			
			// Sets the background image on the main DIV
		function set_canvas_photo(img_path){
			img = new Image();
			img.src = img_path;
			
			img.onload = function(){
				if(img_path){
					getElement("canvas").style.background='url('+ img_path +')';			
				}else{
					getElement("canvas").style.background='none';
				}	
			}
		}
		
		// Sets the background image on the main DIV
		function set_slide_photo(img_path){
			img = new Image();
			img.src = img_path;
			
			img.onload = function(){
				//getElement("main").style.background='url(' + img.src + ')';
				
				getElement("mainImage").src = img_path;
				
				// Fade In behavior
				gBehaviorsArray["main"] = new Spry.Effect.AppearFade('main',{duration: 1000,from: 0,to: 100, finish: 
	        			function(){
	        				gBehaviorsArray["main"] = null; 
	        			}
	        		});
			}
			
		}
		
		// Sets the background color on the main DIV
		function setMainDivColor(hexColor){
			getElement("main").style.background=hexColor;
		}
		
		// Sets the border style on the main DIV
		function setMainDivBorderStyle(borderStyle){
			getElement("main").style.border=borderStyle;
		}
		
		
		// Function to fix PNG transparency problems in IE
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])
		function fixPNG(myImage) 
		{
		    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
		    {
		       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
			   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
			   var imgTitle = (myImage.title) ? 
				             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
			   var imgStyle = "display:inline-block;" + myImage.style.cssText
			   var strNewHTML = "<span " + imgID + imgClass + imgTitle
		                  + " style=\"" + "width:" + myImage.width 
		                  + "px; height:" + myImage.height 
		                  + "px;" + imgStyle + ";"
		                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
			   myImage.outerHTML = strNewHTML	  
		    }
		}


		// AJAX FUNCTIONS
		function retrieveURL(url, element) {
		    if (window.XMLHttpRequest) { // Non-IE browsers
		      req = new XMLHttpRequest();
		      nesbit = element;
		      req.onreadystatechange = processStateChange;
		      try {
		      	// alert("get " + url);
		        req.open("GET", url, true);
		      } catch (e) {
		        alert(e);
		      }
		      req.send(null);
		    } else if (window.ActiveXObject) { // IE
		      req = new ActiveXObject("Microsoft.XMLHTTP");
		      if (req) {
		        nesbit = element;
		        req.onreadystatechange = processStateChange;
		        req.open("GET", url, true);
		        req.send();
		      }
		    }
		}
		 
		function processStateChange() {
		  	//alert("readyState " + req.readyState);
		    if (req.readyState == 4) { // Complete
		      if (req.status == 200) { // OK response
		        getElement(nesbit).innerHTML = req.responseText;
		        //alert("response ok.  --> " + req.responseText );
		      } else {
		        alert("Problem: " + req.statusText + ": " + req.status);
		      }
		    }
		}

		function showDropdown(menubar){
			displayOff("menubar_portfolio_wedding");
			displayOff("menubar_stories_wedding");
			displayOff("menubar_portfolio_portrait");
			displayOff("menubar_portfolio_commercial");
			displayFadeOn(menubar, 85);
		}
		
		function collapseLogo(){
			var l = getElement('logo');
			l.innerHTML = "Gregg Nye Photography";
			l.style.backgroundImage = 'none';
			l.style.top = l.style.top - 65;
		}
		
		function setBreadcrumb(crumb){
			getElement('breadcrumb').innerHTML = crumb;
		}
		
		function appendBreadcrumb(crumb){
			var bc = getElement('breadcrumb');
			bc.innerHTML = bc.innerHTML + crumb;
		}
