var fade = 500;			// set the fade in/out speed
var slide = 300;		// set the slide up/down speed
var flip = 200;			// set the flip speed

// these are some variables that get used to track window locations
var currentDetailP = "";
var viewing = "";
var cFloor = 0;
var cClass = "";
var autoClose = "";
var screensaver = "";
var hashV = "";		// if the hash value is present in the URL jump to this property
var bHash = "";
var bHash_s = "";


// this is for demo only!!
var cFloor = 14;
var cFloorOptions_14 = "";
var cFloorOptions_21 = "";
var fCount = 0;
var aFloors = new Array();
var cFloorr = 0;

var pName = "";
var pNameS = "";


//All the dimensions of all the DIVs -- TIMO//
// **SEE DOCUMENT WHERE I DRAW IT ALL OUT** //
//var a = 892; 	// Containing DIV (mask)
var a = 1025; 	// Containing DIV (mask)
//var b = 1025; 	// Slide Bar movement
var b = boundaryA;
var c = 314;	// width of the Drag Bar
//var d = 1290;	// width of the upper sliding hero images, full width is 3347
var d = boundaryB;
var e = (d-a)/(b-c);
var f = (b-c);
var g = (b/2)-(c/2);

var h = 1200;
var i = (h-b)/(b-c);
var j = 1150;
var k = (j-b)/(b-c);


// this function will move a floor down 1 when the arrow is clicked
// a = the property id
function floorDown(a){
	if(aFloors[(cFloorr-1)]){
		changeFloor(a,(cFloorr-1),aFloors[(cFloorr-1)],'ignore');
		cFloorr = (cFloorr-1);
	}
}

// this function will move a floor up 1 when the arrow is clicked
// a = the property id
function floorUp(a){
	if(aFloors[(cFloorr+1)]){
		changeFloor(a,(cFloorr+1),aFloors[(cFloorr+1)],'ignore');
		cFloorr = (cFloorr+1);
	}
}


// this function will open the share this window
// a = the state of the window
function shareThis(a){
	if(a == 1){
		// open the share window
		document.onselectstart = function() {return true;}
  		document.onmousedown = function() {return true;}
		document.getElementById('sneezeguard_small').style.display='block';
		document.getElementById('sharelink').value = window.location;
	}else{
		// close the share window
		document.onselectstart = function() {return false;}
  		document.onmousedown = function() {return false;}
		document.getElementById('sneezeguard_small').style.display='none';
		document.forms['share'].reset();
	}
}


// this function will determine if we need to jump to a specific property based on the URL hash
if(window.location.hash){
	hashV = window.location.hash.substring(2,window.location.hash.length);
	hashV = hashV.split("&");
	for (x in hashV){
		temp = hashV[x].split("=");
		hashV[x] = temp[1];
	}
	setTimeout("getProperty(hashV[0],hashV[1],hashV[2]);",1000);
	if(hashV[5] && hashV[5] != "details"){
		if(hashV[6]){
			setTimeout("setSpecific(hashV[5],hashV[0],hashV[5]);",2000);
			setTimeout("changeFloor(hashV[0],hashV[6],hashV[7]);",3000);
			if(hashV[(hashV.length-1)] == "details"){
				setTimeout("switchDetails();",4000);
			}
		}else{
			setTimeout("setSpecific(hashV[5],hashV[0],hashV[5]);",2000);
			if(hashV[(hashV.length-1)] == "details"){
				setTimeout("switchDetails();",3000);
			}
		}
	}else{
		if(hashV[3] && hashV[4]){
			setTimeout("changeFloor(hashV[0],hashV[3],hashV[4]);",2000);
			if(hashV[(hashV.length-1)] == "details"){
				setTimeout("switchDetails();",3000);
			}
		}else{
			if(hashV[(hashV.length-1)] == "details"){
				setTimeout("switchDetails();",3000);
			}
		}
	}
}



// this function will obscure email addresses from spam bots
	function jemail(user, domain, suffix, linkText, cssClass){
		var cssText = "";
		if(cssClass){
			cssText = ' class="'+cssClass+'"';
		}
		if(linkText){
			document.write('<a href="'+'mailto:'+user+'@'+domain+'.'+suffix+'"'+cssText+' title="click here to send us an email">'+linkText+'</a>');
		}
		else{
			document.write('<a href="'+'mailto:'+user+'@'+domain+'.'+suffix+'"'+cssText+' title="click here to send us an email">'+user+'@'+domain+'.'+suffix+'</a>');
		}
	}


