
$(document).ready(function() {
	
	$('h1').fadeTo(0, 0.9);
	
	// galleries sub menu hover states
	var onSub = false;
	var onGall = false;
	
	$('#navGalleries').hover(
		
		function() {
			onGall = true;
			$(this).css({'background-position':'0px -44px'});
			
			if($(window).width() < 861) {
				$('#subNav').css({'right':($(window).width() - 470)+'px'});
			} else {
				$('#subNav').css({'right':'390px'});
			}
			
			$('#subNav').fadeIn();
		},
		
		function() {
			onGall = false;
			var gallNav = $(this);			
			var rollInt = setInterval(function() {
				clearInterval(rollInt);
								
				if(!onSub) {
					if(gallNav.hasClass('focus')) {
						gallNav.css({'background-position':'0px -88px'});			
					} else {
						gallNav.css({'background-position':'0px 0px'});
					}

					$('#subNav').fadeOut();
				}
				
			}, 50);
		}
	);
	
	
	// disbale gallery click
	$('#navGalleries').click(function(){
		return false;
	});
	
	$('#subNav').hover(
	
		function() {
			onSub = true;
		},
		
		function() {
			onSub = false;
			var gall = $(this);
			var rollInt = setInterval(function() {
				clearInterval(rollInt);
				
				if(!onGall) {
					$('#navGalleries').css({'background-position':'0px 0px'});
					gall.fadeOut();
				}
				
			}, 50);
			
		}
	
	);
	
	$('#subNav a:last-child').css({'border-bottom':'none'});
	// end sub menu
	
	
	// if pricing page (currently page id 20)
	if($('body').hasClass('page-id-20') || $('body').hasClass('page-id-18')) {
		
		
		var imgSrc = ($('body').hasClass('page-id-18')) ? '/images/aboutPic.jpg' : '/images/pricingPic1.jpg';
		
		var pageImg = new Image();
		var pageImgWidth;
		var pageImgHeight;

		$(pageImg)
			.load(function() {
				$(this).hide();
				pageImgWidth = this.width;
				resizePageImage();
				$('#displayImage').append(this);
				$(this).fadeIn(1000);
			})
			
			.error(function() {})
			.attr('src', siteRoot+imgSrc)
		;
		
		
		// function to resize image on pricing and about page
		function resizePageImage()
		{
			if($(window).height() > pageImgWidth) {
				$(pageImg).css({'height':''+$(window).height()+'px'});
			}
		}
		
		
		// once loaded, setup resize
		$(window).resize(function() { resizePageImage(); });
	
	}

	
	
	// if a gallery page
	if(isGallery) {
		
		var gallWidth = $('#galleryContainer').width();
		var totalImgs = $('#images').children().length;
		var ctr = 1;
		var xLoc = 0;
		
		$('#btnLeft').fadeTo(0, 0.4);
		$('#btnStart').fadeTo(0, 0.4);
			
		$('#btnLeft').click(function() {
			movePanel($(this).attr('id'));		
			return false;
		});
		
		$('#btnStart').click(function() {
			movePanel($(this).attr('id'));		
			return false;
		});
		
		$('#btnRight').click(function() {
			movePanel($(this).attr('id'));
			return false;
		
		});
						
		function movePanel(btn) {
							
			if(btn == 'btnRight' && ctr < (totalImgs-1)) {
				
				var focusImg = $('#images :nth-child('+ctr+')');
				
				xLoc -= (focusImg.width()+20);
				
				ctr ++;
				
			}
			
			if(btn == 'btnLeft' && ctr>1) {
				
				var focusImg = $('#images :nth-child('+(ctr-1)+')');
				
				xLoc += (focusImg.width()+20);
				
				ctr --;
			}
			
			if(btn == 'btnStart' && ctr>1) {
				
				xLoc = 0;
				
				ctr = 1;
			}
			
			var msg = (ctr>0) ? ctr+' of '+(totalImgs-1) : ' ';
			
			sIFR.replacements['#counter'][0].replaceText(msg);
			
			// toggle visibility of buttons, depending on gallery position
			
			if(ctr == (totalImgs-1)) {
				$('#btnRight').fadeTo(400, 0.4);
			}
			if(ctr == (totalImgs-2)) {
				$('#btnRight').fadeTo(400, 1);
			}
			if(ctr == 1) {
				$('#btnLeft').fadeTo(400, 0.4);
				$('#btnStart').fadeTo(400, 0.4);
				$('#btnRight').fadeTo(400, 1);
			}
			if(ctr == 2) {
				$('#btnLeft').fadeTo(400, 1);
				$('#btnStart').fadeTo(400, 1);
				$('h1').fadeOut();
			}
			
							
			
			// toggle visibility of logo, depending on gallery position
			
			$('#imageContainer').animate({left:xLoc}, 500, function() {
				if($(this).css('left') == '0px') {
					$('h1').fadeIn();
				}
			});
			
		}	
		
	}
	
	// end gallery
	
	
	// contact click
	
	$('.contactBtn').click(function() {
		
		if(isHome) {
			$('#contactInfo').css({'top' : '35%'});
		} else {
			$('#contactInfo').css({'top' : '45%'});
		}
		
		$('#contactus').show();
		
		sIFR.replace(gothamMedium, {
		  selector: '#tel'
		  , css: {
		  	'.sIFR-root': {'color': '#ffffff'}
		  }
		  , transparent: true
		});
		
		sIFR.replace(gothamMedium, {
		  selector: '#email'
		  , css: {
		  	'.sIFR-root': {'color': '#1db4e9'},
		  	'a' : {'color': '#ffffff', 'text-decoration' : 'none'},
		  	'a:link' : {'text-decoration' : 'none'},
		  	'a:hover' : {'text-decoration' : 'underline', 'color': '#ffffff'}
		  }
		  , selectable: false
		  , transparent: true
		});
		
		return false;
	
	});
	
	$('#closeContact').click(function() {
		$('#contactus').hide();
	});

	
	// homepage uniquness
	if(isHome) {
		
				
		function positionMenu() {
			
			var winHeight = $(window).height();
			var yOffset = Math.floor(winHeight*0.2);
			var ftrLoc = (winHeight - yOffset);
			
			$('#imageRotation').css({'height' : ftrLoc+'px'});
			$('#contactus').css({'height' : ftrLoc+'px'});
			$('#footer').css({'top' : (ftrLoc-44)+'px'});
			$('#subNav').css({'bottom' : (yOffset+44)+'px'});
		}		
		
		positionMenu();
		$('#footer').fadeIn();
		
		// home page image function
		
		var origW = leadImage[1];
		var origH = leadImage[2];
		
		// load image
		
		var img = new Image();
		
		$('#imageRotation').addClass('isLoading');
		
		$(img)
		
			.load(function() {
				$('#imageRotation').removeClass('isLoading');
				$(this).hide();
				$('#imageRotation').append(this);
				renderImage();
				$(this).fadeIn(1000);
			})
			
			.error(function() {})
			.attr('src', leadImage[0])
		;
		
		// once loaded, setup resize
		$(window).resize(function() { renderImage(); });
		
		
		function renderImage()
		{
			positionMenu();
			
			if($(window).width() > origW) {
				$(img).css({'margin-left':'0px'});
				$(img).css({'width':''+$(window).width()+'px'});
			} else {
				$(img).css({'width':''+origW+'px'});
				var xOffset = (($(window).width() - origW) / 2);
				$(img).css({'margin-left' : xOffset+'px'});
				$(img).css({'left':''+xOffset+'px'});
			}
		}
		
		// end home page images
	}
	
});
