/**
* This JavaScript Library was written by Alex Dobson. Credits to quirksmode.org for the browser detection script.
*
* Still to do:
* Scroll bars of dimmedFrame (not main body) in ie6, ie7 & ie8 & Chrome
* Do javascript keyboard access script
*/


var g_isDimmedVisible = false;
var scrollTopPos;
var pageHeight;
var theNextClickList = 'Home/, About/, About/Alex-Dobson/, About/Alex-Dobson/Education#subs, About/Alex-Dobson/Skills#subs, About/Alex-Dobson/Software#subs, About/Alex-Dobson/Professional-Experience#subs, About/Dobos-Multimedia/, About/Website/, About/Clients/, Services/, Services/Consultancy/, Services/Development/, Services/Design/, Services/Hosting/, Services/Accessibility/, Services/SEO/, Gallery/, Gallery/Websites/, Gallery/Graphics/, Gallery/Restoration/, Contact/';
var FF = false;
var CRO = false;
var SAF = false;
var OP = false;
var brIE6 = false;
var brIE7 = false;
var brIE8 = false;

//document.onkeyup = KeyCheck;       

/*
function KeyCheck(e){

   var KeyID = (window.event) ? event.keyCode : e.keyCode;

   switch(KeyID){
		case 16:
			//document.Form1.KeyName.value = "Shift";
			alert("Shift");
			nextClick();
			break; 
		case 17:
			//document.Form1.KeyName.value = "Ctrl";
			alert("Ctrl");
			break;
		case 18:
			//document.Form1.KeyName.value = "Alt";
			alert("Alt");
			break;
		case 188:
			//document.Form1.KeyName.value = "<";
			alert("<");
			break;
		case 190:
			//document.Form1.KeyName.value = ">";
			alert(">");
			break;
   }

}
*/
	
var browserCheck = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

browserCheck.init();
if(browserCheck.browser == 'Firefox'){ FF = true; }
if(browserCheck.browser == 'Chrome'){ CRO = true; }
if(browserCheck.browser == 'Opera'){ OP = true; }
if(browserCheck.browser == 'Safari'){ SAF = true; }
if((browserCheck.browser + browserCheck.version) == 'Explorer6'){ brIE6 = true; }
if((browserCheck.browser + browserCheck.version) == 'Explorer7'){ brIE7 = true; }
if((browserCheck.browser + browserCheck.version) == 'Explorer8'){ brIE8 = true; }


var nextClick = function(){
		
		var theDomain = "http://" + window.location.toString().split("//")[1].split("/")[0] + "/";
		var theURL = window.location.href;
		var URLParams = theURL.replace(theDomain, "");
		var thisPage = '';
		var nextPage = '';		
		var theNextClick=theNextClickList.split(", ");
		
		if(URLParams == ""){
			doClick("About/");
		}
		else{			
			for (var i=0;i<theNextClick.length;i++) {
				
				if(theNextClick[i] == URLParams){
					thisPage = "/" + theNextClick[i];
					nextPage = "/" + theNextClick[i+1];
					doClick(nextPage);
				}
					
			}
		}
	
};

var doClick = function($thePage){
	window.location = $thePage;
};

var displayElements = function(arrayOn, arrayOff) {
	  	/* This function turns divs ON/OFF by ID using the display CSS property
		- it requires an array of IDs to switch on & another array of IDs to switch off */
		
		if(arrayOn != ''){
			for (var onI=0; onI<arrayOn.length; onI++)
			{
				//alert("Element On=" + arrayOn[onI]);
				var theElement = document.getElementById(arrayOn[onI]);
				if (theElement != null){
					theElement.style.display = "block";
				}	
			}
		}
		
		if(arrayOff != ''){
			for (var offI=0; offI<arrayOff.length; offI++)
			{
				//alert("Element Off=" + arrayOff[offI]);
				var theElement = document.getElementById(arrayOff[offI]);
				if (theElement != null){
					theElement.style.display = "none";
				}
			}
		}
};

