var groups = ["","",""];
var lastImage = "";
var floor = "5";
var suite = "a";
var pagenum = 0;
var perspectnum = 0;
var rendernum = 0;
var showFurniture = 0;
/* 0: N/A, 1: No, 2: Yes */

function teamNameChange(pagenum) {
	for (var i = 0; i < 5; i++) {
		if (pagenum == i) {
			$(".t" + i).children("span").show();
		} else {
			$(".t" + i).children("span").hide();
		}
	}
}

function slideChange(elem, oldnum, newnum) {
	groups = ["","",""];
	hideSlideImage(oldnum);
	$("#slide" + oldnum).hide();
	$("#slide" + newnum).show();
	checkFurniture(newnum);
	setFurnitureControls(newnum);
	showSlideImage(newnum, elem);
	if ($(elem).hasClass('team'))
		teamNameChange(newnum);
	if ($("#room" + newnum).length > 0) 
		setSchemeDefaults(newnum);
}

function playSlides(max) {
	var newnum = pagenum + 1;
	if (newnum >= max)
		newnum = 0;
	slideChange($("#slidedelay"), pagenum, newnum);
	pagenum = newnum;
}

function hideSlideImage(num) {
	$("#img" + num).hide();
	$("#nofurniture_img" + num).hide();
	$("#furniture_img" + num).hide();
}

function showSlideImage(num, elem) {
	var img_elem;
	if (showFurniture == 0)
		img_elem = $("#img" + num);
	else if (showFurniture == 1)
		img_elem = $("#nofurniture_img" + num);
	else if (showFurniture == 2)
		img_elem = $("#furniture_img" + num);
	if ($(elem).hasClass('nofade'))
		img_elem.show();
	else
		img_elem.fadeIn();
}

function checkFurniture(num) {
	if ($("#nofurniture_img" + num).length > 0)
		showFurniture = Math.max(1, showFurniture);
	else
		showFurniture = 0;
}

function setFurnitureControls(num) {
	if (showFurniture == 1) {
		$("#f_on" + num).html("<a href='JavaScript:void(0);'>On</a>");
		$("#f_off" + num).html("Off");
	} else if (showFurniture == 2) {
		$("#f_on" + num).html("On");
		$("#f_off" + num).html("<a href='JavaScript:void(0);'>Off</a>");
	}
}

function designAnimation(step) {
	switch(step) {
		case 0:
			$("#design_left_image").fadeIn();
			setTimeout("designAnimation(1)", 200);
			break;
		case 1:
			$("#design_left_lnum").html("63'");
			$("#design_left_bnum").html("17'");
			//$("#design_left_label").fadeIn();
			$("#design_left_label").html("Typical Pearl shotgun<br />1,070 sq ft<br />Bay depth 63 ft<br />105 sq ft of glass");
			setTimeout("designAnimation(2)", 700);
			break;
		case 2:
			var canv = document.getElementById("design_rotation");
			var ctx = canv.getContext("2d");
			var imageObj = new Image();
			imageObj.src = '/design_left0trans.png';
			imageObj.onload = function() {
				ctx.translate(0, 16);
				designRotate(0, ctx, imageObj);
			};
			setTimeout("designAnimation(3)", 1000);
			break;
		case 3:
			$("#design_right_image").show();
			$("#design_rotation").hide();
			setTimeout("designAnimation(4)", 300);
			break;
		case 4:
			$('#design_right_image').cycle({ 
    			fx: 'fade,fade,wipe',
    			randomizeEffects: false,
    			speed:   1000, 
   				timeout: 6000, 
  				delay:  -2000,
    			clip: 'b2t'
			});
			$('#design_right_image').cycle("pause");
			$('#design_right_image').cycle("next");
			setTimeout("designAnimation(5)", 200);
			break;
		case 5:
			$("#design_right_description").html('The innovative floor plan departed from the typical "shotgun" floot plan which exists predominantly in the Pearl. Notice that while the square footage of each home is nearly the same, the 937 residence has twice as much glass, and the bay depth is half the length.');
			$("#design_right_bnum").show();
			$("#design_right_label").fadeIn();
			setTimeout("designAnimation(6)", 1300);
			break;
		case 6:
			$("#design_left_image").cycle({
				fx: 'fade,wipe',
				randomizeEffects: false,
				speed:   1000, 
   				timeout: 6000, 
  				delay:  -2000,
				clip: 'b2t',
			});
			$('#design_left_image').cycle("pause");
			$('#design_left_image').cycle("next");
			$('#design_right_image').cycle("next");
			setTimeout("designAnimation(7)", 1200);
			break;
		case 7:
			$('#design_left_image').cycle("next");
			setTimeout("designAnimation(8)", 1500);
			break;
		case 8:
			$('#design_right_image').cycle("next");
			break;
	}
}

