// JavaScript Document

$(window).load( function () {
	setPage();
} );

$(window).resize(function() {
	setPage();
	setContentDiv();
} );




function setPage() {
	//setContentDiv();
	unhighlightNav2();
	newBGHeight = $(window).height() - 100 - parseInt($("#contentBG").css('marginTop'))-parseInt($("#contentBG").css('marginBottom'))-50;
	if(newBGHeight < 500) {
		newBGHeight = 500;
	}
	if(newBGHeight > 800) {
		newBGHeight = 800;
	}

	//alert(newBGHeight);
	$("#contentBG").css('height', newBGHeight);
	
	newImgHeight = newBGHeight - parseInt($("#contentBG").css('paddingBottom'));
//	alert(newImgHeight);
	$(".imgWrapper").css("height", newImgHeight);
	if( $("#puschinoDesc").is("*") ) {
		//only for the aUtopia page
		offset1 = $("#puschinoCaption1").outerHeight(true) + parseInt($("#puschinoDesc").css("marginTop")) + parseInt($("#puschinoDesc").css("marginBottom")) + 60;
//		alert(offset1);
		breakCounter = 0;
		if($("#puschinoDesc").height() > newImgHeight - offset1 ) {
			while($("#puschinoDesc").height() > newImgHeight - offset1 && breakCounter < 200) {
				$("#puschinoDesc").css("width", $("#puschinoDesc").width() + 1)
				breakCounter++;
			}
//			$("#puschinoDesc").css("width", $("#puschinoDesc").width() - 1)
		} else {
			while($("#puschinoDesc").height() < newImgHeight - offset1 && breakCounter < 200) {
				$("#puschinoDesc").css("width", $("#puschinoDesc").width() - 1)
				breakCounter++;
			}
			$("#puschinoDesc").css("width", $("#puschinoDesc").width() + 1)
		}
		$("#puschinoCaptionWrap").width($("#puschinoDesc").outerWidth(true));
//		$("#puschinoDesc").css("height", newImgHeight - offset1 - 15)
	}


	$(".contentImg").fadeIn(2000);
	divWidth = 0;
	imgDivName = "imgDiv1";
	i=1;
	while( $("#"+imgDivName).length ) {
		divWidth += parseInt($("#"+imgDivName).css("width")) + parseInt($("#"+imgDivName).css("marginRight"));
		i++;
		imgDivName = "imgDiv"+i;
	}
	divWidth += parseInt($("#desc").css("width"))+ parseInt($("#desc").css("marginRight")) + parseInt($("#contentBG").css('padding'));
//	alert(divWidth);
	$("#contentBG").css('width', divWidth);
}


function GetWidth()
{
   	var x = 0;
   	if (self.innerWidth)
   	{
		if(document.body.offsetWidth) {
			if(document.body.offsetWidth < self.innerWidth) {
           	    x = document.body.offsetWidth;
			} else {
           	    x = self.innerWidth;
			}
		}
  	}
   	else if (document.documentElement && document.documentElement.clientHeight)
   	{
  	    x = document.documentElement.clientWidth;
  	}
  	else if (document.body)
   	{
  	    x = document.body.clientWidth;
  	}
   	return x;
}
		
function GetHeight() {
	var y = 0;
	if (self.innerHeight) {
    	y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight)  {
    	y = document.documentElement.clientHeight;
	} else if (document.body) {
    	y = document.body.clientHeight;  
	}
 	return y;
}

function setContentDiv () {
	var box = document.getElementById("contentWrap");
	box.style.height = (GetHeight() - 140)+"px";
	box.style.width = (GetWidth() - 308 - 70 - 70)+"px";
}

function findCaller(e)
{
var targ;
if (!e)
  {
  var e=window.event;
  }
if (e.target)
  {
  targ=e.target;
  }
else if (e.srcElement)
  {
  targ=e.srcElement;
  }
if (targ.nodeType==3) // defeat Safari bug
  {
  targ = targ.parentNode;
  }
return targ;
}

function highlightNav1 (e) {
	var box = findCaller(e);
//	box.style.color="#FFFF00";
//	for( var i=0; i < box.childNodes.length; i++) {
//		if(box.childNodes[i].className == "readMore") {
			box.style.color="#feffb7";
//		}
//	}
}

function unhighlightNav2 (e) {
	//alert("working");
	var box = document.getElementById("descDiv");
	for( var i=0; i < box.childNodes.length; i++) {
		for( var j=0; j < box.childNodes[i].childNodes.length; j++) {
			if(box.childNodes[i].childNodes[j].className == "menuItem") {
				box.childNodes[i].childNodes[j].style.color="#ffffff";
			}
		}
	}
	//highlight active page
	highlightDiv = "";
	switch(queryString("page")) {
		case "perfectCicle":
			highlightDiv = "menu10";
			break;
		case "sky":
			highlightDiv = "menu9";
			break;
		case "place":
			highlightDiv = "menu8";
			break;
		case "bldg_grid":
			highlightDiv = "menu7";
			break;
		case "bldg_view":
			highlightDiv = "menu6";
			break;
		case "bldg_dna":
			highlightDiv = "menu5";
			break;
		case "aUtopia":
			highlightDiv = "menu4";
			break;
		case "strips":
			highlightDiv = "menu3";
			break;
		case "plane":
			highlightDiv = "menu2";
			break;
		case "platform":
			highlightDiv = "menu1";
			break;
		case "about":
			highlightDiv = "menu0";
			break;
		default:
			highlightDiv = "";
			break;
	}
	var box2 = document.getElementById(highlightDiv);
	box2.style.color="#feffb7";
}

function loadPage (loadString, infoNum) {
	location.href='index.php?page='+loadString;
}

function queryString(searchString) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == searchString) {
			return ft[1];
		}
	}
}
