(function($){
    $.fn.extend({
        center: function () {
            return this.each(function() {
                var top = ($(window).height() - $(this).outerHeight()) / 2;
                var left = ($(window).width() - $(this).outerWidth()) / 2;
                $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
            });
        }
    }); 
})(jQuery);

var fuv = {};
fuv.VIDEO_PLAYER_DIV = 'fuv-video-show';

jQuery(document).ready( function($) {

	$('#'+fuv.VIDEO_PLAYER_DIV).center();
	
	fuv.showVideo = function(videoId, flagTv) {
	//	$('#'+fuv.VIDEO_PLAYER_DIV).center();
		$('#'+fuv.VIDEO_PLAYER_DIV).show();
		var params = 'queryType=show_video&videoId=' + videoId;
		if ( flagTv == 1 ) {
		    params += '&tv=1';
		}
		var filePath = '/wp-content/plugins/univerde_tv/echoVideo.php';
		fuv.sendRequest(filePath, params, fuv.VIDEO_PLAYER_DIV);
	}

	fuv.closeVideo = function() {
	    $('#'+fuv.VIDEO_PLAYER_DIV).html('');
	    $('#'+fuv.VIDEO_PLAYER_DIV).hide();
	}

	fuv.sendRequest = function(filePath, params, resultDivName) {
	  if (window.XMLHttpRequest) {
	    var xmlhr = new XMLHttpRequest();
	  } else {
	    var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
	  }

	  xmlhr.open('POST', filePath, true);
	  xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

	  xmlhr.onreadystatechange = function() {
	    var resultDiv = document.getElementById(resultDivName);
	    if (xmlhr.readyState == 1) {
	      resultDiv.innerHTML = '<b>Loading...</b>'; 
	    } else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
	      if (xmlhr.responseText) {
	        resultDiv.innerHTML = xmlhr.responseText;
	      }
	    } else if (xmlhr.readyState == 4) {
	      alert('Invalid response received - Status: ' + xmlhr.status);
	    }
	  }
	  xmlhr.send(params);
	}

});