function designRotate(deg, ctx, imageObj) {
	ctx.clearRect((-8),(-8),537,414);
	ctx.translate((imageObj.width), imageObj.height);
	ctx.rotate(3 * (Math.PI/180));
	//ctx.translate((-(imageObj.width)),(14 - imageObj.height));
	ctx.translate((-(imageObj.width)),(-(imageObj.height)));
	ctx.save();
	ctx.drawImage(imageObj, 0, 0, imageObj.width, imageObj.height);
	ctx.restore();
	//ctx.translate(0, (-14));
	deg += 3;
	if (deg < 90) {
		setTimeout(function(){designRotate(deg, ctx, imageObj); parameter = null},6);
	}
}

function setSchemeControls(room) {
	/* Unhighlight everything. If the selections fit a complete scheme, highlight the scheme. Else, just highlight individuals. */
	if (!(groups[0] == "" && groups[1] == "" && groups[2] == "")) {
		$(".g0").attr("class","scheme_button g0");
		$(".g1").attr("class","scheme_button g1");
		$(".g2").attr("class","scheme_button g2");
	}
	if (room == "gr") {
		$("#scheme_gr_floorcab_" + groups[1]).attr("class","scheme_button emphasized g1");
		$("#scheme_gr_counter_" + groups[2]).attr("class","scheme_button emphasized g2");
		if (groups[1] == groups[2]) {
			groups[0] = groups[1];
			$("#scheme_gr_scheme_" + groups[0]).attr("class","scheme_button emphasized g0");
		} else {
			groups[0] = "";
		}
	}
	else if (room == "bed") {
		$("#schemebutton_bed_" + groups[0]).attr("class","scheme_button emphasized g0");
	}
	else if (room == "bath") {
		$("#schemebutton_bath_cab_" + groups[0]).attr("class","scheme_button emphasized g0");
		$("#schemebutton_bath_floorcounter_" + groups[1]).attr("class","scheme_button emphasized g1");
	}
}

function setSchemeDefaults(newnum) {
	var room = $("#room" + newnum).val();
	if (room == "gr") {
		groups[0] = "light";
		groups[1] = "light";
		groups[2] = "light";
		lastImage = "scheme_gr_light_light";
	}
	else if (room == "bed") {
		groups[0] = "carpet_light";
		lastImage = "scheme_bed_carpet_light";
	}
	else if (room == "bath") {
		groups[0] = "light";
		groups[1] = "sile";
		groups[2] = "";
		lastImage = "scheme_bath_light_sile";
	}
	setSchemeControls(room);
}

function setFloorDefaults() {
	floor = "5";
	suite = "a";
	$("#unittitle_" + floor + "_" + suite).show();
	$("#unitplan_" + floor + "_" + suite).show();
	$("#elevation_img_" + floor).show();
	$("#floorplan_" + floor + "_" + suite).show();
}

function updateFloorImages(lvl) {
	$(".plan_unittitle").hide();
	$(".plan_unitplan").hide();
	$("#unittitle_" + floor + "_" + suite).show();
	$("#unitplan_" + floor + "_" + suite).show();
	if (lvl < 2) {
		$(".plan_floorplan").hide();
		$("#floorplan_" + floor + "_" + suite).show();
		if (lvl < 1) {
			$(".elevation_img").hide();
			$("#elevation_img_" + floor).show();
		}
	}
}

function clearCanvas(context, canvas) {
  context.clearRect(0, 0, canvas.width, canvas.height);
  var w = canvas.width;
  canvas.width = 1;
  canvas.width = w;
}

function renderingChange(elem, oldnum, newnum) {
	$(".rendering_image_" + suite).hide();
	$("#rendering_image_" + suite + "_" + (newnum + 1)).show();
	$(".rendering_desc_num").html(newnum + 1);
	var renderRoom = $("#rendering_image_" + suite + "_" + (newnum + 1)).attr("class").split(' ').pop().split('_').pop();
	$(".rendering_desc_room").html(renderRoom.replace("0", " "));
	//var dir = parseInt($("#perspective_image_" + floor + "_" + (newnum + 1)).attr("class").split(' ').pop().split('_').pop());
	//$(".perspective_desc_pos").html(perspectiveDir(dir));
}

