

var IE = document.all ? true : false;

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* вывод ошибок */
function errorsInfo(preName, errors)
{
    var erlen = errors.length;


    var divErrors = document.getElementById(preName+"Errors");
    divErrors.innerHTML = '';
    divErrors.style.display = '';

    for (var i=0; i<erlen; i++)
    {

       var tmpInp = document.getElementById(errors[i].inputName);
       tmpInp.style.borderColor = "red";

       tmpInp.onfocus  = function () {
            this.style.borderColor = '';
            var divErrors = document.getElementById(preName+"Errors");
            divErrors.innerHTML = '';
            divErrors.style.display = 'none';
       };

       var erText = document.createTextNode(errors[i].label + " : " + errors[i].message);

       var erLi = document.createElement("li");
       erLi.appendChild(erText);
       divErrors.appendChild(erLi);

    }

}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* отослать форму по нажатию Enter */
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* отослать форму заявки */
function submitForm(formName)
{
    if (!validateForm($(formName))) return false;

    closeOrderForm();
    var serializedForm = Form.serialize(formName);
    var url = '/appl.ajax.php';
    var pars = '';
    pars += 'lang='+cmflang+'&';
    var d = new Date();
    pars += 'date='+d.getTime()+'&';
    pars += serializedForm;

    var myAjax = new Ajax.Request(
        url,
        {
            method: 'get',
            parameters: pars,
            onComplete: alertRes
        });

}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* вывод результата */
function alertRes(res)
{
    var resp = res.responseText;
    alert(resp);
}