var removeElement = function(theID){
	if(theID != null){
		theID.parentNode.removeChild(theID);
	}
	else{
		// Remove all children of iframe <head>
		var cell = window.frames[0].document.getElementsByTagName("head")[0];
		
		if (cell.hasChildNodes()){
		    while (cell.childNodes.length >= 1){
		        cell.removeChild(cell.firstChild);       
		    } 
		}		
	}
  
};

var addEvent = function(obj, type, fn) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	}
	else{
		obj.addEventListener( type, fn, false );
	}
};
	
var removeEvent = function(obj, type, fn) {
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	}
	else{
		obj.removeEventListener( type, fn, false );
	}
};

var getDimensions = function(theImg, theImgUrl, theUpdateElement){
	
	var theBrowser = navigator.appName;
	//alert(theBrowser);
	
	if(theBrowser == "Microsoft Internet Explorer"){
		theImgWidth = theImg.width;
		theImgHeight = theImg.height;
	}
	else{
		theImgWidth = this.width;
		theImgHeight = this.height;
	}
	

	if(theImgWidth != '' && theImgHeight != ''){
		
	}
	else{
		//alert('No dimensions!');
	}
	
	var previewImg  = window.frames[0].document.createElement('img');			
	previewImg.setAttribute("id", "previewImg");
	previewImg.setAttribute("src", theImgUrl);
	previewImg.setAttribute("width", theImgWidth);
	previewImg.setAttribute("height", theImgHeight);
	previewImg.setAttribute("alt", theImgUrl);
	theUpdateElement.appendChild(previewImg);		

	removeElement(window.frames[0].document.getElementById("loadingBox"));

	
};

var createPreviewImage = function(theImgUrl, theUpdateElement){
	
	var theImg = new Image();
	var theImgWidth;
	var theImgHeight;
	
	theImg.onload  = function() { getDimensions(theImg, theImgUrl, theUpdateElement); }
	theImg.src = theImgUrl;
};

var preloader = function(imgArray){
	
	for (var i=0;i<imgArray.length; i++){
		imgArray[i] = new Image();
		imgArray[i].src = imgArray[i];
	}

}

var createImageLoadDiv = function(){
	
	var newEle = window.frames[0].document.body;
	
	var loadWindow = window.frames[0].document.createElement('div');
	loadWindow.setAttribute("id", "loadingBox");
	
	var loadWindowTitle = window.frames[0].document.createElement('h1');
	var theTitle = window.frames[0].document.createTextNode("Loading...");
	
	
	var theLoadingImg = window.frames[0].document.createElement('img');
	theLoadingImg.setAttribute("id", "loadingImg");
	theLoadingImg.setAttribute("src", "/images/loading.gif");
	theLoadingImg.setAttribute("width", "199px");
	theLoadingImg.setAttribute("height", "21px");
	theLoadingImg.setAttribute("alt", "!! Loading !!");
	
	
	if(newEle != null){
		newEle.appendChild(loadWindow);
		loadWindow.appendChild(loadWindowTitle);
		loadWindowTitle.appendChild(theTitle);
		loadWindow.appendChild(theLoadingImg);
	}
	
	
};

