/*************************************

	Kyle Cato
	kylecato.com
	
	Resources:
	http://www.alistapart.com/articles/footers
	http://techpatterns.com/downloads/javascript_cookies.php
	http://wiki.script.aculo.us/scriptaculous/show/EffectsTreasureChest
	http://bennolan.com/behaviour/

*************************************/


/*************************************

	Cookie related functions
	http://techpatterns.com/downloads/javascript_cookies.php

*************************************/


function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this function gets the cookie, if it exists
function Get_Cookie( name ) {
        
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}


// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



/*************************************

	Function for finding the height of the window
	http://www.alistapart.com/articles/footers

*************************************/

function getWindowHeight() 
{
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') 
	{
	windowHeight = window.innerHeight;
	}
		else 
		{
			if (document.documentElement && document.documentElement.clientHeight) 
			{
			windowHeight = document.documentElement.clientHeight;
			}
				else 
				{
					if (document.body && document.body.clientHeight) 
					{
					windowHeight = document.body.clientHeight;
					}
				}
		}
		return windowHeight;
}


/*************************************

	Function that places the footer at the bottom of the page no matter the window size.
	Hacked from: http://www.alistapart.com/articles/footers

*************************************/

function setFooter()
{
	if (document.getElementById) 
	{
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) 
		{
			var contentHeight = document.getElementById('content').offsetHeight;
			var sub_navHeight = document.getElementById('sub_nav').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
				if ((contentHeight + footerHeight) >= sub_navHeight) 
				{
					if (windowHeight - (contentHeight + footerHeight) >= 0) 
					{
						footerElement.style.position = 'absolute';
						footerElement.style.top = (windowHeight - footerHeight) + 'px';
					}
					else 
					{
						footerElement.style.position = 'absolute';
						footerElement.style.top = contentHeight + 'px';
					}
				}
				else 
				{
					if (windowHeight - sub_navHeight >= 0)
					{
						footerElement.style.position = 'absolute';
						footerElement.style.top = (windowHeight - footerHeight) + 'px';
					}
					else 
					{
						footerElement.style.position = 'absolute';
						footerElement.style.top = (sub_navHeight - footerHeight) + 'px';
					}
				}
		}
	}
}



/*************************************

	Function that stretches both columns to the height of the window
	Hacked from: http://www.alistapart.com/articles/footers

*************************************/

function setColumns() {
	if (document.getElementById) 
	{
		var windowHeight = getWindowHeight();
		var contentHeight = document.getElementById('content').offsetHeight;
		var footerElement = document.getElementById('footer');
		var footerHeight  = footerElement.offsetHeight;
		var column1Element = document.getElementById('column1');
		var column1Height = (windowHeight - footerHeight);
		var sub_navHeight = document.getElementById('sub_nav').offsetHeight;
		var column2Element = document.getElementById('column2');
		var column2Height = windowHeight
		if ((contentHeight + footerHeight) >= sub_navHeight) 
		{
			if (windowHeight >= (contentHeight + footerHeight)) 
			{
				column1Element.style.height = (windowHeight - footerHeight) + 'px';
				column2Element.style.height = windowHeight + 'px';
			}
				else 
				{
					column1Element.style.height = contentHeight + 'px';
					column2Element.style.height = (contentHeight + footerHeight) + 'px';
				}
		}
		else 
		{
			if (windowHeight >= sub_navHeight) 
			{
				column1Element.style.height = (windowHeight - footerHeight) + 'px';
				column2Element.style.height = windowHeight + 'px';
			}
				else 
				{
					column1Element.style.height = (sub_navHeight - footerHeight) + 'px';
					column2Element.style.height = sub_navHeight + 'px';
				}
		}
	}
}



/*************************************
	
	function for the nav button
	http://wiki.script.aculo.us/scriptaculous/show/EffectsTreasureChest

*************************************/

function setClosed () 
{
	document.getElementById('sub_nav_content').style.display = 'none';
	document.getElementById('button1').style.backgroundPosition = '0px 0px';
	setColumns();
	setFooter();
}

function setClosed2 () 
{
	document.getElementById('button1').style.backgroundPosition = '0px -55px';
}


var folded = new Object();
function slide(el)
{            
    if(folded[el] == 1)
    {
			document.getElementById('column2').style.margin = '0 0 0 151px';
			setTimeout('setClosed()', 1000);
			Effect.SlideLeft(el);
			folded[el] = 0;
			Delete_Cookie("pref", "/", "kylecato.com");
    }

    else
    {
			document.getElementById('sub_nav_content').style.display = 'block';
			setTimeout('setClosed2()', 1000);
			Effect.SlideRight(el);
			folded[el] = 1;
			setColumns();
			setFooter();
			Set_Cookie("pref", "open", "", "/", "kylecato.com", "");
    }
}



/*************************************
	
	JavaScript Image Gallery
	http://www.alistapart.com/articles/imagegallery

*************************************/



function showPic (whichpic) { 
	if (document.getElementById) { 
		document.getElementById('placeholder').src = whichpic.href; 
		return false; 
	} else { 
		return true; 
	} 
}


/*************************************
	
	Apply behaviors to CSS styled elements
	http://bennolan.com/behaviour/

*************************************/



var myrules = {
	'#port_nav li a' : function(element){
		element.onclick = function(){
			return showPic(this);
			}
		},
	'#button1' : function(element){
		element.onclick = function(){
			slide('column2');
			}
		}
	};
        
Behaviour.register(myrules);


/*************************************
	
	Test to see if pref cookie is set
	if so, slide the column out and set the button to state 1

*************************************/


function testCookie() {
	if ( Get_Cookie( 'pref' ) ) 
	{
		folded['column2'] = 1;
		document.body.style.background = '#ffe url(/images/bg.gif) repeat-y top center';
		document.getElementById('button1').style.backgroundPosition = '0px -56px';
		document.getElementById('button1').style.display = 'block';
	}
		else 
		{
			document.body.style.background = '#ffe url(/images/bg.gif) repeat-y top center';
			document.getElementById('column2').style.margin = '0 0 0 151px';
			document.getElementById('sub_nav_content').style.display = 'none';
			document.getElementById('button1').style.display = 'block';
			setColumns();
			setFooter();
		}
}



/*************************************
	
	Onload/onresize Functions

*************************************/


window.onload = function() {
	testCookie();
	setColumns();
	setFooter();
	Behaviour.apply();
	JSFX_FloatDiv("button1", 0,76).floatIt();
}

window.onresize = function() {
	setColumns();
	setFooter();
}



/*************************************

	Position the button at the top of the window
	"borrowed" from http://www.travel-dive.com/
	
	I will write a script of my own, I'm just pressed for time

*************************************/


var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
function JSFX_FloatDiv(id, sx, sy)
{
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};

	el.floatIt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.floatIt()", 40);
	}
	return el;
}