// this function will start the timeout / reset timer
function resetCounter(){
clearTimeout(screensaver);
screensaver = setTimeout("startScreensaver()", 3000);
screensaver = setTimeout("startScreensaver()", 300000);
}

function startScreensaver(){
	//location.href = "screensaver.php";	
}

// this function will take a user from a stack back to a map if the property has multiple buildings
// a = the id of the property they wish to look at
function backToMap(a){
	resetCounter();
	pNameS = "";
	if(currentDetailP == "Y"){
		switchDetails();	
	}
	$.post('_libraries/php/library.market.php?nonspecific',{v:0},function(data){getProperty(a);});
	
	document.getElementById('close').innerHTML = '<a href="javascript:;" onclick="escapeAll();"><img src="_images/detail_close_button.jpg" /></a>';
}

// this function will let the user go from the map to a specific building on that map
// a = the name of the property we will be looking through
// b = the property id
// c = the actual name of the property
function setSpecific(a,b,c){
	if(a){
		window.location.hash = bHash+"&b=m&q=1&s="+a;
		bHash_s = bHash+"&b=m&q=1&s="+a;
	}
	pageTracker._trackPageview("/"+window.location.hash);
	pName = a;
	pNameS = b;
	resetCounter();
	$.post('_libraries/php/library.market.php?specific',{specific:a},function(data){getProperty(b);});
	document.getElementById('close').innerHTML = '<a href="javascript:;" onclick="backToMap('+b+');"><img src="_images/detail_close_button.jpg" /></a>';
}


// this function will switch markets when one is selected from the dropdown in the header of the page
// a = the name of the market we wish to switch to
// the url to switch to
function switchMarket(a,b){
	resetCounter();
	$.post('_libraries/php/library.market.php?switch',{market:a},function(data){if(data == "success"){location.href=b+'?market='+a;}});
}


// this function will handle opening and closing the filter dropdowns depending on which is clicked
// a = the dropdown that was clicked on
function filterDropdown(a){
	clearTimeout(autoClose);
	if(a == "subm"){
		$('#sqft').slideUp(slide);
		$('#subm').slideToggle(slide);
		$('#buildingsss').slideUp(slide);
		autoClose = setTimeout("filterDropdown()", 5000);
	}
	if(a == "sqft"){
		$('#sqft').slideToggle(slide);
		$('#subm').slideUp(slide);
		$('#buildingsss').slideUp(slide);
		autoClose = setTimeout("filterDropdown()", 5000);
	}
	if(a == "buildingsss"){
		$('#sqft').slideUp(slide);
		$('#subm').slideUp(slide);
		$('#buildingsss').slideToggle(slide);
		autoClose = setTimeout("filterDropdown()", 5000);
	}
	if(!a){	// we clicked something else, close them all
		$('#sqft').slideUp(slide);
		$('#subm').slideUp(slide);
		$('#buildingsss').slideUp(slide);

	}
}


// this function will open the requested div and toggle the sneezeguard, will also make sure that the window is reset to show availability first!
// a = the div that we are looking to open
function showDivLayer(a){
	resetCounter();
	filterDropdown();
	$("#sneezeguard").fadeIn(250);	
	setTimeout("showDivLayer2('"+a+"')",900);
	
}

function showDivLayer2(a){
	if(a == 'detailwindow'){	
		$("#"+a).slideDown(slide,function(){$("#"+a+"_content").slideDown(slide);});
		setTimeout("$('#floormattes').fadeIn(fade)",900);
	}else{
		$("#"+a).slideDown(slide,function(){$("#"+a+"_content").slideDown(slide);});	
	}
}


// this function will close all other layers and open the contact layer
function showContact(){	
	$("#detailwindow").slideUp(slide,function(){$("#contactwindow").slideDown(slide,function(){$("#contactwindow_content").slideDown(slide);});});
}


