
var oNewWin;
var _images=100;
var _shemes=20;
var __img='';
var __s_img='';

//for IE 
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

function features_to_int(elem)
{
    
    var res=0;
    if(elem){
    for(var i=0;i<elem.length;i++){
        if(elem[i].checked){
            res |=elem[i].value;
        }
    } 
    return res;
    }else
    {
    return 0;
    }
}

function filterSubmit(fmfilter){
    var query=[];
    var item=[];
    var querystring='';
    var features=features_to_int(fmfilter.features);
    $.each($('#fmfilter').serialize().split('&'),function(i,val){
                        item=this.split('=');            
                        if((item[1]!=0)&&(item[1]!='')&&(item[0]!='features')){
                        query.push(this); 
                       }                      
                    }
    );
    if(features){ 
            query.push('f_features='+features);
        }                   
    if(query.length){
            querystring='?usedfilter=on&'+query.join('&');
    }
    var pagetest=/(page\/)\d+/;
    document.location =fmfilter.action.replace(pagetest,'')+querystring;  
}

function filter_binding(){
    $('#fmfilter :input').change(function(e){
      on_filter_change(this);
    }
    );
    $('#fmfilter :checkbox').click(function(e){
      on_filter_change(this);
    }
    ); 
    $('#fmfilter :text').keyup(function(e){
      on_filter_change(this);
    }
    ); 
    
}

function on_filter_change(e){

if (e.checked){
 $(e).parent().addClass('f_highlight'); 
}else
 if ((e.value!=0)&&(e.value!='')&&(e.value!='on')){
 //alert(e.value);
    $(e).addClass('f_highlight') ;
 }else {
    $(e).removeClass('f_highlight'); 
    $(e).parent().removeClass('f_highlight');
 };
 }


function filter_prepare(){
$('#fmfilter :input:not(:button):not(:submit)').each(function () {
    
        on_filter_change(this); 
     
      });
} 
 
function getScrollY() {
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop ;
}
function getScrollX() {
return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft ;
}

function hidePopup(){
      var popUp = document.getElementById("popupcontent");
      popUp.style.visibility = "hidden";
}
     
function showPopup(w,h,title,text){
      var popUp = document.getElementById("popupcontent");
         sT=getScrollY();
        popUp.style.top = Math.ceil(sT+(screen.height-2*h)/2)+"px";
        
        popUp.style.left = Math.ceil((screen.width - w) / 2)+"px"; 
        popUp.style.width = w + "px";
        popUp.style.height = h + "px";
        var title='<div id="popup_title"><span>'+title+'</span></div>';
        var status='<div id="popup_statusbar"><button onclick="hidePopup();">Закрыть</button></div>';
        popUp.innerHTML=title+'<div id="popup_text"><span>'+text+'</span></div>'+status;
        popUp.style.visibility = "visible";
      }
