// (c) 2005-2006, YS for MrMobile

// IE previews using filters

function showPreview(im) {
  im = getElem(im);
  if (typeof(im.filters)=="undefined" || im.filters.length==0) return;
  im.filters.item(0).enabled=false;
}

function hidePreview(im) {
  im = getElem(im);
  if (typeof(im.filters)=="undefined" || im.filters.length==0) return;
  im.filters.item(0).enabled=true;
}

// MrMobile utils

function showPic(im, url) {
  im = getElem(im);
  if (typeof(im.src)=="undefined") return;
  im.src = url;
}

function showInfo(e) {
  e = getElem(e);
  var w = document.body.offsetWidth;
  e.style.top = "135px";
  e.style.left = (w/2 - e.offsetWidth/2) + "px";
  e.style.visibility = "visible";
}

function hideInfo(e) {
  e = getElem(e);
  e.style.visibility = "hidden";
}

function showDesc(e) {
  e = getElem(e);
  e.style.display = (e.style.display!="inline")?"inline":"none";
}

function showSearch() {
  showInfo("search");
  document.forms.searchForm.sk.select();
  document.forms.searchForm.sk.focus();
}


// Sound support v3.0

var IEBGS=(navigator.appVersion.indexOf("MSIE")>=0);

var bgPlayr=null;
var winPlayr=null;
var playingUrl=null;
var playingImg=null;

var plrPlayImgUrl="/plrPlay.gif";
var plrStopImgUrl="/plrStop.gif";

function startPlaying(s, im) {
  if (playingUrl==s) {
    stopPlaying(); // stop playing
    return;
  }
  if (playingUrl) stopPlaying();
  try {
    if (IEBGS) {
      bgPlayr=document.createElement("bgsound");
      bgPlayr.loop=-1;
      bgPlayr.src=s;
    }
    else {
      bgPlayr=document.createElement("object");
      bgPlayr.style.position="absolute";
      bgPlayr.style.x="1px";
      bgPlayr.style.y="1px";
      bgPlayr.style.visibility="hidden";//"visible/hidden";
      var p;
      p=document.createElement("param");
      p.setAttribute("name","autostart");
      p.setAttribute("value","true");
      bgPlayr.appendChild(p);
      p=document.createElement("param");
      p.setAttribute("name","loop");
      p.setAttribute("value","true");
      bgPlayr.appendChild(p);
      p=document.createElement("param");
      p.setAttribute("name","hidden");
      p.setAttribute("value","false");
      bgPlayr.appendChild(p);
      bgPlayr.data=s;
    }
    document.body.appendChild(bgPlayr);
    im = getElem(im);
    if (im!=null && typeof(im.src)!="undefined") im.src = plrStopImgUrl;
    playingUrl=s;
    playingImg=im;
  }
  catch (e) {
    bgPlayr=null;
    winPlayr = window.open('','playwin','width=300,height=100,scrollbars=no,status=no,resizeable=no');
    winPlayr.document.write("<title>Образец звучания мелодии</title>");
    winPlayr.document.write("<div align='center'><embed loop='true' width='200' height='40' align='center' mastersound='mastersound' controls='smallconsole' autostart='true' src='"+s+"'/><br>");
    winPlayr.document.write("<button onclick='window.close()'>Стоп</button></div>");
    winPlayr.document.close();
    winPlayr.focus();
    playingUrl=s;
  }
}

function stopPlaying() {
  if (bgPlayr!=null) {
    document.body.removeChild(bgPlayr);
    bgPlayr=null;
  }
  if (winPlayr!=null) {
    winPlayr.close();
    winPlayr=null;
  }
  playingUrl='';
  if (playingImg!=null && typeof(playingImg.src)!="undefined") playingImg.src=plrPlayImgUrl;
  playingImg=null;
}

// Model selection

function selectPhoneId(id) {
  hideInfo('phSel');
  var u = phChgUrl1 + id + phChgUrl2;
  window.location.href = u;
  return false;
}

function selectOpt(sel, idx){
  sel.selectedIndex = idx;
  sel[idx].selected = true;
}

function selectOptByVal(sel, val){
  var i;
  for ( i=0; i<sel.length; i++ ) {
    if (sel[i].value==val||sel[i].text==val) selectOpt(sel, i);
  }
}

