
var lastscrollpos=0;
var menu_hided = false;
var ietid2=0;

var loading = ['Загрузка','зАгрузка','заГрузка','загРузка','загрУзка','загруЗка','загрузКа','загрузкА','загрузкa'];
var loadingi = 0;


var ie6=false;
var ie7=false;
var ie8=false;


var anc_id='0';
var ancp_id='0';
var ancp_id0= '0';

var xmlcontent;
var xmlid;

var bind_addr_l1_last='0';
var bind_addr_l2_last;
var bind_addr_l2_new='0';

var desc_margin = 0;

if (document.all && !window.opera) {
	is_ie=true;
	if (navigator.userAgent.indexOf("MSIE 6") >= 0) {ie6=true;}
	if (navigator.userAgent.indexOf("MSIE 7") >= 0) {ie7=true;}
	if (navigator.userAgent.indexOf("MSIE 8") >= 0) {ie8=true;}
}



var desc_mass = [];

var bp_cache_x = 0;

var w = 0;

$(function(){
	
	o_chudo();
	
	$("#l").click(function(){
		
		w = getClientWidth();
		mr = Math.floor(w/10);
			
		$("#c_cont").css({width: w+"px",height: ($(".cold").height()+115)+"px", position: "relative"});
		$(".cold").css({margin: "0px", position:"absolute","left": mr+"px",top:"0px", width: (w-mr-mr)+"px"});
		
		return false;

	});
	


	$("#sp1 a").click(function(){
		o_chudo();		
		return false;

	});
	
	
		
	
	loading_animation();
	
	
	
	$("#m a").live("click",function(){
		document.location.href = "/#"+($($("#m a")).index(this) + 1);
		return false;
	});
	
	
	
	
});



$(window).load(function(){
	if (ie6) {
		bind_ie6_menu();
		x1 = getClientHeight();
		x2 = getBodyScrollTop();
		$("#m").css({position:"absolute", top: (x1+x2-60)+"px",left:"0"});

	}
});





function getBodyScrollTop() {
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getClientHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function getClientWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}





function loading_animation() {
	if ($("#sp2 div").hasClass('loan')) {
		loadingi++; if (loadingi>8) {loadingi=0;}
		$("#sp2 div").text(loading[loadingi]);
		setTimeout(loading_animation,250);
	}
}



function bind_ie6_menu() {
	newscrollpos = getBodyScrollTop();
	if (newscrollpos!=lastscrollpos) {
		clearTimeout(ietid2);
		$("#m").fadeOut(200);
		lastscrollpos=newscrollpos;
		ietid2 = setTimeout(show_ie6_menu,500);
	}
	setTimeout(bind_ie6_menu,100);
}


function show_ie6_menu() {
	if (ie6) {
		x1 = getClientHeight();
		x2 = getBodyScrollTop();
		$("#m").css({position:"absolute", top: (x1+x2-60)+"px",left:"0"});
		$("#m").fadeIn(200);
	}
}


function o_chudo() {
	
	$.post("/system/plugins/splash.php",{getem:1},function(data){
		xmlcontent = data;
		
		bp_load();
		
	},'xml');
	
}




function bp_load() {
	
	if ((bp_cache_x+1) == $(xmlcontent).find("product").length) {
		load_bg();
		return;
	}
		
	if ($(xmlcontent).find("product:eq("+bp_cache_x+")").length > 0) {
		id = get1($(xmlcontent).find("product:eq("+bp_cache_x+")").attr('index'));
		tmp = document.createElement("img");
		$(tmp).load(function(){
			bp_cache_x++;
			bp_load();
		});
		tmp.src = "/files/prods/id"+id+".jpg";
	}
}



var loadbg=0;
function load_bg(){
	if (loadbg==0) {
		tmp = document.createElement("img");
		$(tmp).load(function(){ loadbg=1;load_bg();});
		tmp.src = "/design/bghead.jpg";
	}

	if (loadbg==1) {
		tmp = document.createElement("img");
		$(tmp).load(function(){ loadbg=2;load_bg();});
		tmp.src = "/design/bgbody.jpg";
	}

	if (loadbg==2) { poyaviska();	}


}



