
function isTouchDevice() {
	var ua = navigator.userAgent.toUpperCase();
	
	//alert(ua); // uncomment this out to find out the device name
	
	var isTouchDevice = new Array("IPAD", "IPHONE", "ANDROID");
	
	for (var i in isTouchDevice) {
		if (ua.indexOf(isTouchDevice[i]) >= 0) return true;
	}
	return false;
}

	
function __TAG() {} // Create html elements 

function Timer() {this.construct();}

//alert(isTouchDevice());

Timer.prototype = {
	construct : function () {
		this.interval = 1000;
		this.enable = false;
		this.timerId = 0;
		this.lastTime = 0;
	},
	
	start : function(interval, callback, callbackClass) 
	{
        this.enable = true;
		this.callbackClass = callbackClass;
		this.callback = callback;
		this.interval = interval;
		this.lastTime = new Date().getTime();
		

		var capture = this;
        if(this.enable)
        {
			this.timerId = setInterval(function()
										{
											capture.dispatcher();
										}, interval);
        }
    },

    dispatcher : function()
    {
    	var newTime = new Date().getTime();
    	this.callback.call(this.callbackClass, (newTime-this.lastTime)/1000);
    	this.lastTime = newTime;
    },
    
    stop : function() 
    {            
        this.enable = false;
        clearInterval(this.timerId);
    }
};


__TAG.prototype = {
    style: function(obj, style)
    {
        for (var i in style)
            obj.style[i] = style[i];
    },

    fn: function(obj, fn)
    {
        for (var i in fn) {
		   obj[i] = fn[i];
		}
    },

    add: function(tag, className, parent, appendStyle, appendFN)
    {
        var tagInfo = tag.split(",");
        var obj = document.createElement(tagInfo[0]);
        if (tagInfo[0] == "IMG") {
            obj.src = tagInfo[1];
		}

        if (className) {
            obj.className = className;
        }

        parent.appendChild(obj);

        if (appendStyle) {
            this.style(obj, appendStyle);
        }
        
        if (appendFN) {
            this.fn(obj, appendFN);
        }

        return obj;
    },
    
    text: function(text, parent) {
        parent.innerHTML = text; // ToDo, use createText? instead....
    },
    
    textEx: function (text, parent) {
        parent.appendChild(document.createTextNode(text));
    }
}

function Twitter () {}

function showTwitter(JSONData) {
	var twitter = new Twitter();
	var idx = Math.floor(Math.random()*6);
	var tweet = JSONData[idx];
		
	document.getElementById("twitterOutput").innerHTML = tweet.text + "\n<span class='tweetTime'>" + twitter.getTimeTweeted(tweet.created_at) + "</span>";
	document.getElementById("twitter").style.display = "block";
}

Twitter.prototype = {

	isIE : function () {
		return navigator.userAgent.match(/MSIE\s([^;]*)/);
	},

	getTimeTweeted : function (tweetTime) {
		var now = new Date();
		var when = new Date(tweetTime);
		
		if (this.isIE()) {
			try {
				when = Date.parse(when.replace(/( \+)/, ' UTC$1'));
			}
			catch (err) {
				return "";
			}
		}
		
		var timeDiff = now - when;
		var microSeconds = 1000;
		
		var minute = microSeconds * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var week = day * 7;
			
		if (isNaN(timeDiff) || timeDiff < 0) return "";
		if (timeDiff < microSeconds * 10) return "right now";
		if (timeDiff < minute) return Math.floor(timeDiff / microSeconds) + " seconds ago";
		if (timeDiff < minute * 2) return "about 1 minute ago";
		if (timeDiff < hour) return Math.floor(timeDiff / minute) + " minutes ago";
		if (timeDiff < hour * 2) return "about 1 hour ago";
		if (timeDiff < day) return Math.floor(timeDiff / hour) + " hours ago";
		if (timeDiff > day && timeDiff < day * 2) return "yesterday";
		if (timeDiff < day * 365) return Math.floor(timeDiff / day) + " days ago";
		
		return "over a year ago"
	}
}

function animatePopMenus() {
	this.construct();
}

animatePopMenus.prototype = {
	construct : function ()  {
		this.hideYPOS = 100;
		this.popupMode = new Array();
		this.popupModeYPOS = new Array();
		this.initEvents();
		
		this.popupTimer = new Timer();
		this.popupTimer.start(15, this.popup, this);
		this.isTouchDevice = isTouchDevice();
		window.onunload = this.hidePoppedUpCharacters;
	},
	
	hidePoppedUpCharacters : function() {
		for (var i in animatePopupMenu.popupMode) {
			animatePopupMenu.popupMode[i] = 0;
		}
	},
		
	popup : function () {
		for (var i in this.popupMode) {
			var puppet = this.divs[i];
			var isHidden = this.popupMode[i] == 0;
			
			targetY = (isHidden) ? this.hideYPOS : 0;
			
			results = this.isTouchDevice ? targetY : (targetY - this.popupModeYPOS[i]) / 1.5;
			if (Math.abs(results) < 0.01) {
				this.popupModeYPOS[i] = isHidden ? this.hideYPOS : 0;
			}
			else {
				this.popupModeYPOS[i] += results;
			}
			puppet.style.top = parseInt(this.popupModeYPOS[i])+"px"; 
		}
	},
	
	initEvents : function () {
		this.divs = new Array();
		var menus = document.getElementById("primarymenu");
		
		var li = menus.childNodes;
		var noOfNodes = li.length;
		
		for (var i = 0; i < noOfNodes; i++) {
			var tag = li[i];
			
			if (tag.tagName != null && tag.tagName.toUpperCase() == "A") {
				//var a = this.walkDOMTree(tag, new Array("A"), 0);
				var div = this.walkDOMTree(tag, new Array("DIV", "DIV", "DIV"), 0);
				
				if (div != null) {
					div.style.top = "100px";
					if (tag != null) {
						this.popupMode.push(0);
						this.popupModeYPOS.push(this.hideYPOS);
						var divInx = this.divs.length;
						tag.divInx = divInx;
						tag.classOwner = this;
						tag.onmouseover = this.showCharacters;
						tag.onmouseout = this.hideCharacters;
						this.divs[divInx] = div;
					}
				}
			}
		}
	},
	
	showCharacters : function () { 
		this.classOwner.popupMode[this.divInx] = 1;
	},
	
	hideCharacters : function () {
		this.classOwner.popupMode[this.divInx] = 0;
	},
	
	walkDOMTree : function(p, tagsToFind, inx) {
		var c = p.childNodes;
		var noOfNodes = c.length;
		for (var i = 0; i < noOfNodes; i++) {
			var tag = c[i];
			if (tag.tagName != null && tag.tagName.toUpperCase() == tagsToFind[inx]) {
				if (inx == tagsToFind.length - 1) {
					return tag;
				}
				else {
					return this.walkDOMTree(tag, tagsToFind, inx + 1);
				}
			}
		}
		return null;
	}
}