/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* отослать форму входа */
function sendEnterForm(formName)
{
    closeEnterForm();
    $(formName).submit();
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* показать добавить новую запись (? наверное )*/
function showAddNew(obj, sId)
{
    var t = 'mess_'+sId;
    var n = 'new_'+sId;
    if ($(n).getStyle('display') == 'none')
    {
        if ($(t)) $(t).setStyle({'display': ''});
        $(n).setStyle({'display': ''});
    }
    else
    {
        if ($(t)) $(t).setStyle({'display': 'none'});
        $(n).setStyle({'display': 'none'});
    }
//    alert($(obj).getStyle('color'));
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* не находится над определенными объектами */
function notIn(evt)
{
    var notIn = new Array();
    notIn[notIn.length] = 'btnorder';
    notIn[notIn.length] = 'phnum';
    notIn[notIn.length] = 'entertable';
    notIn[notIn.length] = 'logo';
    var tempX = 0;
    var tempY = 0;
    if (IE) {
		tempX = event.clientX;
		tempY = event.clientY;
	} else {
		tempX = evt.pageX;
		tempY = evt.pageY;
	}

    for (var i=0; i < notIn.length; i++)
    {
        if ($(notIn[i]) && Position.within($(notIn[i]), tempX, tempY))
        {
            return true;
        }
    }
    return false;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* находиться ТОЛЬКО над определенными объектами */
function inOnly(evt)
{
    var inOnly = new Array();
    inOnly[inOnly.length] = 'aimg_yach';
    inOnly[inOnly.length] = 'aimg_eqip';
    inOnly[inOnly.length] = 'aimg_model';
    inOnly[inOnly.length] = 'aimg_cisern';
    inOnly[inOnly.length] = 'aimg_tech';


    inOnly[inOnly.length] = 'title_yach';
    inOnly[inOnly.length] = 'title_eqip';
    inOnly[inOnly.length] = 'title_model';
    inOnly[inOnly.length] = 'title_cisern';
    inOnly[inOnly.length] = 'title_tech';

    var tempX = 0;
    var tempY = 0;
    if (IE) {
		tempX = event.clientX;
		tempY = event.clientY;

    	tempX = tempX - parseInt(document.body.scrollLeft);
    	tempY = tempY - parseInt(document.body.scrollTop);

	} else {
		tempX = evt.pageX;
		tempY = evt.pageY;
	}

	var c = 0;
    for (var i=0; i < inOnly.length; i++)
    {
        if ($(inOnly[i]) && Position.within($(inOnly[i]), tempX, tempY))
        {
            c++;
        }
    }

    return (c > 0);
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* скроллирование центральной части*/
function scrollCenter(evt)
{
    var leftB = 40;
    var rightB = parseInt(document.body.clientWidth)/2;
    var leftB_2 = parseInt(document.body.clientWidth)/2;
    var screenX = Event.pointerX(evt)-document.body.scrollLeft;
    var screenY = Event.pointerY(evt)-document.body.scrollTop;
    var clWidth = document.body.clientWidth;
    var boundX = clWidth - screenX;

    var endFD = -40;
    var endOD = 0;
    var endFI = -40;
    $('info').innerHTML = '';
    if (screenX <= leftB_2)
    {
        var cursor = Event.pointerX(evt);
        var cursor_p  = (100 - 100/leftB_2 * cursor) * 2;
        var ofs = Math.round(cursor_p * 40/100);
        ofs = (ofs > 40) ? 40 : ofs;
        var endFD = -40 + ofs;
        var endFI = -40 + ofs;
    }
    else
    {
        var endFD = -1*leftB;
    }

    if (screenX > clWidth - rightB&& clWidth <1220)
    {
        var r1 = rightB - (clWidth - screenX);
        var p1 = r1 * 100 / rightB;

        var o1 = p1 * (1220 - clWidth)/100;
        o1 = (o1 < 0) ? 0 : o1;
        var endOD = Math.round(o1);
        var endFI = -1*leftB - o1;
    }
    else
    {
        var endOD = 0;
    }

    if (screenX > leftB_2 && clWidth - screenX > rightB )
    {
        var endFI = -1*leftB;
    }

    if ($('hideall').getStyle('display') != 'none')
    {
        var endFD = -40;
        var endOD = 0;
        var endFI = -40;
    }
    $('floatDiv').setStyle({'marginLeft':endFD+'px'});
    $('floatDivImg').setStyle({'marginLeft':endFI+'px'});
    $('outerDiv').scrollLeft = endOD;

}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* предзагрузка картинок */
function preloadImages ()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function makePreload(respText)
{
    for (var i=0; i<respText.length; i++)
    {
        if (respText[i].sml != null)
            preloadImages('/images/photo/' + respText[i].sml, '/images/photo/' + respText[i].src);
    }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* показать окно ввода */
function showEnter(obj)
{
    $('hideall').setStyle({'display': ''});
    $('hideall').setStyle({'height': document.body.clientHeight});
    $('hideall').setStyle({'width': document.body.clientWidth});
    $('diventer').setStyle({'display': ''});
    $('diventer').setStyle({'left': Element.cumulativeOffset(obj)[0] + obj.offsetWidth - $('diventer').offsetWidth });
    $('diventer').setStyle({'top': Element.cumulativeOffset($('btnorder'))[1]});
    return false;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* показать окно заявки*/
function showOrder(obj)
{

    $('hideall').setStyle({'display': ''});
    $('hideall').setStyle({'height': document.body.clientHeight});
    $('hideall').setStyle({'width': document.body.clientWidth});
    $('divorder').setStyle({'display': ''});
    $('divorder').setStyle({'left': Element.cumulativeOffset(obj)[0] + obj.offsetWidth - $('divorder').offsetWidth});
    $('divorder').setStyle({'top': Element.cumulativeOffset(obj)[1]});
    return false;

}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* валидация формы */
function validateForm(formObj)
{
    var inputs = formObj.elements;
    var ilen = inputs.length;

    var vfErrors = new Array();

    for (var i=0; i<ilen-1; i++)
    {


        var input = inputs[i];
        var inputName = inputs[i].id;
        var inputValue = inputs[i].value;
        var rules = input.getAttribute("rules");
        var label = input.getAttribute("title");

        if (!rules) { continue; }
        eval("rules= ({"+rules+"})");
        for (var prop in rules)
        {
            switch (prop)
            {
                case 'required':
                    if (inputValue.length < 1)
                    {
                        vfErrors[vfErrors.length] = {label:label, inputName:inputName, message:'Поле обязательно для заполнения'};
                    }
                    break;
                case 'maxlenght':
                    if (inputValue.length > rules[prop])
                    {
                        vfErrors[vfErrors.length] = {label:label, inputName:inputName, message:'Длина поля не должна быть более '+rules[prop]+' символов'};
                    }
                    break;
                case 'regexp':
                    if (!inputValue.match(/^\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}\b$/))
                    {
                        vfErrors[vfErrors.length] = {label:label, inputName:inputName, message:'Введен некорректный email адрес'};
                    }
                    break;
                case 'text':
                    // пока не обрабатывается
                    break;
                default:
                    // пока не обрабатывается
            }
        }
    }


    if (vfErrors.length > 0)
    {
        errorsInfo(formObj.id, vfErrors);
        return false;
    }

    return true;

}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* проверка сдвига - видимо для первой страницы */
function checkOffset()
{
    if ($('hideall').getStyle('display') != 'none')
    {
        var obj = $('btnorder');
        $('divorder').setStyle({'left': Element.cumulativeOffset(obj)[0] + obj.offsetWidth - $('divorder').offsetWidth});
        $('divorder').setStyle({'top': Element.cumulativeOffset(obj)[1]});

        obj = $('btnenter');
        $('diventer').setStyle({'left': Element.cumulativeOffset(obj)[0] + obj.offsetWidth - $('diventer').offsetWidth + 8});
        $('diventer').setStyle({'top': Element.cumulativeOffset($('btnorder'))[1]});
    }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* закрыть форму заявки  */
function closeOrderForm()
{
    $('hideall').setStyle({'display': 'none'});
    $('divorder').setStyle({'display': 'none'});
    return false;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* закрыть форму ввода */
function closeEnterForm()
{
    $('hideall').setStyle({'display': 'none'});
    $('diventer').setStyle({'display': 'none'});
    return false;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* смена класса (?) */
/*
function chBG(obj, imgCl)
{
    $(obj).className = imgCl;
}
*/


/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* определение нахождения точки в полигоне */
function pointInPoly(areaid, x, y)
{
    var poly_1 = eval('['+$(areaid).coords+']');
    var xp = new Array();
    var yp = new Array();
    var c = 0;
    for (var i=0; i<poly_1.length;i += 2)
    {
        xp[c] = poly_1[i];
        yp[c] = poly_1[i+1];
        c++;
    }

    var c = 0;
    var j = xp.length-1;
    for (var i=0; i<xp.length; j = i++)
    {
        if ((((yp[i]<=y) && (y<yp[j])) || ((yp[j]<=y) && (y<yp[i]))) &&
           (x > (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
           c = !c;
    }
    return c;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* смена картинки раздела */
function chImage(evt)
{
    if (notIn(evt)) return false;
    var screenX = Event.pointerX(evt);
    var screenY = Event.pointerY(evt);
    var activeImages = new Array();
    var imgs = $('imgarea').getElementsByTagName('img');

    var x = 0;
    var y = 0;
    var objMaxZIndex = 0;
    var img_length = imgs.length;
    for (var i=0; i<img_length; i++)
    {
        if (imgs[i].id.match(/^aimg_\w+/) && Position.within(imgs[i], screenX, screenY))
        {
            var uid = imgs[i].id.replace(/^aimg_(\w+)$/g, "$1");
            x = screenX - Position.cumulativeOffset(imgs[i])[0];
            y = screenY - Position.cumulativeOffset(imgs[i])[1];
            if (pointInPoly('poly_'+uid, x, y))
            {

                if (parseInt($(imgs[i]).getStyle('zIndex')) >= objMaxZIndex)
                {
                    objMaxZIndex = parseInt($(imgs[i]).getStyle('zIndex'));
                }

            }
        }
    }
    for (var i=0; i<img_length; i++)
    {
        if (!$(imgs[i]).className.match(/\w+_active$/))
        {
            if ($(imgs[i]).getStyle('zIndex') == objMaxZIndex)
            {
                $(imgs[i]).className = $(imgs[i]).className.replace(/^(\w+)_\w+$/g, "$1_hover");
                currentSection = $(imgs[i]).className.replace(/^(\w+)_\w+$/g, "$1");
            }
            else
            {
                $(imgs[i]).className = $(imgs[i]).className.replace(/^(\w+)_\w+$/g, "$1_inactive");
            }
        }
    }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* сдвиг машины на нужное положение */
function alignCar()
{

    if (aSec)
    {
        var curObjX = Element.cumulativeOffset($('aimg_'+aSec))[0] - 52 - $('poly_'+aSec).alt;
    //    $('car').setStyle({'marginLeft':curObjX+'px'});
        if (curObjX + $('car').offsetWidth > window.screen.width - 10)
        {
            curObjX = parseInt(window.screen.width) - parseInt($('car').offsetWidth);
        }
        curObjX = curObjX - 60;
        curObjX = curObjX + "px";
//        alert(curObjX);
        jQuery("#car").animate({marginLeft: curObjX},600);
    }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
/* получить данные по разделу */
function getData(dataUID)
{
    currentSection = dataUID;
    switch (dataUID)
    {
        case 'yach':
            dataUID = 'yachta';
            break;
        case 'tech':
            dataUID = 'technical';
            break;
        case 'cisern':
            dataUID = 'emkost';
            break;
        case 'eqip':
            dataUID = 'equip';
            break;
        case 'model':
            dataUID = 'module';
            break;
    }
    var url = '/services2.ajax.php';
    var pars = '';
    pars += 'action='+'getpage'+'&';
    pars += 'page='+'spage'+'&';
    pars += 'uid='+dataUID+'&';
    pars += 'lang='+cmflang+'&';
    var myAjax = new Ajax.Request(
        url,
        {
            method: 'get',
            parameters: pars,
            onComplete: function (res){
                $('mainContent').innerHTML = res.responseText;

                jQuery('#mainContent').html(jQuery('#mainContent').html() + '<img src="/i/spacer.gif" border="0" alt="" width="1" height="10" id="pdot" />'); // <table border="0" style="height:190px;width:10px "><tr><td></td></tr></table>
                var offset = jQuery("#pdot").offset()
                jQuery("#portcont_2").css("top", offset.top);
                jQuery("#portcont_2").css("left", 400);
                jQuery("#portcont_2").css("display", "");
            }
        });

    var urlPort = '/port2.ajax.php';
    var parsPort = '';
    parsPort += 'uid='+dataUID+'&';
    parsPort += 'lang='+cmflang+'&';
    var myAjaxPort = new Ajax.Request(
        urlPort,
        {
            method: 'get',
            parameters: parsPort,
            onComplete: function (res){
                respText = res.responseText;
                respText = eval(respText);
                if (respText.length < 1)
                {
                    return true;

                }


                jQuery("#containerTable td:first").css({width: "518px", backgroundImage: "url(/images/photo/"+respText[0].src+")", backgroundPosition: "center center", backgroundRepeat: "no-repeat"});

                jQuery("#text1").html(respText[0].name);
                jQuery("#text2").html(respText[0].name2);

                jQuery(".thumb").removeClass("opacity");

                jQuery("#pimg_"+"1").addClass("opacity");

                currentThumbId = 1;

                jQuery(".ptop img").unbind("click")
                jQuery(".pleft img").unbind("click")

                jQuery(".ptop img").removeClass("thumb");
                jQuery(".pleft img").removeClass("thumb");

                jQuery(".ptop img").attr("src","/images/photo/spacer.gif");
                jQuery(".pleft img").attr("src","/images/photo/spacer.gif");

                jQuery(".ptop img").attr("bigimg","");
                jQuery(".pleft img").attr("bigimg","");

//                jQuery(".thumb").click(function(){return false;});


                var  minD = (respText.length+1 >  10)?10:respText.length+1;
                for (var i=1; i<minD ; i++)
                {
                  if (respText[i-1].sml == null) continue;
                  jQuery("#pimg_"+i).attr("src", '/images/photo/'+respText[i-1].sml);
                  jQuery("#pimg_"+i).attr("bigimg", '/images/photo/'+respText[i-1].src);
                  if (respText[i-1].src != 'spacer.gif')
                  {
                    jQuery("#pimg_"+i).addClass("thumb");
                    jQuery("#portcon").css("display","");
                  }
                }

                makePreload(respText);

                jQuery(".thumb").click(clickThumb);

            }
        });
}



/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
var current = 0;
var respText = '';
function loadimgs()
{

    var arr = eval('{'+respText+'}');
    var curImg = '';
    $('portcon').setStyle({'display':'none'});
    $('portcon_m').setStyle({'display':'none'});
    for (var i=0; i<arr.length; i++)
    {
        var imgname = 'pimg_'+(i+1);
        if (!$(imgname))
            continue;
        if (arr[i]['sml'] != 'spacer.gif')
        {
            $('portcon').setStyle({'display':''});
            $('portcon_m').setStyle({'display':''});
        }
        $(imgname).title = arr[i]['name'];
        $(imgname).alt = arr[i]['src'];
        $(imgname).src = '/images/photo/' + arr[i]['sml'];
        preloadImages(arr[i]['src']);

        if (i<=2)
        {
            if (arr[i]['start'] == "1")
            {
                $(imgname).parentNode.className += ' '+'cur_lb';
            }
            if (arr[i]['end'] == "1")
            {
                $(imgname).parentNode.className += ' '+'cur_lt';
            }
            if (arr[i]['start'] == "1" && arr[i]['end'] == "1")
            {
                $(imgname).parentNode.className += ' '+'cur_12';
            }
        }
        if (i>=2)
        {
            if (arr[i]['start'] == "1")
            {
                $(imgname).parentNode.className += ' '+'cur_tr';
            }
            if (arr[i]['end'] == "1")
            {
                $(imgname).parentNode.className += ' '+'cur_tl';
            }
            if (arr[i]['start'] == "1" && arr[i]['end'] == "1")
            {
                $(imgname).parentNode.className += ' '+'cur_34';
            }
        }
        if (i == 2 && arr[i]['start'] == "1" && arr[i]['end'] == "1")
        {
                $(imgname).parentNode.className += ' '+'cur_13';
        }

    }

    if (current < arr[0].current)
    {
        $('td1').style.background = "url(/images/photo/"+arr[current]['src']+") no-repeat center center";
        $('td2').style.background = "url(/images/photo/"+arr[0]['src']+") no-repeat center center";
        jQuery('#containerTable').animate({marginLeft:'-518px'},500);
    }
    else
    {
        $('td2').style.background = "url(/images/photo/"+arr[current]['src']+") no-repeat center center";
        $('td1').style.background = "url(/images/photo/"+arr[0]['src']+") no-repeat center center";
        jQuery('#containerTable').animate({marginLeft:'0px'},500);
    }

}

function getPort(res)
{
    var resp = res.responseText;
    respText = resp;
    alert(respText);
}


/* функция изменения раздела */
function changeSection(evt)
{
    var ni = notIn(evt);
    var iO = inOnly(evt);

    if (ni) return false;
    if (!iO) return false;
    if (currentSection == '') return false;
    var imgs = $('imgarea').getElementsByTagName('img');
    var img_length = imgs.length;

    var tdTitles = $('mserv').getElementsByTagName('td');
    var imgTitles = $('mserv').getElementsByTagName('img');

    for (var i=0; i<img_length; i++)
    {
        var zIndex = parseInt($(imgs[i]).getStyle('z-index'));
        $(tdTitles[i]).setStyle({'background':''});
        $(imgTitles[i]).className = $(imgTitles[i]).className.replace(/^(\w+)_\w+$/g, "$1_inactive");
//        alert("get data  -"+currentSection+" - "+$(imgs[i]).className.replace(/^(\w+)_\w+$/g, "$1"));
        if (currentSection == $(imgs[i]).className.replace(/^(\w+)_\w+$/g, "$1"))
        {
            $(imgs[i]).className = $(imgs[i]).className.replace(/^(\w+)_\w+$/g, "$1_active");
            if (zIndex < 100 )
            {
                $(imgs[i]).style.zIndex = zIndex + 100;
            }
            var curObjX = Element.cumulativeOffset($('aimg_'+currentSection))[0] - 52 - $('poly_'+currentSection).alt;

            if (curObjX + $('car').offsetWidth > document.body.clientWidth - 10)
            {
                curObjX = parseInt(document.body.clientWidth) - parseInt($('car').offsetWidth);
            }

            curObjX = curObjX - 60;

            curObjX = curObjX + "px";

            jQuery("#car").animate({marginLeft: curObjX}, 600);

//            alert("getData");
            getData(currentSection);

            aSec = currentSection;
        }
        else
        {

            $(imgs[i]).className = $(imgs[i]).className.replace(/^(\w+)_\w+$/g, "$1_inactive");
            if (zIndex > 100)
            {
                $(imgs[i]).style.zIndex = zIndex - 100;
            }
        }
    }

    var cTd = 'td_'+currentSection;
    var ciM = 'title_'+currentSection;

    $(cTd).style.backgroundColor = '#ffffff';
    $(ciM).className = $(ciM).className.replace(/^(\w+)_\w+$/g, "$1_active");
}

var currentThumbId = 1;
jQuery(document).ready(function(){


   jQuery(".thumb[@src='/images/photo/spacer.gif']").removeClass("thumb");

   jQuery(".thumb[@id='pimg_"+currentThumbId+"']").addClass("opacity");

   jQuery(".thumb").click(clickThumb);

   jQuery(".goLeft").click(function(){
       var offset = parseInt(currentThumbId) - 1;
       if (offset < 1) offset = 1;
       jQuery("#pimg_"+offset).click();
   });

   jQuery(".goRight").click(function(){
       var offset = parseInt(currentThumbId) + 1;
       if (offset > 9) offset = 9;
       jQuery("#pimg_"+offset).click();
   });
   jQuery("#perevozki").tablesorter();

});

function clickThumb()
{
      jQuery(".thumb").removeClass("opacity");
      jQuery(this).addClass("opacity");
      var newThumbId = parseInt(jQuery(this).attr("id").replace(/pimg_(\d+)/g, "$1"));

      if (newThumbId == currentThumbId)
      {
      }

      if (newThumbId > currentThumbId)
      {
          var newTd = document.createElement("td");
          jQuery(newTd).css({width: "518px", backgroundImage: "url("+jQuery(this).attr("bigimg")+")", backgroundPosition: "center center", backgroundRepeat: "no-repeat"});
          jQuery("#containerTable").css({width: "1036px", marginLeft:"0px"});
          jQuery("#containerTable tr:first").append(newTd);
          jQuery("#containerTable").animate({marginLeft:"-518px"}, 600, function(){jQuery("#containerTable td:first").remove(), jQuery("#containerTable").css({width: "518px", marginLeft:"0px"}); } );
      }

      if (newThumbId < currentThumbId)
      {
          var newTd = document.createElement("td");
          var bgNew = jQuery("#containerTable td:first").css("background-image");
          jQuery(newTd).css({width: "518px", backgroundImage: bgNew, backgroundPosition: "center center", backgroundRepeat: "no-repeat"});
          jQuery("#containerTable").css({width: "1036px", marginLeft:"-518px"});
          var bg2 = "url('"+jQuery(this).attr("bigimg")+"')";
          jQuery("#containerTable td:first").css({backgroundImage: bg2});
          jQuery("#containerTable tr:first").append(newTd);
          jQuery("#containerTable").animate({marginLeft:"0px"}, 600 , function(){jQuery("#containerTable td:last").remove(),  jQuery("#containerTable").css({width: "518px", marginLeft:"0px"}); } );
      }


      currentThumbId = newThumbId;
      if (respText.length > 9)
      {
          if (newThumbId == 9)
          {

              var c = 1;
              var get = false;
              var newPicArray = new Array();
              for (var i=0; i<respText.length; i++)
              {

                  if (jQuery("#pimg_9").attr("src") == '/images/photo/'+respText[i].sml)
                  {
                     get = true;
                     newPicArray[newPicArray.length] = respText[i];
                     continue;
                  }
                  if (get && c < 9)
                  {
                      newPicArray[newPicArray.length] = respText[i];
                      c++;
                  }
              }
              var c1 = 0;
              while (newPicArray.length < 9)
              {
                  newPicArray[newPicArray.length] = respText[c1];
                  c1++;
              }
              for(var i=1; i<10; i++)
              {
                  jQuery("#pimg_"+i).attr("src", '/images/photo/'+newPicArray[i-1].sml);
                  jQuery("#pimg_"+i).attr("bigimg", '/images/photo/'+newPicArray[i-1].src);
              }
              jQuery(".thumb").removeClass("opacity");
              jQuery("#pimg_"+"1").addClass("opacity");

              currentThumbId = 1;
          }

          if (newThumbId == 1)
          {
              var get = false;
              var newPicArray = new Array();

              var indexOfLast = false;
              for (var i=respText.length-1; i >= 0; i--)
              {

                  if (jQuery("#pimg_1").attr("src") == '/images/photo/'+respText[i].sml)
                  {
                     indexOfLast = i;
                     break;
                  }
              }


              for (i=indexOfLast-8; i<=indexOfLast; i++)
              {
                  newPicArray[newPicArray.length] = respText[i >= 0 ? i : respText.length + i];
              }


              for (i=0; i<newPicArray.length; i++)
              {
                  var e = i+1;
                  jQuery("#pimg_"+e).attr({
                    "src":      '/images/photo/'+newPicArray[i].sml,
                    "bigimg":   '/images/photo/'+newPicArray[i].src
                  });
              }

              jQuery(".thumb").removeClass("opacity");
              jQuery("#pimg_"+"9").addClass("opacity");

              currentThumbId = 9;
          }
      }

      for (var i=0; i<respText.length; i++)
      {

          if (jQuery("#pimg_" + newThumbId).attr("src") == '/images/photo/'+respText[i].sml)
          {
            jQuery("#text1").html(respText[i].name);
            jQuery("#text2").html(respText[i].name2);
          }
      }
//      alert(this.id);
   }