function poyaviska() {

		$("#sp1, #sp2").hide();
		$("#sp2 div").removeClass('loan');
		$("body").attr("class","main");
		
		$("body").append($(xmlcontent).find("sh").text());
		
		$("#pm").empty();
		pmob='';
		$(xmlcontent).find("product").each(function(){
			tmp = $(this).attr("index");
			tmp2 = tmp.split("_");
			pmob = pmob + "<li><a id='pmaid_"+tmp2[1]+"' href='#3-"+tmp2[1]+"'>"+$(this).text()+"</a></li>"
			$(this).text()
		});
		
		
		$("#pm").append("<ul>"+pmob+"</ul>");
		
		
		
		
		tmp = document.location.href;
		tmp2 = tmp.split("#");
		if (tmp2[1]==undefined) { document.location.href='/#1';  }
				
		bind_addr();
		
		btnclick();
		
		$(document).pngFix();

	
}




/*

function bind_addr() {
	
	tmp = document.location.href;
	tmp2 = tmp.split("#");
	
	tmp3 = tmp2[1];
	if (tmp3!=undefined) {
		tmp4 = tmp3.split("-");
		anc_id2 = tmp4[0];
		ancp_id = tmp4[1];
	} else {
		anc_id2 = tmp2[1];
	}
	
	if (anc_id!=anc_id2 && anc_id2!=undefined) {
		//alert('переход');
		if (parseInt(anc_id2,10) > parseInt(anc_id,10)) {
			anc_id = anc_id2;
			hit(anc_id2, true);
		} else {
			anc_id = anc_id2;
			hit(anc_id2, false);
		}
	}
	
	setTimeout(bind_addr,300);
	
}

				*/


function bind_addr() {
	
	tmp = document.location.href;
	tmp2 = tmp.split("#");
	
	if (tmp2[1]!=undefined) {
		tmp3 = tmp2[1].split("-");
	} else {
		tmp3 = [undefined,undefined];
	}

	if (bind_addr_l1_last!=tmp3[0] || (bind_addr_l2_last!=tmp3[1] && tmp3[0]=='3')) {
		bind_addr_l2_new = tmp3[1];
		if (parseInt(tmp3[0],10) > parseInt(bind_addr_l1_last,10)) {
			bind_addr_l1_last = tmp3[0];
			hit(tmp3[0], true);
		} else {
			bind_addr_l1_last = tmp3[0];
			hit(tmp3[0], false);
		}
	}
	
	setTimeout(bind_addr,300);
	
}





function hit(linkid,mor) {
	
	if (linkid!='3' && $("body").hasClass("products")) {product_show(false); $("body").attr("class","main"); $(".c").attr("class","c cempty").hide();  $(".bp").attr("class","bp pempty").hide(); }
	
	if (linkid=='3' && $("body").hasClass("products")) { hit2(); }
	
	
	if (linkid=='3' && $("body").hasClass("main")) {
		window.scrollTo(0,0);
		show_ie6_menu();
		product_show(true);
		$("body").attr("class","products");
		$(".msel").attr("class","");
		$("#m a:eq("+(linkid-1)+")").addClass("msel");
		hit2();
		return;
	}
	
	
	w = getClientWidth();
	mr = Math.floor(w/10);
	
	if (linkid=='1') {xmlid='1';}
	if (linkid=='2') {xmlid='2';}
	if (linkid=='4') {xmlid='3';}
	if (linkid=='5') {xmlid='4';}
	
	if ($(".c").hasClass("cempty")) { // появление
		
		tmp = $(xmlcontent).find("page[index="+xmlid+"]").text();
		
		$(".c").hide();
		
		$(".c").html("<h1>"+$(xmlcontent).find("page[index="+xmlid+"]").attr("topic")+"</h1>"+tmp); show_fb();
		$(".c").css({margin: "0px", position:"absolute","left": mr+"px",top:"0px", width: (w-mr-mr)+"px"});
		$("#c_cont").css({height: ($(".c").height()+115)+"px", position: "relative"});
		$("#w").css("top",($(".c").height()+300)+"px");
		$(".c").attr("class","c cold").fadeIn(500);

		$(".msel").attr("class","");
		$("#m a:eq("+(linkid-1)+")").addClass("msel");

		
	} else {

		$("#c_cont").append("<div class='c cnew'></div>");
		tmp = $(xmlcontent).find("page[index="+xmlid+"]").text();
		$(".cnew").html("<h1>"+$(xmlcontent).find("page[index="+xmlid+"]").attr("topic")+"</h1>"+tmp); show_fb();

		if (mor) {

			$(".cnew").css({margin: "0px", position:"absolute","left": w+"px",top:"0px", width: (w-mr-mr)+"px"});
			$(".cnew").animate({"left": mr+"px"});
			
			$("#c_cont").css({height: ($(".cnew").height()+115)+"px", position: "relative"});
			$("#w").css("top",($(".cnew").height()+300)+"px");
			
			$(".cold").animate({"left": (-w+mr+mr)+"px"},function(){
				$(this).remove();
				$(".cnew").attr("class","c cold");
				$(".msel").attr("class","");
				$("#m a:eq("+(linkid-1)+")").addClass("msel");
			});
			
			
		} else {
			
			$(".cnew").css({margin: "0px", position:"absolute","left": (-w+mr+mr)+"px",top:"0px", width: (w-mr-mr)+"px"});
			$(".cnew").animate({"left": mr+"px"});
			
			$("#c_cont").css({height: ($(".cnew").height()+115)+"px", position: "relative"});
			$("#w").css("top",($(".cnew").height()+300)+"px");
			
			$(".cold").animate({"left": w+"px"},function(){
				$(this).remove();
				$(".cnew").attr("class","c cold");
				$(".msel").attr("class","");
				$("#m a:eq("+(linkid-1)+")").addClass("msel");
			});

			
		}
		
	}
	
	show_ie6_menu();
	

	
}




