var linkDescriptionTimer;
var orgLinkDesc = "";
var currProjectID = "";
var currPopupID = "";
var mouseX = 0;
var mouseY = 0;

String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.repeat = function(num) {
  return new Array(isNaN(num)? 1 : ++num).join(this);
}


$(document).ready(function() {
   $(document).mousemove(function(e){
      mouseX = e.pageX;
      mouseY = e.pageY;
   });

  //Insert functions to be run at pageload here
  centerSiteContent();
  centerLinkDescription();
  $(window).bind("resize", function() {
    centerSiteContent();
  })});




function centerSiteContent() {
  var viewWidth = $(window).width();
  var viewHeight = $(window).height();
  var siteWrapperWidth = $("#site_wrapper").outerWidth(true);
  var siteWrapperHeight = $("#site_wrapper").outerHeight(true);
  $("#site_wrapper").css({
      "position": "absolute",
      "top": viewHeight/2-siteWrapperHeight/2,
      "left": viewWidth/2-siteWrapperWidth/2
  });
}

function centerLinkDescription() {
  var siteWrapperWidth = $("#site_wrapper").outerWidth(true);
  var siteWrapperHeight = $("#site_wrapper").outerHeight(true);
  var descriptionWidth = $("#link_description").outerWidth(true);
  var descriptionHeight = $("#link_description").outerHeight(true);
  $("#link_description").css({
      "position": "absolute",
      "top": siteWrapperHeight/2-descriptionHeight/2,
      "left": siteWrapperWidth/2-descriptionWidth/2
  });
}

function showLinkDescription(linkDescription) {
  clearTimeout(linkDescriptionTimer);
  if (orgLinkDesc == "") {
    orgLinkDesc = $("#link_description").html();
  }
  $("#link_description").hide(0, function() {
    $("#link_description").html(linkDescription);
    centerLinkDescription();
    $("#link_description").show(0);
  });
}

function hideLinkDescription() {
  clearTimeout(linkDescriptionTimer);
  if (orgLinkDesc != "") {
    linkDescriptionTimer = setTimeout("showLinkDescription('"+orgLinkDesc+"');", 300);
  } else {
    linkDescriptionTimer = setTimeout('$("#link_description").hide(0);', 100);
  }
}

function showPopup(id) {
  if ( ($(window).height()-600)/2 > 40 ) {
    var startPoint = (1000-(menuItemCount*50))/2;
    for (x=0; x<menuItemCount; x++) {
      $('#circle_point_'+x).animate({
        top: '-40px',
        left: startPoint+(x*50),
        width: '50px',
        height: '50px'
      }, 500);
    }
  }

  if (currPopupID != "") {
    $("#content_"+currPopupID).fadeOut("fast", function() {
      $("#content_"+id).fadeIn("fast", function() {
        currPopupID = id;
      });
    });
  } else {
     $("#content_"+id).fadeIn("fast", function() {
      currPopupID = id;
    });
  }
}

function hidePopup(id) {
  $("#content_"+id).fadeOut("fast", function() {
    for (x=0; x<menuItemCount; x++) {
      $('#circle_point_'+x).animate({
        top: menuItemYpos[x],
        left: menuItemXpos[x],
        width: '128px',
        height: '128px'
      }, 1000);
    }
  });
}

function togglePopup(fromID, toID) {
  $("#content_"+fromID).fadeOut("fast", function() {
    $("#content_"+toID).fadeIn("fast");
  });
}

function showProjectDetails(id) {
  if (currProjectID.trim() != "") {
    $("#"+currProjectID+"_open").slideUp("fast", function() {
      $("#"+currProjectID+"_closed").slideDown("fast");
      currProjectID = "";
    })
  }

  if (id.trim() != "") {
    $("#"+id+"_closed").slideUp("fast", function() {
      $("#"+id+"_open").slideDown("fast");
      currProjectID = id;
      //location.href="#header_" + id;
    })
  }
}

function changePopupContent(id, url) {
  $("#content_"+id+" div.popup_content").load(url);
  return false;
}

function viewImage(id, path, maxWidth, maxHeight) {
  var viewWidth = $(window).width();
  var viewHeight = $(window).height();
  var thumbnailWidth = $("#"+id).innerWidth();
  var thumbnailHeight = $("#"+id).innerHeight();
  var previewWidth = 0;
  var previewHeight = 0;

  if ( (maxWidth/thumbnailWidth) < (maxHeight/thumbnailHeight) ) {
    previewWidth = Math.floor(thumbnailWidth*(maxWidth/thumbnailWidth));
    previewHeight = Math.floor(thumbnailHeight*(maxWidth/thumbnailWidth));
  } else {
    previewWidth = Math.floor(thumbnailWidth*(maxHeight/thumbnailHeight));
    previewHeight = Math.floor(thumbnailHeight*(maxHeight/thumbnailHeight));
  }

  $('#image_viewer').animate({
    top: mouseY+'px',
    left: mouseX+'px'
  }, 100, function() {
    $('#image_viewer').show(0, function() {
      $('#image_viewer').html('<img src="_resizejpg.php?Picture='+path+'&MaxX=800&MaxY=600" alt="Klik for at lukke" title="Klik for at lukke" id="image_prewview" class="image_prewview" onclick="closeImage();"/>');
      $('#image_viewer').animate({
        top: Math.floor( (viewHeight/2)-(previewHeight/2)-2 )+'px',
        left: Math.floor( (viewWidth/2)-(previewWidth/2)-2 )+'px',
        width: previewWidth+'px',
        height: previewHeight+'px',
        borderWidth: '2px'
      }, 250, function() {
        //$('#image_viewer').html('<img src="_resizejpg.php?Picture='+path+'&MaxX=800&MaxY=600" alt="Klik for at lukke" title="Klik for at lukke" id="image_prewview" class="image_prewview" onclick="closeImage();"/>');
      });
    });
  });
}

function closeImage() {
  $('#image_viewer').animate({
    top: mouseY+'px',
    left: mouseX+'px',
    width: '1px',
    height: '1px',
    borderWidth: '0px'
  }, 250, function() {
    $('#image_viewer').hide(0, function() {
      $('#image_viewer').html('');
    });
  });
}
