var baseURL = "http://picastudio.com/"; 
var testStr = "picastudio.com";


/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	DOCUMENT READY
	================================================================================ */
	$(document).ready(function() {
							  
							   
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	VARIABLES
	================================================================================ */
	var minDimensions = { width: 800, height: 600 }; 
	var currentDimensions = { width: 0, height: 0 };
	var prevImg;
	var currentImg;
	var currentUrl;
	var hrefHash = "#home";
	var prevImgRatio = 1;
	var currentImgRatio = 1;
	var windowRatio = 1;
	var contentArray = new Array();
	var navArray = new Array();
	var wRatio = 1;
	var currentMapItm;
	var mapViewFrame;
	var mapMask;
	var idCounter = 1;
	var sidebarOpen = true;
	var pageOpen = true;
		var closedW = 16;
		var lCol = 187;
		var cCol = 600; //temp val
		var rCol = 252;
		var lMargin = 16;
	var targProj;
	var targImg = 0;
	var galleryOpen = false;
	var isTweening = false;
	var imgTweening = false;
	var currentToggle;
	var scrollTimer;
	var winTimer;
	var speed = 1000;
	var currentClick = '';
	var lastClick = '';
	
	
	/*
	var map = null;
    var geocoder = null;
	*/
		function tweenComplete() {
			isTweening = false;
		}

/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	SLIDING PANELS
	================================================================================ */
	
	function resizeWin() {
		
		var sCount = 0;
			if ( minDimensions.width >= $(window).width() ) {
				currentDimensions.width = minDimensions.width;
			} else {
				currentDimensions.width = $(window).width();
			}
				if ( minDimensions.height >= $(window).height() ) {
					currentDimensions.height = minDimensions.height;
				} else {
					currentDimensions.height = $(window).height();
				}
		
		$('.chapter').each(function() {
			sCount++;
			$(this).width(currentDimensions.width);
		});
			$('#content').width(currentDimensions.width * sCount);
				$('.subpage').each(function() {					 
					if ($(this).height() < 	currentDimensions.height) {				 
						$(this).height(currentDimensions.height);
					}
						if (pageOpen) {
							$(this).width(currentDimensions.width - rCol - lMargin - lCol);
						}
							$(this).children('.pad').width(currentDimensions.width  - rCol - lMargin - lMargin - lMargin - lCol); //i don't know exactly why lMargin * 3
				});
				
				$('#home ul, #philosophy ul').each(function() {
					$(this).width(currentDimensions.width  - rCol - lMargin - lMargin - lMargin - lCol);
					
				});
				
				$('#home ul li, #philosophy ul li').each(function() {
					var maxW = 800;
					var theW = currentDimensions.width  - rCol - lMargin - lMargin - lMargin - lCol;											  
					$(this).width(theW);
					var percentage = theW/ maxW;
					
						if (percentage > 1) {
							percentage = 1;
						} else if (percentage < 0) {
							percentage = 0;
						}
					
					var size = ( 0.5 + (9.5 * percentage) ) * 10;
					
					$(this).css('font-size', size + 'px');
					$(this).css('line-height', (size + 3) + 'px');
					
				});
				
				cCol = currentDimensions.width - rCol - lMargin - lCol;
				
			$('#map_canvas').width(cCol - lMargin - lMargin - 2 -200);	
				
		$('#container').width(currentDimensions.width - lCol -25);
		
		
		//$('#container').height(currentDimensions.height);
		$('#container').scrollTo( $(hrefHash), {axis:'xy'} ); // this is causing problems on reload of href hash location???
	}
	
	function bindCloseBtns() {
		
		$(".close").each(function() {					 
			$(this).click(function(event) {			   
				event.preventDefault();
				if (galleryOpen) {
					closeGallery();
				}
				togglePageW(this, null);
			});		
		});
		
		$("#closenav").click(function(event) {			   
			event.preventDefault();
			if (galleryOpen) {
				closeGallery();
			}
			toggleSidebarW(null);
		});	
	}
	
	function togglePageW(targ, cllbck) {
			var targX = closedW;
					if (pageOpen) {
							$(".close").each(function() {					 
								$(this).css('background-image', 'url(/wordpress/wp-content/themes/index/images/global/arrow-right.gif)');		
							});
						
						pageOpen = false;
					} else {
						targX = cCol;
							$(".close").each(function() {					 
								$(this).css('background-image', 'url(/wordpress/wp-content/themes/index/images/global/arrow-left.gif)');		
							});
						
						pageOpen = true;
					}
					
			// open/close all other pages
				var targID = $(targ).parent().attr('id');
					$(".subpage").each(function() {				  
							if ($(this).attr('id') != targID) {
								$(this).width(targX);
							} 
					});
			// animate open/close of this page
				$(targ).parent().animate({
					width: targX
					}, speed, function() {
					toggleMiniPageW( $(targ).parent().attr('id'), cllbck );
				 });
	}
	
	function toggleMiniPageW(targ, cllbck) {
		
		
		
		var targID = "#" + targ;
		var targX = Math.round( $(targID).width()  *wRatio );
		

		
			// open/close all other pages
					$(".map_page").each(function() {				  
						if ($(this).attr('id') != "map_" + targ) {
							$(this).width(targX);
						} 
					});
					
			// animate open/close of this page
				$('#map_' + targ).animate( { width: targX }, speed, cllbck );
				
	}
	

		
	function toggleSidebarW(cllbck) {
		
		var targX = closedW;
			if (sidebarOpen) {
				$("#closenav").css('background-image', 'url(/wordpress/wp-content/themes/index/images/global/arrow-right.gif)');
				sidebarOpen = false;
			} else {
				targX = lCol;
				$("#closenav").css('background-image', 'url(/wordpress/wp-content/themes/index/images/global/arrow-left.gif)');
				sidebarOpen = true;
			}
		
		$('#sidebar').animate({
			width: targX
		  	}, speed);
		
		$('#container').animate({
			left: targX
		  	}, speed, function() {
			toggleMiniSidebarW(cllbck);
		  });
	}
	
	function toggleMiniSidebarW(cllbck) {
		var targX = Math.round( $('#sidebar').width() *wRatio );
		$('#map_sidebar').animate({
			width: targX
		  	}, speed, cllbck );
		
		$('#map_container').animate({
			left: targX + 1
		  	}, speed );
	}

	function toggleCycle() {
		if(hrefHash == '#home') {
			$('#philosophy ul').cycle('pause');
			$('#home ul').cycle('resume');
			
		} else if(hrefHash == '#philosophy') {
			$('#philosophy ul').cycle('resume');
			$('#home ul').cycle('pause');

		} else {
			$('#philosophy ul').cycle('pause');
			$('#home ul').cycle('pause');

		}
	}
	
	
	function navigateTo(itm) {
		var pathItms = $('#' + itm).attr("href").split("/");
		hrefHash = "#" + pathItms[ pathItms.length - 2 ].toLowerCase();
		
			if ( hrefHash.indexOf(testStr) != -1 ) {
				hrefHash = "#home";
			}
			if ( hrefHash.indexOf('#wordpress') != -1 ) {
				hrefHash = "#home";
			}
			pageTracker._trackEvent("navigation", hrefHash);
			
		toggleCycle();						 
			if ( $('#' + itm).parent().attr('class').indexOf('sect') != -1) {
				$('#' + itm).parent().siblings().children('ul').slideUp( 'normal');
				$('#' + itm).parent().children('ul').slideToggle('normal', toggleMiniNav );
			}
		$('#container').scrollTo( $(hrefHash), speed, {axis:'xy', queue:true, onAfter:setHash} );
		var mapTarg = "#map_" + hrefHash.substring(1);
			$('#map_container').scrollTo( $(mapTarg), speed, {axis:'xy', queue:true } );
	}
	
	
	function navigateURL(itm) {
		convertURLtoHref(itm)
		/*
		var pathItms = itm.split("/");
		hrefHash = "#" + pathItms[ pathItms.length - 2 ].toLowerCase();
			if ( hrefHash.indexOf(testStr) != -1 ) {
				hrefHash = "#home";
			}
			if ( hrefHash.indexOf('#wordpress') != -1 ) {
				hrefHash = "#home";
			}
			*/
			
		toggleCycle();						 
			
		$('#container').scrollTo( $(hrefHash), speed, {axis:'xy', queue:true, onAfter:setHash} );
		var mapTarg = "#map_" + hrefHash.substring(1);
			$('#map_container').scrollTo( $(mapTarg), speed, {axis:'xy', queue:true } );
	}
	
	
	function convertURLtoHref(itm) {
		var pathItms = itm.split("/");
		hrefHash = "#" + pathItms[ pathItms.length - 2 ].toLowerCase();
			if ( hrefHash.indexOf(testStr) != -1 ) {
				hrefHash = "#home";
			}
			if ( hrefHash.indexOf('#wordpress') != -1 ) {
				hrefHash = "#home";
			}
		
		$(".project a").each(function() {	
			var tempHash = "#" + $(this).attr('title');
			if ( tempHash == hrefHash ) {
				hrefHash = "#work";
				imgTweening = true;
				openGallery(this);
			}
		});
	
	}
	
	
	function setHash() {
		if (currentClick == lastClick) {
			window.location.hash = hrefHash;
			animateScrollbar();
		} else {
			currentClick = lastClick;
			navigateTo(currentClick);
		}
	}
	
	function bindNavBtns() { 
	
		$("#nav li a").each(function() {				 
			$(this).click(function(event) {	   
				lastClick = $(this).attr('id');
				if ( $(this).attr("href").indexOf(baseURL) != -1) {		   
					event.preventDefault();
					if (isTweening == false) {	
						isTweening = true;
						currentClick = lastClick;
						navigateTo(currentClick);
					}
				}
								   
								   
				/*if ( $(this).attr("href").indexOf("#") != -1) {	
					event.preventDefault();
					if (isTweening == false) {
							hrefHash = $(this).attr("href");
							toggleCycle();
							
							setTimeout( setHash, 1000 );
								if ( $(this).parent().attr('class') == 'sect' ) {
									$(this).parent().siblings().children('ul').slideUp( 'normal');
									$(this).parent().children('ul').slideToggle('normal', toggleMiniNav );
								}
								
							isTweening = true;	 
							$('#container').scrollTo( $(hrefHash), speed, {axis:'xy', queue:true, onAfter:setHash} );
							
							var mapTarg = "#map_" + hrefHash.substring(1);
							$('#map_container').scrollTo( $(mapTarg), speed, {axis:'xy', queue:true } );
					
					}
					
				}*/
				
			});	
			
		});
	}
	
	function toggleMiniNav() { 
		
		var targ = "#map_nav_" + hrefHash.substring(1);
		
		for ( i = 0; i < navArray.length; i++ ) {
			resizeMapElement(navArray[i]);
		}
		
		for ( i = 0; i < navArray.length; i++ ) {
			 if ( navArray[i].mapItm == targ ) {
				 $(targ).parent().siblings().children('ul').hide();
				
					if ( $(targ).parent().children('ul').css('display') == 'none' ) {
						$(targ).parent().children('ul').show();
					} else {
						$(targ).parent().children('ul').hide();
					}
			 } 
		}
		
	}
	

	
	
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	BG IMG GALLERY CONTROLS
	================================================================================ */
	
	function bindGalleryControls() {
		
		
		$('.project a').click(function(event) {	
			if ($(this).parent().parent().attr('class') != 'link') {
				// lispan inserted accounts for extra parent();
				event.preventDefault();
				imgTweening = true;
				
				//pageTracker._trackEvent( "projects", $(this).attr("href") );
				
				openGallery(this);
			}
		});
		
		$('#bcntrls .back').click(function(event) {	
			event.preventDefault();
			if (!imgTweening) {
				imgTweening = true;
				
				//original order
					/*var num = targImg - 1;
						if (num < 0) {
							num = $('#' + targProj + ' ul.imgs li').length -1;
						}*/
				
				//reverse order
					var num = targImg + 1;
					if (num >= $('#' + targProj + ' ul.imgs li').length ) {
						num = 0;
					}
					
				targImg = num;
				popControls();
				switchImgs();
			}
		});
		
		$('#bcntrls .hide').click(function(event) {	
			event.preventDefault();
			toggleSidebarW(null);
			var targPg = $(hrefHash).children('.close');	
			togglePageW(targPg, null);
			closeGallery();
		});
		
		$('#bcntrls .next').click(function(event) {	
			event.preventDefault();
			if (!imgTweening) {
				imgTweening = true;
				
				//original order
					/*var num = targImg + 1;
						if (num >= $('#' + targProj + ' ul.imgs li').length ) {
							num = 0;
						}*/
				
				//reverse order
					var num = targImg - 1;
						if (num < 0) {
							num = $('#' + targProj + ' ul.imgs li').length -1;
						}
					
				targImg = num;
				popControls();
				switchImgs();
			}
		});
		
		
		$('#selectprj').change(function () {
			//event.preventDefault();
			targProj = $(this).val();
			
			//original order
				//targImg = 0;
			
			//reverse order
				targImg = $('#' + targProj + ' ul.imgs li').length -1;
			
			popControls();
			switchImgs();
		});
		
		$('#cprv').click(function(event) {
		event.preventDefault();
		
		//original order
			/*var num = targImg - 1;
				if (num < 0) {
					num = $('#' + targProj + ' ul.imgs li').length -1;
				}*/
		
		//reverse order
			var num = targImg + 1;
			if (num >= $('#' + targProj + ' ul.imgs li').length ) {
				num = 0;
			}
			
		targImg = num;
		popControls();
		switchImgs();
		});
	
		$('#cnxt').click(function(event) {
			event.preventDefault();
			
			//original order
				/*var num = targImg + 1;
					if (num >= $('#' + targProj + ' ul.imgs li').length ) {
						num = 0;
					}*/
			
			//reverse order
				var num = targImg - 1;
				if (num < 0) {
					num = $('#' + targProj + ' ul.imgs li').length -1;
				}
				
			targImg = num;
			popControls();
			switchImgs();
		});
		
		$('#cprvprj').click(function(event) {
			event.preventDefault();
			
			var projNum = $('.project').index( document.getElementById(targProj) );
				var num = projNum - 1;
					if (num < 0) {
						num = $('.project').length -1;
					}
				projNum = num;
			targProj = $('.project').eq(projNum).attr('id');
			
			//original order
				//targImg = 0;
				
			//reverse order
				targImg = $('#' + targProj + ' ul.imgs li').length -1;
			
			
			popControls();
			switchImgs();
		});
		
		$('#cnxtprj').click(function(event) {
			event.preventDefault();
			
			var projNum = $('.project').index( document.getElementById(targProj) );
				var num = projNum + 1;
					if (num >= $('.project').length ) {
						num = 0;
					}
				projNum = num;
			targProj = $('.project').eq(projNum).attr('id');
			
			//original order
				//targImg = 0;
				
			//reverse order
				targImg = $('#' + targProj + ' ul.imgs li').length -1;
			
			popControls();
			switchImgs();
		});
		
		$('#cgrd').click(function(event) {	
			event.preventDefault();
			toggleSidebarW(null);
			var targPg = $(hrefHash).children('.close');	
			togglePageW(targPg, null);
			toggleGrid();
			closeGallery();
		});
		
		$('#clst').click(function(event) {			   
			event.preventDefault();
			toggleSidebarW(null);
			var targPg = $(hrefHash).children('.close');	
			togglePageW(targPg, null);
			toggleList();
			closeGallery();
		});
		
	}
	function openGallery(targ) {
		galleryOpen = true;
		if ($(targ).attr('class') == 'thmb') {
			targProj = $(targ).parent().attr('id');
		} else {
			targProj = $(targ).parent().parent().parent().parent().attr('id');// lispan inserted accounts for extra parent();
		}
		
		pageTracker._trackEvent( "project link", $("#" + targProj + " .thmb").attr("title") );

		if (sidebarOpen) {
			toggleSidebarW(null);
		}
		var targPg = $(hrefHash).children('.close');	
		togglePageW(targPg, openControls);
		
		
		fadegBGIn();
		
		
	}
	
	function closeGallery() {
		galleryOpen = false;
		$('#cntrl').slideToggle('normal');
		$('#bcntrls').fadeOut();
		fadeOut();
		//animate arrows up
		$('#closenav').animate({
			top: 3 }, 'normal' );
		$(hrefHash + ' .close').animate({
			top: 3 }, 'normal' );
		
		
		fadegBGOut();
		
		
	}
	
	function openControls() {
		
			//original order
				//targImg = 0;
				
			//reverse order
				targImg = $('#' + targProj + ' ul.imgs li').length -1;
			
			popControls();
			$('#cntrl').slideToggle('normal');
			
			$('#bcntrls').fadeIn();
			
			switchImgs();
			//animate arrows down
			$('#closenav').animate({
			top: 31 }, 'normal' );
			$(hrefHash + ' .close').animate({
			top: 31 }, 'normal' );
	}
	
	
	function popControls() {
		var projNum = $('.project').index( document.getElementById(targProj) );
		var tProjNum = $('.project').length;
		var weblink = $('#' + targProj + ' ul.dat li.link span').html();
		
		/*
		var title = "Project " + (projNum+1) + " / " + tProjNum + " : " + $('#' + targProj + ' ul.dat li.title a').html();
			if (weblink) {
				title += " " + weblink;
			}
			*/
			
		$('#ctitle').html(weblink);
		
		var num = $('#' + targProj + ' ul.imgs li').length;
			var str = (targImg + 1) + " / " + num;
			$('#ccntr span').html(str);
	}
	
	
	
		
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	BACKGROUND IMG LOADING
	================================================================================ */
		
	function resizeImg() {
		
		hght = window.innerHeight ? window.innerHeight : $(window).height(); //fix for iphone???
		
		windowRatio = $(window).width() / hght; 
		/* wide window > 1.0, tall window < 1.0 */
			if (currentImg && currentImg.attr('class') != 'pattern') {
				//currentImgRatio = currentImg.width() / currentImg.height();
					/* wide img > 1.0, tall img < 1.0 */
					if (windowRatio >= 1) {
							currentImg.height( $(window).height() );
							currentImg.css("width", "auto");
					} else {
							currentImg.width( $(window).width() );
							currentImg.css("height", "auto");
					}
			}
			
		if (prevImg  && prevImg.attr('class') != 'pattern') {
			//prevImgRatio = prevImg.width() / prevImg.height();
				if (windowRatio  >= 1) {
					prevImg.height( $(window).height() );
					prevImg.css("width", "auto");
				} else {
					prevImg.width( $(window).width() );
					prevImg.css("height", "auto");
				}
		}		
	}
	
	/*
	function resizeImg() {
		if (currentImg) {
			currentImg.height( $(window).height() );
			currentImg.css("width", "auto");
		}
		if (prevImg) {
			prevImg.height( $(window).height() );
			prevImg.css("width", "auto");
		}		
	}
	*/
	function switchImgs() {
		var imgUrl = $('#' + targProj + " .imgs li a").eq(targImg).attr('href');
		//var imgUrl = $('#' + targProj + " .imgs li img").eq(targImg).attr('src');
			currentUrl = imgUrl;
				if (prevImg) {
					prevImg.fadeOut("normal", fadeOutComplete);
				}
			fadeIn();
	}
	
	function fadeIn(op) {
		 currentImg = $('<img />').attr('src', currentUrl).load(function(){
			$('.bckgrnd').append( $(this) );
			resizeImg();
		   $(this).fadeIn("normal", fadeInComplete);
		});
	 }
	 
	 function fadeOutComplete () {
		 prevImg.remove();
	 }
	 
	 function fadeInComplete () {
		prevImg = currentImg;
		imgTweening = false;
	 }
	
	function switchBGImgs() {
			fadeBGOut();
			//fadeBGIn();
	}
	
	function fadeBGIn() {
			var imgUrl = $("#home .imgs li a").eq(0).attr('href');
			currentUrl = imgUrl;
			currentImg = $('<div class="pattern" style="background: url(' + imgUrl + ') repeat top left; width: 100%; height: 100%;"></div>' );
			$('.bckgrnd').append( currentImg );
				resizeImg();
			   $(currentImg).fadeIn("normal", fadeInBGComplete);	
	}
	
	function fadeBGOut() {
		//prevImg = currentImg;
		if (prevImg) {
			prevImg.fadeOut("normal", fadeOutBGComplete);
		}
	}
	
	 function fadeOut() {
		targImg = 0;
		prevImg = currentImg;
		if (prevImg) {
			prevImg.fadeOut("normal", fadeOutComplete);
		}
		//switchBGImgs();
	 }
	 
	 function fadeOutBGComplete () {
		 //prevImg.remove(); 
		 // above is causing a problem with background patterns after gallery close
		 // still don't know why.
	 }
	 
	 function fadeInBGComplete () {
		prevImg = currentImg;
		imgTweening = false;
	 }
	 
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	NEW GALLERY BG PATTERN
	================================================================================ */ 
	
	var bgPatt;
	var bgTweening = false;
	 
	 function fadegBGIn() {
		 	if (!bgTweening) {
				bgPatt = $('<div  style="position: absolute; top: 0; left: 0; background: url(/wordpress/wp-content/themes/index/images/bg/diagonal-lgt-red.gif) repeat top left; width: 100%; height: 100%;"></div>' );
				$('.bckgrnd').append( bgPatt );
				bgTweening = true;
	 		}
				//resizeImg();
			   $(bgPatt).fadeIn("normal", fadeIngBGComplete);	
	}
	
	function fadegBGOut() {
		if (bgPatt) {
			bgPatt.fadeOut("normal", fadeOutgBGComplete);
			bgTweening = true;
		}
	}
	
	
	 function fadeOutgBGComplete () {
		 bgPatt.remove();
		 bgTweening = false;
	 }
	 
	 function fadeIngBGComplete () {
		 bgTweening = false;
	 }

/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	CONTENT ACCORDIAN
	================================================================================ */
	
	
	function bindContentAccordians() {
		
		
		$("#content .accordian .toggle, #content .accordian a.hspan").each(function() {					 						 
			$(this).click(function(event) {			   
				event.preventDefault();
				currentToggle = '#' + $(this).attr('id');
				
					$(this).parent().parent().siblings().children('.slider').slideUp( 'normal');
					$(this).parent().parent().children('.slider').slideToggle('normal', toggleMiniAccordian );
					
				$(this).parent().parent().siblings('dl').attr('class', 'closed');
				
				if ( $(this).parent().parent().attr('class') == 'open') {
					$(this).parent().parent().attr('class', 'closed');
				} else {
					$(this).parent().parent().attr('class', 'open');
				}
			});	
		});
		
		
		$("#content .accordian").each(function() {	
			// open first accordian item on init
			$(this).children('dl').eq(0).attr('class', 'open');
		});
		
	}
							   
	function toggleMiniAccordian() {
		for ( i = 0; i < contentArray.length; i++ ) {
			thisID = contentArray[i].contentItm;	
				if ( $(thisID).height() != 0 ) {
					resizeMapElement(contentArray[i]);
				}
		}
			for ( i = 0; i < contentArray.length; i++ ) {
				if( contentArray[i].contentItm == currentToggle ) {
					$(contentArray[i].mapItm).parent().parent().siblings().children('.map_slider').slideUp( 'normal');
					$(contentArray[i].mapItm).parent().parent().children('.map_slider').slideToggle('normal', animateScrollbar );
				}
			}		
	}
		
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	RESIZE SCROLLBAR
	================================================================================ */
	
	function animateScrollbar() {	
		var h = ( $(hrefHash).height()) + "px";
		$('#container').animate( { height: h }, "slow", "linear", tweenComplete );
	}
	
	function bindScrollMap() {
		$(window).scroll( function () { 
			clearTimeout(scrollTimer);
			scrollTimer = setTimeout(scrollMap, 1000);
		});
	}
	
	function scrollMap() {
		var cTop = $('#content').position().top;
		var sTop = $(window).scrollTop();
		var cOffSet  = (cTop * -1) + sTop;
		var mOffSet = Math.round(cOffSet * wRatio);
		$('#map_container').scrollTo( mOffSet + 'px', 1000, {axis:'y' } );
	}
		
		
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	BUILD MINIMAP
	================================================================================ */	
		
	function buildMiniMap() {	
		$("#sidebar").before("<div id='minimap'></div>");
	
		mapMask = $("<div id='map_mask'></div>").appendTo("#minimap");
	
		currentMapItm = $("<div id='map_container'></div>").appendTo(mapMask);
			addToArray( $('#container'), currentMapItm, contentArray);
				parseHTMLNode( $('#container'), $('#map_container') );
		
		currentMapItm = $("<div id='map_sidebar'></div>").appendTo(mapMask);
			parseNavNode( $('#sidebar'), $('#map_sidebar') );
	}
	
			function addToArray(cItm, mItm, arr) {
				if ( !cItm.attr('id') ) {
					cItm.attr('id', "cItm" + idCounter);
				}
				if ( !mItm.attr('id') ) {
					mItm.attr('id', "mItm" + idCounter);
				}
				idCounter++;
				arr[arr.length] = { contentItm: '#' + cItm.attr('id'), mapItm: '#' + mItm.attr('id') };
			}
			
			function parseHTMLNode(content_container, map_container) {
				
				$(content_container).children().each(function() {	
							var isValid = true;							  
							var thisTagName = this.tagName.toLowerCase();	
							var thisClass = $(this).attr('class');						  
							var thisID = $(this).attr('id');
							
								switch ( thisTagName ) {
									
									case "form":
									case "a":
									case "img":
										thisTagName = 'div';
										break;
										
									case "select":
										thisTagName = 'span';
										break;
									
									default:
										break;
								}
								
						switch ( thisClass ) {
							case "toggle":		
							case "close":
							
								isValid = false;
								break;
							
							default:
								break;
						}
						
						switch ( thisID ) {
									
							case "closenav":
								isValid = false;
								break;
							
							default:
								break;
						}
							
					if (isValid) {	
								var str = '<' + thisTagName;
									if (thisID) {
										str += ' id="map_' + thisID + '"';
									}
										if (thisClass) {
											str += ' class="map_' + thisClass + '"';
										}
								str += '></' + thisTagName + '>';	
							currentMapItm = $(str).appendTo(map_container);
															  
						switch ( thisTagName ) {
							case "h1":
							case "h2":
							case "h3":
							case "h4":
							case "h5":
							case "h6":
								$(this).wrapInner('<span class="hspan"></span>');
								parseHTMLNode( $(this), currentMapItm );
								break;
								
							case "ul":
								if (thisClass != 'bg' && thisID != 'grid_list') {
									parseHTMLNode( $(this), currentMapItm );
								}
								break;
								
							case "dl":
							case "dt":
								parseHTMLNode( $(this), currentMapItm );
								break;
								
							case "a":
							case "span":
							case "p":
							
								// this is where there is a problem with ul ul li not displaying
								
								addToArray( $(this), currentMapItm, contentArray);
								break;
								
							case "li":	
								addToArray( $(this), currentMapItm, contentArray);
								
								var prntID = $(this).parent().parent().parent().parent().parent().parent().attr('id');
								
									if (prntID != 'process' && thisClass != 'project clearfix') {
										
										// perhaps you could adjust this so that it excludes process li ????
										
										$(this).wrapInner('<span class="lispan"></span>');
										
									} 
									
								parseHTMLNode( $(this), currentMapItm );
								break;
							
							case "dd":
							default:
								addToArray( $(this), currentMapItm, contentArray);
								parseHTMLNode( $(this), currentMapItm );
								break;
						}						   		
					}
				});	
			}
			
			
			function parseNavNode(content_container, map_container) {
				$(content_container).children().each(function() {									  
						switch ( this.tagName.toLowerCase() ) {
							case "ul":
								currentMapItm = $("<ul class='map_ul'></ul>").appendTo(map_container);
								addToArray( $(this), currentMapItm, navArray);
								parseNavNode( $(this), currentMapItm );
								break;
							
		
							case "li":
								var thisClass = "";
									if ( $(this).attr('class').indexOf('sect') != -1 ) {
										thisClass = "sect";
									} 
								currentMapItm = $("<li class='" + thisClass + "'></li>").appendTo(map_container);
								parseNavNode( $(this), currentMapItm );
								break;
								
							case "a":
								if($(this).attr('id') != 'closenav') {
									var thisID = $(this).attr('href');
										if (thisID.substring(0, 1) == "#") {
											var testStr = "map_nav_" + thisID.substring(1);
											currentMapItm = $("<div class='map_a' id='" + testStr + "'></div>").appendTo(map_container);
										} else {
											currentMapItm = $("<div class='map_a'></div>").appendTo(map_container);
										}
									addToArray( $(this), currentMapItm, navArray);
									parseNavNode( $(this), currentMapItm );
								}
								
								break;
								
							case "h2":
								currentMapItm = $("<span class='map_lispan'></span>").appendTo(map_container);
								addToArray( $(this), currentMapItm, navArray);
								break;

							case "span":
								currentMapItm = $("<span class='map_span'></span>").appendTo(map_container);
								addToArray( $(this), currentMapItm, navArray);
								break;
							
							
							case "p":
								currentMapItm = $("<p class='map_p'></p>").appendTo(map_container);
								addToArray( $(this), currentMapItm, navArray);
								break;
								
							case "h1":
								currentMapItm = $("<h1></h1>").appendTo(map_container);
								//addToArray( $(this), currentMapItm, navArray);
								parseNavNode( $(this), currentMapItm );
								break;
							
							default:
								break;
						}							   							   
				});	
				
			}
			
			
			
	
	
	function resizeMap() {
		if ( minDimensions.width < $(window).width()  && minDimensions.height < $(window).height() ) {
			$('#map_container').css('display', 'block');
			$('#map_sidebar').css('display', 'block');
				wRatio = $('#minimap').width() / $(window).width();
			var containerX = Math.round( $('#container').position().left * wRatio );
			$('#map_container').css('left', containerX);
				 for ( i = 0; i < contentArray.length; i++ ) {
					resizeMapElement(contentArray[i]);
					resizeMapPad(contentArray[i]);
				 }
					 for ( i = 0; i < navArray.length; i++ ) {
						resizeMapElement(navArray[i]);
						resizeMapPad(navArray[i]);
					 }
			var h = Math.round( $(window).height() * wRatio ); 
				mapMask.height( h );	
		} else {
			$('#map_container').css('display', 'none');
			$('#map_sidebar').css('display', 'none');
		}
		
		//scroll to new position
		var mapTarg = "#map_" + hrefHash.substring(1);
			$('#map_container').scrollTo( $(mapTarg), {axis:'xy', queue:true } );
		
		
	}	
	
	function resizeMapElement(arrItm) {
		var obj = new Object();
		
			if ($( arrItm.contentItm ).attr('class') == 'slider') {
				//adjust for extra 1px margin
				var count = $( arrItm.contentItm + ' ul li').length
				var h = Math.round( $( arrItm.contentItm ).height() * wRatio );
				obj['height'] = (h + count) + "px";
				
			} else {
				obj['height'] = Math.round( $( arrItm.contentItm ).height() * wRatio ) + "px";
			}
			
			//obj['height'] = Math.round( $( arrItm.contentItm ).height() * wRatio ) + "px";
			obj['width'] = Math.round( $( arrItm.contentItm ).width() * wRatio ) + "px";
			
		$( arrItm.mapItm ).css( obj );
	}
	
	
	function resizeMapPad(arrItm) {
		
		var obj = new Object();
			//margin:
				obj['margin-top'] = Math.round( convertPxString( $( arrItm.contentItm ).css('margin-top') ) * wRatio ) + "px";
				//obj['margin-bottom'] = Math.round( convertPxString( $( arrItm.contentItm ).css('margin-bottom') ) * wRatio ) + "px";
				if ( $( arrItm.contentItm ).attr('class') != 'slider' ) {
					obj['margin-left'] = Math.round( convertPxString( $( arrItm.contentItm ).css('margin-left') ) * wRatio ) + "px";
				}
			//padding:
				//obj['padding-top'] = Math.round( convertPxString( $( arrItm.contentItm ).css('padding-top') ) * wRatio ) + "px";
				//obj['pdding-bottom'] = Math.round( convertPxString( $( arrItm.contentItm ).css('padding-bottom') ) * wRatio ) + "px";
				obj['padding-left'] = Math.round( convertPxString( $( arrItm.contentItm ).css('padding-left') ) * wRatio ) + "px";
				obj['padding-right'] = Math.round( convertPxString( $( arrItm.contentItm ).css('padding-right') ) * wRatio ) + "px";
			//float:
				//obj['float'] = $( arrItm.contentItm ).css('float');
				//obj['clear'] = $( arrItm.contentItm ).css('clear');
			
		$( arrItm.mapItm ).css( obj );
		
	}
	
	
	function convertPxString(str) {
		var idx = str.indexOf("px");
		return Number( str.substring(0, idx) );
	}
		
		
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	GLOBAL WINDOW RESIZE EVENT
	================================================================================ */	
	
	function bindResize() {
		$(window).resize(resizeTimer);
	}
	
	function resizeTimer() {
		clearTimeout(winTimer);
		winTimer = setTimeout(resizeAll, 100);
	}
	
	function resizeAll() {
		resizeWin();
		resizeImg();
		resizeMap();
	}
	
	//resizeAll();
	//resizeAll();

	function initHash() {
		if (location.hash) {
			$("#nav li a").each(function() {
										 
				if  ($(this).attr("href") == location.hash ) {
					if ( $(this).parent().attr('class') == 'sect' ) {
						$(this).parent().children('ul').slideToggle('normal');
					} else {
						$(this).parent().parent().parent().children('ul').slideToggle('normal');
						
					}
				}						 					 
			});
			
			/*
			$("#content .chapter, #content .subpage").each(function() {				   
				if ( $(this).attr('id') == location.hash.substring(1) ) {
					//$('#container').scrollLeft(0);
					$('#container').scrollTo( $(this), 1000, {axis:'xy', queue:true} );
					
					var mapT = "#map_" + location.hash.substring(1);
					
					alert( $('#map_container').attr('id') );
					
					$('#map_container').scrollTo( $(mapT), 1000, {axis:'xy', queue:true } );
				}
			});
			*/
			
			hrefHash = window.location.hash;	
			
		} else {
			convertURLtoHref(window.location.href);
			//navigateURL(window.location.href);
		}
		
		toggleCycle();
		
		
	 }
	
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	WORK MENU TOGGLE LIST VS GRID PRESENTATION
	================================================================================ */	
	function bindGridList() {
		$('a.list_on, a.grid_off, a.list_off, a.grid_on').each(function() {
			$(this).click(function (event) {
					event.preventDefault();	
					//toggleGridList( $(this) );
					if ($(this).attr('class') == 'list_off' || $(this).attr('class') == 'list_on') {															
						toggleList();
					} else if ($(this).attr('class') == 'grid_off' || $(this).attr('class') == 'grid_on') {
						toggleGrid();
					}
			});	
			
		});	
	}
	
		function toggleGrid() {
			$('#list').attr('id', 'grid');
			$('#map_list').attr('id', 'map_grid');
			resizeMap();
			$('a.list_on').attr('class', 'list_off');
			$('a.grid_off').attr('class', 'grid_on');
			// imgs don't disappear in ie 6 or 7
		}
		
		function toggleList() {														
				$('#grid').attr('id', 'list');
				$('#map_grid').attr('id', 'map_list');
				resizeMap();
				$('a.list_off').attr('class', 'list_on');
				$('a.grid_on').attr('class', 'grid_off');
				// imgs don't disappear in ie 6 or 7
		}

/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	WORK MENU FILTER PROJECTS LIST
	================================================================================ */	
	function bindWorkFilters() {
		$('#work select').each(function() {							   
			$(this).change(function () {
				var filterBy = $(this).val();
				filterProj(filterBy);
					$('#work select').each(function() {
						if ($(this).val() != filterBy) {
							$(this).val('');
						}
					});					
			});	
		});
	}
	function filterProj(filterBy) {
		$('.project').each(function() {
			hasFilter = $(this).attr('rel').indexOf(filterBy);
				if (hasFilter == -1) {
					$(this).fadeTo("fast", 0.2);
				} else {
					$(this).fadeTo("fast", 1);
				}							   							   
		});
	}
	
	
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	FEEDBACK FORM VALIDATION
	================================================================================ */
	
	/*
	function bindFeedbackForm() {
		if ( $('body').attr('id') == 'contact') {
			$("#feedbackForm").validate();
			
			if (jQuery.url.param("confirm") == 'true') {
				$('#confirmation').show();
			} else {
				$('#confirmation').hide();
			}
		}
	}
	*/
	
	
	
/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	GOOGLE MAPS
	================================================================================ */

	
    function initMap() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(39.859123, -86.14593), 13);
		map.removeMapType(G_HYBRID_MAP);
		
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GSmallMapControl());
		
        geocoder = new GClientGeocoder();
		showAddress("5771 Central Ave, Indianapolis, IN");
      }
    }
	
	function unloadMap() {
		GUnload();
	}

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              //marker.openInfoWindowHtml("<h6>Pica Studio</h6><ul><li>5771 Central Avenue</li><li>Indianapolis, IN 46220</li><li>317 847 0744</li></ul>");
			  
            }
          }
        );
      }
    }
	

	

