﻿window.onload = lockedAndLoaded


function lockedAndLoaded()
{
	var loadText = document.getElementById("loadingText")
	loadText.style.display = 'none';
	resizeContent();
	//slideShow();
}

function slideShow()
{
	var randomNumber=Math.floor(Math.random()*(totalProjects-1)+1);
	var randomProject="project_" + randomNumber;
	setTimeout(function(){switch_Gallery_Single(randomProject)},10000);
	setTimeout(slideShow,10000);
}


function resizeContent() 
{  
	var myWidth = 0, myHeight = 0;
	if (typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if (document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	if (myWidth <= 1000)
	{
		myWidth = 1000;
	}
	mainContentWidth = ((myWidth-170)/10);
	changeCSS('.wrapper', 'width', ((mainContentWidth) + 'em'))
	changeCSS('.mainImage', 'height', ((mainContentWidth/2.5) + 'em'))
	changeCSS('.mainContent', 'height', ((mainContentWidth/2.38) + 'em'))
	//changeCSS('.smallGallery', 'margin-top', ((mainContentWidth/2.44) + 'em'))
	//changeCSS('.mainGallery', 'margin-top', ((mainContentWidth/2.28) + 'em'))
	//changeCSS('.footer', 'padding-top', ((mainContentWidth/2.4) + 'em'))
	changeCSS('.smallGalleryChild img', 'width', ((mainContentWidth/totalProjects) + 'em'))
	changeCSS('.mainGallery img', 'width', ((mainContentWidth/totalProjects-0.001) + 'em'))
}


var currQualName = "singleSmall/";
var currQualDim = "1000";

function switchQual()
{
	var imgTags = document.getElementsByTagName("img")
	
	var lowQualName = "singleSmall/";
	var lowQualDim = "1000";
	var highQualName = "single/";
	var highQualDim = "1750";
	
	if (currQualName == "singleSmall/") {newQualName = highQualName; newQualDim = highQualDim}
	else {newQualName = lowQualName; newQualDim = lowQualDim}
	
	
	for (var i = 0; i <= (imgTags.length - 1); i++)
	{
		if ((imgTags[i].id.substr(0,3) == "mai"))
		{
			imgTags[i].style.opacity = 0.2;
			imgTags[i].style.filter  = 'alpha(opacity=' + 20 + ')';
		}
	}
	
	
	for (var i = 0; i <= (imgTags.length - 1); i++)
	{
		if (imgTags[i].src && (imgTags[i].id.substr(0,3) == "pro"))
		{
			firstNewSrc = imgTags[i].src.replace(currQualName, newQualName);
			secondNewSrc = firstNewSrc.replace(currQualDim, newQualDim);
			
			imgTags[i].src = secondNewSrc;
		}
	}
	currQualName = newQualName;
	currQualDim = newQualDim;
	
	switch_Photo_Simple(currPhoto);
}


function setOpacity(testObj,value)
{
	testObj.style.opacity = value/100;
	testObj.style.filter  = 'alpha(opacity=' + value + ')';
}


function fadeIn(objID, opacity)
{
	obj = document.getElementById(objID);
	if (opacity <= 100) 
	{
		setOpacity(obj, opacity);
		opacity += 10
		setTimeout(function(){fadeIn(objID,opacity)},30);
	}
}


function fadeOut(objID, opacity)
{
	obj = document.getElementById(objID);
	if (opacity >= 0) 
	{
		setOpacity(obj, opacity);
		opacity -= 10
		setTimeout(function(){fadeOut(objID,opacity)},30);
	}
}


var currPhoto = 'project_1_photo_1';
function switch_Photo_Simple(nextPhoto)
{
	if (currPhoto != nextPhoto)
	{
		if (typeof prevPhoto != "undefined")
		{	
			document.getElementById(prevPhoto).style.display = 'none';
			document.getElementById(currPhoto).style.zIndex = '0';
		}

		var galleryID = document.getElementById(nextPhoto);
		galleryID.style.zIndex = 1;
		galleryID.style.opacity = 0;
		galleryID.style.display = 'block';
		fadeIn(nextPhoto,0);
		
		prevPhoto = currPhoto;
		currPhoto = nextPhoto;
	}
}


//var currDiv = 'project_1';
//var go = 1
function switch_Gallery_Single(nextDiv)
{
	if (typeof currDiv == "undefined")
	{
		currDiv = 'project_0';	// there is no 'project_0' -- currDiv must be defined in order to 'initialize it' but is not yet allowed to equal any possible nextDiv
	}
	
	if (currDiv != nextDiv)
	{
		if (typeof prevDiv != "undefined")
		{	
			//document.getElementById(prevDiv).style.display = 'none';
			document.getElementById(currDiv).style.zIndex = '0';
			fadeOut(currDiv,100);
			
			setTimeout("document.getElementById(prevDiv).style.display = 'none';", 300);
		}
			
		var galleryID = document.getElementById(nextDiv);
		galleryID.style.zIndex = 1;
		galleryID.style.opacity = 0;
		galleryID.style.display = 'block';
		fadeIn(nextDiv,0);

		prevDiv = currDiv;
		currDiv = nextDiv;


		// also turn on next gallery's first photo	
		switch_Photo_Simple(nextDiv + "_photo_1");
		switch_TextBox(nextDiv);
	}
}


var currText = 'textBox_1';
function switch_TextBox(nextText)
{
	var nextText = nextText.replace("project", "textBox");
	if (currText != nextText)
	{
		if (typeof prevText != "undefined")
		{	
			document.getElementById(prevText).style.display = 'none';
			document.getElementById(currText).style.zIndex = '0';
		}

		var galleryID = document.getElementById(nextText);
		galleryID.style.zIndex = 1;
		galleryID.style.opacity = 0;
		galleryID.style.display = 'block';
		
		
		//setTimeout("document.getElementById(prevDiv).style.display = 'none';", 300);
		
		fadeOut(currText,100);
		setTimeout("document.getElementById(prevText).style.display = 'none';", 500);
		fadeIn(nextText,0);
		
		prevText = currText;
		currText = nextText;
	}
}



function galReady(galNum)
{
	targetOpacity = 100;
	document.getElementById('mainGallery_' + galNum).style.opacity = (targetOpacity/100);					// for good browsers
	document.getElementById('mainGallery_' + galNum).style.filter  = 'alpha(opacity='+targetOpacity+')';	// for IE
}


function browserDecider() 
{
// Get the first stylesheet of the document
var ss = document.styleSheets[0];

// Get the rules array using W3C or IE API
var rules = ss.cssRules?ss.cssRules:ss.rules;
return rules;
}


function changeCSS(selectorText, style, styleValue)
{

var theRules = browserDecider()

	for (var n = 0; n <= theRules.length; n++)
	{
	if (theRules[n] == "[object CSSStyleRule]")
		{
			selectorTester = theRules[n].selectorText.toUpperCase();
			selectorTextGiven = selectorText.toUpperCase();
			
			if (selectorTester == selectorTextGiven)
			{
				theRules[n].style[style] = styleValue;
			}
		}
	}
}


function outPutJavaScriptHere(firstArg, secondArg)
{
	theRules = browserDecider()

	var varList = "";
/*   	for (var n = 0; n <= theRules.length; n++)
	{
		if (theRules[n] == "[object CSSStyleRule]")
		{
			var varList = varList + theRules[n].selectorText + " --------{ " + theRules[n].style.cssText + " } " + "<br />"; 
			//var varList = varList + theRules[n].selectorText + "<br />"; 
		}
	}    
	document.write(varList); */
//	document.write(theRules[1].selectorText);	

//var styleObj = document.styleSheets[0].cssRules[0].style;
//document.write(styleObj);
}


