var d$ = document;
function e$(id){return e$$(d$,id)}
function e$$(e,id){return e.getElementById(id)||null}
function v$(id){return d$.getElementById(id).value}
function t$(e,t){return e.getElementsByTagName(t)}
function a$(e,a){try{return typeof e=='object'?e.getAttribute(a):e$(e).getAttribute(a)}catch(err){}return 0}
//function a$$(e,a,v){e.setAttribute(a,v)}
function a$$(e,a,v){e.setAttribute(a+(a=='class'&&ua$('ie 7')?'Name':''),v)}
function new$(e,o){var x=d$.createElement(e);if(o)for(k in o)a$$(x,k,o[k]);return x}
function sh$(e,v){typeof e=='object'?e.style.display=v:e$(e).style.display=v}
function s$(e){sh$(e,'block')}
function h$(e){sh$(e,'none')}
function ua$(t){return navigator.userAgent.toLowerCase().indexOf(t)>-1}
function ce$(p,t,v){var e=d$.createElement(t);v=v||'';if(null==e.canHaveChildren||e.canHaveChildren)e.appendChild(d$.createTextNode(v));else e.text=v;p.appendChild(e);return e}
function rand$(a,b){var r=Math.random();if(typeof a=="undefined")return r;if(typeof b=="undefined"){b=a;a=0}return a+Math.floor(r*(b-a+1))}
function round$(x,k){if(k&&k>0){var p=Math.pow(10,k);return Math.round(x*p)/p}else return Math.round(x)}

function add_event(element, event_name, func)
{ if(element.attachEvent)element.attachEvent("on" + event_name, func);
  else if(element.addEventListener)element.addEventListener(event_name, func, true);
  else element["on" + event_name] = func;
}

function remove_event(element, event_name, func)
{ if(element.detachEvent)element.detachEvent("on" + event_name, func);
  else if(element.removeEventListener)element.removeEventListener(event_name, func, true);
  else element["on" + event_name] = null;
}


function bindReady(handler)
{ var called = false
  
  function ready()
  { if(called)return;
    called = true;
    handler()
  }

  if(document.addEventListener)
  { document.addEventListener("DOMContentLoaded", function(){document.removeEventListener("DOMContentLoaded", arguments.callee, false);ready()}, false)
  }
  else if(document.attachEvent)
  { if(document.documentElement.doScroll&&window==window.top)
    { function tryScroll()
      { if(called)return;
        try
        { document.documentElement.doScroll("left");
          ready()
        }
        catch(e)
        { setTimeout(tryScroll, 18)
        }
      }
      tryScroll()
    }
    
    document.attachEvent("onreadystatechange", function()
    { if(document.readyState==="complete")
      { document.detachEvent("onreadystatechange", arguments.callee);
        ready()
      }
    })
  }

  if(window.addEventListener){window.addEventListener('load', ready, false);}
  else if(window.attachEvent)window.attachEvent('onload', ready);
}

var readyList = [];
function onready$(handler)
{ if(!readyList.length)
  { bindReady(function(){for(var i=0;i<readyList.length;i++)readyList[i]()})
  }
  readyList.push(handler)
}



var fb_timer;
var clients_slider, co_slider, prod_slider;
var wnd, wnd0, wnd1;
function initSys()
{ //if(e$('clients-slider'))clients_slider=new hslider$('clients-slider',{cv:6});
  //if(e$('co-slider'))co_slider=new hslider$('co-slider',{cv:5,auto:1});
  //if(e$('prod-slider')){prod_slider=new hslider$('prod-slider',{k:(location.hash.substr(4)-1)||0,cv:1,pt:location.hash.substr(1,3)=='pos'?0:1,onmove:1})}
  //if(e$('prod-client-slider')){prod_slider=new hslider$('prod-client-slider',{cv:1})}
  //initBanners();
  //initProducts('hit');
  //initProducts('new');
  lightgallery.init({enableZoom:false,overlayOpacity:.01});
  //if(e$('rightcol')){e$('resize-box').style.minHeight=e$('rightcol').clientHeight-4+'px'};
  //if(e$('footer-buttons'))fb_timer=setInterval(function(){clearInterval(fb_timer);h$('footer-buttons')},1000);
}
add_event(window,"load",initSys);


