//////////////// General Functions ///////////////
var is_ie;
var is_ie6up;
var is_ie5_5up;
var is_iemac;
    
function browserCheck(agent) {
	var agt=navigator.userAgent.toLowerCase();
	
	// *** BROWSER VERSION ***
	// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
	var is_major = parseInt(navigator.appVersion);
	var is_minor = parseFloat(navigator.appVersion);
	
	// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
	// If you want to allow spoofing, take out the tests for opera and webtv.
	var is_nav    = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
		&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
		&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	var is_gecko  = (agt.indexOf('gecko') != -1);
	
	    is_iemac  = (agt.indexOf("msie") >= 0 && agt.indexOf("mac_powerpc") >= 0);
	    is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_ie3    = (is_ie && (is_major < 4));
	var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
	var is_ie4up  = (is_ie && (is_major >= 4));
	var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
	var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
	var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
	    is_ie5_5up= (is_ie && !is_ie3 && !is_ie4 && !is_ie5);
	var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
	    is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
	
	if (agent == 'ie') return is_ie;
	else return is_nav;
}

function getXMLHttpRequest() {
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {};
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {};
	try { return new XMLHttpRequest(); } catch(e) {};
	return null;
}

var xhr = getXMLHttpRequest();

//////////////// News Functions //////////////////
var status = false;
function toggleNews() {
	if(status) {
		status = false;
		pathname = location.pathname;
		myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		// Set expiration date to 1 year from now.
		var expDate = new Date ();
		expDate.setTime(expDate.getTime() + (365 * 24 * 3600 * 1000));
		SetCookie('story','main',expDate,myDomain);
		document.getElementById("toggle").innerHTML = "<A CLASS='frontpagesmall' HREF='javascript:toggleNews();'>^ Toggle News</A>";
		showStory("main", "story");
	} else {
		status = true;
		pathname = location.pathname;
		myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		// Set expiration date to 1 year from now.
		var expDate = new Date ();
		expDate.setTime(expDate.getTime() + (365 * 24 * 3600 * 1000));
		SetCookie('story','news',expDate,myDomain);
		document.getElementById("toggle").innerHTML = "<A CLASS='frontpagesmall' HREF='javascript:toggleNews();'>^ Toggle Front Page</A>";
		showStory("news", "story");

	}
}
var story = GetCookie('story');

function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                        return getCookieVal (j);
                i = document.cookie.indexOf(" ", i) + 1;
                        if (i == 0)
                                break;
                }
   return null;
}
function SetCookie (name, value) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape (value) +
                ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
                ((path == null) ? "" : ("; path=" + path)) +
                ((domain == null) ? "" : ("; domain=" + domain)) +
                ((secure == true) ? "; secure" : "");
}

////////////////// AJAX News Functions //////////////////////
function parseResponse($layerVal) {
	// Get variable ready
	var $response = "";
	var $layer = parent.document.getElementById($layerVal);
	
	// Wait for HTTP response
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			$response = xhr.responseText;
			$layer.innerHTML = $response;
		} else {
			$layer.innerHTML = "Problem: xhr.status: '" + xhr.status + "'";
		}
	}
	
}

function showStory($item, $layer) {
	browserCheck("ie");
	if(is_ie && !is_ie6up) {
		// Disable Ajax and use conventional page loading.
		parent.document.location.href="/frontpage.php?item=" + escape($item);
	} else {
		// Ajax photo changing
		xhr.open("GET", "/frontpage.php?item=" + escape($item), true);
		xhr.onreadystatechange = function () { parseResponse($layer); }
		xhr.send(null);
	}
}

////////////////// Search Bar Functions //////////////////////
function searchOnClick() {
	var text = document.getElementById("footerSearch");
	if(text.value == "Search") text.value = "";
}
function searchOnBlur() {
	var text = document.getElementById("footerSearch");
	if(text.value == "") text.value = "Search";
}

////////////////// User Account Functions //////////////////////
function toggleRelationship($relationship) {
	if($relationship == "Parent" || $relationship == "Friend" || $relationship == "Relative" || $relationship == "Other") {
		document.getElementById("connection").style.display = '';
	} else {
		document.getElementById("connection").style.display = 'none';
	}
}

////////////////// Star Photos //////////////////////
function starPhoto($picture, $star) {
	toggleLoading('loading', 'show');
	xhr.open("GET", "/gallery/starPhoto.php?filename=" + escape($picture), true);
	xhr.onreadystatechange = function () { parseStarResponse($picture, $star); }
	xhr.send(null);
}

function parseStarResponse($picture, $star) {
	// Get variable ready
	var $response = "";
	var $link     = document.getElementById("control/" + $picture);
	
	// Wait for HTTP response
	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			$response = xhr.responseText;
			if($response.indexOf("star") == 0) {
				toggleLoading($star, "show");
				$link.innerHTML = "<A HREF=\"javascript:starPhoto('" + $picture + "', '" + $star + "')\" CLASS='frontpagesmall'>Unstar This Photo</A>";
			} else if($response.indexOf("unstar") == 0) {
				toggleLoading($star, "hide");
				$link.innerHTML = "<A HREF=\"javascript:starPhoto('" + $picture + "', '" + $star + "')\" CLASS='frontpagesmall'>Star This Photo</A>";
			}
			toggleLoading('loading', 'hide');
		} else {
			toggleLoading('loading', 'hide');
		}
	}
	
}

function toggleLoading(layer, state) {
	if (state == 'show') {
		state = 'visible';
	} else {
		state = 'hidden';
	}
	
	if (browserCheck('ie')) { // IE 4, 5 or 6
		eval( "document.all." + layer + ".style.visibility = state");
	} else {
		document.getElementById(layer).style.visibility = state;
	}
}


