var Whsub;
var Wwsub;

var timersub;
var last_tosub;
var last_tolsub;




var divcont = '<div  id="SUBFORMDIV"> <a class="autoriz_form_close" href="/" onclick="return hidesubwin();"></a> <div class="log_pad"></div><div class="bottom"><div class="bottom_pad"></div></div></div>';
    
function detectIE6(){
	  var browser = navigator.appName;
	  var b_version = navigator.appVersion;
	  var version = parseFloat(b_version);
	  if ((browser == "Microsoft Internet Explorer") && (version <= 6)){
	    return true;
	  }else{
	    return false;
	  }
	}		 

function showsubwin(toptext, bottomtext, showclose)
{
	 
        $("body").append('<div id="white_bg"></div>');
        if (detectIE6()) $("select").hide();                       
                      
        //$("#white_bg").height($(document).height());
       
        $("#white_bg").fadeTo(0,0.5);

  
      $("body").append(divcont);
       if (!showclose)     $("#SUBFORMDIV .autoriz_form_close").remove();
       $("#SUBFORMDIV .log_pad").html(toptext);
        $("#SUBFORMDIV .bottom_pad").html(bottomtext);
        init_cor_top_subwin();
    
 
   return false;
}

function hidesubwin()
 {     if (detectIE6())  $("select").show();
      $("#white_bg").remove();
      $("#SUBFORMDIV").remove();
      clearTimeout(timer);
      return false;
 }
  
function init_cor_top_subwin()
{
    Wwsub=document.body.clientWidth;
    Whsub=document.body.clientHeight;
    Whsub=parseInt((Whsub-$("#SUBFORMDIV").height())/2);
    Wwsub=parseInt((Wwsub-$("#SUBFORMDIV").width())/2);
    if (Whsub<0) Whsub=0;
    if (Wwsub<0) Wwsub=0;
    
    top_body=$(document).scrollTop();
    left_body=$(document).scrollLeft();
    to=top_body+Whsub;
    tol=left_body+Wwsub;

     /*$('body').mousewheel(function(event) {
		event.preventDefault();
	});*/

    $("#SUBFORMDIV").animate({top: to, left:tol},1,function(){
                                               timersub=setTimeout ('correct_top_sub()', 0 );
                                            });
  
}


function correct_top_sub()
{
    //top_body=5;
    $("#white_bg").height($(document).height());
    Wwsub=document.body.clientWidth;
    Whsub=document.body.clientHeight;

    Whsub=parseInt((Whsub-$("#SUBFORMDIV").height())/2);
    Wwsub=parseInt((Wwsub-$("#SUBFORMDIV").width())/2);
    if (Whsub<0) Whsub=0;
    if (Wwsub<0) Wwsub=0;
    
    top_body=$(document).scrollTop();
    left_body=$(document).scrollLeft();
    to=top_body+Whsub;
    tol=left_body+Wwsub;
    if (last_tosub!=to ||last_tosubl!=tol )
      {
         $("#SUBFORMDIV").animate({top: to, left:tol}, 200 );
         last_tosub=to;
        last_tosubl=tol;
      } 
    clearTimeout(timersub);timersub=setTimeout ('correct_top_sub()', 300 ); 
}

var WhsubBig;
var WwsubBig;

var timersubBig;
var last_tosub;
var last_tosubBig;
var last_tolsubBig;

//var bigContent = '<div class="popup" id="BIGWINDOW"><a class="close" href="/" onclick="return hideSubWin();"></a><div class="popup-cont"><h3></h3><p></p></div><div class="info-link"></div></div>';
var bigContent = '<div class="popup" id="BIGWINDOW">'+
	'<div class="info-link">'+
'<div class="info-l">Полиграфическая энциклопедия</div>'+
'<div class="info-r"><a href="#" onclick="return hideSubWin();"><img src="/images/bg-close-popup.png" width="25" height="25" alt=""/></a></div>'+
'</div>'+
'<div class="popup-cont">'+
'<h3></h3>'+
'<p></p>'+
'</div>'+
'</div>';