onready$(function()
{ if(e$('clients-slider'))clients_slider=new hslider$('clients-slider',{cv:6});
  if(e$('co-slider'))co_slider=new hslider$('co-slider',{cv:5,auto:1});
  if(e$('prod-slider')){prod_slider=new hslider$('prod-slider',{k:(location.hash.substr(4)-1)||0,cv:1,pt:location.hash.substr(1,3)=='pos'?0:1,onmove:1})}
  if(e$('prod-client-slider')){prod_slider=new hslider$('prod-client-slider',{cv:1})}
  initBanners();
  initProducts('hit');
  initProducts('new');
  //lightgallery.init({enableZoom:false,overlayOpacity:.01});
  if(e$('rightcol')){e$('resize-box').style.minHeight=e$('rightcol').clientHeight-4+'px'};
  if(e$('footer-buttons'))fb_timer=setInterval(function(){clearInterval(fb_timer);h$('footer-buttons')},1000);
})

/*==========================================================*/
var banners_data={};
function initBanners()
{ if(e$('banners'))
  { for(var i=0,x=t$(e$('banners-data'),'span'),n=x.length;i<n;i++)
    { var block = parseInt(a$(x[i],'block'));
      if(typeof banners_data[block]=='undefined')banners_data[block]=[];
      banners_data[block].push(x[i].innerHTML);
    }
    fade$(e$('banners'),1);
    setInterval(function(){x_rand_banners(1)},9000);
    setTimeout(function(){setInterval(function(){x_rand_banners(2)},9000)},3000);
    setTimeout(function(){setInterval(function(){x_rand_banners(3)},9000)},6000)
  }
}

var products_data={'hit':[], 'new':[]};
function initProducts(t)
{ if(e$('is_'+t))
  { for(var i=0,x=t$(e$(t+'-data'),'span'),n=x.length;i<n;i++)
    { products_data[t].push(x[i].innerHTML);
    }
    fade$(e$('is_'+t),1);
    if(t=='hit')setInterval(function(){x_rand_product('hit')},5000);else setTimeout(function(){setInterval(function(){x_rand_product('new')},5000)},1500)
  }
}


function calcprice_wnd(id){wnd=new wnd$({sajax:["products","x_calcprice_wnd",id]})}
function calcprice_update(m)
{ var x=JSON.parse(e$('json_price').innerHTML), k=parseInt(v$('cp_k'))||0, z=0, r=true;
  if(k<m){inlineMsg('cp_k',e$('cp_msg').innerHTML);r=false}
  for(var i in x){if(k>=parseInt(i))z=parseFloat(x[parseInt(i)])}
  e$('form2').k.value=k;
  if(z){e$('cp_1').innerHTML=z;e$('cp_2').innerHTML=round$(z*k,2)}else r=false;
  return r;
}
function tostep2(m)
{ if(!calcprice_update(m))return;
  var k=v$('cp_k')||0;
  if(k<m){inlineMsg('cp_k',e$('cp_msg').innerHTML);return false}
  e$('cp_step1').style.display='none';
  fade$('cp_step2',1,0,0,function(){wnd.pos()});
}
function valid_order()
{ var f=e$('form2'), x=['user','phone','email'];
  for(var i in x)if(f[x[i]].value==''){inlineMsg(f[x[i]].id,a$(f[x[i]],'msg'));return}
  sajax('products','x_captcha',v$('o_captcha'),function(r){if(r=='1')f.submit();else{inlineMsg('o_captcha',a$('o_captcha','msg'));e$('img_captcha').src='captcha.php?rnd='+Math.round(Math.random(0)*1000);return}})
}
function check_basket(m){if(!calcprice_update(m))return;valid_order()}
function x_rand_product(t)
{ var x=e$('is_'+t);
  if(products_data[t].length>1)
  { var i=rand$(products_data[t].length-1);
    if(i==parseInt(a$(x,'i'))){x_rand_product(t);return}
    var r=products_data[t][i];
    fade$(x,0,0,0,function(){x.innerHTML=r;a$$(x,'i',i);fade$(x,1,100,1)})
  }
}

function calcprice()
{ var prod=v$('idx');
  var x=JSON.parse(e$('json_price').innerHTML), k=parseInt(v$('cp_k'))||0, z=0, min=0, j=1;
  for(var i in x[prod]){j=0;if(!min){min=parseInt(i)}}
  if(j)inlineMsg('cp_k',e$('cp_msg2').innerHTML);
  if(k<min){inlineMsg('cp_k',e$('cp_msg').innerHTML.replace('#min_part#',min))}
  for(var i in x[prod]){if(k>=parseInt(i))z=parseFloat(x[prod][parseInt(i)])}
  if(z){e$('cp_1').innerHTML=z;e$('cp_2').innerHTML=round$(z*k,2)}
}

