var windowid = 1;
//var window = [];
function NewWindow(param){
  windowid++;
  var page_size = getPageSize();
  var width  = param['width'];
  var height = param['height']; 
  var id = windowid;
  var bwidth = width; 
  if(param['html']){
    var result = $(param['result']).html();
  }else{
    var result = param['result'];
  }
  //var group = param['group'];
  var obj = param['obj'] ? param['obj'] : false;
     
  window[id]        = {}
  window[id].close  = $('<div class="NW-close"><img src="/data/img/close.png"></div>');
  window[id].shadow = $('<div class="NW-shadow"></div>');
  window[id].plash  = $('<div class="NW-plash"></div>');
  
  if(param['button']){
    window[id].button = $('<div class="NW-button"><button class="this_print">'+param['button']+'</button></div>');
    $(window[id].button).mousedown(function(){
      $(window[id].conteiner).remove();
      $(window[id].shadow).remove();
      $(window[id].window).remove();
    });
  }
  
  $(window[id].close).mousedown(function(){
      $(window[id].conteiner).remove();
      $(window[id].shadow).remove();
      $(window[id].window).remove();
  });
  
  $(window[id].shadow).mousedown(function(){
      $(window[id].conteiner).remove();
      $(window[id].shadow).remove();
      $(window[id].window).remove();
  });
  
  if(param['image']){
    window[id].content = $('<div class="NW-result"><img src="'+result+'"></div>');
  }else{
    window[id].content = $('<div class="NW-result">'+result+'</div>');
  }
    window[id].conteiner = $('<div class="NW-window"></div>');
    $(window[id].plash).append(window[id].close);
    $(window[id].conteiner).append(window[id].plash);
    $(window[id].conteiner).append(window[id].content);
    if(param['button']){
      $(window[id].conteiner).append(window[id].button);
    }
    
    if(param['code'] || param['down'] || param['full']){
       window[id].action  = $('<div class="NW-action"></div>');
       if(param['code']){ 
        var playvideohtml = $('<img border="0" style="cursor:pointer;" title="Посмотреть" src="/data/img/play_s.png" class="playvideo" code="'+param['code']+'">');
        $(window[id].action).append(playvideohtml);
       }
       
       if(param['down']){
        var download  = $('<a href="/download.php?file='+param['down']+'" target="_blank" title="Скачать"><img border="0" src="/data/img/dnl_s.png"></a>');
        $(window[id].action).append(download);
        
       }
       
       if(param['full']){ 
        $(window[id].action).append('<a href="'+param['full']+'" target="_blank" id="tooltip_original" title="Оригинал"><img border="0" src="/data/img/smaximize.png"></a>');
       }
       
       $(window[id].content).append(window[id].action);
    }
     
  
    if(param['title'] || param['desc']){
      window[id].comment  = $('<div class="photocomment"></div>');
      
      if(param['title']){
        window[id].title = $('<div><b>'+param['title']+'</b></div>');
        $(window[id].comment).append(window[id].title);
      }
      if(param['desc']){
        window[id].desc  = $('<div>'+param['desc']+'</div>');
        $(window[id].comment).append(window[id].desc);
      }
      $(window[id].conteiner).append(window[id].comment);
    }
    $(window[id].window).append(window[id].conteiner);
    $('body').append(window[id].shadow);
    var zindex = eval($(window[id].shadow).css('z-index'))+id;
    $(window[id].shadow).css({
      width: '100%',
      height: page_size[1]+'px',
      position: 'absolute'
    });
    $(window[id].shadow).css('z-index',zindex);
    //$(window[id].shadow).css('opacity','0.5');
        //if(param['scrol']) $(window[id].content).jScrollPane({showArrows:true, scrollbarWidth:25, dragMaxHeight:24}); 
        
        //$(window[id].content).css('overflow', 'auto');
        if(width) $(window[id].content).css('width', width+'px');
        if(height) $(window[id].content).css('height', height+'px');
        if(obj){
          var back = null;
          var next = null;
   
          var imgArr = {}
          var img_i = 0;
          var in_id = 0;
          var img_in = 0;
          $(obj).parent().parent().parent().parent().find('.galleria-image').each(function(){
            var src = $(this).find('img').attr('src');
            if(src){
              if(img_i>0){       
                if(src.indexOf('mebel_photo')!=-1 || src.indexOf('mebel_project')!=-1){
                  var ff = src.replace('big_','full_');
                }else{
                  var ff = src.replace('big_','view_');
                }
                
                if(ff==result) in_id = img_in;
                imgArr[img_in] = ff;
                
                tb_image = new Image();
                tb_image.src = ff; 
                
                img_in++;
              }
              img_i++;
            }
          });
        }  

        if(img_in>0){
            var nav = $('<div></div>');
            
            var back_id = (in_id-1);
            if(imgArr[back_id]){
              var in_back = $('<img style="float:left;cursor:pointer;" src="/templates/js/themes/classic/back.png">');
              $(in_back).click(function(){
                
                NewWindow({
                 'title': param['title'],
                 'desc': param['desc'],
                 'result': imgArr[back_id],
                 'obj':obj,
                 'image': true 
                });
                $(window[id].conteiner).remove();
                $(window[id].shadow).remove();
                $(window[id].window).remove();
              });
              $(nav).append(in_back);
            }
            
            var next_id = (in_id+1);
            if(imgArr[next_id]){
              var in_next = $('<img style="float:right;cursor:pointer;" src="/templates/js/themes/classic/next.png">');
              $(in_next).click(function(){
                NewWindow({
                 'title': param['title'],
                 'desc': param['desc'],
                 'result': imgArr[next_id],
                 'obj':obj,
                 'image': true 
                });
                $(window[id].conteiner).remove();
                $(window[id].shadow).remove();
                $(window[id].window).remove();
              });
              $(nav).append(in_next);
            }
            $(window[id].conteiner).append(nav);
        }
        
        $('body').append(window[id].conteiner);
            if(param['image']){
              t_image = new Image();
              t_image.src = result; 
              if(t_image.width>0){
                    var width = t_image.width;
                    if(width) $(window[id].conteiner).css('width', width+'px');
                    if(height) $(window[id].conteiner).css('height', height+'px');
                    var win_width = $(window[id].conteiner).width();
                    var win_height = $(window[id].conteiner).height();
                    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[id].conteiner).css('left',marginLeft); 
                    $(window[id].conteiner).css('top',marginTop);
                    $(window[id].conteiner).css('z-index',(zindex+1));
                    $(window[id].conteiner).css('display','block');

              }else{ 
                t_image.onload = function mm(){
                    var width = t_image.width;
                    if(width) $(window[id].conteiner).css('width', width+'px');
                    if(height) $(window[id].conteiner).css('height', height+'px');
                    var win_width = $(window[id].conteiner).width();
                    var win_height = $(window[id].conteiner).height();
                    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[id].conteiner).css('left',marginLeft); 
                    $(window[id].conteiner).css('top',marginTop);
                    $(window[id].conteiner).css('z-index',(zindex+1)); 
                    $(window[id].conteiner).css('display','block');    
                }
              }
            }else{
              
              if(bwidth) $(window[id].conteiner).css('width', bwidth+'px');
              if(height) $(window[id].conteiner).css('height', height+'px');
              var win_width = $(window[id].conteiner).width();
              var win_height = $(window[id].conteiner).height();
              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[id].conteiner).css('left',marginLeft); 
              $(window[id].conteiner).css('top',marginTop);
              $(window[id].conteiner).css('z-index',(zindex+1));
              $(window[id].conteiner).css('display','block');
              windowid++;
              //if(param['scrol']) $(window[id].content).jScrollPane({speed:100, showArrows:true, scrollbarWidth:25, verticalDragMaxHeight:24});
              if(param['scrol']){
                 $(window[id].content).css('overflow','auto');
              }
              return id;
            }   
  windowid++;
}

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];
}
