// JavaScript Document
/////////////////////////////////////////////////////////////////////////////Set Variables//////////////////////////////////////////////////////
var currentSection=0;
var nextSection=0;

var currentPhoto=1;
var nextPhoto=1;

var transTime=1000;
var transTime2=500;


var type_FP = "#fpUnit";
var type_R = "#rImage";

var currentType=type_FP;
var nextType=type_FP;

var currentCluster = 1;
var nextCluster = 1;

var firstFP=[0,1,5,10,11,14,16,20,26,27];
var firstR=[0,1,0,0,0,0,0,6,0,0];

//////////////////roll over attributes
var ROTime=400;
var ROon=1.0;
var ROoff=0.5;
/////////////////////////////////////////////////////////////////////////////Functions//////////////////////////////////////////////////////////
function imageFade(ct, nt, cp, np){
	$("img#fpbutton"+cp).stop().fadeTo(ROTime,0.0);
	$("img#fpbutton"+np).stop().fadeTo(ROTime,1.0);
	
	$("img#rbutton"+cp).fadeTo(ROTime,ROoff);
	$("img#rbutton"+np).fadeTo(ROTime,ROon);
	
	$(ct+cp).fadeOut(transTime);
	$(nt+np).delay(transTime).fadeIn(transTime);
	
	currentType = nt;
	currentPhoto = np;
}

function navFade(ct, nt, cp, np, cc, nc){
	$("img#keyMO"+cc).fadeTo(ROTime,0.0);
	$("img#keyMO"+nc).fadeTo(ROTime,1.0);
	if(ct==type_FP){
		thisType = "#fpNav";
		thisKey = "#fpKey";
		thisToggle = "#fpToggle";
	}
	if(ct==type_R){
		thisType = "#rNav";
		thisKey = "#rKey";
		thisToggle = "#rToggle";
	}
	if(nt==type_FP){
		thisTypeNEXT = "#fpNav";
		thisKeyNEXT = "#fpKey";
		thisToggleNEXT = "#fpToggle";
		np=firstFP[nc];
		
		$("img#pdfBTN-off").delay(transTime*1.5).fadeIn(transTime2);
		$("img#pdfBTN-on").stop().delay((transTime*1.5)+transTime2).fadeIn(0);
	}
	if(nt==type_R){
		thisTypeNEXT = "#rNav";
		thisKeyNEXT = "#rKey";
		thisToggleNEXT = "#rToggle";
		np=firstR[nc];
		
		$("img#pdfBTN-off").fadeOut(transTime2);
		$("img#pdfBTN-on").stop().fadeOut(0);
	}
	if(nt=='x'){
		thisTypeNEXT = "x";
		thisKeyNEXT = "x";
		thisToggleNEXT = "x";
		
		$("img#pdfBTN-off").fadeOut(transTime2);
		$("img#pdfBTN-on").stop().fadeOut(0);
		
	}
	
	if(ct!=nt){
		$(thisKey+"BG").fadeOut(transTime2);
		$(thisKeyNEXT+"BG").delay(transTime2).fadeIn(transTime);
		$(thisKey).fadeOut(0);
		$(thisKeyNEXT).fadeIn(0);
		$(thisToggle).fadeOut(transTime2);
		$(thisToggleNEXT).fadeIn(transTime2);
	}
	
	$(thisType+cc).fadeOut(transTime2);
	$(thisTypeNEXT+nc).delay(transTime2).fadeIn(transTime2);
	currentType = nt;
	currentCluster = nc;
	imageFade(ct, nt, cp, np);
}


function thBrowseEnter(ct, nt, cp, np, cc, nc){
	navFade(ct, nt, cp, np, cc, nc);
	$("img#imageMap").fadeOut(0);
	$("#landing0").fadeOut(0);
	$(".coverText").fadeOut(transTime2);
	$("#coverParent").delay(transTime).fadeOut(0);
	
	$("img#backBTN").delay(transTime*2).fadeIn(transTime2);
	$("img#backBTN-on").delay((transTime*2)+transTime2).fadeIn(0);
}