function x_rand_banners(k)
{ if(banners_data[k].length>1)
  { var x=e$('banners-'+k),i=rand$(banners_data[k].length-1);
    if(i==parseInt(a$(x,'i'))){x_rand_banners(k);return}
    var r=banners_data[k][i];
    fade$(x,0,0,1,function(){x.innerHTML=r;a$$(x,'i',i);fade$(x,1)})
  }
}

function hslider$(id,o)
{ var sender=this;
  this.ul=e$(id),this.id=id,this.li=[],this.v={cv:4,pt:0,auto:0,interval:4000},this.k=0,this.w;

  this.init=function(o)
  { for(s in o)this.v[s]=o[s];
    for(var i=0,j=1,li=t$(this.ul,'li'),n=li.length;i<n;i++)
    { if(li[i].parentNode==this.ul)
      { this.li.push(li[i]);
        this.li[this.li.length-1].idx=a$(li[i],'idx');
        if(this.v.pt&&a$(li[i],'idx')==this.v.k+1){this.v.k=j-1;this.v.pt=0}
        j++;
      }
    }
    this.w=this.li[0].offsetWidth;this.width=this.w*this.li.length;
    this.ul.style.width=this.width+'px';
    this.ul.style.left='0px';
    sender.ul.parentNode.style.height=this.li[0].offsetHeight+'px';
    if(this.v.k>this.li.length||this.v.k<0)this.v.k=0;
    this.pos(this.v.k||0);if(this.v.auto)this.v.auto=setInterval(function(){sender.move(1)},this.v.interval)
  }
  this.move=function(d){var n=this.k+d,i=d==1?n==this.li.length?0:n:n<0?this.li.length-1:n;this.pos(i)}
  this.pos=function(p)
  { n=this.li.length-this.v.cv;
    var sl=e$(this.id+'-left'),sr=e$(this.id+'-right');
    sl.src='images/arrow-leftxx.gif';sl.disabled=true;sr.src='images/arrow-rightxx.gif';sr.disabled=true;
    if(p>n){p=0}else if(n&&p){sl.src='images/arrow-left.gif';sl.disabled=false}
    if(p==n||n<0){sr.src='images/arrow-rightxx.gif'}else if(n){sr.src='images/arrow-right.gif';sr.disabled=false}
    
    if(this.v.onmove){if(co_slider)clearInterval(co_slider.v.auto);sajax('products','x_client_product',this.li[p].idx,function(r){e$('client-product').innerHTML=r;lightgallery.init({enableZoom:false,overlayOpacity:.01});if(e$('co-slider'))co_slider=new hslider$('co-slider',{cv:5,auto:1});})}
    if(e$(this.id+'-legend'))
    { for(var i=0,n=this.li.length;i<n;i++)e$('legend-'+i).className='legend';
       e$('legend-'+p).className='legendx';
    }
    if(this.li[p].idx)location.hash='idx'+this.li[p].idx;

    clearInterval(this.ul.ai);clearInterval(this.ul.si);
    var o=parseInt(this.ul.style.left),t=p*this.w,d=t>Math.abs(o)?1:-1;t=t*-1;this.k=p;
    this.ul.si=setInterval(function(){sender.slide(t,d)},20)
  }
  this.slide=function(t,d)
  { var o=parseInt(sender.ul.style.left),hh=sender.li[sender.k].offsetHeight,h=parseInt(sender.ul.parentNode.style.height)||0;
    if(o==t){clearInterval(sender.ul.si);sender.ul.parentNode.style.height=hh+'px'}
    else{sender.ul.style.left=o-Math.ceil(Math.abs(t-o)*.15)*d+'px';sender.ul.parentNode.style.height=h-Math.ceil((h-hh)*.15)+'px'}
  }
  this.init(o||{});
}


function fade$(e,k,o,del,onsuccess)
{ e = typeof e=='object'?e:e$(e);
  if(k)e.style.display='';
  clearInterval(e.si);
  e.o = o?o:k?100:0;
  e.del = del?1:0;
  e.k = k||-1;
  e.alpha = e.style.opacity?parseFloat(e.style.opacity)*100:0;
  e.si = setInterval(function(){e.tween()},10);
  e.tween=function()
  { if(this.alpha==this.o)
    { clearInterval(this.si);
      if(!k)
      { this.style.display='none';
        if(!this.del)this.innerHTML='';
      }
      if(onsuccess)onsuccess();
    }
    else
    { var value = Math.round(this.alpha+((this.o-this.alpha)*0.2))+(1*this.k);
      this.style.opacity = value/100;
      this.style.filter = 'alpha(opacity='+value+')';
      this.alpha = value
    }
  }
}


