$(document).ready( function() {

	var profil = $("#menuitem2-6");
	var dropmenu = $("#floatmenu");
	if (profil.length > 0 && dropmenu.length > 0) {
		profil.hover( function() {
			floatmenushow();
		}, function(e) {
			floatmenuhide(e);
			//console.log('profil mouseleave');
		});
		$("#floatmenu").mouseleave( function(e) {
			//console.log('menu mouseleave');
			floatmenuhide(e);
		});
	}
	
	function hittest(ob,e) {
		var offset = ob.offset();
		var minx = Math.floor(offset.left)+1;
		var maxx = Math.floor(offset.left + ob.width()-1);
		var miny = Math.floor(offset.top);
		var maxy = Math.floor(offset.top + ob.height()-1);
		var mousex = e.clientX;
		var mousey = e.clientY;
		if ((mousex >= minx && mousex <= maxx) && (mousey >= miny && mousey <= maxy)) return true;
		else return false;
	}
	
	function floatmenushow() {
		var offset = profil.offset();
		var midx = offset.left + profil.width()/2;
		var dropmenu = $("#floatmenu");
		var dropw = dropmenu.width();
		var thetop = Math.floor(offset.top + profil.height());
		var theleft = midx - (dropw / 2);
		dropmenu.css("top",thetop+"px");
		dropmenu.css("left",theleft+"px");
		dropmenu.css("display","block");
	}
	
	function floatmenuhide(e) {
		var menutest = hittest($("#floatmenu"),e);
		//console.log('menu: '+menutest);
		var profiltest = hittest(profil,e);
		//console.log('profil: '+profiltest);
		if (!(menutest || profiltest)) {
			$("#floatmenu").css("display","none");
			$("#floatmenu").css("top","-1000px");
		}
	}
  
	function cresize() {
		var min_height = $("#maincontainer").height(); 
		if ($(window).height() >= min_height ) { 
			$("#topbg").height($(window).height()); 
			$("#bottombg").height($(window).height());
			$("#topbg2").height($(window).height()); 
			$("#topbg3").height($(window).height()); 
			$("#bottombg2").height($(window).height()); 
			$("#bottombg3").height(min_height + 40); 
		} else { 
			$("#topbg").height(min_height); 
			$("#bottombg").height(min_height);
			$("#topbg2").height(min_height+35); 
			$("#topbg3").height(min_height+35); 
			$("#bottombg2").height(min_height+35); 
			$("#bottombg3").height(min_height+35); 
		} 
	} 
  
	cresize(); 
	setInterval(cresize, 3000); 
	$(window).resize( function(){ cresize(); } ); 

	var gadzopacity = 0.75

	$("#tapeta1").find('img').css("opacity",gadzopacity);
	$("#tapeta2").find('img').css("opacity",gadzopacity);
	$("#tapeta3").find('img').css("opacity",gadzopacity);

	$("#tapeta1").find('img').hover( function() {
	$(this).css("opacity",1);
	}, function() {
	$(this).css("opacity",gadzopacity);
	});

	$("#tapeta2").find('img').hover( function() {
	$(this).css("opacity",1);
	}, function() {
	$(this).css("opacity",gadzopacity);
	});

	$("#tapeta3").find('img').hover( function() {
	$(this).css("opacity",1);
	}, function() {
	$(this).css("opacity",gadzopacity);
	});

	$("#tapeta1").hover( function() {
	$("#tapeta1int").fadeIn(150);
	}, function() {
	$("#tapeta1int").fadeOut(150);
	});

	$("#tapeta2").hover( function() {
	$("#tapeta2int").fadeIn(150);
	}, function() {
	$("#tapeta2int").fadeOut(150);
	});

	$("#tapeta3").hover( function() {
	$("#tapeta3int").fadeIn(150);
	}, function() {
	$("#tapeta3int").fadeOut(150);
	});
	
	$(".ocenaback").hover( function() {
		$(this).css("cursor","pointer");
	}, function() {
		$(this).css("cursor","auto");
		$("#ocenafront").width(0);
	}).mousemove( function(e) {
		$off = $("#ocenafront");
		var rate = 1;
	    var left = e.clientX - $(this).offset().left;
		if (left >= 0 && left < 20) {
			$off.width(20);
			rate = 1;
		}
		if (left >= 20 && left < 40) {
			$off.width(40);
			rate = 2;
		}
		if (left >= 40 && left < 60) {
			$off.width(60);
			rate = 3;
		}
		if (left >= 60 && left < 80) {
			$off.width(80);
			rate = 4;
		}
		if (left >= 80 && left < 100) {
			$off.width(100);
			rate = 5;
		}
		$(this).attr("title","oceń grę na "+rate);
	}).click( function(e) {
		$off = $("#ocenafront");
	    var left = e.clientX - $(this).offset().left;
		var ocena = 0;
		var gra = $("#graid").val();
		if (left >= 0 && left < 20) ocena = 1;
		if (left >= 20 && left < 40) ocena = 2;
		if (left >= 40 && left < 60) ocena = 3;
		if (left >= 60 && left < 80) ocena = 4;
		if (left >= 80 && left < 100) ocena = 5;
		$("#ocenacontent").fadeOut(300, function() {
			$("#ocenacontent").load('content/ajax_graocena.asp?r='+ocena+'&g='+gra, function() {
				$("#ocenacontent").fadeIn(300);
			});
		});
	});
	/*
	$.fn.klubox.open({
		width: 709,
		height: 380,
		iframe: true,
		modalClose: "<a id='kluboxClose' style='display: none;' href='#' title='zamknij okno'></a>",
		href:"http://jojo.net.pl/lidl.html"
	});
	*/
});

