/**
* Author:      Dan (http://thewebpageofdan.com)
* Description: Common javascript functions
*/

// Determine browser
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = ((userAgent.indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'));
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_ie7    = ((is_ie) && (userAgent.indexOf('msie 7.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);

var firstTab = "games";

// Cookies
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
	var j;
	var cookieName;
	var cookie = cookies[i];
	
	while (cookie.charAt(0) == " ")
	{
		cookie = cookie.substring(1, cookie.length);
	}

	if (cookie.indexOf("lasttwpodtab=") != -1)
	{
		cookieName = "lasttwpodtab";
		firstTab = cookie.substring(cookieName.length + 1, cookie.length);
	}
}

function switchTab(newTab)
{
	var tabs = new Array("sites", "games", "apps");

	for (var i = 0; i < tabs.length; i++)
	{
		document.getElementById(tabs[i] + "_tab").className = "";
		document.getElementById(tabs[i] + "_content").style.display = "none";
	}
	
	document.getElementById(newTab + "_tab").className = "current";
	document.getElementById(newTab + "_content").style.display = "block";
	document.getElementById(newTab + "_content").style.className = "portfolio_content";

	var date = new Date();
	date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000)); // 365 days

	document.cookie = "lasttwpodtab=" + newTab + "; expires=" + date.toGMTString() + "; path=/";
}

function enableFormInteraction()
{
	var objEvt = {
		input   : ["hover", "focus"],
		select  : ["hover", "focus"],
		textarea: ["hover", "focus"]
	};

	// spare use variables
	var temp;
	var tempLen = 0;

	// hover/focus functions
	function hoverFunc()
	{
		this.className += " hover";
	}

	function unHoverFunc()
	{
		this.className = this.className.replace(" hover", "");
		this.className = this.className.replace("hover", "");
	}

	function focusFunc()
	{
		this.className += " focus";
	}

	function unFocusFunc()
	{
		this.className = this.className.replace(" hover", "");
		this.className = this.className.replace(" focus", "");
		this.className = this.className.replace("hover", "");
		this.className = this.className.replace("focus", "");
	}

	for (var i in objEvt)
	{
		temp = document.getElementsByTagName(i);
		tempLen = temp.length;

		for (var j = 0; j < tempLen; j++)
		{
			for (var k = 0; k < objEvt[i].length; k++)
			{
				if (objEvt[i][k] == "hover")
				{
					temp[j].onmouseover = hoverFunc;
					temp[j].onmouseout = unHoverFunc;
				}
				else if (objEvt[i][k] == "focus")
				{
					temp[j].onfocus = focusFunc;
					temp[j].onblur = unFocusFunc;
				}
			}
		}
	}
}

function openGame(id, width, height, name)
{
	name = (name) ? name : 'game';
	window.open('/game.php?g=' + id, name, 'width=' + width + ',height=' + height + ',resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
}

$(document).ready(function() {
	$('.page-item-0 a').click(function (){
		$('body').scrollTo($('#footer'), {duration: 1000});
		return false; event.returnValue = false;
	});
});