function hit2(){
	

	if (bind_addr_l2_new==undefined) {
		sl = get1($("#pm a:first").attr("id"));
		document.location.href="/#3-"+sl;
		bind_addr_l2_last = bind_addr_l2_new;
		return;
	}
		
	
	$(".msel2").attr("class","");
	$("#pmaid_"+bind_addr_l2_new).attr("class","msel2");
	
	
	rxnew = $("#pm a").index($("#pmaid_"+bind_addr_l2_new));
	rxlast = $("#pm a").index($("#pmaid_"+bind_addr_l2_last));
	if (rxnew>rxlast) {mor=true;} else {mor=false;}
	
	//rdnew = $("#pm a").index($("#pm a:first"));
	rdlast = $("#pm a").index($("#pm a:last"));
	
	if (rxnew==0 && rdlast==rxlast && $(".bp").hasClass("pold")) {mor=true;}
	if (rxnew==rdlast && rxlast==0 && $(".bp").hasClass("pold")) {mor=false;}
	
	
	
	bind_addr_l2_last = bind_addr_l2_new;
	
	
	
	////////////////////////
	
	//alert("h " + $("html").height());
	//w = $("html").width();
	//h = $("html").height();
	//alert('h '+h);
	//$("#bp_cont, .bp").css({height: (h-150)+"px"});
	

	
	if ($(".bp").hasClass("pempty")) { // появление
			
		$(".bp").hide();
		$(".bp").css({backgroundImage:"url('/files/prods/id"+bind_addr_l2_new+".jpg')"});
		$(".bp").attr("class","bp pold").fadeIn(500);

	} else {
		w = getClientWidth();
		if (mor) {
			$(".bp").after("<div class='bp pnew'></div>");
			$(".pnew").css({left: w+"px", backgroundImage:"url('/files/prods/id"+bind_addr_l2_new+".jpg')"});
			$(".pnew").animate({"left": "0px"});				
			$(".pold").animate({"left": (-w)+"px"},function(){
				$(this).remove();
				$(".pnew").attr("class","bp pold");
			});
		} else {
			$(".bp").after("<div class='bp pnew'></div>");
			$(".pnew").css({left: -w+"px", backgroundImage:"url('/files/prods/id"+bind_addr_l2_new+".jpg')"});
			$(".pnew").animate({"left": "0px"});				
			$(".pold").animate({"left": w+"px"},function(){
				$(this).remove();
				$(".pnew").attr("class","bp pold");
			});
		}
	}
	
	
	
}



















function product_show(show) {
	
	if (show) {
		$("#l2, #pm, #bp_cont, #btn1, #btn2, #btn3, #btn4").show();
		$("#h, #c_cont").hide();
	} else {
		$("#l2, #pm, #bp_cont, #btn1, #btn2, #btn3, #btn4").hide();
		$("#h, #c_cont").show();
	}	
	
}





