function setFocus()
{
document.getElementById("BasketName").focus();
}

function AddToBasket(ID)
{
    $.ajax({
       type: "POST",
       dataType:"json",
       url: base_url+"f_ajax/basket/Add",
       data: ("Id="+ID),
       success: function(data){
            if(data.status=="OK")
            {
                 $('#BacketPrice').text(data.price);
                 $('#BacketNum').text(data.Num);                 
            } else {
				alert('data.status=' + data.status);
			}
         },
       error: function(){
        alert("Отсутствует соединение с сервером " + base_url+"f_ajax/basket/Add");  }

       });

	   return false;
}

function DeleteBuys(ID)
{
    $.ajax({
       type: "POST",
       dataType:"json",
       url: base_url+"f_ajax/basket/DeleteFromBasket",
       data: ("Id="+ID),
       success: function(data){
            if(data.status=="OK")
            {
                $('#tr'+ID).remove();
                $('#BacketPrice').text(data.price);
                $('#BacketNum').text(data.Num);
                $('#BasketSunnary').text(data.price+' руб.');
            }
         },
       error: function(){
        alert("Отсутствует соединение с сервером");}
       });

}

function RecountBuys()
{

    var Goods=new Array();
    $('#BasketTable input').each(function(){
            Goods[$(this).attr("name")]=($(this).attr("value"));
        });

    if(Goods.length==0)
        return;
    var serGoods=js_array_to_php_array(Goods);

    $.ajax({
       type: "POST",
       dataType:"json",
       url: base_url+"f_ajax/basket/RecountBuys",
       data: {'Goods':serGoods},
       success: function(data){
            if(data.status=="OK")
            {
                $('#BacketPrice').text(data.price);
                $('#BacketNum').text(data.Num);
                $('#BasketSunnary').text(data.price+' руб.');
            }
         },
        error: function(){
        alert("Отсутствует соединение с сервером");}
   /*error: function(XMLHttpRequest, textStatus, errorThrown){
                alert(XMLHttpRequest.responseText);}*/
       });

}




function js_array_to_php_array (a)
{
    var a_php = "";
    var total = 0;
    for (var key in a)
    {
        ++ total;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + String(a[key]) + "\";";
    }
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php;
}

$(document).ready(function(){
   $('#BasketForm').submit(function(){
        var Goods=new Array();
        $('#BasketTable input').each(function(){
                Goods[$(this).attr("name")]=($(this).attr("value"));
            });

        if(Goods.length==0)
            return false;
        var serGoods=js_array_to_php_array(Goods);

        $.ajax({
           type: "POST",
           dataType:"json",
           url: base_url+"f_ajax/basket/Submit",
           data: {'Goods':serGoods,'name':$('[name=name]').attr("value"),
                'e_mail':$('[name=e_mail]').attr("value"),
                'phone':$('[name=phone]').attr("value"),
                'mess':$('[name=mess]').attr("value"),
                'captcha':$('[name=captcha]').attr("value")},
           success: function(data){
                if(data.status=="OK")
                {
                    $('#BacketPrice').text('0');
                    $('#BacketNum').text('0');
                    $('#BasketPage').html('<B>Ваш заказ отправлен.</B>');
                }
                else
                {
                    $('#ErrorMsg').text('Символы, представленные на картинке, введены неправильно!');
                    document.images.cryptogram.src=base_url+'crypt/cryptographp.php?&'+Math.round(Math.random(0)*1000)+1;
                }
             },
            error: function(){
            alert("Отсутствует соединение с сервером");}

           });


       return false;
   });
    
   $('#FaqForm').ajaxForm({
        dataType:  'json',
        url: base_url+"f_ajax/Faq/Submit",
        beforeSubmit:function(){
            hs.close();
	},
           success: function(data){
                $('#FaqMsg').text('Ваш вопрос отправлен');
             },
	error: function(){
           alert("Отсутствует соединение с сервером");
	}
    });



});

function SetPhoto(Src)
{
    var arrayPageSize = ___getPageSize();
    var arrayPageScroll = ___getPageScroll();
    if(!ImgBoxOpened)
    {
        $('#Mainfoto').attr('src', Src);
        $('#LinkToShow').attr('href', "javascript:ShowBigPhoto('"+Src+"')");

    }
    else
    {
        $('#Imgbox>img').attr('src', Src);
        var newImg = new Image();
        newImg.src=Src;

    var offset=0;
    if((arrayPageSize[3] -newImg.height)/2<130)
        offset=130;

        $('#Imgbox-wrap').css({
                display: 'block',
            top:	arrayPageScroll[1] + (arrayPageSize[3] -newImg.height)/2+offset,
            left:	arrayPageScroll[0]
        });

        $('#Imgbox').css({
                width: newImg.width+10,
                height: newImg.height+20
        });


    }

    




}


var ImgBoxOpened=0;
function ShowBigPhoto(Src)
{
    ImgBoxOpened=1;
    var arrayPageSize = ___getPageSize();
    var arrayPageScroll = ___getPageScroll();

    $('#Imgoverlay').css({
            opacity:		0.8,
            width:		arrayPageSize[0],
            height:		arrayPageSize[1]
    });

     $('#Imgoverlay').fadeIn();

    var newImg = new Image();
    newImg.src=Src;

    var offset=0;
    if((arrayPageSize[3] -newImg.height)/2<130)
        offset=130;
    $('#Imgbox-wrap').css({
            display: 'block',
        top:	arrayPageScroll[1] + (arrayPageSize[3] -newImg.height)/2+offset,
        left:	arrayPageScroll[0]
    });

    $('#PlaceToCarousel-wrap').css({
        display: 'block',
        top:	arrayPageScroll[1] + 10,
        left:	arrayPageScroll[0]
    });

    $('#Imgbox>img').attr('src', Src);


    $('#Imgbox').css({
            width: newImg.width+10,
            height: newImg.height+20
    });




//alert('1');
//alert(newImg.width);


}


function BigImgClose()
{
    $('#Imgoverlay').hide();
    $('#Imgbox-wrap').hide();
    $('#PlaceToCarousel-wrap').hide();
    ImgBoxOpened=0;
}

// menu hide / show
function showSubCat(subID){		
	$("#subcats-" + subID).toggle();		
}