// this function will hide the requested div layer and toggle the sneezeguard
// a = the div that we are looking to hide
function hideDivLayer(a){
	$("#"+a+"_content").slideUp(slide,function(){
		$("#"+a).slideUp(slide,function(){
			$("#sneezeguard").fadeOut(fade,function(){
				if(a == "detailwindow"){
				$("#amenitycontenthere").fadeOut(fade,function(){$("#availabilitycontenthere").fadeIn(fade);});
				document.getElementById('amenitycontenthere').style.display = "none";
				currentDetailP = "";
				$("#navoptions").fadeIn(fade);
				document.getElementById('amenities_tool').innerHTML = "<a href='javascript:;' title='click to view available space at this property' onclick='switchDetails();'>The Building</a>";
				document.getElementById('amenities_tool').className = "";
				document.getElementById('availability_tool').innerHTML = "Availability";
				document.getElementById('availability_tool').className = "activetool";											 
			}});
		});
	});
	viewing = "";
	$("#backtoproperty").hide();
}


// this function will close the content layers when the escape key is pressed
function escapeAll(){
	window.location.hash = "#";
	document.getElementById('floormattes').style.display='none';
	document.getElementById('campusmap').style.display='none';
	resetCounter();
	$.post('_libraries/php/library.market.php?nonspecific',{v:0},function(data){});
	if(document.getElementById('contactwindow').style.display == "block"){
		a = "contactwindow";
		$("#us").slideDown(fade);
		$("#ou").slideUp(fade);
		document.forms.contact.reset();
	}else if(document.getElementById('leasingwindow').style.display == "block"){
		a = "leasingwindow";
	}else if(document.getElementById('detailwindow').style.display == "block"){
		a = "detailwindow";
	}
	hideDivLayer(a);	// call the hide function and pass it the name of the content layer we want to close
	viewing = "";
	$("#backtoproperty").hide();
}


// this function will swap gallery images
// a = the src of the large image to be displayed
// b = the id of the div that we want to highlight as the current
// c = caption of the new image
function gallerySwap(a,b,c){
	resetCounter();
	for(i=1;i<=6;i++){
		if(document.getElementById("thumb_"+i)){
			document.getElementById("thumb_"+i).className = 'thumbnail';
		}
	}
	document.getElementById("thumb_"+b).className = 'thumbnail thumbnailactive';
	document.getElementById("gpos").innerHTML = b;
	document.getElementById("imagecaption").innerHTML = c;
	$("#largeimagecontainer").fadeOut(250,function(){document.getElementById('largeimagee').src = a;});
	$("#largeimagecontainer").fadeIn(250);
}


// this function handles turning down the floor highlights and then turning on the one needed
// a = the floor we want to have highlighted
function highlightFloorOn(a){
	if(a != cFloor){
		$("#f_"+a).animate({'opacity':.75},0);
	}
}
function highlightFloorOff(a){
	if(a != cFloor){
		$("#f_"+a).animate({'opacity':.4},0);
	}
}



// this function will swap the floor id we are looking at
// a = the property id
// b = the floor id from the data array
// c = the floor matte number we want to highlight
// d = a check if we used the arrows to not reset cFloorr again!!
function changeFloor(a,b,c,d){	
	if(!d){
		cFloorr = b;	
	}	
	if(b >= 0 && c){
		if(bHash_s){
			window.location.hash = bHash_s+"&f="+b+"&t="+c;
		}else{
			window.location.hash = bHash+"&f="+b+"&t="+c;
		}
	}
	pageTracker._trackPageview("/"+window.location.hash);
	resetCounter();
	for(i=1;i<=100;i++){
		if(document.getElementById('f_'+i)){
			$("#f_"+i).animate({"opacity":.4},0);
		}
	}
	$("#detailcontent_right").fadeOut(fade,function(){$("#navoptions").fadeOut(fade,function(){$("#navoptions").load("_libraries/php/library.property.platenav.php?pid="+a+"&fid="+b,function(){$("#navoptions").fadeIn(fade,function(){$("#detailcontent_right").load("_libraries/php/library.property.floorplate.php?pid="+a+"&fid="+b,function(){$("#detailcontent_right").fadeIn(fade);});});});});});	
	$("#f_"+c).animate({"opacity":1},0);
	cFloor = c;
}