function wnd$(o)
{ var sender=this;
  this.v,this.m,this.b,this.id,this.timer;
  if(typeof wnd$.zIndex=='undefined')wnd$.zIndex=10000;
  this.correct={i:0,k:10,t:100}

  this.show=function(o)
  { this.v={opacity:70,fixed:0,topsplit:2,height:0,width:0,sajax:0,html_id:0,html:0,bg:0,tpl:'default'};
    for(s in o)this.v[s]=o[s];
    this.id=rand$(1,9999);
    this.m=new$('div',{'id':'mask'+this.id,'class':'wnd_mask'});this.m.style.zIndex=wnd$.zIndex++;
    this.b=new$('div',{'id':'inner'+this.id,'class':'wnd_inner'});this.b.style.zIndex=wnd$.zIndex++;
    d$.body.appendChild(this.m);
    d$.body.appendChild(this.b);
    if(this.v.bg)this.b.style.background=this.v.bg;

    if(this.v.html)this.fill(this.v.html);
    else if(this.v.html_id)this.fill(e$(this.v.html_id).innerHTML);
    
    fade$(this.m.id,1,this.v.opacity);
    fade$(this.b.id,1);

    add_event(this.m,"click",function(){sender.hide()});
    add_event(window,"resize",function(){sender.mask()});
    
    if(this.v.sajax)this.sajax(this.v.sajax)
  }
  this.hide=function(){fade$(this.m.id,0,1);fade$(this.b.id,0,1)}
  this.fill=function(r){this.b.innerHTML=this.tpl[this.v.tpl].replace('#content#',r);this.mask()}
  this.mask=function(){this.m.style.height=this.total(1)+'px';this.pos()}
  this.sajax=function(a) 
  { this.b.innerHTML=this.tpl.preload;this.mask();
    var p = [];for(var i=0;i<a.length;i++)p.push('args[]='+encodeURIComponent(a[i]));p = p.join('&');
    var x = sajax_init_object();
    if(x==null)return false;
    else
    { x.wnd = this;
      x.open("POST", "index.php", true);x.setRequestHeader("Method", "POST index.php HTTP/1.1");x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      x.onreadystatechange=function(){if(x.readyState!=4)return;x.wnd.fill(x.responseText)}
    }  
    x.send(p);
    delete x;
    return true;
  }
  this.pos=function()
  { if(this.correct.i==0)this.timer=setInterval(function(){sender.pos()},this.correct.t);
    this.correct.i++;
    if(this.correct.i==this.correct.k){clearInterval(this.timer);this.correct.i=0}
    
    var t;
    this.b.style.width=this.v.width?this.v.width+'px':'auto';
    this.b.style.height=this.v.height?this.v.height+'px':'auto';
    if(typeof this.v.top!='undefined'){t=this.v.top}else{t=(this.height()/this.v.topsplit)-(this.b.offsetHeight/2); t=t<20?20:t}
    if(!this.v.fixed&&!this.v.top){t+=this.top()}
    this.b.style.top=t+'px'; 
    this.b.style.left=typeof this.v.left!='undefined'?this.v.left+'px':(this.width()/2)-(this.b.offsetWidth/2)+'px'
  }
  this.top=function(){return d$.documentElement.scrollTop||d$.body.scrollTop}
  this.width=function(){return self.innerWidth||d$.documentElement.clientWidth||d$.body.clientWidth}
  this.height=function(){return self.innerHeight||d$.documentElement.clientHeight||d$.body.clientHeight}
  this.total=function(d){var b=d$.body,e=d$.documentElement;return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):Math.max(e.clientWidth,b.scrollWidth)}
  this.tpl={'default':'<div class="wnd_default">#content#</div>','preload':'<div class="wnd_preload"></div>'}
  this.show(o||{});
}


/*======= form validator =======*/
var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 5;

function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('tt2msg')) {
    msg = document.createElement('div');
    msg.id = 'tt2msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('tt2msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

function hideMsg(msg) {
  var msg = document.getElementById('tt2msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('tt2msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

