﻿/*---------------------------------------------
Generic cookie set, read and erase functions 
---------------------------------------------*/

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


//Running the vertical positioning immediately
//positionViewport();
function positionViewport () {
	var dimensions = getWindowHeight();
	var windowWidth = dimensions[0];
	var windowHeight = dimensions[1];
	var wrapper = document.getElementById('Wrapper');
	
	if (windowHeight > wrapper.offsetHeight)
		wrapper.style.top = ((windowHeight - wrapper.offsetHeight) / 2) + "px";
	else
		wrapper.style.top = 0;
		
	if (windowWidth < 885)
		closeDrawer();
	else
		openDrawer();			
}

//All event handler attachments kept at the top of the page
$(document).ready( function () {
//	positionViewport();
	initGallery();
	initThumbsPage();
	});
/*	
window.onresize = function () {
	positionViewport();
	}
*/	
function closeDrawer () {
	if (document.getElementsByTagName("body")[0].className.indexOf("NoDrawer") == -1) {
		document.getElementById("Drawer").className = "closed";
		document.getElementById("Viewport").style.marginLeft = "20px";
		document.getElementById("Disclaimer").style.marginLeft = "20px";
		document.getElementById("Wrapper").style.width = "755px";
	}
}

function openDrawer () {
	if (document.getElementsByTagName("body")[0].className.indexOf("NoDrawer") == -1) {
		document.getElementById("Drawer").className = "";
		document.getElementById("Viewport").style.marginLeft = "148px";
		document.getElementById("Disclaimer").style.marginLeft = "148px";
		document.getElementById("Wrapper").style.width = "883px";
		}
	}

/* Adapted from Mark "Tarquin" Wilton-Jones' - http://www.howtocreate.co.uk/tutorials/javascript/browserwindow */

function getWindowHeight() {
	var myWidth = 0;
	var 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;
	}
	return [myWidth, myHeight];
}
	
document.getElementById('morphus').onmouseover = function () {
	showContact();
	return false;
	};

document.getElementById('morphus').onmouseout = function () {
	hideContact();
	return false;
	};

function showContact () {
	document.getElementById('morphusContact').innerHTML = "morphusdesigns";
	document.getElementById('morphusContact').innerHTML += "@";
	document.getElementById('morphusContact').innerHTML += "gmail.com";
	
	document.getElementById('morphus').href = "mailto:";
	document.getElementById('morphus').href += "morphusdesigns";
	document.getElementById('morphus').href += "@";
	document.getElementById('morphus').href += "gmail.com";
	}

function hideContact () {
	document.getElementById('morphusContact').innerHTML = "";
	document.getElementById('morphus').href = "#";
	}

var currentImage = 0; //keeps track of current displayed image for gallery
	
function initGallery () {
	if (document.getElementsByTagName('body')[0].className.indexOf('Gallery') == -1)
		return;
	
	var mainImg = document.getElementById('MainImg');
	var imgTitle = document.getElementById('ImageTitle');
	var prevLink = document.getElementById('PrevLink');
	var nextLink = document.getElementById('NextLink');
	var drawer = document.getElementById('thumbsWrapper');
	var thumbs = drawer.getElementsByTagName('a');
	
	jQuery.each(thumbs, function (index, value) {
		$(this).bind("click", function(evt){
			switchImage(index);
			return false;
		});
	});
	
	var hash = location.hash;
/*	if (hash.indexOf('#') == 0)
		hash = hash.substring(1, hash.length);
*/
	hash = parseInt(hash, 10);
	
	(hash == "" || isNaN(hash)) ? hash = 1 : true;
	
	(hash > thumbs.length || hash <= 0) ? switchImage(0) : switchImage(hash - 1);
		
	$(prevLink).bind("click", function(evt){
		switchImage(currentImage - 1);
		return false;
	});
		
	$(nextLink).bind("click", function(evt){
		switchImage(currentImage + 1);
		return false;
	});
	
	function switchImage(num) {
		num = parseInt(num, 10);
	
		if (num < 0) num = thumbs.length - 1;
		else if (num >= thumbs.length) num = 0;
		 
		currentImage = num; 
		window.location.hash = currentImage + 1;
		 
		mainImg.innerHTML = "";
		var img = document.createElement("img");
		img.src = thumbs[num].href;
		mainImg.appendChild(img);
		
		imgTitle.innerHTML = thumbs[num].title;
	}
}


/*------------------------------------------------------------
Temporary Thumbs Pagination Scheme - Handwritten by Varun
------------------------------------------------------------*/

var thumbsPageWidth = 122;
var animationCount = 0;
var currentPageNumber = 1;
var totalThumbsPages = 0;

//projectNumber is set by the drawer include

function initThumbsPage() {
	//$('#thumbsPage1').css('left', '0px');
	
	try {
	      if (projectNumber) {
				
				currentPageNumber = parseInt(readCookie('thumbsPage_' + projectNumber), 10);
				if (!currentPageNumber) currentPageNumber = 1;

				var thumbsPages = $(".thumbsPage");
				totalThumbsPages = thumbsPages.length;

				$('#pageCount').text('Page ' + currentPageNumber + ' of ' + totalThumbsPages);

				for (i=1; i<=totalThumbsPages; i++)
				{
					if (i < currentPageNumber)
						thumbsPages[i-1].style.left = '-'+thumbsPageWidth+'px';
					else if (i == currentPageNumber)
						thumbsPages[i-1].style.left = '0px';
					else
						thumbsPages[i-1].style.left = thumbsPageWidth+'px';
				}

				$('#previousThumbsPage').click( function () {
					switchThumbsPage('prev');
					return false;
					});
				$('#nextThumbsPage').click( function () {
					switchThumbsPage('next');
					return false;
					});
		
			}
	} 
	
	catch (err) {
		return;
	}

}

function switchThumbsPage(direction) {
	
	var currentPage = $('#thumbsPage' + currentPageNumber);
	var next;
	
	if (direction == "prev")
		next = currentPageNumber - 1;
	else
		next = currentPageNumber + 1
	
	if (next < 1 || next > totalThumbsPages)
		return;
	
	var nextPage = $('#thumbsPage' + next);
	
	if (animationCount < thumbsPageWidth)
	{
		animationCount++;
		
		if (direction == "prev") {
			currentPage.css('left', animationCount + 'px');
			nextPage.css('left', (animationCount-thumbsPageWidth) + 'px');
		}
		else {
			currentPage.css('left', '-' + animationCount + 'px');
			nextPage.css('left', (thumbsPageWidth-animationCount) + 'px');
		}
		
		window.setTimeout("switchThumbsPage('"+direction+"')", 0)
	}
	
	else {
		$('#pageCount').text('Page ' + next + ' of ' + totalThumbsPages);
		currentPageNumber = next;
		animationCount = 0;
		createCookie('thumbsPage_'+projectNumber,currentPageNumber,'');
		return;
	}
}