$(function() {
	/*
	$(".calendarcontainer").load("/kalenteri", {}, function() {
		refreshCalNav();
	});	
	*/
	/* submenu */
	$(".menuitemcontainer").mouseover(function() {
		$(this).stopTime();
		$(this).children(".submenu").show();
	});
	$(".menuitemcontainer").mouseout(function() {
		$(this).oneTime(1000, function() {
			$(this).children(".submenu").hide();
		})
	});
	$(".autosubmitOnEnter").keydown(function(event) {
		if (event.keyCode == 13) $("#loginform").submit();
	});
	$(".objlightbox").click(function() {
		var oid = $(this).attr("id");
		if (!oid) {
			var srcsplit=$(this).attr("src").split("oid=");
			var oid = srcsplit[1].split("&");
			oid = oid[0];
		}
		showObjLb(oid);
	});
	$("form.submitonenter input").keyup(function(event) {
		if (event.keyCode == 13) {
			var counter = 0;
			var stop = false;
			newobj = $(this).parent();
			while (!stop) {
				if (newobj) {
					if (newobj[0].tagName.toLowerCase() == "form") newobj.submit();
					newobj = newobj.parent();
				}
				if (counter++ > 10) stop = true;
			}
		}
	});
	if ($(".autoshow").attr("id")) showObjLb($(".autoshow").attr("id"));
});

function checkBid() {
	var oid = $(".bidamountfield").attr("id");
	var bidsum = $(".bidamountfield").val();
	var isbuy = $(".bidamountfield").hasClass("buyfield") ? 1 : 0;
	if (confirm($("#bidconfirm").html())) {
		$.post("/ajax/checkbid", {oid: oid, bidsum: bidsum, isbuy: isbuy}, function(data) {			
			if (data == 'error') alert($("#biderror").html());
			else if (data == 'low') alert($("#bidlow").html());
			else if (data == 'lowbefore') alert($("#bidlowbefore").html());
			else if (data == 'ok') alert($("#bidok").html());
			else alert($("#bidminraise").html() + " " + data + "€");
			// refresh page
			location.reload(true);
		});
	}
}

function refreshCalNav() {
	$(".calprev").unbind("click");
	$(".calnext").unbind("click");
	$(".hasevent").unbind("click");
	
	$(".calprev").click(function() {
		var ts = $(".monthcalendar").attr("id");
		$(".calendarcontainer").load("/kalenteri", {ts: ts, operation: "prev"}, function() {
			refreshCalNav();
		});
	});
	$(".calnext").click(function() {
		var ts = $(".monthcalendar").attr("id");
		$(".calendarcontainer").load("/kalenteri", {ts: ts, operation: "next"}, function() {
			refreshCalNav();
		});
	});
	$(".hasevent").mouseover(function() {
		var ts = $(this).attr("id");
		$(".eventinfo").html("");
		$(".eventinfo").show();	
		$.post("/kalenteri/dayevents", {ts: ts}, function(data) {
		    $(".eventinfo").html(data);
		});
	});
	$(".monthcalendar").mouseout(function() {
		$(".eventinfo").hide();
	});
	$(".monthitem").mouseout(function() {
		$(".eventinfo").hide();
	});
	$(".hasevent").click(function() {
		var ts = $(this).attr("id");
		var url = "/auctions/?ats=" + ts;
		document.location.href = url;
	});
}

function closeObjLb() {
	$("#lbWindow").remove();
	$("#lbOverlay").remove();
	$(window).unbind('keydown');
}

function refreshObjLbListener()
{
	$(window).unbind('keydown');
	$(window).keydown(function(event) {
		if (event.keyCode == 27) closeObjLb();
		else if (event.keyCode == 39) {
			if ($(".nextobj").attr("id")) showObjLb($(".nextobj").attr("id"));
		}
		else if (event.keyCode == 37) {
			if ($(".prevobj").attr("id")) showObjLb($(".prevobj").attr("id"));
		}
	});
}

function showObjLb(oid) {
	var downloadable = $("#" + oid).hasClass("downloadable") ? 1 : 0;
	var bidobj = $("#" + oid).hasClass("bidobj") ? 1 : 0;
	if (!$("#lbOverlay").length) {
		$("body").prepend('<div id="lbWindow"><div class="navi"><a href="javascript:closeObjLb();" class="closebutton"></a></div><div id="lbImage"></div><div id="lbDescription"></div></div>');
		$("body").prepend('<div id="lbOverlay"></div>');
	}
	else {
		$("#lbWindow").remove();
		$("body").prepend('<div id="lbWindow"><div class="navi"><a href="javascript:closeObjLb();" class="closebutton"></a></div><div id="lbImage"></div><div id="lbDescription"></div></div>');
	}
	
	var img = new Image();
	var imageUrl = "/objimage/show?oid=" + oid + "&width=700&height=500";
	$(img)
	    .attr('src', imageUrl)
	    .attr('id', 'bigpicture')
	    .load(function() {
		    if (downloadable) {
		    	$("#lbImage").append('<a href="/sendfile/?fid='+oid+'"></a>');
		    	$("#lbImage a").append($(this));
		    }
	    	else $("#lbImage").append($(this));
	    	$("#lbOverlay").css('width', $(document).width());
	    	$("#lbOverlay").css('height', $(document).height());
	    	$("#lbOverlay").show();
	    	$("#lbOverlay").click(function() { closeObjLb(); });
	    	$("#lbDescription").width($("#lbWindow").outerWidth(true)-20);
	    	$("#lbDescription").load("/ajax/objlbdescription/", {oid: oid, downloadable: downloadable, bidobj: bidobj}, function() {
	    		$(".closebutton").html($(".closelabel").html());
	    		refreshObjLbListener();
	    	});
	    	// calculate coordinates for centering lightbox
	    	var newx = $(document).scrollLeft() + $(window).width()/2 - $("#lbWindow").outerWidth(true)/2;
	    	var newy = $(document).scrollTop() + $(window).height()/2 - $("#lbWindow").outerHeight(true)/2 - 100;
	    	$("#lbWindow").css('left', newx);
	    	$("#lbWindow").css('top', newy);
	    	$("#lbWindow").fadeIn("normal");
	    });
}

