var refreshAction = function() {
  $("#creatorWin").remove();
  document.location = document.location;
};

$(document).ready(function(){
  // Warstwy
  $(".layer").openLayer(function(){
    $('a.user').openLayer(function(){
      $('a.creator').openCreator();
    });
  });
  // Kreator
  $(".creator").openCreator();
});

function lockScreen() {
  w = Math.min($('html').innerWidth(), $(document).innerWidth());
  h = Math.max($('html').innerHeight(), $(document).innerHeight());
  $('#lockScreen').show();
  $('#lockScreen').css({
    left: '0',
    top: '0',
    width: w + 'px',
    height: h + 'px'
  });
}

function unlockScreen() {
  $('#lockScreen').hide();
}

jQuery.fn.openLayer = function(callback) {
  $(this).bind("click", function(event){ 
    event.stopPropagation();
    l = event.pageX - 30;
    t = event.pageY + 10;
    $('#layerWin').remove();
    showIndicator(event);
    layerWin = $("<table id=\"layerWin\"><tbody><tr class=\"top\"><td class=\"lt\"></td><td class=\"t\"></td><td class=\"rt\"></td></tr><tr class=\"middle\"><td class=\"l\"></td><td class=\"m\"><div class=\"close\"></div><h1>"+$(this).attr('title')+"</h1></td><td class=\"r\"></td></tr><tr class=\"middle\"><td class=\"l\"></td><td class=\"m\"></div><div class=\"content\"></div></td><td class=\"r\"></td></tr><tr class=\"bottom\"><td class=\"lb\"></td><td class=\"b\"></td><td class=\"rb\"></td></tr></tbody></table>").appendTo('body');
    $('#layerWin div.content').load('pop/'+encodeURI($(this).attr('rel')), function(){
      lockScreen();
      layerWin.show('normal',function(){
        sw = $(window).width();
        sh = $(window).height();
        st = $(window).scrollTop();
        lw = $("#layerWin").width();
        lh = $("#layerWin").height();
        l = (sw-lw)/2;
        t = t-(lh/2);
        if (t<st) t = st + 10;
        if (t+lh>st+sh) t = st + sh - 10 - lh;
        if (t<20) t = 20;
        layerWin.css({
          left: l + 'px',
          top:  t + 'px'
        });
        $('div.close').click(function(){
          $('#layerWin').hide('fast', function(){
            $('#layerWin').remove();
            unlockScreen();
          });
        });
        hideIndicator();
        callback = callback || function(){};
        callback();
      });
    });
    return false;
  });
};

jQuery.fn.openImage = function() {
  $(this).bind("click", function(event){ 
    event.stopPropagation();
    l = event.pageX - 30;
    t = event.pageY + 10;
    $('#imgLayer').remove();
    showIndicator(event);
    imgLayer = $("<img id=\"imgLayer\" src=\""+$(this).attr('rel')+"\" />").appendTo('body');
    l = ($(window).width()-imgLayer.width())/2;
    imgLayer.css({
      left: l + 'px',
      top: t + 'px'
    });
    hideIndicator();
    imgLayer.click(function(){
      $(this).remove();
    });
    return false;
  });
};

jQuery.fn.openCreator = function() {
  $(this).bind("click", function(e){ 
    $("#layerWin").remove();
    if($(this).attr('question') && !confirm($(this).attr('question'))) return false;
    showIndicator(e);
    $('#creatorWin').hide();
    var lnk = $(this).attr('rel');
    if (lnk.indexOf("/")<0) lnk = '/creator/' + lnk;
    window.creatorFrame.document.location = encodeURI(lnk);
    checkCreator()
    return false;
  });
};

function checkCreator() {
  if (window.creatorFrame.creatorSignal==1) {
    lockScreen();
    $('#creatorWin').show();
    w = $('#creatorWin').width();
    h = $('#creatorWin').height();
    l = ($(window).width()-w)/2;
    t = (($(window).height()-h)/2)+$(window).scrollTop();
    $('#creatorWin').css({
      left: l + 'px',
      top: t + 'px'
    });
    window.creatorFrame.creatorSignal = 0;
    hideIndicator();
  }
  if (window.creatorFrame.creatorSignal==2) {
    window.creatorFrame.creatorSignal = 0;
    $('#creatorWin').hide();
    unlockScreen();
    return;
  }
  if (window.creatorFrame.creatorSignal==3) {
    window.creatorFrame.creatorSignal = 0;
    $('#creatorWin').hide();
    unlockScreen();
    refreshAction = refreshAction || function(){};
    refreshAction();
    return;
  }
  if (window.creatorFrame.creatorSignal==4) {
    $('#creatorWin').hide();
    unlockScreen();
    document.location = 'profile.php';
    return;
  }
  if (window.creatorFrame.creatorSignal==5) {
    window.creatorFrame.creatorSignal = 0;
    $('#creatorWin').hide();
    unlockScreen();
    refreshAction = refreshAction || function(){};
    refreshAction('del');
    return;
  }
  setTimeout(checkCreator, 100);
}

/* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com) and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Version: 1.0a
 */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(8(g){g.W.J({1j:g.W.t,18:g.W.C,t:8(a){4(1[0]==9)5 L.1w||g.v&&6.T.1m||6.j.1m;4(1[0]==6)5 1i.1d(6.j.1J,6.j.V);5 1.1j(a)},C:8(a){4(1[0]==9)5 L.19||g.v&&6.T.1o||6.j.1o;4(1[0]==6)5 1i.1d(6.j.1y,6.j.U);5 1.18(a)},1w:8(){5 1[0]==9||1[0]==6?1.t():1.K(\':D\')?1[0].V-h(1,\'n\')-h(1,\'1u\'):1.t()+h(1,\'15\')+h(1,\'1s\')},19:8(){5 1[0]==9||1[0]==6?1.C():1.K(\':D\')?1[0].U-h(1,\'m\')-h(1,\'1p\'):1.C()+h(1,\'10\')+h(1,\'1n\')},1M:8(){5 1[0]==9||1[0]==6?1.t():1.K(\':D\')?1[0].V:1.t()+h(1,\'n\')+h(1,\'1u\')+h(1,\'15\')+h(1,\'1s\')},1L:8(){5 1[0]==9||1[0]==6?1.C():1.K(\':D\')?1[0].U:1.C()+h(1,\'m\')+h(1,\'1p\')+h(1,\'10\')+h(1,\'1n\')},k:8(a){4(a!=1h)5 1.1g(8(){4(1==9||1==6)9.1f(a,g(9).l());q 1.k=a});4(1[0]==9||1[0]==6)5 L.1H||g.v&&6.T.k||6.j.k;5 1[0].k},l:8(a){4(a!=1h)5 1.1g(8(){4(1==9||1==6)9.1f(g(9).k(),a);q 1.l=a});4(1[0]==9||1[0]==6)5 L.1G||g.v&&6.T.l||6.j.l;5 1[0].l},1E:8(c,a){u x=0,y=0,z=0,s=0,7=1[0],3=1[0],r,P,H=g.F(7,\'1x\'),B=g.o.1O,A=g.o.1N,1t=g.o.14,1q=g.o.Q,13=I,12=I,c=g.J({11:p,N:p,Y:I,w:p,1l:I},c||{});4(c.1l)5 1.1k(c,a);4(7.E.G()==\'j\'){x=7.X;y=7.Z;4(B){x+=h(7,\'O\')+(h(7,\'m\')*2);y+=h(7,\'S\')+(h(7,\'n\')*2)}q 4(1q){x+=h(7,\'O\');y+=h(7,\'S\')}q 4(A&&1r.v){x+=h(7,\'m\');y+=h(7,\'n\')}}q{R{P=g.F(3,\'1x\');x+=3.X;y+=3.Z;4(B||A){x+=h(3,\'m\');y+=h(3,\'n\');4(B&&P==\'1e\')13=p;4(A&&P==\'1K\')12=p}r=3.1v;R{4(c.w){z+=3.k;s+=3.l}4(B&&3!=7&&g.F(3,\'1I\')!=\'D\'){x+=h(3,\'m\');y+=h(3,\'n\')}3=3.1c}M(3!=r);3=r;4(3.E.G()==\'j\'||3.E.G()==\'1b\'){4((1t||(A&&g.v))&&H!=\'1e\'&&H!=\'1a\'){x+=h(3,\'O\');y+=h(3,\'S\')}4((B&&!13&&H!=\'1a\')||(A&&H==\'1F\'&&!12)){x+=h(3,\'m\');y+=h(3,\'n\')}1D}}M(3)}u b=i(7,c,x,y,z,s);4(a){g.J(a,b);5 1}q{5 b}},1k:8(c,a){u x=0,y=0,z=0,s=0,3=1[0],r,c=g.J({11:p,N:p,Y:I,w:p},c||{});R{x+=3.X;y+=3.Z;r=3.1v;4(c.w){R{z+=3.k;s+=3.l;3=3.1c}M(3!=r)}3=r}M(3&&3.E.G()!=\'j\'&&3.E.G()!=\'1b\');u b=i(1[0],c,x,y,z,s);4(a){g.J(a,b);5 1}q{5 b}}});u h=8(b,a){5 1C(g.F(b.1B?b[0]:b,a))||0};u i=8(c,d,f,a,b,e){4(!d.11){f-=h(c,\'O\');a-=h(c,\'S\')}4(d.N&&(g.o.14||g.o.Q)){f+=h(c,\'m\');a+=h(c,\'n\')}q 4(!d.N&&!(g.o.14||g.o.Q)){f-=h(c,\'m\');a-=h(c,\'n\')}4(d.Y){f+=h(c,\'10\');a+=h(c,\'15\')}4(g.o.Q&&d.w&&g.F(c,\'1A\')==\'1z\'){b-=c.k;e-=c.l}5 d.w?{17:a-e,16:f-b,l:e,k:b}:{17:a,16:f}}})(1r);',62,113,'|this||parent|if|return|document|elem|function|window||||||||||body|scrollLeft|scrollTop|borderLeftWidth|borderTopWidth|browser|true|else|op|st|height|var|boxModel|scroll|||sl|ie|mo|width|visible|tagName|css|toLowerCase|elemPos|false|extend|is|self|while|border|marginLeft|parPos|opera|do|marginTop|documentElement|offsetWidth|offsetHeight|fn|offsetLeft|padding|offsetTop|paddingLeft|margin|relparent|absparent|safari|paddingTop|left|top|_width|innerWidth|fixed|html|parentNode|max|absolute|scrollTo|each|undefined|Math|_height|offsetLite|lite|clientHeight|paddingRight|clientWidth|borderRightWidth|oa|jQuery|paddingBottom|sf|borderBottomWidth|offsetParent|innerHeight|position|scrollWidth|inline|display|jquery|parseInt|break|offset|static|pageYOffset|pageXOffset|overflow|scrollHeight|relative|outerWidth|outerHeight|msie|mozilla'.split('|'),0,{}))