function btnclick() {
	
	//alert('btnclick');
	$("#prdesc1").css("opacity","0");
	
	$("#btn1,#btn2,#btn3,#btn4").hover(function(){ $(this).addClass($(this).attr('id')+'h');},function(){$(this).attr('class','');});
	$("#btn1,#btn2,#btn3,#btn4").mousedown(function(){ $(this).addClass('btnc'); });
	$("#btn1,#btn2,#btn3,#btn4").mouseup(function(){ $(this).removeClass('btnc'); });
	
	
	$("#btn1").click(function(){
		if ($(".msel2").parent().prev().find("a").length >= 1) {
			sl = get1($(".msel2").parent().prev().find("a").attr("id"));
		} else {
			sl = get1($("#pm a:last").attr("id"));
		}
		document.location.href="/#3-"+sl;
	});

	$("#btn2").click(function(){
		if ($(".msel2").parent().next().find("a").length >= 1) {
			sl = get1($(".msel2").parent().next().find("a").attr("id"));
		} else {
			sl = get1($("#pm a:first").attr("id"));
		}
		document.location.href="/#3-"+sl;
	});




	$("#btn3").click(function(){
		$("#pm ul").prepend($("#pm li:last"));
	});

	$("#btn4").click(function(){
		$("#pm ul").append($("#pm li:first"));
	});




	$("#prdesc1").mouseenter(function(){
		$("#prdesc3").html("<img src='/design/al.gif' alt='загрузка...' />");
		
		if (desc_mass[bind_addr_l2_new]==undefined) {
			//$("#prdesc3").hide();
			$.post("/system/plugins/splash.php",{desc:bind_addr_l2_new},function(data){
				desc_mass[bind_addr_l2_new] = $(data).find("text").text();
				$("#prdesc3").html(desc_mass[bind_addr_l2_new]);
				desc_set_btns(); $("#prdesc3").css({display:'block',marginTop:"0px"}); desc_margin=0;
				//$("#prdesc3").animate({opacity:"1"});
			},'xml');
		} else {
			$("#prdesc3").html(desc_mass[bind_addr_l2_new]);
			desc_set_btns(); $("#prdesc3").css("marginTop","0px"); desc_margin=0;
			//$("#prdesc3").css("opacity","1");
		}

		$(this).stop();
		$(this).animate({opacity:0.7});
		if (ie8) { $("#prdesc3").stop(); $("#prdesc3").animate({opacity:1}); }
		desc_set_btns();
	});

	$("#prdesc1").mouseleave(function(){
		$(this).stop();
		$(this).animate({opacity:0});
		if (ie8) { $("#prdesc3").stop(); $("#prdesc3").animate({opacity:0}); }
		//$("#prdesc3").animate({opacity:0});
		desc_set_btns();
	});
	
//	$("#prdesc1").animate({opacity:0});
//	$("#prdesc3").animate({opacity:0});

	
	
	
	$("#prdescb2").click(function(){	
		x1 = $("#prdesc2").height();
		x2 = $("#prdesc3").height();
		fs = x2-x1;
		if ((fs+desc_margin) > 60) {
			desc_margin = desc_margin-60;
		} else {
			desc_margin = -fs;
		}
		$("#prdesc3").animate({marginTop: desc_margin+"px"},function(){
			desc_set_btns();
		});
	});

	$("#prdescb1").click(function(){	
		x1 = $("#prdesc2").height();
		x2 = $("#prdesc3").height();
		fs = x2-x1;
		
		if ((desc_margin+60)<0){
			desc_margin = desc_margin+60;
		} else {
			desc_margin = 0;
		}
		$("#prdesc3").animate({marginTop: desc_margin+"px"},function(){
			desc_set_btns();
		});
	});

	
	
}



function get1(p){
	tmp = p.split("_"); return tmp[1];
	
	
}






function desc_set_btns(){
	
	x1 = $("#prdesc2").height();
	x2 = $("#prdesc3").height();
	fs = x2-x1;
	
	if (desc_margin < 0) { $("#prdescb1").fadeIn(300); } else { $("#prdescb1").fadeOut(300);}
	if ((fs+desc_margin)>0)  { $("#prdescb2").fadeIn(300); } else { $("#prdescb2").fadeOut(300); }
	
}







