var popupStatus = 0;

function loadPopup(video){
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#videoPopup").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#videoPopup").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(){
	var offset = $("#videoPopup").offset();
	var popupWidth = $("#videoPopup").width();
	var popupHeight = $("#videoPopup").height();
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
    var topPadding = 150;
	
	$("#backgroundPopup").css({
		"height": $(document).height()
	});

    /*if ($(window).scrollTop() > offset.top) {*/
    $("#videoPopup").stop().animate({
		marginTop: $(window).scrollTop() + topPadding,
		marginLeft: windowWidth/2-popupWidth/2
    });
  /*  } else {
		$("#videoPopup").stop().animate({
			marginTop: 0+topPadding,
			marginLeft: windowWidth/2-popupWidth/2
		});
    }*/
}

$(document).ready(function(){
	$('.video_title, .video_img, .v_loop_link').click(function(){
		
		$('#player').html('');
		$('#videoPopup h2').text(this.title);
		//alert($(this).attr('href'));
      if ($(this).attr('href') != 'youtube'){
         $('#player').flash(
           { 
             src: 'client/js/player.swf',
             width: 470,
             height: 320,
   			 allowfullscreen: true,
             flashvars: { file: this.rel }
           },
           { version: 8 }
         );
      } else {
      
        $('#player').html('<object style="height: 320px; width: 470px;"><param value="" name="movie"><param value="true" name="allowFullScreen"><param value="always" name="allowScriptAccess"><embed height="320" width="470" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash" src="'+$(this).attr('rel')+'"></object>'); 
        //$('#player embed').attr('src', $(this).attr('rel')); 
      
      }

		centerPopup();
		loadPopup();
	});
	
	$(window).scroll(function() {
		centerPopup();
	});

	$("#videoPopupClose").click(function(){
		disablePopup();
	});

	$("#backgroundPopup").click(function(){
		disablePopup();
	});

	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
