
window.resize=onResize;
function onResize(){
    var b=document.getElementById("body"); 
    var h=b.clientHeight;
    var w=b.clientWidth;
    $("#mask").css({width:w,height:h});
}
function log(o){
    try {
        console.log(o);
    }catch(e){
    }
}

function rotateText(){
    var m=$("#message").html();
    var msg='"Non nascondermi il tuo volto" <span>Sal. 26,9</span>';
    //alert(m.indexOf("Non"));
    if (m.indexOf("Non")>=0){
        msg='"Don\'t hide your face from me" <span>Psalm 26,9</span>';
    }
    $("#message").hide().html(msg).fadeIn("slow");
    setTimeout("rotateText()",10000);
}
var mdown=false;
var mX=0;
var mY=0;
$(window).load(function() {
    setTimeout("rotateText()",10000);
    var b=document.getElementById("body"); 
    var h=b.clientHeight;
    var w=b.clientWidth;
    //$("#bar").show();
    $("#message").show();
    $("#mask").css({width:w,height:h }); //,'opacity':0.9

    $("#mask").mouseup(function(e){
      mdown=false;
      mX=0;
      mY=0;
    })
    $("#mask").mousedown(function(e){
      mdown=true;
      mX=e.clientX;
      mY=e.clientY;
      //log(mX+" "+mY);
      //$("#miniframe").hide();
    });


    $("#aclose").click(function (e){
        $('#frame').fadeOut('slow');
        e.stopPropagation();
        return false;
    }); 
    
    $("#frame").click(function (e){
        e.stopPropagation();
        return false;
    }); 

    $("#photo").mousemove(function(e){
        e.stopPropagation();
    });

    $("#body").mousemove(function(e){
        var b=document.getElementById("body"); 
        var h=b.clientHeight;
        var w=b.clientWidth;
        $("#mask").css({width:w,height:h});
    });
    
    $("#mask").mousemove(function(e){
      $target=$(e.target);
      var x=e.clientX;
      var y=e.clientY;
      var i=Math.floor(x/50)*50;
      var j=Math.floor(y/50)*50;
      
      var coo=  $("#body").css('backgroundPosition');
      if (coo==undefined)
        coo='0 0';
      coo=coo.replace(/px/g,"").replace(/pt/g,"").split(" ");

      if (mdown){
          oX=e.clientX-mX;
          oY=e.clientY-mY;
          //log(oX+" "+oY);
      }

      if (false &&  mdown){
          //log(mX+" "+mY);
          var ox=coo[0]+(e.clientX-mX);
          var oy=coo[1]+(e.clientY-mY);
          var bp=""+ox+" " +oy+"";
          //console.log(bp);
          $("#body").css({
            backgroundPosition:bp
          });
          mX=e.clientX;
          mY=e.clientY;
          return;
      }
      var side=50;
      if (!jQuery.support.boxModel){
        side=54;
      }
      $("#miniframe").css({
        left:i-2,top:j-2,
        width:side,height:side
      });

      if ($("#frame").is(":hidden")) {
        $("#miniframe").show();
      } 
    });
    $("#mask").click(function(e){
      $("#miniframe").hide();
      
      if (!$("#frame").is(":hidden")) {
          $("#frame").fadeOut();
          $("#miniframe").fadeIn();
          return false;
      } 
      $("#frame").hide();
      
      $("#iphoto").attr("src","empty.gif");
      $("#frame").css({
        left:e.clientX-100,
        top:e.clientY-125
      });
      
      var coo=  $("#body").css('backgroundPosition');
      //alert(coo);
      if (coo==undefined)
        coo='0 0';
      coo=coo.replace(/px/g,"").replace(/pt/g,"").split(" ");
      
      var b=document.getElementById("body"); 
      var h=b.clientHeight;
      var w=b.clientWidth;
     
      //log(coo);
      var ox=(e.clientX-coo[0]);
      var oy=(e.clientY-coo[1]);

      var x=(e.clientX-coo[0]+1200000)%1200;
      var y=(e.clientY-coo[1]+1000000)%1000;
      var xx=Math.floor(x/50)*50;
      var yy=Math.floor(y/50)*50;
      
      var i=Math.floor(x/200)%6;
      var j=Math.floor(y/250)%4;
      var i1=Math.floor((x%200)/50);
      var j1=Math.floor((y%250)/50);
      var p=((i*4)+j)*20+((i1*5)+j1);
      //var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
      //var clientCoords = "( " + i + ", " +j + ","+i1+","+j1 + " )";
      var l=data.length;
      //console.log(clientCoords+"="+p+"/"+l);
      var d=data[p%120];
      if (d==undefined) {
          log("!");
          window.location="wp-signup.php";
          return;
      }
      var img=d[1].replace("thumb","full");
      var ox=Math.floor((w/2-ox)/50)*50; 
      var oy=Math.floor((100-oy)/50)*50; 
      var bp=""+ox+" " +oy+"";
      //console.log(bp);
      $("#body").animate({
        backgroundPosition:bp
        //'background-color':'#ffffff'
        //'background-image':"url("+img+")"
      },1000,"linear",function(){
      });
      $("#frame").show();
      //$("#iphoto").attr("src",img);
      var id="id="+d[0];
      $.get("./mosaic.user.php",id,function(response){
           var h="<a href='#' id='userlink' onclick=window.location='http://staging.josp.tbtv.net/members/";
           h=h+response;
           h=h+"'>";
           a=h+"<img id='iphoto' src='"+img+"'/></a>";
           b=h+"<div id='info'>"+response+"</div></a>";
           $('#photo').html(a+b);
           //log(response);
           //$('#info').html(a);
      });
      
      $("#iphoto").show();
      $("#photo").fadeIn("slow").slideDown();
      var xx=Math.floor((w/2)/50)*50;
      var yy=Math.floor(100/50)*50;
      $("#frame").animate({
        left:xx-50,
        top:yy-50,
        width:200,
        height:250
      },1000);

      //console.log(data[p][0]);
      //console.log(img);
    });
});
