$(document).ready(function(){
	var username;
	username = getUsername();
	if (username != null) {
		$("#my-coolibri-box span.username").html("Hallo " + username + "!");
		$("#my-coolibri-box span.logout").html(" | <a href=\"/logout.html\">Logout</a>");
	}
});


var externerLinkText = "Externer Link";
var textNeuesFenster = "(Öffnet in einem neuen Fenster)";
function titleAnpassen(linkObjekt) {
	if (linkObjekt.getAttribute) {
		textAlt = linkObjekt.getAttribute("title");
		if(textAlt && textAlt.indexOf(textNeuesFenster) == -1) {
			textZusatz = textAlt.substring((textAlt.indexOf(externerLinkText)) + externerLinkText.length, textAlt.length);
			linkObjekt.setAttribute("title", externerLinkText + " " + textNeuesFenster + textZusatz);
		}
	}
}

function popUpLink(url, title) {
	fenster = window.open(url, title, "innerWidth=810,innerHeight=550,screenY=100,screenX=100,resizable=yes,scrollbars=yes");
	fenster.focus();
}

var location_text = "";

function rendermap(location, address, city) {
	if ((location != null) && (address != null)) {
		location_text += "<strong>" + location + "</strong><br/>";
	}
	if (address != null) {
		location_text += address + "<br/>";
	}
	location_text += city;
//	alert(location_text);

	map = new GMap2(document.getElementById("locationmap"));
	if (address != null) {
		address = address + ", " + city + ", Germany";
	}
	else {
		//address = location + ", " + city + ", Germany";
		address = city + ", Germany";
	}

	var icon = new GIcon();
	icon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);

	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	geocoder = new GClientGeocoder();

	geocoder.getLatLng(address, function(point){
		if (!point) {
			//alert(address + " not found");
			$('div.term-det-gmaps').css('display', 'none');
		} else {
			map.setCenter(point, 15);
			var marker = new GMarker(point);
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(location_text);
				});
		}
	});
}

function submitEventJumpbox(selected) {

	var returnBool = false;

	if(selected != "") {
		if(selected == "insertrange") {
			$("#vonbisbox").slideDown("2500");
		} else {
			$("#eventjumpbox").submit();
		}
		returnBool = true;
	}

	return returnBool;
}

function toggleSlideBox() {
	//icon_toggle_events_close.gif
}

function getUsername() {
	if (document.cookie) {
		//alert("Cookies: " + document.cookie);
		var cookies = document.cookie.split(";");
		for (var i=0; i<cookies.length; i++) {
			//alert(cookies[i]);
			var stop = cookies[i].indexOf("=");
			var key = cookies[i].substring(0,stop).replace(/ /g, "");
			var value = cookies[i].substring(stop+1,cookies[i].length).replace(/ /g, "");
			//alert("<" + key + ">: <" + value + ">");
			if (key == "username")
				return $.base64Decode(value);
		}
		return null;
	}
}


/** Für's ' zählen von c-tv */
var playing = false;
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	//alert (playing + ": " + obj.oldstate + " -> " + obj.newstate);
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	if ((currentState == "COMPLETED") && playing) {
		c_tv_action('completed');
		playing = false;
	}
	if ((currentState == "IDLE") && playing) {
		c_tv_action('aborted');
		playing = false;
	}
	if ((currentState == "PLAYING") && !playing) {
		c_tv_action('view');
		playing = true;
	}
}