function loadOpt(sel,arr,cur){
  var i;
  for ( i=sel.length-1; i>=0; i-- ) {
    sel[i] = null;
  }
  if (arr!=null) {
    var si=0;
    for ( i=0; (i+1)<arr.length; i+=2 ) {
      var o = new Option(arr[i+1],arr[i]);
      if (cur!=null&&(arr[i]==cur||arr[i+1]==cur)) si=i/2;
      sel[i/2] = o;
    }
    selectOpt(sel, si);
  }
}

function brSelected(curBrand, curModel){
  var form,idx;
  form = document.forms["ulaForm"];
  if (curModel) {
    var i,j;
    for (i=0;i<cModels.length;i++) {
      for (j=0;(j+1)<cModels[i].length;j+=2) {
        if (cModels[i][j]==curModel) {
          selectOpt(form.gBrand.options, i);
          loadOpt(form.goodsId.options,cModels[i],curModel);
          return;
        }
      }
    }
  }
  if (curBrand) selectOptByVal(form.gBrand.options, curBrand);
  idx = form.gBrand.selectedIndex;
  loadOpt(form.goodsId.options, cModels[idx], 0);
}

function phSelected(){
  var form;
  form = document.forms["ulaForm"];
  var ModelID   = form.goodsId.options[form.goodsId.selectedIndex].value;
  selectPhoneId(ModelID);
}


// Popup implementation
// Tested with: IE 5.5/6.0, Opera 6.04/7.54, Netscape 6.2/7.1/7.2, FireFox 1.0

var PDT = new Array();

function showPopup(p, r, m) {
  p = getElem(p);
  var pd = PDT[p.id];
  if (pd) { pd.mIn(); return; }
  r = getElem(r);
  m = getElem(m);
  if (m) {
    var pw = p.offsetWidth;
    p.style.left = (getOffsetLeft(r, p.offsetParent) - getOffsetLeft(m, p) - (m.offsetWidth-r.offsetWidth)/2) + "px";
    if (p.offsetWidth!=pw) p.style.width = pw + "px"; // Opera7 fix
    p.style.top = (getOffsetTop(r, p.offsetParent) - getOffsetTop(m, p) - (m.offsetHeight-r.offsetHeight)/2) + "px";
  }
  else {
    p.style.left = (getOffsetLeft(r, p.offsetParent) - (p.offsetWidth-r.offsetWidth)/2) + "px";
    p.style.top = (getOffsetTop(r, p.offsetParent) - (p.offsetHeight-r.offsetHeight)/2) + "px";
  }
  p.style.visibility = "visible";
//  var s = "Top="+p.style.top+"; Left="+p.style.left+"; p.clientLeft="+0+"; p.clientTop="+0
//  +"; p.offsetWidth="+p.offsetWidth+"/"+pw+"/"+"pww"+"; p.offsetHeight="+p.offsetHeight+"; getOffsetTop(m, p)="+getOffsetTop(m, p);
//  window.status = s;
  pd = new PopupData(p, r);
  PDT[p.id] = pd;
  r.onmouseout = new Function("PDT['"+p.id+"'].mOut()");
  return true;
}

function hidePopup(p) {
  p = getElem(p);
  p.style.visibility = "hidden";
  if (PDT[p.id]!=null) PDT[p.id] = null;
}

function PopupData(p, r) {
  this.popup = p;
  this.ref = r;
  this.timer = null;
  this.mIn = pdIn;
  this.mOut = pdOut;
  p.onmouseover = pupIn;
  p.onmouseout = pupOut;
}

function pdIn() {
  if (this.timer) {
    clearTimeout(this.timer);
    this.timer = null;
  }
}

function pdOut() {
  if (this.timer) {
    clearTimeout(this.timer);
  }
  this.timer = setTimeout("hidePopup('"+this.popup.id+"')", 10);
}

function pupIn(o) {
  if (typeof(o)=="undefined" || o==null) o = window.event.srcElement;
  else o = o.target; // NN or Opera
  while (typeof(o)!="undefined" && o!=null) {
    if (typeof(o.onmouseover)!="undefined" && o.onmouseover!=null) break;
    o = o.parentNode;
  }
  if (typeof(o)=="undefined" || o==null) return;
  if (typeof(o.id)=="undefined" || o.id==null) return;
  var pd = PDT[o.id];
  if (typeof(pd)=="undefined" || pd==null) return;
  pd.mIn();
}

