jQuery(document).ready(function(){

  $('.openvideo').click(function(){
      var href = $(this).attr('href');
      //alert(PlayerVideo(href));
      OpenWindow('510','390',PlayerVideo(href));
      return false;
  });

  $('.openaudio').click(function(){
      var href = $(this).attr('href');
     OpenWindow('480','80',AudioVideo(href));
      return false;
  });
  
});

function PlayerVideo(url){
  var player   = '/soft/videoplayer.swf';
  var width    = '500';
  var height   = '375';
  var autoplay = '1';
  var volume   = '70';
  var pic      = '';
  if(!url) return false; 
  var html = '<object type="application/x-shockwave-flash" data="'+player+'" height="'+height+'" width="'+width+'">';
      html+= '<param name="bgcolor" value="#FFFFFF">';
      html+= '<param name="allowFullScreen" value="true">';
      html+= '<param name="allowScriptAccess" value="always">';
      html+= '<param name="movie" value="/soft/videoplayer.swf">';
      html+= '<param name="FlashVars" value="way='+url+'&amp;swf='+player+'&amp;w='+width+'&amp;h='+height+'&amp;pic='+pic+'&amp;autoplay='+autoplay+'&amp;tools=1&amp;skin=white&amp;volume='+volume+'&amp;q=&amp;comment=">';
      html+= '</object>'; 
  return html;
} 

function AudioVideo(url){
  var player   = '/soft/audioplayer.swf';
  var width    = '470';
  var height   = '70';
  var autoplay = '1';
  var volume   = '70';
  var pic      = '';
  if(!url) return false;
  var html = '<object type="application/x-shockwave-flash" data="'+player+'" height="'+height+'" width="'+width+'">';
      html+= '<param name="bgcolor" value="#FFFFFF">';
      html+= '<param name="allowFullScreen" value="true">';
      html+= '<param name="allowScriptAccess" value="always">';
      html+= '<param name="movie" value="/soft/audioplayer.swf">';
      html+= '<param name="FlashVars" value="way='+url+'&amp;swf='+player+'&amp;w='+width+'&amp;h='+height+'&amp;pic='+pic+'&amp;autoplay='+autoplay+'&amp;tools=1&amp;skin=white&amp;volume='+volume+'&amp;q=&amp;comment=">';
      html+= '</object>'; 
  return html;
} 
 
function CloseWindow(){
  $('#window').remove();
  $('#shadow').fadeOut(function(){
          
          $('#shadow').remove();
   });

}
function Position(){
   $('#window').css('padding','10px');
   var win_height = $('#window').height();
   var win_width  = $('#window').width();  
   var marginLeft = Math.max(40, parseInt($(window).width()/2 - win_width/2)) + 'px';
   var marginTop  = Math.max(40, parseInt(($(window).height()/2 - win_height/2)+$(document).scrollTop())) + 'px';
   $('#window').css('margin-left',marginLeft); 
   $('#window').css('margin-top',marginTop); 
}
function OpenWindow(width,height,result){
    var page_size = getPageSize();
    Loading();
    $('#window').css('width',width+'px');
    $('#window').css('height',height+'px');
    Position();
    /*$(window).scroll(function(){
      Position();
    });
    Position();*/
    $('#shadow').animate({
                  opacity: '0.7'
                },function(){
                      
                      
                  $('#window').html('<div style="text-align:right;" id="CloseWindow"><a onclick="CloseWindow();" style="cursor:pointer;">[закрыть]</a></div>');
                  $('#window').append('<div id="player"></div>');
                  $('#player').append(result);
                   
                }
                );
                
}
//Функция загрузки
function Loading(){
/*  var page_size = getPageSize();
  var div = document.createElement("div");
  div.innerHTML = '<div id="shadow" style="position: absolute;width:100%;height:'+page_size[1]+'px;"></div>';
  document.body.appendChild(div);
  
  var marginLeft = -Math.round((200/2)-(page_size[2]/2))
  var marginTop  = -Math.round((80/2)-(page_size[3]/2)+30)  
  var div = document.createElement("div");
  div.innerHTML = '<div id="window" style="position: absolute;"></div>';
  
  document.body.appendChild(div);*/
  
  var page_size = getPageSize();
  $('body').append('<div id="shadow" style="position: absolute;width:100%;height:'+page_size[1]+'px;"></div>');
  $('body').append('<div id="window" style="position: absolute;"></div>');
  
}

  function  getPageSize(){
       var xScroll, yScroll;

       if (window.innerHeight && window.scrollMaxY) {
                  xScroll = document.body.scrollWidth;
                  yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                  xScroll = document.body.scrollWidth;
                  yScroll = document.body.scrollHeight;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
                  xScroll = document.documentElement.scrollWidth;
                  yScroll = document.documentElement.scrollHeight;
       } else { // Explorer Mac...would also work in Mozilla and Safari
                  xScroll = document.body.offsetWidth;
                  yScroll = document.body.offsetHeight;
       }
 

       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
                 windowWidth = self.innerWidth;
                 windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                 windowWidth = document.documentElement.clientWidth;
                 windowHeight = document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
                 windowWidth = document.body.clientWidth;
                 windowHeight = document.body.clientHeight;
       }
  
  
       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
          pageHeight = windowHeight;
       } else {
          pageHeight = yScroll;
       }

       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){
                 pageWidth = windowWidth;
       } else {
                 pageWidth = xScroll;
       }

       return [pageWidth,pageHeight,windowWidth,windowHeight];
  }