function ShowImage(uri,alt){ 
if(!alt){alt='';}
  if(!oNewWin||(oNewWin&&oNewWin.closed)) {
  oNewWin=window.open("about:blank", "",
 "");
 }
 var windowHTML='<html><head><title></title></head><body><img  src='+uri+' alt="'+alt+'"></body></html>';

  oNewWin.document.write(windowHTML);
  oNewWin.document.title='';
  oNewWin.document.title="Просмотр избражения c "+oNewWin.location.host;
 oNewWin.document.close();
 oNewWin.focus();
}
function moreimages(fm){
       var id;
       var newdiv;
       if (_images>114){return;} ;
       id='images';
       newdiv=document.createElement('div');
       newdiv.innerHTML='<iframe frameborder="0" class="ifr_img" src="addimg.php?pos='+(_images++)+'&udata='+fm.udata.value+'"></iframe>';
       document.getElementById(id).appendChild(newdiv);
        }
 function moreshemes(fm){
       var id;
       var newdiv;
       if (_shemes>29){return;} ;
       id='shemes';
       newdiv=document.createElement('div');
       newdiv.innerHTML='<iframe frameborder="0" class="ifr_img" src="addimg.php?pos='+(_shemes++)+'&udata='+fm.udata.value+'"></iframe>';
       document.getElementById(id).appendChild(newdiv);
        }
 function changeimg(_id) {
	var vpath=document.getElementById("vpath").value;
	var arrayID=document.getElementById("varr").value.split("|");
	var arrayName=document.getElementById("varrn").value.split("|");
   with (document.getElementById("mainimg")){
   	src = vpath+"/cardmimg/"+arrayID[_id];
   	alt=arrayName[_id];
   	title=arrayName[_id];
   }
   document.getElementById("nameimg").innerHTML = arrayName[_id];
}

 function s_changeimg(_id) {
	var vpath=document.getElementById("vpath").value;
	var arrayID=document.getElementById("s_varr").value.split("|");
	var arrayName=document.getElementById("s_varrn").value.split("|");
   with (document.getElementById("s_mainimg")){
   	src = vpath+"/cardmimg/"+arrayID[_id];
   	alt=arrayName[_id];
   	title=arrayName[_id];
   }
   document.getElementById("s_nameimg").innerHTML = arrayName[_id];
}


 function changeimg_f(_id) {
    var vpath=document.getElementById("vpath").value;
    var arrayID=document.getElementById("varr").value.split("|");
    var arrayName=document.getElementById("varrn").value.split("|");
   var img=document.getElementById("mainimg"); 
   with (img){
        info=arrayID[_id].split(".");
        src = vpath+"/img/"+info[0]+'_m.'+info[1];
        alt=arrayName[_id];
        title=arrayName[_id];      
   }
    __img=vpath+"/img/"+info[0]+'_l.'+info[1]; 
   document.getElementById("nameimg").innerHTML = arrayName[_id];
}

 function s_changeimg_f(_id) {
    var vpath=document.getElementById("vpath").value;
    var arrayID=document.getElementById("s_varr").value.split("|");
    var arrayName=document.getElementById("s_varrn").value.split("|");
    var img=document.getElementById("s_mainimg"); 
   with (img){
        info=arrayID[_id].split(".");   
        src = vpath+"/img/"+info[0]+'_m.'+info[1];
        alt=arrayName[_id];
        title=arrayName[_id];    
   }
    __s_img=vpath+"/img/"+info[0]+'_l.'+info[1]; 
   document.getElementById("s_nameimg").innerHTML = arrayName[_id];
    
}

 function showmoreimages(_from)
   {
    var vpath=document.getElementById("vpath").value;
	var arrayID=document.getElementById("varr").value.split("|");
	var arrayName=document.getElementById("varrn").value.split("|");
   _from.style.display="none";
   	var imgcount=4;
   	var i=0;
    var tbr=document.createElement("tr");
    var el=document.getElementById("bcardimages");
    var tbd;

   	for(i=imgcount;i<arrayID.length;i++){

   	if (!(i%imgcount)){
   		tbr=document.createElement("tr");
      	el.appendChild(tbr);
   		}

      	tbd=document.createElement("td");
   		tbd.innerHTML="<img src="+vpath+"/cardsimg/"+arrayID[i]+" title=\'"+arrayName[i]+"\' alt=\'"+arrayName[i]+"\'onClick=changeimg("+i+")>";
   	 	tbr.appendChild(tbd);

   	}
   }

  function showmoreshemes(_from)
   {
    var vpath=document.getElementById("vpath").value;
    var arrayID=document.getElementById("s_varr").value.split("|");
    var arrayName=document.getElementById("s_varrn").value.split("|");
   _from.style.display="none";
       var imgcount=4;
       var i=0;
    var tbr=document.createElement("tr");
    var el=document.getElementById("s_bcardimages");
    var tbd;

       for(i=imgcount;i<arrayID.length;i++){

       if (!(i%imgcount)){
           tbr=document.createElement("tr");
          el.appendChild(tbr);
           }

          tbd=document.createElement("td");
           tbd.innerHTML="<img src="+vpath+"/cardsimg/"+arrayID[i]+" title=\'"+arrayName[i]+"\' alt=\'"+arrayName[i]+"\'onClick=s_changeimg("+i+")>";
            tbr.appendChild(tbd);

       }
   }
 
  function showmoreimages_f(_from)
   {
    var vpath=document.getElementById("vpath").value;
    var arrayID=document.getElementById("varr").value.split("|");
    var arrayName=document.getElementById("varrn").value.split("|");
   _from.style.display="none";
       var imgcount=4;
       var i=0;
    var tbr=document.createElement("tr");
    var el=document.getElementById("bcardimages");
    var tbd;

       for(i=imgcount;i<arrayID.length;i++){

       if (!(i%imgcount)){
           tbr=document.createElement("tr");
          el.appendChild(tbr);
           }

          tbd=document.createElement("td");
           info=arrayID[i].split(".");
           tbd.innerHTML="<img src="+vpath+"/img/"+info[0]+'_s.'+info[1]+" title=\'"+arrayName[i]+"\' alt=\'"+arrayName[i]+"\'onClick=changeimg_f("+i+")>";
            tbr.appendChild(tbd);

       }
   }
 function showmoreshemes_f(_from)
   {
    var vpath=document.getElementById("vpath").value;
	var arrayID=document.getElementById("s_varr").value.split("|");
	var arrayName=document.getElementById("s_varrn").value.split("|");
   _from.style.display="none";
   	var imgcount=4;
   	var i=0;
    var tbr=document.createElement("tr");
    var el=document.getElementById("s_bcardimages");
    var tbd;

   	for(i=imgcount;i<arrayID.length;i++){

   	if (!(i%imgcount)){
   		tbr=document.createElement("tr");
      	el.appendChild(tbr);
   		}
         info=arrayID[i].split(".");
          
      	tbd=document.createElement("td");
   		tbd.innerHTML="<img src="+vpath+"/img/"+info[0]+'_s.'+info[1]+" title=\'"+arrayName[i]+"\' alt=\'"+arrayName[i]+"\'onClick=s_changeimg_f("+i+")>";
   	 	tbr.appendChild(tbd);

   	}
   }


