function get_slider_count() {
    swidth = $('#slider').width();      
    return Math.round(swidth / 140);
}

function sliderResize() {
    if (exists('#slider')) {
        cw = document.body.clientWidth;
        div = $('#lineMenu').width();
        
        $('#slider').width(div - 455);
        
        //one_row_count = get_slider_count();
        //var width_of_one = $('#slider') / one_row_count;
        //$('#slider .tovarsPic1').width(width_of_one);
        //$("#slider").jCarouselLite({ visible: one_row_count });
    }
}

$(document).ready(function(){
    

    $(window).resize(function () {
        sliderResize();
    });

    $(".quantity").change(function(){
        url = location.href;
        limit = "limit="+$(this).val();
        if (url.indexOf('limit=') >= 0) {
            url = url.replace(/limit=\d+/, limit)
            location.href = url
        }
        else {
            if (url.indexOf('?') >= 0) limit = '&'+limit;
            else limit = '?'+limit;
            location.href += limit;
        }
    });

    if ($("#slider").length == 1) {
        
        var lisz = 0;
        var vc = get_slider_count();
        var lisz = $('#slider').width() / vc;
        var go_btn = []
        for (var i = 1; i<=products_new_count; i++) {
        	s = '#sliderNav .'+i;
        	go_btn.push(s);
        }

        $("#slider").jCarouselLite({
            btnNext: "#btnNext",
            btnPrev: "#btnPrev",
            visible: vc,
            liSize: lisz,
            liHeight: 182,
            auto: 10000,
            speed: 1000,
            btnGo: go_btn,

            beforeStart: function(a) {

            },
            afterEnd: function(a) {

                $("#sliderNav button").each(function() {
                    this.className = this.className.toString().replace(" sel", "");
                });
                if (a) {
                    var item = a[0].id.toString().replace("item", "");
                    sliderNavSelItem = item;
                    $("#sliderNav button." + item)[0].className += " sel";
                }
            }
        });


        $("#sliderNav button").focus(function(){
            $(this)[0].blur();
        });

        var sliderNavSelItem = "0";

        $("#sliderNav button").mouseover(function(){
            $(this)[0].className += " sel";
        });
        $("#sliderNav button").mouseout(function(){

            if ($(this)[0].className != sliderNavSelItem + " sel")
                $(this)[0].className = $(this)[0].className.toString().replace(" sel", "");
        });

        $("#sliderNav button").mousedown(function(){

            $("#sliderNav button").each(function() {
                this.className = this.className.toString().replace(" sel", "");
            });
            sliderNavSelItem = $(this)[0].className;
            $(this)[0].className += " sel";
        });
        $("#btnPrev").mouseover(function(){
            $(this)[0].className = "hov";
        });
        $("#btnPrev").mouseout(function(){
            $(this)[0].className = "";
        });

        $("#btnNext").mouseover(function(){
            $(this)[0].className = "hov";
        });
        $("#btnNext").mouseout(function(){
            $(this)[0].className = "";
        });
    }

    $("a.tovarsPicNext").click(function() {

        $(this).addClass("tpActive").next().children(":first-child").animate({top:"0px"}, 250).next().animate({top:"160px"}, 250);

        $(this).parent().bind('mouseleave',function() {
            $(this).children(":last-child").children(":first-child").animate({top:"-160px"}, 250).next().animate({top:"0px"}, 250).parent().prev().removeClass("tpActive");
        });

        return false;

    });


    
    $('#uploadfile').click(function (e) {
        e.preventDefault();
        $('#basic-modal-content3').modal({
        containerCss:{
            background:"url(../img/bg_grey.gif) left top repeat",
            height:200,
            padding:0,
            width:230
    },
        overlayClose:true});
    });

    $('input.formBrowseButton').click(function (e) {
        e.preventDefault();
        $('#basic-modal-content3').modal({
        containerCss:{
            background:"url(/img/bg_grey.gif) left top repeat",
            height:200,
            padding:0,
            width:230
    },
        overlayClose:true});
    });


    $('#send_review input[type=submit]').click(function(){
        $('#send_review div.review_auth input[name="auth"]').val('')
        v = $('#send_review input[name="security_hash"]').val()
        $('#send_review div.review_auth input[name="password"]').val(v)
    })
    
    $('div.SendError a').click(function (e) {
        e.preventDefault();
        $('#basic-modal-content2').modal({
            containerCss:{
                background:"url(/img/bg_grey.gif) left top repeat",
                height:200,
                padding:0,
                width:230
            },
                overlayClose:true
        });
    });

     $("input[type=file].file").filestyle({
         image: "/img/browse.jpg",
         imageheight : 22,
         imagewidth : 100,
         width : 0
    });

    $('input.SendButton').click(function (e) {
        $(this).parent().parent().ajaxForm({
            type : 'POST',
            dataType : 'json',
            success : function(){
                alert('s');
            }
        }).submit();
        return false;
    });
    
    $('.tovarindent img').click(function (e) {
        link = $(this).parent().parent();
        location.href = $(link).attr('href');
    });

    if ($("#findListActive").length == 1){
        $("#findListActive").sSelect();
    }
    //if ($(".findPole")[0].value != "������")
     //   $(".findPole").css("color", "#000");
    
    
});

function removeParent(event) {
    $(event.target).parent().remove();
    event.preventDefault();
};

// function MM_changeProp(objId,x,theProp,theValue) { //v9.0
//   var obj = null; with (document){ if (getElementById)
//   obj = getElementById(objId); }
//   if (obj){
//     if (theValue == true || theValue == false)
//       eval("obj.style."+theProp+"="+theValue);
//     else eval("obj.style."+theProp+"='"+theValue+"'");
//   }
// }