function bigSubWin(name,content,link, tech) { 
	
	$("body").append('<div id="white_bg"></div>');
    if (detectIE6()) $("select").addClass("hideselect");
	$("#white_bg").fadeTo(0,0.5);
    $("#hlayer2").fadeTo(0,0.9);
    $("body").append(bigContent);
    if (tech == 'order') 
	{
    	$("#BIGWINDOW").hide();
	}
    
    if (tech == 'tech' || tech == 'spec') 
	{
    	$("#BIGWINDOW").addClass('techInfo');
    	$("#BIGWINDOW").css('height','');
	}
    
    if (tech == "authform")
    {
        $("#BIGWINDOW .info-l").html(name);
        $("#BIGWINDOW").css('width','auto');
        $("#BIGWINDOW h3").remove();
    }
    
    if (tech == 'order') 
	{
    	$("#BIGWINDOW").css('width','auto');
    	$("#BIGWINDOW").css('height','auto');
	}
    
    $("#BIGWINDOW h3").text(name);
    if (tech == 'order') 
	{
    	$("#BIGWINDOW h3").remove();
    	$("#BIGWINDOW .popup-cont").css('padding','10px 0px 0px');
    	
	}
    $("#BIGWINDOW p").html(content);
    if (tech == 'spec') 
	{
    	$("#BIGWINDOW .info-l").html('Специальная цена');
	}
//    $("#BIGWINDOW .info-link").html(link);
    init_cor_top_bigsubwin();
    return false;
}
function hideSubWin() {
	$("#white_bg").remove();
	$("#BIGWINDOW").remove();
	clearTimeout(timersubBig);
	//$('html').css('overflow','visible');
	$('body').unbind();
	return false;
}



function init_cor_top_bigsubwin()
{
	WwsubBig=document.body.clientWidth;
	temp = document.body.clientHeight;
    WhsubBig=document.body.clientHeight;
    WhsubBig=parseInt((WhsubBig-$("#BIGWINDOW").height())/2);
    WwsubBig=parseInt((WwsubBig-$("#BIGWINDOW").width())/2);
    if (WhsubBig<0) WhsubBig=0;
    if (WwsubBig<0) WwsubBig=0;
    
    top_body=$(document).scrollTop();
    left_body=$(document).scrollLeft();
    to=top_body+WhsubBig;
    tol=left_body+WwsubBig;

   /*$('body').mousewheel(function(event) {
		event.preventDefault();
	});*/
    
    //$('html').css('overflow','hidden');
    //$(document).scrollTo({top:'300px', left:'0px'}, 800);
    
    $("#BIGWINDOW").animate({top: to, left:tol},0,function(){
    	timersubBig=setTimeout ('correct_top_bigsub()', 0 );
    	
    });
  
    $("#white_bg").height($(document).height());
    return false;
}

function correct_top_bigsub()
{
    //top_body=5;
    //$("#white_bg").height($(document).height());
    WwsubBig=document.body.clientWidth;
    WhsubBig=document.body.clientHeight;

    WhsubBig=parseInt((WhsubBig-$("#BIGWINDOW").height())/2);
    WwsubBig=parseInt((WwsubBig-$("#BIGWINDOW").width())/2);
    if (WhsubBig<0) WhsubBig=0;
    if (WwsubBig<0) WwsubBig=0;
    
    top_body=$(document).scrollTop();
    left_body=$(document).scrollLeft();
    to=top_body+WhsubBig;
    tol=left_body+WwsubBig;
    if (last_tosubBig!=to ||last_tosubl!=tol )
      {
         $("#BIGWINDOW").animate({top: to, left:tol}, 0 );
         last_tosubBig=to;
         last_tosubl=tol;
         
        
      } 
   
    clearTimeout(timersubBig);
    timersubBig=setTimeout ('correct_top_bigsub()', 0 ); 
}

// изменяем размер телефонного блока
var width = 89;
function ResizePhoneBlock(){
    $("#header .phones li").each(function (i, el) {
        newWidth = $(el).css("width");
        newWidth = ~~newWidth.replace("px", "");
        if (newWidth > width) width = newWidth;
    });

    if (width > 89) $("#header .phones").css("top", "15px");
}
    
// Выбор валюты
$(document).ready(function(){
    $("#currency").change(function (){
        $.ajax({
            type: "POST",
            url: "/_ajax/set_currency.php", 
            data: "currency="+$(this).val(),
            dataType: "json",
            success: function (data) {
                //$(".currency-message").text("Выбрана валюта " + data);
                //$(".currency-message").fadeIn().delay(3000).fadeOut("quick");
                location.reload(true);                 
            }
        });
    });
    
    $("#site").change(function (){
         window.location.href = $(this).val();      
    });
    
    // изменяем размер телефонного блока
    setTimeout(ResizePhoneBlock, 2000);
    setTimeout(ResizePhoneBlock, 4000);
    setTimeout(ResizePhoneBlock, 6000);
    setTimeout(ResizePhoneBlock, 8000);
    setTimeout(ResizePhoneBlock, 10000);
    
});