function votegame(rate,game) {
	$("#ocenacontent").fadeOut(300, function() {
		$("#ocenacontentint").load("content/ajax_graocena.asp?r="+rate+"&g="+game, function() {
			$("#ocenacontent").fadeIn(300);
		});
	});
}

function msg_get_cat(catnum) {
	var znaki = $("#notags").val();
	var len = znaki.length;
	if (len < 500) {
		$("#msg-texts-int").load("content/ajax_messages.asp?cat="+catnum, function() {
			$("#msg-interior").css("display","none");
			$("#msg-texts").css("display","block");
		});
	} else {
		alert("Nie można dodać nowych słów do wiadomości, gdyż jest ona zbyt długa. Usuń część słów z wiadomości za pomocą przycisku COFNIJ.");
	}
}

function msg_texts_close() {
	$("#msg-texts").css("display","none");
	$("#msg-interior").css("display","block");
}

function update_msg() {
	var temp = '';
	$("#msgcontent").find('span').each( function() {
		temp += $(this).html()+' ';
 	});
	var znaki = temp.length*1;
	znaki = 500 - znaki;
	if (znaki <= 0) $("#znaki").addClass("red");
	else $("#znaki").removeClass("red");
	$("#znaki").html(znaki);
	$("#notags").val(temp);
}

function insert_msg(html) {
	html = '<span>'+html+'</span> ';
	$("#msgcontent").append(html);
	update_msg();
	msg_texts_close();
}

function del_last_msg() {
	$("#msgcontent span:last").remove();
	update_msg();
}

function msg_form_check() {
	var znaki = $("#notags").val();
	var len = znaki.length;
	if (len > 500) {
		alert("Twoja wiadomość jest zbyt długa! Limit to 500 znaków. Usuń część słów z wiadomości za pomocą przycisku COFNIJ.");
		return false;
	} else if (len == 0) {
		alert("Nie można wysłać pustej wiadomości!");
		return false;
	} else return true;
}

function get_newest(what) {
	if (what == 'people') {
		$("#fronttab1").css("background-position","0px 0px");
		$("#fronttab2").css("background-position","0px -24px");
		$("#nowe-avatary").html('<div class="center"><img id="loader" src="images/content/loader.gif" alt="" title="" /></div>');
		$("#nowe-avatary").load('/content/ajax_newest.asp', function() {
		$('#nowe-avatary').pngFix();
		});

	} else if (what == 'games') {
		$("#fronttab2").css("background-position","0px 0px");
		$("#fronttab1").css("background-position","0px -24px");
		$("#nowe-avatary").html('<div class="center"><img id="loader" src="images/content/loader.gif" alt="" title="" /></div>');
		$("#nowe-avatary").load('/content/ajax_3games.asp', function() {
		$('#nowe-avatary').pngFix();
		});
	}
}

function get_newest2(what) {
	if (what == 'people') {
		$("#tab2-1").css("background-position","0px -32px");
		$("#tab2-2").css("background-position","0px 0px");
		$("#tab2-3").css("background-position","0px 0px");
		$("#nowe-avatary2").html('<div class="center"><img id="loader2" src="images/content/loader2.gif" alt="" title="" /></div>');
		$("#nowe-avatary2").load('/content/ajax_newest.asp', function() {
			$('#nowe-avatary2').pngFix();
		});
	} else if (what == 'games') {
		$("#tab2-2").css("background-position","0px -32px");
		$("#tab2-1").css("background-position","0px 0px");
		$("#tab2-3").css("background-position","0px 0px");
		$("#nowe-avatary2").html('<div class="center"><img id="loader2" src="images/content/loader2.gif" alt="" title="" /></div>');
		$("#nowe-avatary2").load('/content/ajax_3games.asp', function() {});
	} else if (what == 'shop') {
		$("#tab2-3").css("background-position","0px -32px");
		$("#tab2-1").css("background-position","0px 0px");
		$("#tab2-2").css("background-position","0px 0px");
		$("#nowe-avatary2").html('<div class="center"><img id="loader2" src="images/content/loader2.gif" alt="" title="" /></div>');
		$("#nowe-avatary2").load('/content/ajax_shop.asp', function() {});
	}
}

function get_newest3(what) {
	if (what == 'people') {
		$("#front_newest_tab1").css("background-position","0px -8px");
		$("#front_newest_tab2").css("background-position","0px -40px");
		$("#front_newest_tab3").css("background-position","0px -40px");
		$("#front_newest_frame").html('<div class="center"><img id="loader2" src="images/content/loader2.gif" alt="" title="" /></div>');
		$("#front_newest_frame").load('/content/ajax_newest.asp', function() {
			$('#front_newest_frame').pngFix();
		});
	} else if (what == 'games') {
		$("#front_newest_tab2").css("background-position","0px -8px");
		$("#front_newest_tab1").css("background-position","0px -40px");
		$("#front_newest_tab3").css("background-position","0px -40px");
		$("#front_newest_frame").html('<div class="center"><img id="loader2" src="images/content/loader2.gif" alt="" title="" /></div>');
		$("#front_newest_frame").load('/content/ajax_3games.asp', function() {});
	} else if (what == 'shop') {
		$("#front_newest_tab3").css("background-position","0px -8px");
		$("#front_newest_tab1").css("background-position","0px -40px");
		$("#front_newest_tab2").css("background-position","0px -40px");
		$("#front_newest_frame").html('<div class="center"><img id="loader2" src="images/content/loader2.gif" alt="" title="" /></div>');
		$("#front_newest_frame").load('/content/ajax_shop.asp', function() {});
	}
}

function popmeup() {
	//window.open('http://jojo.net.pl/kaufland.html');
}

function login_submit() {
	$("#login_form").submit();
}