function goBack(){
	nextType = 'x';
	nextPhoto = 'x';
	nextCluster = 'x';
	navFade(currentType, nextType, currentPhoto, nextPhoto, currentCluster, nextCluster);
	
	$("img#backBTN").delay(transTime).fadeOut(transTime2);
	$("img#backBTN-on").delay(transTime).fadeOut(transTime2);
	
	$("#coverParent").fadeIn(0);
	$("#landing0").delay(transTime*1.5).fadeIn(transTime);
	$(".coverText").delay(transTime*2).fadeIn(transTime);
	$("img#imageMap").delay(transTime*2.5).fadeIn(0);
}

function sectionRollIn(cSec){	/////Used to fade sections in and out after mouse click
		$("div#landing"+cSec).stop().fadeTo(transTime2,1.0);
}
function sectionRollOut(cSec){	/////Used to fade sections in and out after mouse click
		$("div#landing"+cSec).stop().fadeTo(transTime2,0.0);
}
function sectionRollOff(cSec){	/////Used to fade sections in and out after mouse click
		$("div#landing"+cSec).delay(500).animate({
			opacity: 0.0,
		}, 250);
}
/////////////////////////////////////////////////////////////////////////////Objects Classes Prototypes//////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////--Section Class ///////////////////////
function Section(name, fps, rs, cls) {
	this.name = name;//name
	this.fps = fps;//number of floor plans
	this.rs = rs;//number of renderings
	this.cls = cls;//number of clusters
}

Section.prototype.initSections = function() {
	for(i=1;i<this.fps;i++) {
		$("#fpUnit"+i).fadeOut(0);
		$("img#fpbutton"+i).fadeTo(0,0.0);
	}
	for(i2=1;i2<this.rs;i2++) {
		$("#rImage"+i2).fadeOut(0);
	}
	
	for(i3=1;i3<this.cls;i3++) {
		$("#fpNav"+i3).fadeOut(0);
		$("#rNav"+i3).fadeOut(0);
	}
	
	$("#rKey").fadeOut(0);
	$("#fpKey").fadeOut(0);
	$("#rKeyBG").fadeOut(0);
	$("#fpKeyBG").fadeOut(0);
	
	$("#fpToggle").fadeOut(0);
	$("#rToggle").fadeOut(0);
	
	$("img#pdfBTN-off").fadeOut(0);
	$("img#pdfBTN-on").fadeOut(0);
	
	$("img#backBTN").fadeOut(0);
	$("img#backBTN-on").fadeOut(0);
	
	$("img#imageMap").fadeTo(0,0.0);
	for(i=1;i<this.cls;i++) {
		$("#landing"+i).fadeOut(0);
		$("img#keyMO"+i).fadeTo(0,0.0);
	}

	this.createFPClickHandlers();
	this.createFPRollOverHandlers();
	this.createFPRollOutHandlers();
	
	this.createRClickHandlers();
	this.createRRollOverHandlers();
	this.createRRollOutHandlers();
	
	this.createFPKeyClickHandlers();
	this.createFPKeyRollOverHandlers();
	this.createFPKeyRollOutHandlers();
	
	this.createRKeyClickHandlers();
	this.createRKeyRollOverHandlers();
	this.createRKeyRollOutHandlers();
	
	this.createTHKeyClickHandlers();
	this.createTHKeyRollOverHandlers();
	this.createTHKeyRollOutHandlers();
	
	this.createBackBTNClickHandlers();
	this.createBackBTNRollOverHandlers();
	this.createBackBTNRollOutHandlers();
	
	$("#contentAreaA").fadeTo(transTime,1.0);
	$(".loading").fadeOut(transTime);


}
////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////--Section Buttons- floor plan thumb buttons ///////////////////////
Section.prototype.createFPClickHandlers = function() {
	for(i=1;i<this.fps;i++) {
		$("img#fpbutton"+i).click(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("button")+6);
			if(currentPhoto!=buttonIndex || currentType!=type_FP){
				nextPhoto=buttonIndex;
				nextType=type_FP;
				imageFade(currentType,nextType,currentPhoto,nextPhoto);
			}
		});
	}
}
Section.prototype.createFPRollOverHandlers = function() {
	for(i=1;i<this.fps;i++) {
		$("img#fpbutton"+i).mouseenter(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("button")+6);
			if(currentPhoto!=buttonIndex || currentType!=type_FP){
				$("img#fpbutton"+buttonIndex).stop().fadeTo(ROTime,0.5);
			}
		});
	}
}
Section.prototype.createFPRollOutHandlers = function() {
	for(i=1;i<this.fps;i++) {
		$("img#fpbutton"+i).mouseleave(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("button")+6);
			if(currentPhoto!=buttonIndex || currentType!=type_FP){
				$("img#fpbutton"+buttonIndex).stop().fadeTo(ROTime,0);
			}
		});
	}
}


