$(document).ready
(
  function()
  {
    var flag = 0;
    var direct = -1;
		$(window).bind
		("resize", 
      function()
      {
        $("#iBigBackground").css("height", $(window).height());
        $("#iBigBackground").css("width", $(window).width());
      }
		);
		$("#iBig").live
		( "click",
      function()
      {
        if(flag == 1)
        {
            if(direct == 0)
            {
              $("#iBig").animate({left: "1500"}, "slow");
            }
            if(direct == 1)
            {
              $("#iBig").animate({top: "1500"}, "slow");
            }
            if(direct == 2)
            {
              $("#iBig").animate({left: "-1500"}, "slow");
            }
            if(direct == 3)
            {
              $("#iBig").animate({top: "0"}, "slow");
            }
            $("#iBigBackground").animate({opacity: "hide"}, "slow", iRemove);
            flag = 0;
        }
      }
		);
		$("#iBigBackground").live
		( "click",
      function()
      {
        if(flag == 1)
        {
            if(direct == 0)
            {
              $("#iBig").animate({left: "1500"}, "slow");
            }
            if(direct == 1)
            {
              $("#iBig").animate({top: "1500"}, "slow");
            }
            if(direct == 2)
            {
              $("#iBig").animate({left: "-1500"}, "slow");
            }
            if(direct == 3)
            {
              $("#iBig").animate({top: "0"}, "slow");
            }
            $("#iBigBackground").animate({opacity: "hide"}, "slow", iRemove);
            flag = 0;
        }
      }
		);

    $("a.iOpen").click
    (
      function()
      {
        if($(this).attr("type") == "iOpen")
        {
          iRemove2();
          var iLink = $(this).children("img.iSmall").attr("src");
          if(flag == 0)
          {
            $("body").append("<div id='iBigBackground'></div><img id='iBig'>");
            $("#iBigBackground").css("height", $(document).height());
            $("#iBigBackground").css("width", $(document).width());
            $("#iBigBackground").css("min-height", $(document).height());
            $("#iBigBackground").css("min-width", $(document).width());
            $("#iBigBackground").css("top", "0");
            $("#iBigBackground").css("left", "0");
            $("#iBig").attr("src", iLink);        
            direct = rNum(0,4);
            if(direct == 0)
            {
              var h = $(window).scrollTop() + (	$(window).height() - $("#iBig").height())/4;
              $("#iBig").css("left", "0").css("top", h);
            }
            if(direct == 1)
            {
              var w = (	$(window).width() - $("#iBig").width())/2;
              $("#iBig").css("top", "0").css("left", w);
            }
            if(direct == 2)
            {
              var h = $(window).scrollTop() + (	$(window).height() - $("#iBig").height())/4;
              $("#iBig").css("left", "1500").css("top", h);
            }
            if(direct == 3)
            {
              var w = (	$(window).width() - $("#iBig").width())/2;
              $("#iBig").css("top", $(document).height()+1000).css("left", w);
            }
            $("#iBigBackground").fadeTo(600, 0.7, iPic);
            flag = 1;
          }
        }
        return false;
      }    
    );
    function iPic()
    {
      var h = $(window).scrollTop() + (	$(window).height() - $("#iBig").height())/4;
      var w = (	$(window).width() - $("#iBig").width())/2;
      $("#iBig").animate({opacity: "show", top: h, left: w}, "slow");  
    }
    function iRemove()
    { 
      $("#iBigBackground").remove();
      $("#iBig").remove();
    }
    function iRemove2()
    {
      $("#iDesc").hide();
      $("#iDesc").remove();
    }
    function rNum(min, max)
    {
      return Math.floor(Math.random()*(max - min) + min);
    } 
  }
)

