/**
 * On Ready Event Handler
 *
 * @author Anton Shevchuk
 */
(function($, window, undefined){
    $(document).ready(function(){
        /*Slider on Homepage*/
        if (typeof ($.fn.smoothDivScroll) != 'undefined') {
            if ($.browser.mozilla && $.cookie('autofinder_current_lang') != 'eng') {
                var direction = "backandforth";
            } else {
                var direction = "endlessloopright";
            }

            var language = $.cookie('autofinder_current_lang');
            
            // alert(language);

            var direction = 'endlessloopleft';
            //if(language == 'ar') {
            //    direction = 'endlessloopleft';
            //}
            
            $("div.slider").smoothDivScroll({
                autoScroll: "always",
                autoScrollDirection: direction,
                autoScrollStep: 1,
                autoScrollInterval: 10
            });

            $("div.slider").hover(function(){
                $("div.slider").smoothDivScroll("stopAutoScroll");
            },function(){
                $("div.slider").smoothDivScroll("startAutoScroll");
            });
        }

        /*Dealers tabs*/
        $(".top_dealers a.tab").click(function(){
            var $this = $(this);
            var $tab = $this.parent().find($this.attr("href"));

            $this.parent().find('.tab').removeClass('active');
            $this.addClass('active');

            $this.parent().find('.dealers').hide();
            $tab.show();
            return false;

        });
        // show/hide message from system
        $('#messages').slideDown(function() {
            setTimeout(function(){
                $('#messages').slideUp();
            }, 3200);
        });

        $("#categories").live('change', function() {
            var id = $("#categories option:selected").val();
            if(id) {
                $.ajax({
                    type: "POST",
                    url: "/index/show-makes/format/html/",
                    data: {'category_id': id},
                    success: function(html){
                        $('#makes').html(html);
                    }
                });
            }
        });

        // get models
        $("#makes").live('change', function() {
            var id = $("#makes option:selected").val();
            var c_id = $("#categories option:selected").val();
            if(id) {
                $.ajax({
                    type: "POST",
                    url: "/index/show-models/format/html/",
                    data: {'make_id': id, 'category_id': c_id},
                    success: function(html){
                        $('#models').html(html);
                    }
                });
            }
        });

    });
})(jQuery, window);