function eraseCookie(name) {

    set_cookie(name,"",-1);

}
function clear_array_cookies(element, index, array) {
try {
eraseCookie(element);
}
finally {return;}

}
function set_cookie(name, value, expires, domain, path , secure, reload){
if (expires != null) {
expires = new Date();
var SetTime = 1000*60*60*24*expires;
expires.setTime((expires.getTime() + SetTime));
}
expiresclear = new Date();
var fiveDay = 5*1000*60*60*24;
expiresclear.setTime((expiresclear.getTime() - fiveDay));
var expString = ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
var pathString = (((path==null)) ? "; path=/" : ("; path=" + path));
var domainString = ((domain == null) ? "" : ("; domain=" + domain));
var secureString = ((secure == true) ? "; secure" : "");
var expClearString = ((expires == null) ? "" : ("; expires=" + expiresclear.toGMTString()));
document.cookie = name + "=" + encodeURIComponent(value) + expString + pathString + domainString + secureString;
}


function SetCookies(name, value, expires, domain, path , secure, reload) {
    var cookie_to_clear; 
    set_cookie(name, value, expires, domain, path , secure, reload);
    
/*if ((name=='usedfilter') && (value=='off')) {
   cookie_to_clear=new Array(
 'rid',
 'tid',
 'did',
 'keywords',
 'days',
 'photo',
 'otype',
 'fvol[0]',
 'fvol[1]',
 'fvol[2]',
 'fvol[3]',
 'fvol[4]',
 'f_features',
 'sum_from',
 'sum_to',
 'f_rooms',
 'f_curr',
 'dist_from',
 'dist_to',
 'f_type_of_ob',
 'f_a_tran',
 'f_a_drive', 
 'f_a_eng',
 'f_a_body',
 'f_a_year_1',
 'f_a_year_0',
 'f_a_handler', 
 'f_a_place',
 'f_a_model',
 'f_a_vendor'
 );  
 
 cookie_to_clear.forEach(clear_array_cookies);
}  */
if (name=='cid') {
 cookie_to_clear=new Array('pid');
cookie_to_clear.forEach(clear_array_cookies);  

}
if (reload==1) {location.reload();}

if (typeof(indexpage) == 'undefined')
{
 if (name == 'cid')
  {
    location.reload();
  }
}     

}

function ClearFilter(){
	//SetCookies('usedfilter','off',null,null,'/',false,0);
}

function SetID(id,set) {

 var result="";
 var myCookie = " "+document.cookie+";";
 var startOfCookie = myCookie.indexOf(" select=");
 var endOfCookie;
 if (startOfCookie != -1) {
	startOfCookie += 8;
	endOfCookie = myCookie.indexOf(";",startOfCookie);
	result = unescape(myCookie.substring(startOfCookie,endOfCookie));
}
var arrCookie = result.split(',');
result="";  found=false;
for (i=0;i<arrCookie.length;i++) {
add=arrCookie[i]+",";
if (add==",") {add=""};

if (arrCookie[i]==id) {
 if (set) {found=true;
} else {add=""}

}

result=result+add;
}

if ((!found) & (set)) {
result=result+id+",";
}

document.cookie = "select=" + result;
}

function do_invisible(id){
     document.getElementById(id).style.display="none";

        }
function do_visible(id){
     document.getElementById(id).style.display="block";
        }
function _toggle(id){
if   (document.getElementById(id).style.display=="block"){

  document.getElementById(id).style.display="none"
} else {
	document.getElementById(id).style.display="block"}
}

function show_adv_search(elem){

 _toggle('adv_search');
 if (document.getElementById('adv_search').style.display=="block"){
 document.getElementById('i_adv_search').focus();
 }

}
function show_progress(elem){

var d= document.createElement('div');
d.className="progress";
d.innerHTML='ожидайте...';
elem.parentNode.appendChild(d);
elem.style.display="none";
}
