function IsIe(ver)
{
	temp=navigator.appVersion.split('MSIE');
	ieVer=parseInt(temp[1]);
	
	if(ver)
	{
		if(ieVer && ieVer==ver)
			return true;
		else
			return false;
	}
	
	if(ieVer)
		return true;
	else
		return false;
}


var Profil = {
	wczytajFoto: function(fotoToRead, login, typ)
	{

		//$('#duzefoto').fadeTo("slow", 0.00);
		//var pos = $('#duzefoto').position()
		Profil.zmienAktywny(fotoToRead, typ);
		/*if(!document.getElementById("loadLayer"))
		{		
			var load = document.createElement("div");
			load.innerHTML = '<img src="images/profil_loader.gif" border="0">';
			load.style.position = "relative";
			load.style.left = pos.left-146+"px";
			load.style.top = pos.top-470+"px";
			load.id = "loadLayer";
			document.getElementById('duzefoto').parentNode.appendChild(load);
			$(load).fadeTo("fast", 1);
		}
		else
		{
			$(load).fadeTo("slow", 1);
		}*/
		//setTimeout(function ()
		//{
			var re = / /g;
			$('#duzefoto').css("background-image", 'url(katalogi/'+login.replace(re, '%20')+'/srednie/'+fotoToRead.replace(re, '%20')+')');
			$('#linkBig').attr("href", 'katalogi/'+login.replace(re, '%20')+'/standard/'+fotoToRead);
			//setTimeout(function()
			//{
				//$('#duzefoto').fadeTo("slow", 1.00);
				//$(load).fadeTo("slow", 0);
				//setTimeout(function(){load.parentNode.removeChild(load)},1000);
			//}, 500);
		//}, 500);
		
		//$(".duzefoto").corner();
	},
	zmienAktywny: function(fotoToRead, typ)
	{
		var minis = $(".foto");
		for(var i = 0; i < minis.length; i++)
		{
			var p = $(minis[i]).parents();
			if(p[0].className = typ+'_foto_aktywny')
			{
				p[0].className = 'foto_item';
			}
			var nazwafo = this.wezNazweFota($(minis[i]).css('background-image'));
			
			var re = / /g;
			if(nazwafo==fotoToRead.replace(re, '%20'))
			{
				var parents = $(minis[i]).parents();
				parents[0].className = typ+'_foto_aktywny';
				var pos = $(parents[0]).position();
				var poscont = $('#Profil_belt').position();
				if(pos.top!=(poscont.top*-1))
				{
					$('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ+'duzefotoLeftUpnoaktiv.gif)');
				}
				else
				{
					$('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ+'_duzeFotoLeftUp_corner.gif)');
				}
			}
		}
	},
	wezNazweFota: function(bgImage)
	{
		var arr = bgImage.split("/");
		var nazwa = arr[arr.length-1].split(")");
		if(nazwa[0].indexOf('"')!=-1 && (nazwa[0].length-1)==nazwa[0].indexOf('"'))
			nazwa[0] = nazwa[0].substring(0, nazwa[0].length-1);
		
		return nazwa[0];
	},
	przesunWGore: function()
	{
		var anim = $("div:animated");
		for(var i = 0; i <anim.length; i++)
			if(anim[i].className.match("Profil_belt"))
				return;
		
					
		var pos = $('#Profil_belt').position();
		var height = pos.top+$('#Profil_belt').height();
		var wyscont = $('#Profil_belt').height()
		var poscont = $('#Profile_hide').position();
		
		//alert(poscont.top);
		if(pos.top+$('#Profil_belt').height()<poscont.top+392)
			return;
			
		if(wyscont<384)
			return;
				
		if(height>350 && height<400)
			return;
		
		var pozostalosc = wyscont-((pos.top*-1)+384);
		var selected = this.wezZaznaczony();
		var pos_selected = $(selected).position();
		var typ = selected.className.split('_');
		
		if(pos_selected.top==(pos.top*-1))
			setTimeout(function(){ $('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ[0]+'duzefotoLeftUpnoaktiv.gif)'); }, 300);
		
		if(pozostalosc<$('#Profile_hide').height())
		{
			pozostalosc = pozostalosc;
		}
		else
		{
			pozostalosc = 4*96;
		}
		
		$('#Profil_belt').animate({
				top: pos.top-pozostalosc+"px"
		}, 1000, "swing", function()
		{
			var pos = $('#Profil_belt').position();
			var pos_selected = $(selected).position();
			if(pos_selected.top==(pos.top*-1))
				$('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ[0]+'_duzeFotoLeftUp_corner.gif)');
			Profil.ustawNaroznik(Profil.wezZaznaczony(), typ[0]);
		});
	},
	przesunWDol: function()
	{
		var pos = $('#Profil_belt').position();
		
		var anim = $("div:animated");
		for(var i = 0; i <anim.length; i++)
			if(anim[i].className.match("Profil_belt"))
				return;
		
		var wyscont = $('#Profil_belt').height();
				
		if(wyscont<380)
			return;
			
		if(pos.top==0)
			return;
		
		var pozostalosc = pos.top*-1;
		var selected = this.wezZaznaczony();
		var pos_selected = $(selected).position();
		var typ = selected.className.split('_');
		
		if(pos_selected.top==(pos.top*-1))
			setTimeout(function(){ $('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ[0]+'duzefotoLeftUpnoaktiv.gif)'); }, 300);
			
		if(pozostalosc<$('#Profile_hide').height())
		{
			pozostalosc = pozostalosc;
		}
		else
		{
			pozostalosc = 4*96;
		}
		
		$('#Profil_belt').animate({
				top: pos.top+pozostalosc+"px"
		}, 1000, "swing", function()
		{
			var pos = $('#Profil_belt').position();
			var pos_selected = $(selected).position();
			if(pos_selected.top==(pos.top*-1))
				$('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ[0]+'_duzeFotoLeftUp_corner.gif)');
			Profil.ustawNaroznik(Profil.wezZaznaczony(), typ[0]);
		});
	},
	wezZaznaczony: function()
	{
		var minis = $(".foto");
		for(var i = 0; i < minis.length; i++)
		{
			var parents = $(minis[i]).parents();
			if(parents[0].className.match('_foto_aktywny'))
			{
				return parents[0];
			}
		}
	},
	poprzednieFoto: function(login)
	{
		var selected = this.wezZaznaczony();
		var minis = $(".foto");
		for(var i = 0; i < minis.length; i++)
		{
			var parents = $(minis[i]).parents();
			if(selected == parents[0])
			{
				var pop = $(minis[i-1]).parents()
				if(pop[0])
				{
					var poppos = $(pop[0]).position();
					var poscont = $('#Profil_belt').position();
					var conti = $('#Profile_hide').height();
					//alert(poppos.top)
					if(poppos.top<(poscont.top*-1))
						this.przesunWDol();
						
					var nazwaFoto = $(minis[i-1]).css("background-image").split('/');
					var typ = selected.className.split('_');
					nazwaFoto = nazwaFoto[nazwaFoto.length-1].substring(0, nazwaFoto[nazwaFoto.length-1].length-1);
					nazwaFoto = nazwaFoto.replace('"', '');
					this.wczytajFoto(nazwaFoto, login, typ[0]);
					break;
				}
			}
		}
	},
	nastepneFoto: function(login)
	{
		var selected = this.wezZaznaczony();
		var minis = $(".foto");
		for(var i = 0; i < minis.length; i++)
		{
			var parents = $(minis[i]).parents();
			if(selected == parents[0])
			{
				var pop = $(minis[i+1]).parents()
				if(pop[0])
				{
					var poppos = $(pop[0]).position();
					var poscont = $('#Profil_belt').position();
					var conti = $('#Profile_hide').height();
					//alert(poppos.top)
					if((poscont.top*-1)+conti<=poppos.top)
						this.przesunWGore();
						
					var nazwaFoto = $(minis[i+1]).css("background-image").split('/');
					
					var typ = selected.className.split('_');
					nazwaFoto = nazwaFoto[nazwaFoto.length-1].substring(0, nazwaFoto[nazwaFoto.length-1].length-1);
					nazwaFoto = nazwaFoto.replace('"', '');
					
					this.wczytajFoto(nazwaFoto, login, typ[0]);
					break;
				}
			}
		}
	},
	ustawNaroznik: function(selected, typ)
	{
		var pos = $(selected).position();
		var poscont = $('#Profil_belt').position();
		if(pos.top!=(poscont.top*-1))
		{
			$('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ+'duzefotoLeftUpnoaktiv.gif)');
		}
		else
		{
			$('#duzeFotoLeftUpCorner').css('background-image', 'url(images/'+typ+'_duzeFotoLeftUp_corner.gif)');
		}	
	}
}


function ustawMenu(tab)
{
/*	var szerokosci = new Object();
	szerokosci['panowiemenu'] = 100;
	szerokosci['klubymenu'] = 50;
	szerokosci['przydrodzemenu'] = 110;
	szerokosci['sexshopmenu'] = 87;
	szerokosci['forummenu'] = 60;
	szerokosci['rozrywkamenu'] = 120;
	szerokosci['ciekawostkimenu'] = 146;
	
	
	var items = $('.dzial_naglowek_item')
	if(items.length==8)
		return;
	
	var braki = tab.split(';');
	
	var przesuniecie = 0;
	for(var i = 0; i < braki.length-1; i++)
		przesuniecie += szerokosci[braki[i]];
	

		
	//if(tab[2]=='rozrywka' && tab[1]=='sexshopmenu')
		//przesuniecie += szerokosci[tab[2]];	
	for(var i = 0; i < items.length; i++)
	{
		if(items[i].id=='przydrodzemenu'){
			
			var prev = $(items[i]).prevAll()
		}
	}
	if(!prev)
		prev = $('#klubymenu').prevAll()
	for(var i = 0; i < prev.length; i++)
	{
		var pos = $(prev[i]).position();
		$(prev[i]).css("left", pos.left-przesuniecie+"px");
	}*/
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function PokazStroneKontrolna(tresc)
{
	$("#divZaslona").css("width", ($(window).width())+"px");
	$("#divZaslona").css("height", $(document).height()+"px");
	
	
	$("#divId").addClass("stronaKontrolna");
	
	$("#divZaslona").css("position", "absolute");
	
	if(IsIe(6))
		$("#divId").css("position", "absolute");
	else
		$("#divId").css("position", "fixed");
	
	$("#divZaslona").css("z-index", "200");
	$("#divId").css("z-index", "250");
	$('.main').css("z-index", "-1");	
}

function usunKontrolna()
{
	$('#divZaslona').remove();
	$('#divId').remove();
	$('.main').css("z-index", "20");
	jQuery.get('wchodze.php')
}

function addToBox(idUser, idProfil)
{
	var div = document.createElement('div');
	
	var pos = $('#dodaj_do_galerii').offset();
	
	$(div).css("position", "absolute");
	$(div).css("top", pos.top-2+"px");
	$(div).css("left", pos.left-20+"px");
	$(div).html('<img src="images/smallload.gif" border="0">');
	
	document.body.appendChild(div);
	
	jQuery.get('dodajDoBoxa.php?idUser='+idUser+'&idProfil='+idProfil, function(data)
	{
		setTimeout(function() { $(div).remove() }, 1500)		
	});
}

var Tooltip = {

	
	AddRollOut:function(ElementID, CSSTooltipClass)
	{
		$('#'+ElementID).mouseout(function(){
			$('#TooltipLayer'+ElementID).remove();
		})
	},
	
	ShowTooltip:function(ElementID, CSSTooltipClass, DataToShow, x, y)
	{
		Tooltip.AddRollOut(ElementID, CSSTooltipClass);
		var element = Tooltip.CreateTooltip(ElementID, CSSTooltipClass, x ,y);
		$(element).html(DataToShow);
		$(element).fadeIn("fast");
	},
	
	CreateTooltip:function(ElementID, CSSTooltipClass, x, y)
	{
		var div = document.createElement("div");
		
		$(div).css("position", "absolute");
		$(div).css("display", "none");
		$(div).attr("id", "TooltipLayer"+ElementID);
		
		$(div).addClass(CSSTooltipClass);
		
		var pos = $('#'+ElementID).offset();
		
		$(div).css("left", pos.left+$('#'+ElementID).width()+5+x);
		$(div).css("top", pos.top+y);
		
		document.body.appendChild(div);
		return div;
	}
}

var preloadedimages=new Array();
var timeouts=new Array();
var infotimeouts=new Array();
var NowId;


function isloaded(img) {
  if (!img.complete)
    return false;
  if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
    return false;
  return true;
}


function loadimage(path, id, num){
  if (typeof(preloadedimages[id][num])!='undefined')
    return;
  preloadedimages[id][num]=new Image();
  preloadedimages[id][num].src=MainFotki[id][num];
}

function ShowInfo(id)
{
	var info = document.createElement("div");
	var pos = $('#'+id).offset();
	
	//$(info).addClass("ProfileObrys");
	info.id = id+"_info";
	
	$(info).css("position", "absolute");
	$(info).css("top", pos.top-1);
	$(info).css("left", pos.left+109);
	$(info).css("width", "45px");
	$(info).css("height", "40px");
	$(info).css("z-index", "300"); 
	
	$(info).html('<a onclick="ShowDaneProfil('+id+')"><img src="images/InfoLayer.gif" border="0" id="InfoImage"></a>');
	document.body.appendChild(info);
}

function ShowDaneProfil(id)
{
	stopPreload(id);
	jQuery.get('wezInfoProfil.php?iid='+id, function(data){
		var dane = document.createElement("div");
		var pos = $("#"+id+"_info").offset();
		
		dane.id = id+"_DaneProfil";
	
		$(dane).addClass("InfoDaneProfil");
		$(dane).css("top", pos.top+21);
		$(dane).css("left", pos.left+14);
		$(dane).css("z-index", "300");
		
		$(dane).html(data);
		$(dane).css("opacity", "0.9");
		
		document.body.appendChild(dane);
	});
}

function startPreview(id)
{
  //SetObrys(id, function(){ 
 
  	if(document.getElementById(id+"_obrys").style.backgroundImage=='url(images/ProfilObrys.gif)')
  		return;
  		
  	document.getElementById(id+"_obrys").style.backgroundImage = 'url(images/ProfilObrys.gif)';
  	
  	path='';
	num=MainFotki[id].length;
  	if (typeof(preloadedimages[id])=='undefined')
  	{
	    preloadedimages[id]=new Array();
  	}
  
  	loadimage(path, id, 1);
  	timeouts[id]=setTimeout("showimage('"+path+"', "+id+", "+num+", 1)", 500);
  	infotimeouts[id] = setTimeout("ShowInfo("+id+")", 300);
  	loadimage(path, id, 0);
  //});
}

function showimage(path, id, cnt, num){
  if (!isloaded(preloadedimages[id][num])){
    timeouts[id]=setTimeout("showimage('"+path+"', "+id+", "+cnt+", "+num+")", 20);
    return;
  }
  $("#"+id).css("background-image", "url("+preloadedimages[id][num].src+")");
 
  if (++num==cnt)
    num=0;
  loadimage(path, id, num);
  timeouts[id]=setTimeout("showimage('"+path+"', "+id+", "+cnt+", "+num+")", 500);
}

function checkPreview(id)
{
	if(!document.getElementById(id+"_obryss"))
	{
		stopPreview(id) 
	}
}


function getMousePosition(e)
{
	return e.pageX ? {'x':e.pageX, 'y':e.pageY} : {'x':e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft, 'y':e.clientY + document.documentElement.scrollTop + document.body.scrollTop};
} 


var prev_x = 0;
var prev_y = 0;



function stopPreview(id)
{
	//RealStop(id);	
	var status = '';
	
	if (window.addEventListener)
    	window.addEventListener('DOMMouseScroll', function (){RealStop(id)}, false);
	window.onmousewheel = document.onmousewheel = function (){RealStop(id)};
	
	document.onmousemove = function (e)
	{
		e = e || window.event;
		target = e.target || e.srcElement;
		
		
		if(target.id==id+"_obrys" || target.id=="InfoImage" || target.id==id+"_info" || target.id==id+"_DaneProfil" || target.id=="DaneTable")
		{
			return;
		}
		else
		{
			RealStop(id);
		}
					
	}
	
	
	
	
	/* to jest wersja wedlug wskaznika myszki **************
  var pos = $("#"+id).offset();
  	
  if (window.addEventListener)
        	window.addEventListener('DOMMouseScroll', function (){RealStop(id)}, false);
	window.onmousewheel = document.onmousewheel = function (){RealStop(id)};
	
  document.onmousemove = function (e)
  {
  	e = e || window.event;
  	
  	var mp = getMousePosition(e);
   	  		  	
  	if(pos.left+140<mp.x || pos.left>mp.x || pos.top>mp.y || pos.top+149<mp.y)
  		RealStop(id);
  }*/
  
}

function RealStop(id)
{
  clearTimeout(timeouts[id]);
  clearTimeout(infotimeouts[id]);
  
  document.getElementById(id).style.backgroundImage = 'url('+MainFotki[id][0]+')';
  if(document.getElementById(id+"_info"))
  	document.body.removeChild(document.getElementById(id+"_info"));
  if(document.getElementById(id+"_DaneProfil"))
  	document.body.removeChild(document.getElementById(id+"_DaneProfil"));
  	
  document.getElementById(id+"_obrys").style.backgroundImage = 'none';
}

function stopPreload(id)
{
	clearTimeout(timeouts[id]);
	document.getElementById(id).style.backgroundImage = 'url('+MainFotki[id][0]+')';
}


function CreateCurtain(CSSClass, RemoveFunction)
{
	var divZaslona = document.createElement('div');
	
	$(divZaslona).addClass(CSSClass);
	$(divZaslona).html('<table width="100%" height="100%"></table>')
	divZaslona.id = 'divZaslona';
	
	$(divZaslona).css("width", ($(window).width())+"px");
	$(divZaslona).css("height", $(document).height()+"px");
	$(divZaslona).css("opacity", "0.8");
	//$(divZaslona).css("z-index", "200");
	
	$(divZaslona).click(function(){
		RemoveFunction();
	});
	
	document.body.appendChild(divZaslona);
}

function PrzypomnijHaslo(login){
	if(!login)
		return alert('Podaj swój login!');
	
	var prev = $('.przypomnij').html();
	$('.przypomnij').html('<img src="images/smallload.gif" border="0">');
	$('.przypomnij').css("text-align", "center");
	$('.przypomnij').css("padding-top", "20px");
	
	jQuery.post('_ax_PrzypominHaslo.php', {log:login}, function(data)
	{
		if(data){
			$('.przypomnij').html('Podany przez Ciebie login jest nie prawidłowy.');
			setTimeout(function(){ $('.przypomnij').css("padding-top", "0px");$('.przypomnij').css("text-align", "left"); $('.przypomnij').html(prev); }, 2000);
		} 
		else {
			$('.przypomnij').html('Twoje hasło zostało wysłane na podany przez Ciebie e-mail podczas rejestracji konta.')	
		}
	});
}

function GotoReklama(idRek, href, target){
	jQuery.get('_ax_AddKlikniecieReklama.php?idRek='+idRek);
	window.open(href, target);
}