// site wide jscript for stevenrbassetinc.com

// quick and dirty namespace check
if ( typeof(srb) == "undefined" ) srb = {};

srb.app = (function() {
	
	function init() {
		// these methods need calling for every page
		addListeners();
		highLightCurrentPage();
		// these are page specific
		var locMap = getLocationMap();
		switch(locMap.justFileNameNoExtension) {
			case 'landscapeworks_portfolio':
				if ( isAGradeBrowser() ) {
					initPhotoDisplay();
				}
				break;
			case 'arborcare_portfolio':
				if ( isAGradeBrowser() ) {
					initPhotoDisplay();
				}
				break;
			default:
				// noop
		}
	}

	// menu stuff, happens on all pages

	function srbUnload() {
		getMenuNodes().removeClass('current-page');
	}

	function addListeners() {
		getMenuNodes().addClass('menu-out').hover(menuOver, menuOut);
		$(window).bind('unload', srbUnload);
	}

	function getMenuNodes() {
		return $('#srb-menu ul li');
	}

	function getLocationMap() {
		var locMap = {};
		var loc = window.location + '';
		var locationArray = loc.split('/');
		locMap.justFileName = locationArray[locationArray.length - 1];
		locMap.folderOrDomain = locationArray[locationArray.length - 2];
		locMap.justFileNameNoExtension = locMap.justFileName.split('.')[0];
		return locMap;
	}

	function highLightCurrentPage() {
		var locMap = getLocationMap();
		var justFileName = locMap.justFileName;
		var folderOrDomain = locMap.folderOrDomain;
		var justFileNameNoExtension = locMap.justFileNameNoExtension;
		// now we can loop through the menu nodes and highlight the active one
		getMenuNodes().each(function(index, item) {
    		var currentHref = $('a', this).attr('href');
    		var currentFileNameNoExt = currentHref.split('.')[0]
    		// highlight the current item if start of href matches current url
    		if ( currentFileNameNoExt !== '' && justFileNameNoExtension.indexOf(currentFileNameNoExt) == 0 ) {
    			$(this).addClass('current-page');
    		}
  		});
		// special case for home page, index.html and contact_us/index.php
		if ((justFileName == 'index.php' && folderOrDomain == 'contact_us') || (justFileName == '' && folderOrDomain == 'contact_us')) {
			justFileName = '..\/contact_us\/index.php';
			$("a[href~='" + justFileName + "']", getMenuNodes()).parents('li').addClass('current-page');
		} else if (justFileName == '') {
			justFileName = 'index.html';
			$("a[href~='" + justFileName + "']", getMenuNodes()).parents('li').addClass('current-page');
		}
	}

	function menuOver(event) {
		$(event.currentTarget).addClass('menu-over').removeClass('menu-out');
	}
	
	function menuOut(event) {
		$(event.currentTarget).removeClass('menu-over').addClass('menu-out');
	}

	// photo display methods
	
	var imgLoader = null;
	var photoDisplay = null;
	var mask = null;
	var photoIsShowing = false;
	
	// this should only be called once
	function initPhotoDisplay() {
		// create a mask
		mask = $('<div id="srb-mask"></div>');
		$('body#srb-second-tier').prepend(mask);		
		// create a hidden photodisplay to start
		photoDisplay = $('<div></div>').addClass('srb-portfolio-display').hide();
		$('body#srb-second-tier').prepend(photoDisplay);
		// create an image to load the pics into
		imgLoader = new Image(); // preload image
		// place it in the photo display div
		$(photoDisplay).prepend($(imgLoader));
		
		// add listeners to all the photo items and the displau
		getPhotoItems().click(onPhotoItemClick);
		$(photoDisplay).click(onPhotoDisplayClick);
	}

	function getPhotoItems() {
		return $('.srb-portfolio-item');
	}
	
	// photo display event handlers
	
	function onPhotoItemClick(e) {			
		
		// if there is an image showing, hide it, otherwise do the do
		if ( photoIsShowing ) {
			
			hidePhotoDisplay();

		} else {
			
			// figure out the img src from the markup of the item
			var photoUrl = $(this).children('a').attr('href');
			// set the url of our imgLoader
			$(imgLoader).attr('src', photoUrl);
			
			if ( imgLoader.complete ) {
				showPhotoDisplay(imgLoader.width, imgLoader.height);
			} else {
				$(imgLoader).bind('load', onImgLoad);	
			}
			
		}
		
		// this should effectively eat the click event
		return false;
		
	}

	function onImgLoad(e) {
		$(imgLoader).unbind('load');
		showPhotoDisplay(this.width, this.height);
	}
	
	function onPhotoDisplayClick(e) {
		hidePhotoDisplay();
	}

	function onMaskClick(e) {
		hidePhotoDisplay();
	}

	function onWindowResize(e) {
		// just resizing
		var windowWidth = $(this).width();
		var displayWidth = $(photoDisplay).outerWidth();
		var displayLeft = (windowWidth/2) - (displayWidth/2);
		$(photoDisplay).css('left', displayLeft);
	}

	// photo display action methods
	
	function showPhotoDisplay(width, height) {
		
		var maskSize = 22000;
		var fudgeFactor = 35;
		var padding = 0;
		var positionTop = 10;
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		var documentHeight = $(document).height();
		var documentWidth = $(document).width();
		var displayWidth = width + padding;
		var displayHeight = height + padding;
		var displaytTop = 15;
		var displayLeft = (windowWidth/2) - (displayWidth/2);
		
		// find out if we need to resize or not
		var widthObj = getWidthResize();
		var heightObj = getHeightResize();
		
		function getImgLoaderDims() {
			var ret = {};
			if ( !photoIsShowing ) { $(photoDisplay).show(); }
			ret.width = $(imgLoader).outerWidth();
			ret.height = $(imgLoader).outerHeight();
			if ( !photoIsShowing ) { $(photoDisplay).hide();  }
			return ret;
		}
		
		function getWidthResize() {
			var ret = {}
			// simple boolean so we know if resize was needed
			if (windowWidth - fudgeFactor - ( padding * 2 ) < displayWidth ) {
				ret.needResize = true;
				ret.width =  windowWidth - fudgeFactor - ( padding * 2 );
			} else {
				ret.needResize = false;
				return ret
			}
			return ret
		}
		
		function getHeightResize() {
			var ret = {}
			// simple boolean so we know if resize was needed
			if (windowHeight - fudgeFactor - positionTop - ( padding * 2 ) < displayHeight ) {
				ret.needResize = true;
				ret.height =  windowHeight - fudgeFactor - ( padding * 2 ) - positionTop;
			} else {
				ret.needResize = false;
				return ret
			}
			return ret
		}
		
		function resizePhoto(resizeObj) {
			
			if ( resizeObj.width ) {
				$(imgLoader).attr('width', resizeObj.width );
				var dims = getImgLoaderDims();
				displayWidth = dims.width + padding;
				displayHeight = dims.height + padding;
			}
			if ( resizeObj.height ) {
				$(imgLoader).attr('height', resizeObj.height);
				var dims = getImgLoaderDims();
				displayWidth = dims.width + padding;
				displayHeight = dims.height + padding;
			}
			
			displaytTop = 15;
			displayLeft = (windowWidth/2) - (displayWidth/2);
			
		}
		
		// resize the width first
		if ( widthObj.needResize ) {
			resizePhoto(widthObj);
			// reset the height obj, so we can check again below and resize more if need be
			heightObj = getHeightResize();
			
		}
		
		// do the height if is still needs it
		if ( heightObj.needResize && ((widthObj.needResize && heightObj.width < widthObj.width) || !widthObj.needResize)  ) {
			resizePhoto(heightObj);
		}
		
		// add the listeners that will only be active while the photo is showing
		$(window).bind('resize', onWindowResize);
		$(mask).bind('click', onMaskClick);		
		// style the display, and show the mask before the photo
		var maskStyle = { width: maskSize + 'px', height: maskSize + 'px', opacity: .5 };
		$(mask).css(maskStyle).show();		
		var displayStyle = {width: displayWidth + 'px', height: displayHeight + 'px', top: displaytTop + 'px', left: displayLeft + 'px', display: 'none' };
		$(photoDisplay).css(displayStyle).fadeIn(750);		
		// toggle photoIsShowing to keep state
		photoIsShowing = true;

	}

	function hidePhotoDisplay() {
		// remove the listeners that will onlt be active while the photo is showing
		$(mask).unbind('click');
		$(window).unbind('resize');
		// now hide the photo display blank the source	
		$(photoDisplay).fadeOut(750, function() { $(imgLoader).attr('src', '').removeAttr('width').removeAttr('height'); } );
		// also hide the mask
		$(mask).css('opacity', 0).hide();
		// toggle photoIsShowing to keep state
		photoIsShowing = false;		
	}

	// utility methods
	
	function isAGradeBrowser() {
		if ( $.browser.webkit || $.browser.safari || $.browser.mozilla ) {
			return true;
		} else {
			return false;
		}
	}

	function isInitted() {
		if ( srb.app.isInitted ) {
			return true;
		} else {
			return false;
		}
	}

	return {
		init: init
	}

})();

$(document).ready(function() {
	// do stuff when DOM is ready
	srb.app.init();
});

