// Width and height of the currently active browser window.
var myWidth = 0, myHeight = 0;
var scrOfX = 0, scrOfY = 0;

// constructs email link dynamically to prevent spam
function mailto(name) {
	domain = 'stewartjones.ca';
	emailAddress = name + '@' + domain;
	emailLink = '<a href="mailto:' + emailAddress + '">' + emailAddress + '</a>';
	document.write(emailLink);
}

function email_link(name) {
  domain = 'stewartjones.ca';
  emailAddress = name + '@' + domain;
  emailLink = '<a href="mailto:' + emailAddress + '">';
  document.write(emailLink);
}

function email_end() {
  document.write('</a>');
}

function leftPos(popW) {
	var screenWidth = 650;
	screenWidth = screen.availWidth;

	return (screenWidth - popW) / 2;
}

function topPos(popH) {
	var screenHeight = 480;
	screenHeight = screen.availHeight;

	return (screenHeight - popH) / 2;
}

function viewInWindow(filename, nw, nh, t, bgc) {
	var newWidth = nw + 20;
	var newHeight = nh + 20;
	var fullURL = "view.php?fn=" + filename + "&w=" + nw + "&h=" + nh + "&title=" + t + "&bgcolor=" + bgc;
	window.open(fullURL, 'viewer', 'width=' + newWidth + ', height=' + newHeight + ', top=' + topPos(newHeight) + ', left=' + leftPos(newWidth) + ', toolbar=0, resizable=0, location=0, status=0, menubar=0, scrollbars=0');
}

// switches the default values off if focus is given to a form field, and back on if nothing was typed in
function switchFormText(fieldNum) {
	var nameInput = document.getElementById("theName");
	var emailInput = document.getElementById("theEmail");
	var messageInput = document.getElementById("theMessage");

	if (nameInput.value == "Name" && fieldNum == 1) {
		nameInput.value = "";
	} else if (nameInput.value == "") {
		nameInput.value = "Name";
	}

	if (emailInput.value == "Email" && fieldNum == 2) {
		emailInput.value = "";
	} else if (emailInput.value == "") {
		emailInput.value = "Email";
	}

	if (messageInput.value == "Message" && fieldNum == 3) {
		messageInput.value = "";
	} else if (messageInput.value == "") {
		messageInput.value = "Message";
	}
}

function getWindowSize() {  
  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;
  }
}

function getScrollXY() {  
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  //return [ scrOfX, scrOfY ];
}

function centerLeft(popW, sx) {
	return sx + ((myWidth - popW) / 2 - 30) + 5;
}

function centerTop(popH, sy) {
	return sy + ((myHeight - popH) / 2 - 30) + 20;
}

function viewImage(title, name, w, h) {	
	var filename = name + ".jpg";	
	document.getElementById('photo-holder').src = filename;	
	document.getElementById('photo-title').innerHTML = title;
	
	var width = w + 24;
	var height = h + 74;
	
	var box_width = w + 42 + 48;
	var box_height = h + 43 + 85;
	
	// Set myWidth and myHeight to the current browser window dimensions.
	getWindowSize(); 
	getScrollXY();	
	
	var photo_container = document.getElementById('photo');
	photo_container.style.top = centerTop(height, scrOfY) + "px";
	photo_container.style.left = centerLeft(width, scrOfX) + "px";
	photo_container.style.width = box_width + "px";
	photo_container.style.height = box_height + "px";
	
	// Resize backround image
	var bg_left = document.getElementById('left');
	var bg_right = document.getElementById('right');
	var bg_top = document.getElementById('top');
	var bg_bottom = document.getElementById('bottom');
	var bg_middle = document.getElementById('middle');
	bg_left.height = h;
	bg_right.height = h;
	bg_top.width = w;
	bg_bottom.width = w;
	bg_middle.width = w;
	bg_middle.height = h;
	
	Effect.Appear('photo', {duration: 0.5});
}

function hideImage() {
	//document.getElementById('photo');
	Effect.Fade('photo', {duration: 0.25});
	//document.getElementById('photo-holder').src = "";
}

function viewVideo(title, fn) {	
	var filename = fn;
	var w = 350;
	var h = 315;
	
	document.getElementById('video-title').innerHTML = title;
	
	var so = new SWFObject("video_player.swf", "snav", w, h, "7");
	so.addParam("scale", "noscale");
	so.addParam("salign", "tl");
	so.addVariable("filename", filename);
	so.addVariable("title", title);
	so.write("video-content");
	
	var width = w + 24;
	var height = h + 74;
	
	var box_width = w + 42 + 48;
	var box_height = h + 43 + 85;
	
	// Set myWidth and myHeight to the current browser window dimensions.
	getWindowSize(); 
	getScrollXY();	
	
	var video_container = document.getElementById('video');
	video_container.style.top = centerTop(height, scrOfY) + "px";
	video_container.style.left = centerLeft(width, scrOfX) + "px";
	video_container.style.width = box_width + "px";
	video_container.style.height = box_height + "px";
	
	// Resize backround image
	var bg_left = document.getElementById('vleft');
	var bg_right = document.getElementById('vright');
	var bg_top = document.getElementById('vtop');
	var bg_bottom = document.getElementById('vbottom');
	var bg_middle = document.getElementById('vmiddle');
	bg_left.height = h;
	bg_right.height = h;
	bg_top.width = w;
	bg_bottom.width = w;
	bg_middle.width = w;
	bg_middle.height = h;
	
	Effect.Appear('video', {duration: 0.5});
}

function hideVideo() {
	Effect.Fade('video', {duration: 0.25});
}
