function myPopupRelocate() {
  var scrolledX, scrolledY;
  if( self.pageYOffset ) {
    scrolledX = self.pageXOffset;
    scrolledY = self.pageYOffset;
  } else if( document.documentElement && document.documentElement.scrollTop ) {
    scrolledX = document.documentElement.scrollLeft;
    scrolledY = document.documentElement.scrollTop;
  } else if( document.body ) {
    scrolledX = document.body.scrollLeft;
    scrolledY = document.body.scrollTop;
  }

  var centerX, centerY;
  if( self.innerHeight ) {
    centerX = self.innerWidth;
    centerY = self.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    centerX = document.documentElement.clientWidth;
    centerY = document.documentElement.clientHeight;
  } else if( document.body ) {
    centerX = document.body.clientWidth;
    centerY = document.body.clientHeight;
  }

  var leftOffset = scrolledX + (centerX - 400) / 2;
  var topOffset = scrolledY + (centerY - 300) / 2;

  document.getElementById("styled_popup").style.top = topOffset + "px";
  document.getElementById("styled_popup").style.left = leftOffset + "px";
}

function fireMyPopup() {
  myPopupRelocate();
  document.getElementById("styled_popup").style.display = "block";
  document.body.onscroll = myPopupRelocate;
  window.onscroll = myPopupRelocate;
}

function styledPopupClose() {
  document.getElementById("styled_popup").style.display = "none";
}

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadVideoPopup() {
  //loads popup only if it is disabled
  if(popupStatus==0) {
    $(".av-media").fadeOut("slow");
    $("#backgroundPopup").css({"opacity":"0.7"});
    $("#backgroundPopup").fadeIn("slow");
    $("#popup-video-container").fadeIn("slow");
    $("#video-frame").attr("src","_video.html");
    popupStatus = 1;
  }
}

//disabling popup with jQuery magic!
function disablePopup() {
  //disables popup only if it is enabled
  if(popupStatus==1) {
    $("#backgroundPopup").fadeOut("slow");
    $("#popup-video-container").fadeOut("slow");
    $("#video-frame").attr("src","");
    $(".av-media").fadeIn("slow");
    popupStatus = 0;
  }
}

//centering popup
function centerVideoPopup() {
  //request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var popupHeight = $("#popup-video-container").height();
  var popupWidth = $("#popup-video-container").width();
  var topScroll = $(document).scrollTop();

  //centering
  $("#popup-video-container").css({
    "position": "absolute",
	"top": (windowHeight/2-popupHeight/2)+topScroll,
	"left": windowWidth/2-popupWidth/2
  });

  //only need force for IE6
  $("#backgroundPopup").css({
    "height": windowHeight
  });
}