/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	PHILOSOPHY CYCLE
	================================================================================ */
	function bindCycle() {
		$('#philosophy ul, #home ul').each(function() {
											   
			$(this).cycle({ 
			fx: 'fade', 
			speed: 1000,
			timeout: 12000,
			height: 'auto',
			slideResize: 0
			});
			$(this).cycle('pause');
			
			$(this).hover(function() {
				$(this).cycle('pause');
			},function() {
				$(this).cycle('resume');
				});
		});
	}
	
	function buildFilterMenu() {
		//var counter = 0;
		$("#work .project").each(function() {							  
			var myID = $(this).attr('id');					  
			var myTXT	 = $( '#' + myID + ' .dat .title a' ).text();
			var str ='<option value="' + myID + '">' + myTXT + '</option>';
			$('#selectprj').append(str);
			//counter++;
		});
	}
	
	
	function displayHome() {
		$("#home ul").each(function() {							  
			$(this).show();					  
		});
		
		$("#map_home ul li").each(function() {							  
			$(this).hide();					  
		});
		
		$("#map_home ul li").eq(0).show();
	}

/*	////////////////////////////////////////////////////////////////////////////////

    --------------------------------------------------------------------------------
	
	END DOCUMENT READY
	================================================================================ */
	
	
		var test = ""; //detect browsers
	
	
		if( (navigator.userAgent.match(/MSIE/i)) || (navigator.userAgent.match(/Safari/i)) || (navigator.userAgent.match(/Firefox/i)) || (navigator.userAgent.match(/Opera/i)) ) {
			test = "browser";
		}
		
		if( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) ) {
			test = "iphone";
		}
	
	if( test == "browser" )  {
		
		bindCloseBtns();
		bindNavBtns();
		bindGalleryControls();
		bindContentAccordians();
		bindResize();
		bindScrollMap();
		buildMiniMap();
		initHash();
		bindGridList();
		bindWorkFilters();
		
		initMap();
		$(window).unload( unloadMap );
		
		displayHome();
		bindCycle();
		buildFilterMenu();
		resizeAll();
		//switchBGImgs();
		toggleCycle();
		
	} else if ( test == "iphone" ) { //detect iphone and ipad
		minDimensions = { width: 640, height: 600 };
		speed = 0;
		bindCloseBtns();
		bindNavBtns();
		bindGalleryControls();
		bindContentAccordians();
		bindResize();
		bindScrollMap();
		buildMiniMap();
		initHash();
		bindGridList();
		bindWorkFilters();
		
		initMap();
		$(window).unload( unloadMap );
		
		displayHome();
		bindCycle();
		buildFilterMenu();
		resizeAll();
		//switchBGImgs();
		toggleCycle()
		
	} else { // detect web spiders and everything else 
		displayHome();
	}
	
});

