// JavaScript Document

com.pennylane = {
	
	Nav:  Class.extend({
					   
		_navRoot:Number,
		_navSub:Number,
		_navAnimating:Boolean,
		
		_soundOn:String,
		_flashLoaded:Boolean,
		
		init: function(navroot, navsub) {
			
			var _this = this;
			
			_this._navRoot = navroot;
			_this._navSub = navsub;
			_this._flashIsLoaded = false;
			
			$(document).ready(function(){
				_this._onReady();
			})
			
			$(window).load(function(){
				_this._onLoad();
			})
			
			$(window).resize(function() {
				_this._onWindowResize();
			});
		}, 
		
		_onLoad: function(){
			
			
		},
		
		_onReady: function(){
			var _this = this;
			
			_this._setRootNav();
			
			
			var _this = this;
			
			_this._soundOn = _this._readCookie("soundOn");
			
			// setting _this._soundOn to false by default means  
			// sound is on by default
			if(!_this._soundOn)
				_this._soundOn = "false";
				
			
			
			//Uses a setInterval to determine whether the internal
			//flash functions have loaded
			_this._flashLoaded();
			
			
			//DEPRECATED////////////
			// keep the nav of the current category open
			//_this._openNav();
			
			/*$('#navbar ul > li:not(:eq('+_this._navRoot+'))').hover(
				function() {
					$('ul li', this).slideDown();
				}, function() {
					$('ul li', this).slideUp("fast");
				}
			)*/
			
			if(_this._navSub != undefined)
				_this._setSubNav();
				

			$('.subnav').hover(
				function() {
					var outertarget = $(this);
					//if($(outertarget).attr("state") == "up"){
						$('ul li', this).slideDown("slow", function(){
					//		$(outertarget).attr("state", "down");								
						});
					//}
				}, function() {	
					var outertarget = $(this);
				
					//if($(outertarget).attr("state") == "down"){
						$('ul li', this).slideUp("fast", function(){
						//	$(outertarget).attr("state", "up");						  
						});
					//}
				}
			)
						
			$('.category').mouseover(function(){
				var index = $(this).parent().index();
				getFlashMovie("SoundPlayer").playFlashSound(index);
			 })
			
			$('.subnav ul li').mouseover(function(){
				getFlashMovie("SubnavPlayer").playFlashSound(0);
				
			})
			
			_this.stageRight = $("#stage").offset().left + $("#stage").width();
			
			//position grey logo to the right but don't position on the "team" page
			if ($("#sidelogo").length > 0 && $("#teamoverview").length == 0) {
				$("#sidelogo").css({"left":_this.stageRight + 20 + "px", "display":"block"})
			}
			
			if($("#soundicon").length > 0){
				$("#soundicon").css({"left":_this.stageRight + 10 + "px", "display":"block"})
			}
			
			$('#soundicon').click(function(){
				_this._setSoundState();
			 })
			
			$("img.rollover").hover(function(){
				this.src = this.src.replace("off", "over");					 
			 }, function(){
				this.src = this.src.replace("over", "off");	
			 })
			
			//console.log(window.frames['googlemap'].document)
			$("#test").click(function(){
				var target = top.window.frames['googlemap'].document;
				$('a', target).attr("target", "_blank");
			})
			
		}, 
		
		_onWindowResize: function() {
			var _this = this;
			//position grey logo to the right but don't position on the "team" page
			if ($("#sidelogo").length > 0 && $("#teamoverview").length == 0) {
				var stageRightEdge = $("#stage").offset().left + $("#stage").width();
				$("#sidelogo").css({"left":stageRightEdge + 20, "display":"block"})
			}			
		},
		
		_setRootNav: function(){
			var _this = this;
			$('#navbar ul > li:eq('+_this._navRoot+') > a').addClass("active");
		}, 
		
		_setSubNav: function(){
			var _this = this;
			$('#navbar ul > li:eq('+_this._navRoot+') ul li:eq('+_this._navSub+') a').addClass("active");
		}, 
		
		_flashLoaded: function() {
			
			var _this = this;
			
			var soundCheck = setInterval(function(){
				
				if(getFlashMovie("SoundPlayer").setFlashVolume && getFlashMovie("SubnavPlayer").setFlashVolume){
					_this._flashIsLoaded = true;
				}
				
				if(_this._flashIsLoaded){
					_this._setSoundState();
					clearInterval(soundCheck);
				}
			}, 30)
		},
		
		_setSoundState: function(){
			var _this = this;
			
			if(_this._soundOn == "true"){
				getFlashMovie("SoundPlayer").setFlashVolume(0);
				getFlashMovie("SubnavPlayer").setFlashVolume(0);
				var newSrc = $('#soundicon img').attr("src").replace("soundOn", "soundOff");
				$('#soundicon img').attr("src", newSrc);
				_this._soundOn = "false";
				_this._createCookie("soundOn", "true", 1);
			} else {
				getFlashMovie("SoundPlayer").setFlashVolume(0.5);
				getFlashMovie("SubnavPlayer").setFlashVolume(0.1);
				var newSrc = $('#soundicon img').attr("src").replace("soundOff", "soundOn");
				$('#soundicon img').attr("src", newSrc);
				_this._soundOn = "true";
				_this._createCookie("soundOn", "false", 1);
			}
		},
		
		_createCookie: function(name,value,days) {
			if(days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		},
		
		_readCookie: function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
		
		////////// DEPRECATED ////////////
		/*_openNav: function(){
			var _this = this;
			$('#navbar ul > li:eq('+_this._navRoot+') ul li').show();
		},*/
		
	}), 
	
	Team: Class.extend({
					   
		_teamMembers:Array(),
		_currentIndex:Number,
		
		// the "team" page has two states,
		// that dictate the position of the grey logo
		_teamState:String,
					   	
		init: function() {
			
			var _this = this;
			_this._teamState = "thumbs";
						
			$(document).ready(function(){
				_this._onReady();	
			})
			
			$(window).resize(function() {
				_this._onWindowResize();
			});
		}, 
		
		_onReady: function(){
			var _this = this;
			
			// hide the teamdetail div using javascript so that its contents
			// stays apparent in dreamweaver in design mode (so client can make edits)
			$("#teamdetail").hide();
			$("#teamdetailnav").hide();
			
			_this._positionTeamLogo();
			//////
						
			$(".detail").each(function(){
				_this._teamMembers.push($(this).attr("id"));				
			})
			
			$(".detail").click(function(){
				_this._nextTeamMember();				
			})
			
						
			$(".thumb").click(function(){
				var detailTarget = $(this).attr("rel");	
				
				$("#teamoverview").fadeOut(200, function(){						 
					$("#teamdetail").fadeIn(500);
					$("#teamdetailnav").show();
					
					var contentHeight = $("#teamdetail").height();
					$("#teamdetailwrapper").css({"height": contentHeight + "px"});
					
					
				});
				
				for(var i = 0; i < _this._teamMembers.length; i++){
					if(_this._teamMembers[i] == detailTarget){
						_this._currentIndex = i;
					}
				}
				
				_this._teamState = "detail";
				_this._positionTeamLogo();
				
				_this._goTo(_this._currentIndex);
			})
			
			
			$("#team").click(function(){
				$("#teamdetail").fadeOut(200, function(){
					$("#teamdetailnav").hide();
					$("#teamoverview").fadeIn(500);
					
					_this._teamState = "thumbs";
					_this._positionTeamLogo();
				})
			})
			
			
			$("#team_leftarrow").click(function() {
				_this._previousTeamMember();
			})
			
			$("#team_rightarrow").click(function() {
				_this._nextTeamMember();
			})
	
		},
		
		
		_onWindowResize: function(){
			var _this = this;
			_this._positionTeamLogo();
		},
		
		
		_positionTeamLogo: function(){
			var _this = this;
			
			if(_this._teamState == "thumbs") {
				var stageCenter = ($(window).width() - $("#sidelogo").width())/2;
				$("#sidelogo").css({"left":stageCenter, "display":"block"});
			} else if (_this._teamState == "detail"){
				var stageRightEdge = $("#stage").offset().left + $("#stage").width();
				$("#sidelogo").css({"left":stageRightEdge + 20, "display":"block"})
			}
		},

		
		_nextTeamMember: function(){
			var _this = this;
			
			_this._currentIndex++;
			
			if(_this._currentIndex > _this._teamMembers.length - 1){
				_this._currentIndex = 0;
			}
			
			_this._animateTo(_this._currentIndex);
		}, 
		
		_previousTeamMember: function(){
			var _this = this;
			
			_this._currentIndex--;
			
			if(_this._currentIndex < 0){
				_this._currentIndex = _this._teamMembers.length - 1;
			}
			
			_this._animateTo(_this._currentIndex);
		}, 
		
		_animateTo: function(index){
			$('#teamdetail').animate({ left: 800 * -index }, 1200, "easeOutQuart", function() {
				// Animation complete.
  			});
		},
		
		_goTo: function(index) {
			$('#teamdetail').css({"left": 800 * -index });
		}
		
	}), 
	
	PhotoGallery:  Class.extend({
		
		
		_currentIndex:Number,
		_thumbLength:Number,
		
		init: function() {
			var _this = this;
						
			$(document).ready(function(){
				_this._onReady();	
			})
		}, 
		
		_onReady: function() {
			
			var _this = this;
			
			//Current Index
			_this._currentIndex = 0;
			
			_this._thumbLength = $("#thumbimages img").size();
			
			var contentHolderWidth = (_this._thumbLength * 90) + 25;
			myfy.setContentWidth(contentHolderWidth);
			$("#thumbimages").css({width : contentHolderWidth});
			$("#thumbswrapper").css({width : contentHolderWidth - 25});
			
			// dynamically set the width of the thumbs div (#content) for 
			// the SideScroller class in myfy
			var imagesWidth = $("#largeImages img").length * $("#largeContent").width();
			$("#largeImages").css({"width":imagesWidth});
			
			$("#largeImages img").each(function(i){
				//adjust individual top and left margins
				var top = ($("#largeImages").height() - $(this).height())/2;
				var left = (i * $("#largeContent").width()) + ($("#largeContent").width() - $(this).width())/2 - 10;
				$(this).css({"top":top +"px", "left":left+"px"});
			})
			
			$("#largeImages img").click(function(){
				_this._next();							 
			})
			
			$("#thumbimages img").click(function(){
				_this._animateTo($(this).index());
				_this._currentIndex = $(this).index();
			})
			
			
			$("#thumbimages img").hover(function(){
			   $(this).stop().animate({width: '102px', height: '102px', "margin-top":'-6px'}, 200);
			}, function() {
			   $(this).stop().animate({width: '90px', height: '90px', "margin-top":'0'}, 200);
			})
			
						
			$("#next").click(function(){
				_this._next();			
			})
			
			$("#previous").click(function(){
				_this._previous();				  				
			})

		}, 
		
		_next: function(){
			var _this = this;
			_this._currentIndex++;
				
			if(_this._currentIndex > _this._thumbLength - 1) {
				_this._currentIndex = 0;
			}
			
			_this._animateTo(_this._currentIndex);
			
		},
		
		_previous: function(){
			var _this = this;
			_this._currentIndex--;
				
			if(_this._currentIndex < 0 ) {
				_this._currentIndex = _this._thumbLength - 1;
			}
			
			_this._animateTo(_this._currentIndex);
		},
		
		_animateTo: function(index){
			$("#largeImages").animate({ left: 800 * -index }, 1200, "easeOutQuart");
		},
		
		_goTo: function(index) {
			$("#largeImages").css({"left": 800 * -index });
		}
		
		
	}),	
	
	Utils: Class.extend({
		init: function() {
			var _this = this;
			
			$(document).ready(function(){
				_this._onReady();	
			})
		},   
		
		nothing: function(){
			//do nothing
		}, 
		
		_onReady: function(){
			$("img.rollover").hover(function(){
				this.src = this.src.replace("off", "over");					 
			 }, function(){
				this.src = this.src.replace("over", "off");	
			 })
		}	
					   
	})
}