function pupOut(o) {
  if (typeof(o)=="undefined" || o==null) o = window.event.srcElement;
  else o = o.target; // NN or Opera
  while (typeof(o)!="undefined" && o!=null) {
    if (typeof(o.onmouseout)!="undefined" && o.onmouseout!=null) break;
    o = o.parentNode;
  }
  if (typeof(o)=="undefined" || o==null) return;
  if (typeof(o.id)=="undefined" || o.id==null) return;
  var pd = PDT[o.id];
  if (typeof(pd)=="undefined" || pd==null) return;
  pd.mOut();
}

function getElem(n) {
  if (typeof(n)=="string") return document.getElementById(n);
  return n;
}

function getOffsetLeft(e, r)
{
  var x = e.offsetLeft;
  var rp = null;
  if (r) rp = r; //.offsetParent;
  e=e.offsetParent;
  while (e && e!=rp) { x += e.offsetLeft; e=e.offsetParent; }
  return x;
}

function getOffsetTop(e, r)
{
  var y = e.offsetTop;
  var rp = null;
  if (r) rp = r; //.offsetParent;
  e=e.offsetParent;
  while (e && e!=rp) { y += e.offsetTop; e=e.offsetParent; }
  return y;
}

// request img counter

var reqImg=new Array();
function requestImg(u) {
  var i=new Image();
  i.src=u;
  return reqImg.push(i);
}

function videoFLV(flvPath, width, height, preview) {
  if (!preview) preview='/video/flash/mfvideobg.jpg';
  flash('8', '/video/flash/flvplayer.swf', width, height+20, {'allowfullscreen':'true','FlashVars':'file='+flvPath+'&image='+preview+'&width='+width+'&height='+(height+20)+'&showicons=false&overstretch=true&showdownload=false&volume=100&type=flv'}, '<span style="background-color:yellow;padding:4px;">Для воспроизведения видео требуется Adobe Flash Player 8+</span>');
}

var defaultParams={'allowScriptAccess':'sameDomain','menu':'false','quality':'high','bgcolor':'#ffffff','__escript':'true'};

function flash7(swf, width, height, parms, noflashHtml) {flash('7', swf, width, height, parms, noflashHtml);}
function flash8(swf, width, height, parms, noflashHtml) {flash('8', swf, width, height, parms, noflashHtml);}

function flash(version, swf, width, height, parms, noflashHtml) {
  var fpv=getFlashPlayerVersion();
  if (noflashHtml && fpv==null) {
    document.write(noflashHtml);
  }
  else {
    var o='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+',0,0,0"'
      +' width="'+width+'" height="'+height+'" align="middle">'
      +'<param name="movie" value="'+swf+'" />';
    var e='<embed src="'+swf+'"  width="'+width+'" height="'+height+'" align="middle" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"';
    if (parms) {
      for(var p in parms) {
        o+='<param name="'+p+'" value="'+parms[p]+'" />';
        e+=' '+p+'="'+parms[p]+'"';
      }
    }
    for(var p in defaultParams) {
      if (!parms || !parms[p]) {
        o+='<param name="'+p+'" value="'+defaultParams[p]+'" />';
        e+=' '+p+'="'+defaultParams[p]+'"';
      }
    }
    e+='></embed>';
    o+=e+'</object>';
    document.write(o);
  }
}

function getFlashPlayerVersion() {
  var v=null;
  if(navigator.plugins && navigator.mimeTypes.length){
    var x = navigator.plugins["Shockwave Flash"];
    if(x && x.description) {
      v=x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
    }
  } else if (navigator.userAgent && navigator.userAgent.indexOf("Windows CE") >= 0){ // if Windows CE
    var axo = 1;
    var counter = 3;
    while(axo) {
      try {
        counter++;
        axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+ counter);
        v=[counter,0,0];
      } catch (e) {
        axo = null;
      }
    }
  } else { // Win IE (non mobile)
    try{
      var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
    }catch(e){
      try {
        var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
          v = [6,0,21];
          axo.AllowScriptAccess = "always"; // error if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
      } catch(e) {
        if (v[0] == 6) {
          return v==null? "none" : v[0]+"."+v[1]+"."+v[2];
        }
      }
      try {
        axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
      } catch(e) {}
    }
    if (axo != null) {
      v = axo.GetVariable("$version").split(" ")[1].split(",");
    }
  }
  return v==null? null : v[0]+"."+v[1]+"."+v[2];
}