////////////////////////////////////////////////////////////////--Section Buttons- render Thumb buttons ///////////////////////
Section.prototype.createRClickHandlers = function() {
	for(i=1;i<this.fps;i++) {
		$("img#rbutton"+i).fadeTo(0,ROoff);
		$("img#rbutton"+i).click(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("button")+6);
			if(currentPhoto!=buttonIndex || currentType!=type_R){
				nextPhoto=buttonIndex;
				nextType=type_R;
				imageFade(currentType,nextType,currentPhoto,nextPhoto);
			}
		});
	}
}
Section.prototype.createRRollOverHandlers = function() {
	for(i=1;i<this.fps;i++) {
		$("img#rbutton"+i).mouseenter(function(){

			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("button")+6);
			if(currentPhoto!=buttonIndex){
				$("img#rbutton"+buttonIndex).stop().fadeTo(ROTime,ROon);
			}
		});
	}
}
Section.prototype.createRRollOutHandlers = function() {
	for(i=1;i<this.fps;i++) {
		$("img#rbutton"+i).mouseleave(function(){

			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("button")+6);
			if(currentPhoto!=buttonIndex){
				$("img#rbutton"+buttonIndex).stop().fadeTo(ROTime,ROoff);
			}
		});
	}
}
////////////////////////////////////////////////////////////////--Section Buttons- floor plan key buttons ///////////////////////
Section.prototype.createFPKeyClickHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#fpkey"+i).click(function(e){
			e.preventDefault();
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("key")+3);
			if(currentCluster!=buttonIndex){
				
				nextCluster=buttonIndex;
				nextType=type_FP;
				navFade(currentType,nextType,currentPhoto,nextPhoto,currentCluster,nextCluster);
			}
		});
	}
}
Section.prototype.createFPKeyRollOverHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#fpkey"+i).mouseenter(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("key")+3);
			if(currentCluster!=buttonIndex){
				
				$("img#keyMO"+buttonIndex).stop().fadeTo(ROTime,0.5);
				
			}
		});
	}
}
Section.prototype.createFPKeyRollOutHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#fpkey"+i).mouseleave(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("key")+3);
			if(currentCluster!=buttonIndex){
				
				$("img#keyMO"+buttonIndex).stop().fadeTo(ROTime,0.0);
				
			}
		});
	}
}
////////////////////////////////////////////////////////////////--Section Buttons- Render Key buttons ///////////////////////
Section.prototype.createRKeyClickHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#rkey"+i).click(function(e){
			e.preventDefault();
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("key")+3);
			if(currentCluster!=buttonIndex){
				
				nextCluster=buttonIndex;
				nextType=type_R;
				navFade(currentType,nextType,currentPhoto,nextPhoto,currentCluster,nextCluster);
			}
		});
	}
}
Section.prototype.createRKeyRollOverHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#rkey"+i).mouseenter(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("key")+3);
			if(currentCluster!=buttonIndex){
				
				$("img#keyMO"+buttonIndex).stop().fadeTo(ROTime,0.5);
				
			}
		});
	}
}
Section.prototype.createRKeyRollOutHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#rkey"+i).mouseleave(function(){
			
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("key")+3);
			if(currentCluster!=buttonIndex){
				
				$("img#keyMO"+buttonIndex).stop().fadeTo(ROTime,0.0);
				
			}
		});
	}
}
////////////////////////////////////////////////////////////////--Section Buttons- TownHouseKey buttons///////////////////////
Section.prototype.createTHKeyClickHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#thc"+i).click(function(e){
			e.preventDefault();
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("thc")+3);
			nextCluster=buttonIndex;
			nextType=type_FP;
			currentType=type_R;
			thBrowseEnter(currentType, nextType, 0, 1, 0, nextCluster);
		});
	}
}
Section.prototype.createTHKeyRollOverHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#thc"+i).mouseenter(function(){
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("thc")+3);
			sectionRollIn(buttonIndex);
		});
	}
}
Section.prototype.createTHKeyRollOutHandlers = function() {
	for(i=1;i<this.cls;i++) {
		$("area#thc"+i).mouseleave(function(){
			var id = this.id;
			var buttonIndex = id.substring(id.indexOf("thc")+3);
			sectionRollOut(buttonIndex);
		});
	}
}
////////////////////////////////////////////////////////////////--Section Buttons - Back Button///////////////////////
Section.prototype.createBackBTNClickHandlers = function() {
	$("img#backBTN").click(function(){
		goBack();
	});
}