var closeDimmed = function(scrollTopPos){
	// Turn off elements
	displayElements(new Array(""), new Array("dimmedLayer", "dim-TR", "dim-TL", "dim-BR", "dim-BL", "dim-TM", "dim-BM"));
	
	var preImg = window.frames[0].document.getElementById("previewImg");
	var loadBox = window.frames[0].document.getElementById("loadingBox");
	// rem CSS & JavaScriptfrom iframe head
	removeElement(null);
	
	// remove other dimmed elements
	if(preImg){ removeElement(preImg); }
	if(loadBox){ removeElement(loadBox); }
	
	// Return body attributes to default
	
	if(CRO){
		document.body.style.height = "";
		document.body.style.overflow = "auto";
		window.scroll= "auto";
	}
	if(brIE7){
		document.getElementsByTagName("html")[0].style.overflow = "";
		document.body.style.height = "0";
		document.body.style.overflow = "";
	}
	if(brIE6){
		document.body.style.height = pageHeight;
		document.documentElement.style.overflow = "";
	}
	else{
		document.body.style.height = "0";
		document.body.style.overflow = "";
	}
	
	// Toggle state
	g_isDimmedVisible = false;
	
	// Return scrollbar position
	
	if(window.pageYOffset){
		window.pageYOffset = scrollTopPos;
	}
	else if(document.body.parentElement){
		//document.body.parentElement = scrollTopPos; // causing error in IE7
	}
	else if(document.body.parentElement.scrollTop){
		document.body.parentElement.scrollTop = scrollTopPos;
	}
	else{
		document.body.scrollTop = scrollTopPos;
	}
	
	
//ie		
	//document.getElementsByTagName("html")[0].style.overflow = "auto";
//ie	
	
	
}

