var TWITTER_PATH = "twitter.php";
var mouseX;
var mouseY;
var hideArrows = false;
var isAnimating = false;
var timeout;
var topnavOver = false;

function dtfInit(pageName) {
	emailNoSpam();
	initLeftNav(pageName);
	//getStatus();
	setupZoom();
	
	Event.observe(document, 'mousemove', getCoords);
}


function getCoords(e) {
	mouseX = Event.pointerX(e);
	mouseY = Event.pointerY(e);
	
	if ($("arrowLeft") && $("arrowRight")) {
		if (mouseX > $$('#leftnav')[0].cumulativeOffset()[0] && mouseX < ($$('#leftnav')[0].cumulativeOffset()[0] + $$('#leftnav')[0].offsetWidth)) {
			hideArrows = true;
		} else {
			hideArrows = false;
		}
	}
	
	/*
	if (!topnavOver) {
		if (mouseX < ($$('#leftnav')[0].cumulativeOffset()[0] + $$('#leftnav')[0].offsetWidth)) {
			showLeftNav();
		} else {
			hideLeftNav();
		}
	}
	*/
	
	if ($("arrowLeft") && $("arrowRight") && !zoomOpen) {
		// gallery icons, events
		if (hideArrows == false && mouseY > $$('#main div')[0].cumulativeOffset()[1] && mouseY < ($$('#main div')[0].cumulativeOffset()[1] + $$('#main div div')[0].offsetHeight)) {
			if (mouseX < (document.viewport.getWidth() / 2)) {
				$("arrowLeft").style.visibility = "visible";
				$("arrowRight").style.visibility = "hidden";
				$("arrowLeft").style.left = (mouseX - 20) + "px";
				$("arrowLeft").style.top = (mouseY - 8) + "px";
			} else {
				$("arrowRight").style.visibility = "visible";
				$("arrowLeft").style.visibility = "hidden";
				$("arrowRight").style.left = (mouseX + 12) + "px";
				$("arrowRight").style.top = (mouseY - 8) + "px";
			}
			
			// turn on click
			Event.observe(document, 'click', galleryClick);
			
		} else {
			// turn off click
			Event.stopObserving(document, 'click', galleryClick);
		
			$("arrowLeft").style.visibility = "hidden";
			$("arrowRight").style.visibility = "hidden";
		}
	}
}

function galleryClick(e) {
	if (!zoomOpen) {
		if ($("arrowLeft").style.visibility == "visible") {
			selectedItem--;
			if (selectedItem < 0) selectedItem = 0;
			selectProject(selectedItem);
		} else if ($("arrowRight").style.visibility == "visible") {
			selectedItem++;
			if (selectedItem >= $$("#main div div .image").length) selectedItem = $$("#main div div .image").length - 1;
			selectProject(selectedItem);
		}
	} else {
		zoomOut($("zoomBox"), e);
	}
}


function initLeftNav(pageName) {
	var navElements = $$("#leftnav a");
	for (var i = 0; i < navElements.length; i++) {
		if (navElements[i].getAttribute("rel") != pageName) {
			navElements[i].onmouseover = navOver;
			navElements[i].onmouseout = navOut;			
		}
	}
}

function navOver() {
	var img = this.getElementsByTagName("img")[0];
	img.src = String(img.src).split("-off").join("-on");
}

function navOut() {
	var img = this.getElementsByTagName("img")[0];
	img.src = String(img.src).split("-on").join("-off");
}

function topnavRollOver() {
	topnavOver = true;
	showLeftNav();
}

function topnavRollOut() {
	topnavOver = false;
	hideLeftNav();
}

function showLeftNav() {
	clearTimeout(timeout);
	if (($("leftnav").style.height == "0px" || $("leftnav").style.height == "") && !isAnimating) {
		new Effect.Morph($("leftnav"), {style:'height:408px;', duration:0.5, beforeStart:startAnimating, afterFinish:stopAnimating});
	}
}

function hideLeftNav() {
	clearTimeout(timeout);
	timeout = setTimeout("hideLeftNavRun();", 3000);
}

function hideLeftNavRun() {
	if (($("leftnav").style.height == "408px" || $("leftnav").style.height == "") && !isAnimating) {
		new Effect.Morph($("leftnav"), {style:'height:0px;', duration:0.5, beforeStart:startAnimating, afterFinish:stopAnimating});
	}
}

function startAnimating() {
	isAnimating = true;
}

function stopAnimating() {
	isAnimating = false;
}

function getJSONResponse(transport) {
	var dtfStatus = eval(transport.responseText)[0];
	var timeArray = dtfStatus["created_at"].split(" ");
	timeArray.shift();
	timeArray.shift();
	timeArray.shift();
	dtfStatus["created_at"] = timeArray.join(" ");
	if (dtfStatus != undefined && dtfStatus["text"] != "BLANK") {
		var hours = dtfStatus["created_at"].split(":")[0] - 5;
		if (hours < 0) hours = 24 + hours;
		if (hours > 12) hours = hours - 12;
		var minutes = dtfStatus["created_at"].split(":")[1];
		$$("li.status li")[0].innerHTML = hours + ":" + minutes;
		$$("li.status li.message")[0].innerHTML = dtfStatus["text"];
		$$("li.now")[0].style.marginLeft = "0px";
	} else {
		$$("li.now")[0].parentNode.removeChild($$("li.now")[0]);
	}
}

function getJSONResponseFail() {
}

function getStatus() {
	new Ajax.Request(TWITTER_PATH, {method:'get',onSuccess:getJSONResponse,onFailure:getJSONResponseFail});
}

function emailNoSpam() {
	var anchorArray = document.getElementsByTagName("a");
	for (var i = 0; i < anchorArray.length; i++) {
		if (anchorArray[i].className == "mailto") {
			var a = document.createElement("a");
			a.href = "mailto:" + anchorArray[i].getAttribute("rel").split("|").join("@");
			a.innerHTML = anchorArray[i].innerHTML;
			anchorArray[i].parentNode.replaceChild(a, anchorArray[i]);
			i--;
		}
	}
}

var Cookies = {  
    aliases: {},  
  
    alias: function(alias, name, defaultValue)  
    {  
        Cookies.aliases[alias] = name;  
  
        Cookies[alias] = function(value, days)  
        {  
            if(value == null)  
                return Cookies.get(name, defaultValue);  
            else  
                Cookies.set(name, value, days);  
        }  
    },  
  
    set: function(name, value, days)  
    {  
        name = Cookies.aliases[name] || name;  
  
        var expires = '';  
  
        if(!isNaN(days))  
        {  
            var date = new Date();  
            date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);  
            expires = "; expires=" + date.toGMTString();  
        }  
  
        document.cookie = name + "=" + escape(value) + expires + "; path=/";  
    },  
  
    get: function(name, defaultValue)  
    {  
        name = Cookies.aliases[name] || name;  
  
        var regex = new RegExp(name + "s*=s*(.*?)(;|$)");  
        var cookies = document.cookie.toString();  
        var match = cookies.match(regex);  
  
        if(match)  
            return unescape(match[1]);  
  
        return defaultValue;  
    },  
  
    erase: function(name)  
    {  
        Cookies.set(name, '', -1);  
    }  
}
