$(document).ready(function(){
var menu_sport = '';

$("#navi2_bx ul li a.topic").click(function(e) {
	e.preventDefault();

    if(menu_sport == this.id) {
	    menu_sport = '';
		delCookie('menu_sport');
	} else {
	    menu_sport = this.id;
		setCookie('menu_sport', this.id);
	}

	$(this).toggleClass('selected');
	$("#" + this.id + "_submenu").toggle();

	$("#navi2_bx ul li a.topic").each(function() {
	    if(this.id != menu_sport) {
			$("#" + this.id + "_submenu").hide();
			$(this).removeClass("selected");
		}
	});

	var children = $(this).parent().children().length;
	if(children == 1) {
		$(this).after('<div id="' + this.id + '_submenu" class="sub_bx">Loading...</div>');
		var this_id = $(this).attr('id');
		var this_name = $(this).attr('name');
		$.getJSON('libs/ajaxhandler.php?action=topics_get_link&idparenttopic=' + this.id, function(data){
		    var submenu_str = '<ul>';
			//submenu_str += '<li><a href="sport/' + this_id + '/' + this_name + '/">&Uuml;bersicht</a></li>';
			$.each(data, function(link, name){
				if(name != 'null') {
					submenu_str += '<li><a href="' + link + '">' + name + '</a></li>';
				}
			});
			submenu_str += '</ul>';
			$("#" + this_id + "_submenu").html(submenu_str);
		});
	}
});

$('#navi2_bx div.aff_bx a img, #right_bx div.aff_bx a img').hover(function(){
	$(this).attr('src', $(this).attr('src').replace(/_normal/g, '_hover'));
	}, function() {
	$(this).attr('src', $(this).attr('src').replace(/_hover/g, '_normal'));
});

$("#login form").submit(function(e){
	e.preventDefault();

	var username = $("#username").attr('value');
	var password = $("#password").attr('value');
	$.ajax({
		type: "POST",
		timeout: 7000,
		data: {username: username, password: password},
		url: "libs/ajaxhandler.php?action=login",
		dataType:"json",
		success: function(result) {
			result = result.toString();
			if(result) {
				alert(result);
			} else {
				self.location.reload();
			}
		}
	});
});

$("#logout").click(function(e){
    e.preventDefault();

    $.ajax({
		type: "POST",
		timeout: 7000,
		url: "libs/ajaxhandler.php?action=logout",
		success: function() {
			self.location.reload();
		}
	});
});

$("#login form input").focus(function(){
	$(this).val('');
});

$("#my_bx div.icons a img").hover(function(){
		$("#my_bx div.info").html($(this).attr('title'));
	}, function() {
        $("#my_bx div.info").html(prof_username);
});

$('#pic_link').click(function(e){
	e.preventDefault();

	if(confirm('Namensschild wirklich platzieren?')) {
		var pos = getMousePos(e);
		var pic = document.getElementById('pic_bx');

		// different sizes for mozilla and ie...god knows why?!
		if(e) {
			pos_x = pos.x - getPosX(pic);
			pos_y = pos.y - getPosY(pic);
		} else {
			pos_x = pos.x;
			pos_y = pos.y;
		}

		var iduser = $(this).attr('rel');
		var idpic = $('#pic_img').attr('rel');
		$.getJSON('libs/ajaxhandler.php?action=nametag_add&iduser=' + iduser + '&idpic=' + idpic + '&x=' + pos_x + '&y=' + pos_y, function(result){
			if(result != '') {
				// delete former nametag
				$('#nt_iduser_' + iduser).remove();

				var nametag = '<div class="nametag" id="nt_iduser_' + iduser + '" style="left:' + pos_x + 'px; top:' + pos_y + 'px;">';
				nametag += '<a href="member/' + result + '">' + result + '</a>';
				nametag += '</div>';
				$('#pic_bx').append(nametag);

				$('#nametag_delete').css('display', 'inline');
			}
		});
	}
});

$('#nametag_delete').click(function(e) {
	e.preventDefault();

	if(confirm(unescape('Namensschild wirklich l%F6schen?'))) {
		var iduser = $(this).attr('rel');
		var idpic = $('#pic_img').attr('rel');

		var this_object = $(this);
		$.getJSON('libs/ajaxhandler.php?action=nametag_delete&iduser=' + iduser + '&idpic=' + idpic, function(result){
			if(result == 'success') {
				// delete nametag
				$('#nt_iduser_' + iduser).remove();
				$(this_object).css('display', 'none');
			}
		});
	}
});

$('a.message_delete').click(function(e){
	e.preventDefault();

	if(confirm(unescape('Nachricht wirklich l%F6schen?'))) {
		var idmessage = $(this).attr('rel');

		$.getJSON('libs/ajaxhandler.php?action=message_delete&idmessage=' + idmessage, function(result){
			if(result == 'success') {
				// delete message in list
				$('#msg_idmessage_' + idmessage).remove();
			}
		});
	}
});

$('a.shop_delete').click(function(e){
    e.preventDefault();

	if(confirm(unescape('Position wirklich l%F6schen?'))) {
		var idpic = $(this).attr('rel');

		$.getJSON('libs/ajaxhandler.php?action=shop_delete&idpic=' + idpic, function(result){
			if(result == 'success') {
				// delete message in list
				$('#shp_item_pic_' + idpic).remove();
			}
		});
	}
});

$('div.has_comments').each(function(){
	$(this).attr('stip', 'zu diesem Foto gibt es Kommentare');
});

$('*[stip]').hover(function(e) {
	var text = $(this).attr('stip');
	var pos = getMousePos(e);
	var ix = getPosX(this);
	var iy = getPosY(this);

	$('#spo').append('<div class="stip" style="left:' + ix + 'px; top:' + (iy-this.offsetHeight) + 'px;">' + text + '</div>');
}, function() {
	$('#spo div.stip').remove();
});

$('#search_bx #q, #form_search input').autocomplete('libs/ajaxhandler.php?action=search_ac', {
	max: 0,
	scroll: true,
	scrollHeight: 220
});

$('#prf_sports_btn').click(function(e){
	e.preventDefault();

	insertSports('prf_sel_sports');
});

/*
$('div:not(.locationlist).list_bx .thumb a img').hover(function(){
		$(this).css('z-index', 100);
		$(this).animate({
			width: '150px',
			height: '100px'
		}, 200);
	}, function(){
		$(this).css('z-index', 'auto');
		$(this).animate({
			width: '110px',
			height: '75px'
		}, 200);
});
*/


// actions performed when document is loaded

if(getCookie('menu_sport') != '') {
	//$('#' + getCookie('menu_sport')).click();
}

});