Section.prototype.createBackBTNRollOverHandlers = function() {
	$("img#backBTN").mouseenter(function(){
		$("img#backBTN").fadeTo(ROTime,0.5);
	});
}
Section.prototype.createBackBTNRollOutHandlers = function() {
	$("img#backBTN").mouseleave(function(){
		$("img#backBTN").fadeTo(ROTime,1.0);
	});
}




/////////////////////////////////////////////////////////////////////////////Program--------->>///////////////////////////////////////////////////
$(function(){		///////Start of Program
/////////////////////////////////////////////////////////////////////////////Program--------->>///////////////////////////////////////////////////

	window.onload = function(){
		
		
				var sectionArray = [
				new Section(0, 28, 11, 10),
			];
		
			for(var i=0;i<sectionArray.length;i++) {
				sectionArray[i].initSections();
			};
		
			$("a#toggle-fp").click(function(e){
				e.preventDefault();
				if(currentType!=type_FP){
					nextCluster=currentCluster;
					nextType=type_FP;
					navFade(currentType,nextType,currentPhoto,nextPhoto,currentCluster,nextCluster);
				}
			});
			$("a#toggle-r").click(function(e){
				e.preventDefault();	   
				if(currentType!=type_R){
					if(currentCluster==7){
						nextCluster=7;
					}else{
						nextCluster=1;
					}
					nextType=type_R;
					navFade(currentType,nextType,currentPhoto,nextPhoto,currentCluster,nextCluster);
				}
			});

			$("#instructions").fadeOut(300);
			
			
			$("img#pdfBTN-off").click(function(){  
				var url = "plans/Unit"+currentPhoto+".pdf";
				window.open(url,'plan','left=20,top=20,width=1400,height=800,toolbar=1,resizable=0');
			});
			$("img#pdfBTN-off").mouseenter(function(){  
				$("img#pdfBTN-off").stop().fadeTo(ROTime,0.0);
			});
			$("img#pdfBTN-off").mouseleave(function(){  
				$("img#pdfBTN-off").stop().fadeTo(ROTime,1.0);
			});
		
		};



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
});					///////End of Program
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
