var img=[],
img_big=[],
win=[],
links=[],
content=[],
begun=null,
db={
    a:function a(e,q){
        q='error - '+e.description+"\nfunction "+q;
        if(e.message)q='e.message - '+e.message+"\n"+q;
        alert(q+"\nsender db.a");
    },
    i:0,
    wd:retfalse,
    ws:retfalse,
    dbg:retfalse,
    err:function err(q){
        if(!sendAjax({
            'error':q
        }))
            alert(q+"\nsender db.err");
    }
},
mX,mY;
document.onmousemove = updatXY;
function retfalse(e){
    return false;
}
function updatXY(e){
    if (!e) var e = window.event;
    if (e.pageX || e.pageY){
        mX = e.pageX-18;
        mY = e.pageY;
    }
    else if (e.clientX || e.clientY){
        mX = e.clientX + document.body.scrollLeft+ document.documentElement.scrollLeft-18;
        mY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
}
function $(id){
    return document.getElementById(id);
}
function hide(val){
    if( val.style.display=='block' ) val.style.display='none';else val.style.display='block';
}
function size_frame(val){
    var val2=val.clientWidth;
    $('frame').style.width=val2+'px';
    val2=val.clientHeight;
    $('frame').style.height=val2+'px';
}
function big_image(val){
    if(typeof val=='undefined')val='template/default/nofoto.jpg';
    else {
        val='images/'+val;
        if(!/\.jpg$/.test(val))val+='_big.jpg';
    }
    var n='big_im',el,x,y;
    if($(n)==null)
        bodys(document.body,'<div class="big_img" id="big_im" onclick="this.style.display=\'none\';"></div>');
    el=$(n);
    x=mX-150;
    y=mY-200;
    if(y<0)y=0;
    if(x<0)x=0;
    el.style['left']=x+'px';
    el.style['top']=y+'px';
    el.style.display='block';
    el.style.backgroundImage='url('+val+')';    
}
function start(val,val2){$(val2).innerHTML=val;}
function search_get(){
    $('form_search').submit();
    $('search_cod').value='';
    $('selest_class').selectedIndex=0;
    $('category').selectedIndex=0;
}
function menu_get(val,val1){
    var val2=val;
    if(typeof val1=='undefined'){val1='';val='';}else val2='';
    $('search_cod').value=val2;
    $('selest_class').value=val;
    $('category').value=val1;
    $('form_search').submit();
    $('selest_class').selectedIndex=0;
    $('category').selectedIndex=0;
}
function cB(e){
    if(typeof e!="object")e=window.event;
    if (e.stopPropagation)e.stopPropagation();
    e.cancelBubble = true;
    return false
}
function bodys(el,v,fl){
    if(fl==6||fl==7){
        if(el.firstChild!=null){
            el=el.firstChild;
            fl=2;
            if(fl==7)fl=3;
        }
        else{
            delete fl;
            if(fl==7)fl=1;
        }
    }

    if(fl==4||fl==5){
        if(el.nextSibling!=null){
            el=el.nextSibling;
            fl=2;
            if(fl==5)fl=3;
        }
        else{
            el=el.parentNode;
            delete fl;
            if(fl==5)fl=1;
        }
    }

    var bd=$('bodys');
    bd.innerHTML=v;
    switch(fl){
        case 1:
            while(bd.lastChild)el.appendChild(bd.lastChild);
            break;
        case 2:
            while(bd.firstChild)el.parentNode.insertBefore(bd.firstChild,el);
            break;
        case 3:
            while(bd.lastChild)el.parentNode.insertBefore(bd.lastChild,el);
            break;
        default:
            while(bd.firstChild)el.appendChild(bd.firstChild);
            break;
    }
    bd.innerHTML='';
}
function getatributeStyle(cls,atr){
    try{
        var ot='';
        for(var i=document.styleSheets.length-1;i>=0;i--){
            if(typeof document.styleSheets[i].cssRules!='undefined')var el=document.styleSheets[i].cssRules;
            else el=document.styleSheets[i].rules;                
            for(var j=el.length-1;j>=0;j--){
                ot+=el[j].selectorText+"\n";
                if(el[j].selectorText!=cls)continue;
                return el[j].style[atr];
            }
        }
        return null;
    }catch(e){
        db.a(e,'getatributeStyle')
    }
}
function Begun(name,arr,id){
    var o=this;
    o.name=name;
    o.status=0;
    o.point=1;
    o.time=1;
    o.pause=1000;
    o.arr=arr;
    o.id=id;
    o.dlast=0;
    o.out=function out(){
        if(o.arr.length<1)return;
        o.itemw=parseInt(getatributeStyle('.o_begun_item','width'));
        var out='',i,l=Math.ceil((o.id.offsetWidth)/o.itemw)-1,
        ttl='Остановить показ';
        o.lw=l;
        do{
            i=o.lw-l;
            while(i>=o.arr.length)i-=o.arr.length;
            out+=o.item(i,o.lw-l);
            l--;
        }while(l>=0);
        o.last=i;
        if(o.status>0)ttl='Запустить показ';
        o.id.innerHTML='<div id="object.'+o.name+'.box" class="o_begun_box">'+out+'</div>'+
        '<div id="object.'+o.name+'.play" class="o_begun_play" onmousedown="'+o.name+'.play();" title="'+ttl+'"></div>';
        o.play();
    };
    o.play=function play(){
        o.status=!o.status;
        var el=$('object.'+o.name+'.play'),eli;
        el.style.backgroundPosition='2px 0px';
        el.title='Запустить показ';
        if(o.status==1){
            el.style.backgroundPosition='0px -40px';
            el.title='Остановить показ';
            if(o.intr==null){
                $('bodys').innerHTML=o.item(o.last,o.lw+1);
                el=$('object.'+o.name+'.item'+o.dlast);
                eli=$('object.'+o.name+'.item'+(o.lw+1));
                el.parentNode.insertBefore(eli,el);
                eli.style.left=el.offsetLeft+"px";
                o.intr=setInterval(o.move,o.time);
            }
        }
    };
    o.item=function item(i,l){
        return '<div style="left:'+(l*(o.itemw+2))+'px" id="object.'+o.name+'.item'+l+'" class="o_begun_item">'+
        '<a href="javascript:menu_get('+o.arr[i]['id']+');">'+
        '<div class="o_begun_img" style="background-image: url(images/'+o.arr[i]['id']+'_sml.jpg);"></div>'+'</a>'+
        '<div class="o_begun_price">'+o.arr[i]['price']+'</div>'+
        '<div class="o_begun_cod">'+o.arr[i]['id']+'</div>'+'</div>';
    };
    o.move=function move(){
        if(typeof o.itemwi=='undefined'||o.itemwi<0)o.itemwi=o.itemw;
        var id,
        elt=$('object.'+o.name+'.item'+(o.lw+1)),
        eli=$('object.'+o.name+'.item'+o.dlast);
        if(eli==null)return;
        eli.style.left=eli.offsetLeft+o.point+'px';
        o.itemwi-=o.point;
        if(o.itemwi<0){
            if(elt==null)return;
            id=eli.id;
            eli.parentNode.removeChild(eli);            
            elt.id=id;
            clearInterval(o.intr);
            o.intr=null;
            if(o.status==1){
                o.status=0;
                setTimeout(o.play,o.pause);
            }
            o.last++;
            if(o.last>=o.arr.length)o.last=0;
            o.dlast++;
            if(o.dlast>o.lw)o.dlast=0;
        }
    };
    o.out();
    window.onresize=function resize(){
        o.status=!o.status;
        clearInterval(o.intr);
        o.intr=null;
        o.dlast=0;
        o.out();
    }
};
Array.prototype.fin=function fin(v){
    for(var i in this)if(this[i]==v)return parseInt(i);
    return "empty";
};
Array.prototype.del=function del(v,a){
    var r=[];
    for(var i=0;i<this.length;i++){
        if (i==v){
            if(a>1){
                v++;
                a--
            };
            continue;
        }
        r.push(this[i]);
    }
    return r;
};
Array.prototype.get=function getArray(){
    var ot=[],a=this;
    for(var i=0;i<a.length;i+=2)ot[a[i]]=a[i+1];
    return ot
};