function perspectiveChange(elem, oldnum, newnum) {
	$(".perspective_image_" + floor).hide();
	$("#perspective_image_" + floor + "_" + (newnum + 1)).show();
	$(".perspective_desc_num").html(newnum + 1);
	var dir = parseInt($("#perspective_image_" + floor + "_" + (newnum + 1)).attr("class").split(' ').pop().split('_').pop());
	$(".perspective_desc_pos").html(perspectiveDir(dir));
}

function perspectiveDir(dir) {
	switch(dir) {
		case 0: return "North";
		case 45: return "Northeast";
		case 90: return "East";
		case 135: return "Southeast";
		case 180: return "South";
		case 225: return "Southwest";
		case 270: return "West";
		case 315: return "Northwest";
	}
}

$(document).ready(function() {
	
	var subsection = 0;
	if ($("#img0").length > 0)
		$("#img0").fadeIn();
	if ($("#nofurniture_img0").length > 0) {
		$("#nofurniture_img0").fadeIn();
		showFurniture = 1;	
	}
	if ($("#slidedelay").length > 0) {
		var delay_slides = $("#slidedelay").html();
		setInterval("playSlides(" + delay_slides + ")", 5500);
	}
	if ($("#design_rotation").length > 0) {
		designAnimation(0);
	}
	if ($("#room0").length > 0) {
		setSchemeDefaults(0);
	}
	if ($(".plan_display_col").length > 0) {
		setFloorDefaults();
	}
	$(".nextslide").click(function() {
		slideChange(this, pagenum, (pagenum + 1));
		pagenum += 1;
		return false;
	});
	$(".prevslide").click(function() {
		slideChange(this, pagenum, (pagenum - 1));
		pagenum -= 1;
		return false;
	});
	$(".f_on").click(function() {
		if (showFurniture == 1) {
			$("#nofurniture_img" + pagenum).hide();
			$("#furniture_img" + pagenum).show();
			//$("#imgframe" + pagenum).cycle("next");
			//$('.imgframe').each(function(index) {
			//	$(this).cycle("next");
			//});
			$("#f_on" + pagenum).html("On");
			$("#f_off" + pagenum).html("<a href='JavaScript:void(0);'>Off</a>");
			showFurniture = 2;
		}
		return false;
	});
	$(".f_off").click(function() {
		if (showFurniture == 2) {
			$("#furniture_img" + pagenum).hide();
			$("#nofurniture_img" + pagenum).show();
			//$('.imgframe').each(function(index) {
			//	$(this).cycle("prev");
			//});
			//$("#imgframe" + pagenum).cycle("prev");
			$("#f_on" + pagenum).html("<a href='JavaScript:void(0);'>On</a>");
			$("#f_off" + pagenum).html("Off");
			showFurniture = 1;
		}
		return false;
	});
	$(".team_area").click(function() {
		var myClass = $(this).attr('class').split(' ')[1];
		if (myClass != ("ta" + pagenum)) {
			var target = (myClass.charAt(myClass.length - 1)) * 1;
			$("#slide" + pagenum).hide();
			pagenum = target;
			$("#slide" + pagenum).show();
			teamNameChange(pagenum);
		}
	});
	$(".location_link").click(function() {
		var myId = $(this).attr('id')
		if (myId != ("location_link" + pagenum)) {
			var target = (myId.charAt(myId.length - 1)) * 1;
			$("#slide" + pagenum).hide();
			pagenum = target;
			$("#slide" + pagenum).show();
		}
		return false;
	});
	$(".location_mini").click(function() {
		if (pagenum > 0) {
			$("#slide" + pagenum).hide();
			pagenum = 0;
			$("#slide" + pagenum).show();
		} else {
			$("#slide" + pagenum).hide();
			pagenum = 1;
			$("#slide" + pagenum).show();
		}
		return false;
	});
	$("#broker_contact_submit").click(function() {
		var fname0 = $('#fname').val();
		var lname0 = $('#lname').val();
		var email0 = $('#email').val();
		var phone0 = $('#phone').val();
		var comments0 = $('#comments').val();
		$(".contact_form").html("");
		$(".contact_form").load('portland_condo_info_broker_contact_send.php', {fname: fname0, lname: lname0, email: email0, phone: phone0, comments: comments0});
		return false;
	});
	
	$("#retail_contact_submit").click(function() {
		var fname0 = $('#fname').val();
		var lname0 = $('#lname').val();
		var title0 = $('#title').val();
		var email0 = $('#email').val();
		var bphone0 = $('#bphone').val();
		var hphone0 = $('#hphone').val();
		var fax0 = $('#fax').val();
		var address10 = $('#address1').val();
		var address20 = $('#address2').val();
		var city0 = $('#city').val();
		var state0 = $('#state').val();
		var zip0 = $('#zip').val();
		var howlong0 = $('#howlong').val();
		var location0 = $('#location').val();
		var location20 = $('#location2').val();
		var usetype0 = $('#usetype').val();
		var reqs0 = $('#reqs').val();
		var explain0 = $('#explain').val();
		var info0 = $('#info').val();
		$(".contact_form").html("");
		$(".contact_form").load('portland_condo_leasing_pearl_retail_commercial_contact_send.php', {
			fname: fname0, 
			lname: lname0, 
			title: title0,
			email: email0, 
			bphone: bphone0, 
			hphone: hphone0, 
			fax: fax0, 
			address1: address10, 
			address2: address20,
			city: city0,
			state: state0,
			zip: zip0,
			howlong: howlong0,
			location: location0,
			location2: location20,
			usetype: usetype0,
			reqs: reqs0,
			explain: explain0,
			info: info0});
		return false;
	});

	$(".scheme_button").click(function() {
		var group = $(this).attr('class').split(' ').pop().charAt(1);
		var idStrings = $(this).attr('id').split('_');
		/* Set group string. Bedroom schemes work slightly differently because carpet and wood are not separate groups. */
		if (idStrings[1] == "bed")
			groups[0] = idStrings[2] + "_" + idStrings[3];
		else
			groups[parseInt(group)] = idStrings[3];
		/* For the great room, have the generalized "scheme" list modify the individual values. */
		if (group == '0' && idStrings[1] == "gr") {
			groups[1] = idStrings[3];
			groups[2] = idStrings[3];
		}
		setSchemeControls(idStrings[1]);
		/* Build the name of the image and bring the image with the corresponding id to the front. */
		var imgName = "scheme_";
		if (idStrings[1] == "gr") {
			imgName += "gr_" + groups[1] + "_" + groups[2];
		}
		else if (idStrings[1] == "bed") {
			imgName += "bed_" + groups[0];
		}
		else if (idStrings[1] == "bath") {
			imgName += "bath_" + groups[0] + "_" + groups[1];
		}
		//if (lastImage != "")
		//	$("#" + lastImage).hide();
		$(".scheme_image_" + idStrings[1]).hide();
		$("#" + imgName).show();
		lastImage = imgName;

	});
	
	$(".rendering_link").click(function() {
		var unitnum = parseInt($("#unitnum_" + floor + "_" + suite).val());
		$("#view_pane").load("floorplan_renderings_view.php", {suite: suite, floor: floor, unitnum: unitnum});
		$("#view_pane").show();
	});
	$(".perspective_link").click(function() {
		$("#view_pane").load("floorplan_perspectives_view.php", {floor: floor});
		$("#view_pane").show();
	});
	/*
	$(".floorplan_link").click(function() {
		window.location="PDF/" + $("#unit_" + floor + "_" + suite).val() + ".pdf";
	});
	$(".renderingspdf_link").click(function() {
		window.location="PDF/" + suite.toUpperCase() + ".pdf";
	});
	$(".virtualtours_link").click(function() {
		if (
	});
	*/
	$(".elevation_link").click(function() {
		var imgnum = $(this).find("i").html();
		floor = imgnum;
		if (floor > 14 && suite.localeCompare("j") < 0) suite = "j";
		else if (floor < 15 && suite.localeCompare("h") > 0) suite = "a";
		updateFloorImages(1);
	});
	$(".floorplan_suite").click(function() {
		var imgnum = $(this).find("i").html();
		suite = imgnum;
		updateFloorImages(2);
	});
	
	$(".elevation_link").mouseover(function() {
		/* show temporary selected image */
		var imgnum = $(this).find("i").html();
		$(".elevation_img").hide();
		$("#elevation_img_" + imgnum).show();
	});
	$(".plan_elevation_box").mouseout(function() {
		$(".elevation_img").hide();
		$("#elevation_img_" + floor).show();
	});
	$(".floorplan_suite").mouseover(function() {
		/* show temporary selected image */
		var imgnum = $(this).find("i").html();
		$(".plan_floorplan").hide();
		$("#floorplan_" + floor + "_" + imgnum).show();
	});
	$(".plan_floorplan_box").mouseout(function() {
		$(".plan_floorplan").hide();
		$("#floorplan_" + floor + "_" + suite).show();
	});
});