function show_fb() {
	fr = "<form id='fbform' action='/block_bot.php'><div class='fb_row'><div class='fb_col1'>Ваше имя</div><div class='fb_col2'><input class='fb_field'></div></div><div class='fb_row'><div class='fb_col1'>Телефон</div><div class='fb_col2'><input class='fb_field'></div></div><div class='fb_row'><div class='fb_col1'>E-mail</div><div class='fb_col2'><input class='fb_field'></div></div><div class='fb_row'><div class='fb_col1'>Сообщение</div><div class='fb_col2'><textarea></textarea></div></div><div class='fb_row'><div class='fb_col2'><input value='Отправить' type='button'> <input value='Очистить' type='reset'></div></div></form>";
	
	if ($(".insert_fb_here").length>0) {
		$(".insert_fb_here").parent().append(fr);
	}	
}


$(function(){
    curlang='ru';
    $("#fbform input:button").live("click",function(){
        $("#fbform input:button")[0].disabled = true;
        imya=$("#fbform input:eq(0)").val();
		tel=$("#fbform input:eq(1)").val();
        email=$("#fbform input:eq(2)").val();
        text=$("#fbform textarea").val();
        $.post('/system/plugins/feedback.php',{imya:imya,tel:tel,email:email,text:text,tikto:'4el',l:curlang},function(data){
            $("#fbform input:button")[0].disabled = false;
            if (typeof($(data).find('error')[0]) !== 'undefined') {alert($(data).find('error').text()); return false;}
            $("#fbform input:eq(0)").val("");
            $("#fbform input:eq(1)").val("");
			$("#fbform input:eq(2)").val("");
            $("#fbform textarea").val("");
            alert($(data).find("mess").text());
        },'xml');
    });
});    




































// ###############################################################################


function randomNumber (m,n) // (c) 
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}


(function($){ 
  '$:nomunge'; // Used by YUI compressor.
  
  var cache = {},
    
    // Reused internal string.
    doTimeout = 'doTimeout',
    
    // A convenient shortcut.
    aps = Array.prototype.slice;

  $[doTimeout] = function() {
    return p_doTimeout.apply( window, [ 0 ].concat( aps.call( arguments ) ) );
  };
  
  
  $.fn[doTimeout] = function() {
    var args = aps.call( arguments ),
      result = p_doTimeout.apply( this, [ doTimeout + args[0] ].concat( args ) );
    
    return typeof args[0] === 'number' || typeof args[1] === 'number'
      ? this
      : result;
  };
  
  function p_doTimeout( jquery_data_key ) {
    var that = this,
      elem,
      data = {},
      
      // Any additional arguments will be passed to the callback.
      args = arguments,
      slice_args = 4,
      
      id        = args[1],
      delay     = args[2],
      callback  = args[3];
    
    if ( typeof id !== 'string' ) {
      slice_args--;
      
      id        = jquery_data_key = 0;
      delay     = args[1];
      callback  = args[2];
    }
    
    // If id is passed, store a data reference either as .data on the first
    // element in a jQuery collection, or in the internal cache.
    if ( jquery_data_key ) { // Note: key is 'doTimeout' + id
      
      // Get id-object from the first element's data, otherwise initialize it to {}.
      elem = that.eq(0);
      elem.data( jquery_data_key, data = elem.data( jquery_data_key ) || {} );
      
    } else if ( id ) {
      // Get id-object from the cache, otherwise initialize it to {}.
      data = cache[ id ] || ( cache[ id ] = {} );
    }
    
    // Clear any existing timeout for this id.
    data.id && clearTimeout( data.id );
    delete data.id;
    
    // Clean up when necessary.
    function cleanup() {
      if ( jquery_data_key ) {
        elem.removeData( jquery_data_key );
      } else if ( id ) {
        delete cache[ id ];
      }
    };
    
    // Yes, there actually is a setTimeout call in here!
    function actually_setTimeout() {
      data.id = setTimeout( function(){ data.fn(); }, delay );
    };
    
    if ( callback ) {
      // A callback (and delay) were specified. Store the callback reference for
      // possible later use, and then setTimeout.
      data.fn = function( no_polling_loop ) {
        callback.apply( that, aps.call( args, slice_args ) ) && !no_polling_loop
          
          // Since the callback returned true, and we're not specifically
          // canceling a polling loop, do it again!
          ? actually_setTimeout()
          
          // Otherwise, clean up and quit.
          : cleanup();
      };
      
      // Set that timeout!
      actually_setTimeout();
      
    } else if ( data.fn ) {

      delay === undefined ? cleanup() : data.fn( delay === false );
      return true;
      
    } else {
      // Since no callback was passed, and data.fn isn't defined, it looks like
      // whatever was supposed to happen already did. Clean up and quit!
      cleanup();
    }
    
  };
  
})(jQuery);