// this function will swap the plates for a specific floor
// a = the id of the property we are looking at
// b = the id of the floor we want to see
// c = the id of the plate we want to see
function floorPlateSwap(a,b,c){
	resetCounter();
	$("#detailcontent_right").fadeOut(fade,function(){$("#detailcontent_right").load("_libraries/php/library.property.floorplate.php?pid="+a+"&fid="+b+"&plateid="+c,function(){$("#detailcontent_right").fadeIn(fade);});});
	
	// need to highlight the active plate in the nav
	for(i=0;i<15;i++){
		if(document.getElementById('platenav_'+i)){
			document.getElementById('platenav_'+i).className = "tooloption";
		}
	}
	document.getElementById('platenav_'+c).className = "toolactive";
	
}


// this function allows the user to navigate the main property interface by using the sliding box
function findPos(){
	var obj = document.getElementById('draggable');
	var objParent = document.getElementById('slider_bar');
	var posX = obj.offsetLeft;
	//calculate the x position of the object (in this case the drag object)
	while(obj.offsetParent){
		posX=posX+obj.offsetParent.offsetLeft;
		if(obj==document.getElementsByTagName('body')[0]){break}
		else{obj=obj.offsetParent;}
	}
	//calculate the x position of the PARENT object (in this case the slide bar)
	var posX2 = objParent.offsetLeft;
	while(obj.offsetParent){
		posX2=posX2+objParent.offsetParent.offsetLeft;
		if(objParent==document.getElementsByTagName('body')[0]){break}
		else{objParent=objParent.offsetParent;}
	}
	//calculate the x position of the object within the parent, plus the width of the box
	/* e <--- this number is: "(D - A)/(B - C)" or "(Sliding content - Containing DIV)/(slidebar width - dragbar width)", see project document where I draw everything out -- TIMO */
	$("#slider").animate({"left": -((posX-posX2)*e)}, 0);	// last number here denotes the animation easing
	//$("#parallax1").animate({"left": -((posX-posX2)*i)}, 0);	// last number here denotes the animation easing
	//$("#parallax2").animate({"left": -((posX-posX2)*k)}, 0);	// last number here denotes the animation easing
	
}



$(document).ready(function(){
	$('#draggable').draggable({
		containment: 'parent',
		grid: [1, 1],	// this sets the left to right grid spacing
		axis: 'x',
		drag: function() {
			findPos()
		},
		stop: function(){
		}
	});
});


// this fcuntion will swap back and forth between the availability and amenities information on the detail layer
function switchDetails(){
	resetCounter();
	if(pNameS != ""){
		document.getElementById('close').innerHTML = '<a href="javascript:;" onclick="backToMap('+pNameS+');"><img src="_images/detail_close_button.jpg" /></a>';	
	}
	if(currentDetailP == ""){
		tHash = window.location.hash;
		window.location.hash = window.location.hash+"&pg=details";
		$("#availabilitycontenthere").fadeOut(fade,function(){$("#amenitycontenthere").fadeIn(fade);});
		currentDetailP = "Y";
		$("#navoptions").fadeOut(fade);
		document.getElementById('amenities_tool').innerHTML = "The Building";
		document.getElementById('amenities_tool').className = "toolactive";
		document.getElementById('availability_tool').innerHTML = "<a href='javascript:;' title='click to view available space at this property' onclick='switchDetails();'>Availability</a>";
		document.getElementById('availability_tool').className = "";
		
	}else{
		window.location.hash = tHash;
		$("#amenitycontenthere").fadeOut(fade,function(){$("#availabilitycontenthere").fadeIn(fade);});
		currentDetailP = "";
		$("#navoptions").fadeIn(fade);
		document.getElementById('amenities_tool').innerHTML = "<a href='javascript:;' title='click to view available space at this property' onclick='switchDetails();'>The Building</a>";
		document.getElementById('amenities_tool').className = "";
		document.getElementById('availability_tool').innerHTML = "Availability";
		document.getElementById('availability_tool').className = "toolactive";
	}
}