var dimm = function(theContentUrl, popWidth, popHeight, scrollIframe, scrollIt){
	
	//alert("Vars: URL=" + theContentUrl + ", Width=" + popWidth + ", Height=" + popHeight + ", Scroll iFrame=" + scrollIframe + ", Scroll Page=" + scrollIt);	
	
	if(brIE6){
		document.body.clientHeight;
	}
	else{
		document.documentElement.clientHeight
	}
	
	pageHeight = document.body.clientHeight + "px";
	var scrollTop = document.body.scrollTopPos;
	var thebody = document.getElementsByTagName("body");
	var dimLayer = document.getElementById("dimmedLayer");
	var dimContent = document.getElementById("dimmedContent");
	var dimTM = document.getElementById("dim-TM");
	var dimBM = document.getElementById("dim-BM");
	var dimFrame = document.getElementById("dimmedIframe");
	var dimFrameWidth = "px";
	var dimFrameHeight = "px";
	var widthToUse;
	var heightToUse;
	var iFrameMaxWidth = "1000px";
	var iFrameMaxHeight = "490px";	
	
	
	// Check scroll position
	if (window.pageYOffset){
		scrollTopPos = window.pageYOffset;
	}
	else{
		scrollTopPos = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	
	if ( !g_isDimmedVisible){
		// Popup is OFF - so... Make the Changes to Dimmed layer, turn on elements, and disable scrollbar
		// Work out the popup window width and margins
		
		
		// Check widths & heights don't exceed maximums
		if(parseFloat(popWidth.slice(0, -2)) > parseFloat(iFrameMaxWidth.slice(0, -2))){
			widthToUse = iFrameMaxWidth;
//			dimFrameWidth = parseFloat(iFrameMaxWidth.slice(0, -2)) + 20 + "px";
		}
		else{ widthToUse = popWidth; }

		if(parseFloat(popHeight.slice(0, -2)) > parseFloat(iFrameMaxHeight.slice(0, -2))){
			heightToUse = iFrameMaxHeight;
//			dimFrameHeight = parseFloat(iFrameMaxHeight.slice(0, -2)) + 20 + "px";
		}
		else{ heightToUse = popHeight; }
			
		// Strip 'px' from string & turn string into integer
		var centWidth = parseFloat(widthToUse.slice(0, -2));
		var frameHeight = parseFloat(heightToUse.slice(0, -2));	
			
		// Calculate the minus width to 'minus margin'
		dimContent.style.margin = '0px 0px 0px ' + "-" + parseInt(centWidth/2) + "px";
			
		// Calculate width of the dim-TM & dim-BM (popWidth - width of side pieces (24px) + padding (30px))
		var themiddleBitWidth = ((centWidth-(12 * 2))+30);	
				
		// set the width of the middle bit
		dimTM.style.width = themiddleBitWidth + "px";
		dimBM.style.width = themiddleBitWidth + "px";
		
		// Check for ie6 & adjust the frame width to suit the browser
		if(brIE6){
			dimFrameWidth = (themiddleBitWidth-10) + dimFrameWidth;
		}
		else{
			dimFrameWidth = (themiddleBitWidth-6) + dimFrameWidth;
		}
		
		// calculate the frame height
		dimFrameHeight = (frameHeight+32) + dimFrameHeight;
		
		// Turn on elements
		displayElements(new Array("dimmedLayer", "dim-TR", "dim-TL", "dim-BR", "dim-BL", "dim-TM", "dim-BM"), new Array(""));
		
		// Add close event listener
		addEvent(dimLayer, "click", closeDimmed);
		
		
		if(scrollIt == "false"){
			dimLayer.style.top = scrollTopPos + "px";
			
			if(!brIE6 || !brIE7){
				document.body.style.height = "0";
				document.body.style.overflow = "hidden";
			}
		
			if(brIE6){
				document.body.style.height = "5000px";
				document.documentElement.style.overflow = "hidden";
			}
			if(brIE7){
				document.body.style.height = "5000px";
				document.getElementsByTagName("html")[0].style.overflow = "hidden";
			}
						
		}
		
		
		dimFrame.width = dimFrameWidth;
		dimFrame.height = dimFrameHeight;
		dimContent.style.width = widthToUse;
		dimContent.style.height = parseFloat(heightToUse.slice(0, -2)) + 20 + "px";
		dimFrame.scrolling = scrollIframe;
		
		
		// Add css & javascript to iframe
		var iFrameCSS  = window.frames[0].document.createElement('link');			
		iFrameCSS.setAttribute("href", "css/defaultIframe.css");
		iFrameCSS.setAttribute("type", "text/css");
		iFrameCSS.setAttribute("rel", "stylesheet");
		
		var iFrameJavascript  = window.frames[0].document.createElement('script');			
		iFrameJavascript.setAttribute("src", "js/javascript_lib.js");
		iFrameJavascript.setAttribute("type", "text/javascript");
		
		
		var iframe;
		iframe = window.frames[0];
		
		if(brIE6 || brIE7 || brIE8){
			// Internet Explorer
			iframe.document.createStyleSheet(iFrameCSS.href);
			iframe.document.createStyleSheet(iFrameJavascript.href);
		}
		else{
			iframe.document.getElementsByTagName("head")[0].appendChild(iFrameCSS);
			iframe.document.getElementsByTagName("head")[0].appendChild(iFrameJavascript)
		}
		

		if(theContentUrl.slice(-3) == 'jpg' || theContentUrl.slice(-3) == 'gif' || theContentUrl.slice(-3) == 'png'){
			// If the Iframe reference is to an Image...
			
			
			window.onload = function()
			{
				preloader(new Array(theContentUrl));
			}
			
			
			var newEle = window.frames[0].document.body;
			createImageLoadDiv();
			createPreviewImage(theContentUrl, newEle);
		}
		else{
			//or it's to a web page
			dimFrame.src = theContentUrl;
		}
		g_isDimmedVisible = true;
	}
	
};


/*
var col = function(){
	
	if (document.getElementById("rightCol") != null){
		var contentDiv = document.getElementById("content").offsetHeight;
		var rightDiv = document.getElementById("rightCol").offsetHeight;

		if (rightDiv > contentDiv){
			document.getElementById("content").style.height = rightDiv + "px";
		}
	}
	if (document.getElementById("leftCol") != null){
		var contentDiv = document.getElementById("content").offsetHeight;
		var leftDiv = document.getElementById("leftCol").offsetHeight;

		if (leftDiv > contentDiv){
			document.getElementById("content").style.height = leftDiv + "px";
		}
	}
	
};

*/
