/*
1982 - 2009 © carnero

good ideas collected, vcard designed and scripted by carnero (http://paška.eu/)
inspired by Tim Van Damme (http://timvandamme.com/), Rogie King (http://rogieking.com/) and many other quality vcards
icons Token by brsev (http://brsev.deviantart.com/) and Social.me by jwloh (http://jwloh.deviantart.com/)
*/

var panels = new Array('contact', 'map', 'links', 'social');

var ajax = createXmlHttp();
var latTimer = null;
var panelSTimer = null;
var panelHTimer = null;

function checkPanel() {
	var i = 0;

	try {
		var panel = window.location.hash;
		panel = panel.toLowerCase();
		panel = panel.substring(1);

		for (i = 0; i < panels.length; i ++) {
			if (panel == panels[i]) {
				activate(panels[i]);
				return;
			}
		}
	} catch(e) { }

	return;
}

function activate(content) {
	var i = 0;

	try {
		clearTimeout(panelHTimer);
		clearTimeout(panelSTimer);

		for (i = 0; i < panels.length; i ++) {
			if (panels[i] == (content)) {
				document.getElementById('button-' + panels[i]).setAttribute('class', 'active');
			} else {
				document.getElementById('button-' + panels[i]).setAttribute('class', '');
			}
		}

		for (i = 0; i < panels.length; i ++) {
			var panel = document.getElementById('panel-' + panels[i]);
			if (panels[i] != content && panel.getAttribute('class').indexOf('now') > -1) {
				var opacity = 100;
				if (parseInt(panel.style.opacity) != 0 && !isNaN(panel.style.opacity)) {
					opacity = (panel.style.opacity * 100);
				}
				hidePanel(panels[i], opacity, content);
				continue;
			}
		}

		var loc = window.location.toString();
		var hashPos = loc.indexOf('#');
		if (hashPos > -1) {
			loc = loc.substring(0, hashPos);
		}
		window.location = loc + '#' + content;
		document.title = 'Radovan \'carnero\' Paška ~ ' + content;

		if (content == 'map') {
			if (latTimer != null) {
				clearTimeout(latTimer);
			}
			latitude();
		}
	} catch(e) { }

	return;
}

function setOpacity(el, opacity) {
	try {
		if (opacity > 100) {
			opacity = 100;
		}
		if (opacity < 0) {
			opacity = 0;
		}

		el.style.opacity = (opacity / 100);
		el.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity + ')';
		el.style.filter = 'alpha(opacity=' + opacity + ')';
	} catch(e) { }
}

function hidePanel(panel, opacity, next) {
	try {
		var panelEl = document.getElementById('panel-' + panel);
		opacity = opacity - 10;

		if (opacity <= 0) {
			var panelNextEl = document.getElementById('panel-' + next);

			setOpacity(panelEl, 0);
			panelEl.setAttribute('class', 'content');
			setOpacity(panelNextEl, 0);
			panelNextEl.setAttribute('class', 'content now');

			panelSTimer = setTimeout('showPanel(\'' + next + '\', 0)', 1);
		} else {
			setOpacity(panelEl, opacity);

			panelHTimer = setTimeout('hidePanel(\'' + panel + '\', ' + opacity + ', \'' + next +'\')', 15);
		}
	} catch(e) { }
	
	return;
}

function showPanel(panel, opacity) {
	try {
		var panelEl = document.getElementById('panel-' + panel);
		opacity = opacity + 10;

		if (opacity >= 100) {
			setOpacity(panelEl, 100);
		} else {
			setOpacity(panelEl, opacity);

			panelSTimer = setTimeout('showPanel(\'' + panel + '\', ' + opacity + ')', 30);
		}
	} catch(e) { }

	return;
}

function getAbsPos(el) {
    pos = new Array();
    pos[0] = 0; // x
    pos[1] = 0; // y

    var elChng = el;
    while (elChng != null) {
        pos[0] += elChng.offsetLeft;
        elChng = elChng.offsetParent;
    }

    elChng = el;
    while (elChng != null) {
        pos[1] += elChng.offsetTop;
        elChng = elChng.offsetParent;
    }

    return pos;
}

function createXmlHttp() {
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
		for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++) {
			try {
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			} catch(e) {}
		}
	}

	if (xmlHttp) {
		return xmlHttp;
	}

	return false;
}

function latitude() {
	var lat = 0;
	var lon = 0;
	var acc = 0;
	var tim = 0;
	var plc = '';

		try {
		var img = document.getElementById('panel-map');
		var link = document.getElementById('map-link');

		if (ajax) {
			try {
				ajax.open('GET', '/files/latitude.php', true);
				ajax.onreadystatechange = function() {
					if (ajax.readyState == 4) { if (ajax.status == 200) {
						try {
							var response = ajax.responseText.split(';');
							if (response.length > 0) {
								lat = parseFloat(response[0]);
								lon = parseFloat(response[1]);
								acc = parseInt(response[2]);
								tim = parseInt(response[3]);
								plc = response[4];

								img.style.backgroundImage = 'url(\'http://maps.google.com/staticmap?center=' + lat +',' + lon + '&zoom=11&size=300x180&maptype=mobile&key=ABQIAAAAgci_9p0I41lV4Y8rEucYzBQv42wQuztLRfC-Pt9CqpgjzqdoPBQhd4fAn5-8deWhVYUIZka9JJ6I0g&sensor=false\')';
								img.setAttribute('title', plc);
								if (lat != NaN && lon != NaN) {
									link.href = 'http://maps.google.com/maps?q=' + lat + '%2C' + lon;
								} else {
									link.href = '#';
								}
							}
						} catch(e) { }
					}}
				};
				ajax.send(null);
			} catch(e) { }
		}
	} catch(e) { }

	latTimer = setTimeout(latitude, 60000);
}