// this function will check keys as they
function keynav(e){
	resetCounter();
	var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
	var actualkey=String.fromCharCode(unicode)
	if (unicode=="37"){	// left arraow pressed
		if(document.getElementById('draggable').offsetLeft >= 10){
			document.getElementById('draggable').style.left = ((document.getElementById('draggable').offsetLeft) - 10)+"px";
			findPos();
		}else{
			document.getElementById('draggable').style.left = "0px";
			findPos();
		}
	}
	if (unicode=="39"){		// right arrow pressed
		if(document.getElementById('draggable').offsetLeft <= (f-10)){/* <--- this number is (B - C) or (slidebar width - dragbar width), see project document where I draw everything out -- TIMO */
			document.getElementById('draggable').style.left = ((document.getElementById('draggable').offsetLeft) + 10)+"px";
			findPos();
		}else{
			document.getElementById('draggable').style.left = (f+"px");/* <--- this number is (B - C) or (slidebar width - dragbar width), see project document where I draw everything out -- TIMO */
			findPos();
		}
	}
	if(unicode=="27"){	// if the escape key is pressed we want to close the content layer(s) that is/are open	
		escapeAll();
	}
}
document.onkeypress=keynav


// this function will build the broker listing when the link to open the page is clicked and then only if it hasn't been gathered previously
function getBrokers(){
	resetCounter();
	$("#leasingwindow_content").load("_libraries/php/library.brokers.php");
	showDivLayer('leasingwindow');
}

// this function will build the retailer listing when the link to open the page is clicked and then only if it hasn't been gathered previously
function getRetailers(){
	resetCounter();
	$("#retailwindow_content").load("_libraries/php/library.retailers.php");
	showDivLayer('retailwindow');
}


// this function will return the property information when the link to open the page is clicked, when closed the data is lost until it is gathered up again
// a = the property id they want to look at
// b = the market to look in -> this is in here to allow the building dropdown to work properly!
// c = the actual name of the property in question
function getProperty(a,b,c){
	if(b && c){
		window.location.hash = "/p="+a+"&m="+b+"&n="+c;
		bHash = "/p="+a+"&m="+b+"&n="+c;
	}
	market = "";
	if(b){
		market = '&mid='+b;
	}
	if(c){
		pName = c;
	}
	pageTracker._trackPageview("/"+window.location.hash);
	resetCounter();
	viewing = a;
	$("#availabilitycontenthere").load("_libraries/php/library.property.php?pid="+a+market,function(){$("#navoptions").load("_libraries/php/library.property.platenav.php?pid="+a+market,function(){$("#detailcontent_right").load("_libraries/php/library.property.floorplate.php?pid="+a+market);});});
	showDivLayer('detailwindow');
	$("#backtoproperty").show();
	$("#amenitycontenthere").load("_libraries/php/library.details.php?pid="+a+market);
}


// this function will close the contact window and go back to the property details page that the user was just looking at
// a = the property id they want to look at
function backToProperty(a){
	resetCounter();
	$("#contactwindow").slideUp(slide,function(){$("#detailwindow").slideDown(slide,function(){$("#detailwindow_content").slideDown(slide);});});	
	if(currentDetailP == "Y"){
		$("#navoptions").fadeOut(fade);
		document.getElementById('amenities_tool').innerHTML = "The Building";
		document.getElementById('amenities_tool').className = "toolactive";
		document.getElementById('availability_tool').innerHTML = "<a href='javascript:;' title='click to view available space at this property' onclick='switchDetails();'>Availability</a>";
		document.getElementById('availability_tool').className = "";		
	}else{
		$("#navoptions").fadeIn(fade);
		document.getElementById('amenities_tool').innerHTML = "<a href='javascript:;' title='click to view available space at this property' onclick='switchDetails();'>The Building</a>";
		document.getElementById('amenities_tool').className = "";
		document.getElementById('availability_tool').innerHTML = "Availability";
		document.getElementById('availability_tool').className = "toolactive";
	}
}


// this function will swap contact form elements depending on whether they are inside the US or not
// a = the field area we want to see
function swapContactFields(a){
	resetCounter();
	if(a == "OU"){
		$("#us").slideUp(fade);
		$("#ou").slideDown(fade);
	}else{
		$("#us").slideDown(fade);
		$("#ou").slideUp(fade);
	}
}
