var popupStatus = 0;

/*Browser compatibilty for Opera*/
var windowWidth = $.browser.opera && $.browser.version > "9.5" && 
  $.fn.jquery <= "1.2.6" ? document.documentElement["clientWidth"] : 
  $(window).width();
var windowHeight = $.browser.opera && $.browser.version > "9.5" &&
  $.fn.jquery <= "1.2.6" ? document.documentElement["clientHeight"] : 
  $(window).height();

function disablePopupWindow(){
  if(popupStatus==1){
    $(".backgroundPopup").fadeOut("slow");
    $(".popupWindow").fadeOut("slow");
    popupStatus = 0;
  }
}

function showPopupWindow(countid){
  var id = countid;
  var popupHeight = $(".popupWindow").height();
  var popupWidth = $(".popupWindow").width();

  var bgHeight = $.browser.opera && $.browser.version > "9.5" &&
    $.fn.jquery <= "1.2.6" ? (
		                            document.documentElement["scrollHeight"] >
		                            document.documentElement["clientHeight"] ? 
		                            document.documentElement["scrollHeight"] : 
                                document.documentElement["clientHeight"]
														 ) : $(document).height();
    $(".popupWindow").css({
      "position": "absolute", "top": windowHeight/2-popupHeight/2,
      "left": windowWidth/2-popupWidth/2
  });
  if(popupStatus==0){
    $(".backgroundPopup").css({"opacity": "0.3"});
    $(".backgroundPopup").css("height", bgHeight);
    $(".backgroundPopup").fadeIn("slow");
    popupStatus = 1;
  }
  $("#view-popup-"+id+"").fadeIn("slow");
}

$(document).ready(function(){
  var $scrollingVideo = $(".popupWindow");
  $(window).scroll(function(){
    $scrollingVideo.stop().animate({"marginTop": 
    ($(window).scrollTop())}, "slow" );
  });

  $(".popup-btn").click(function(){
    var myid = $(this).attr("id");
    var count = myid.split("-");
    var countid = count[1];
    var imgHt = $("#image").height();
    showPopupWindow(countid);
  });

  $(".popupClose").click(function(){
    disablePopupWindow();
  });

  $(".backgroundPopup").click(function(){
    disablePopupWindow();
  });
});
