$(function() {
    /* slider */
    $("#main_slider .scroll_pane").scrollable({
        items: '.scroll_items',
        circular: true,
        onSeek: function (obj, index){
            var cSlide = $("#main_slider .scroll_item:not(.cloned)").get(index);
            $(cSlide).find('div.scroll_navi_inside').show('slow');
        }
    }).autoscroll({ 
        interval: 7000,
        autopause: true
    }).navigator({
        navi: $("div.slider_menu ul"),
        naviItem: 'a',
        history: true
    });
    
    var apiMainSlider = $("#main_slider .scroll_pane").data('scrollable');

    $("div.slider_menu ul a").click(function (){
        $("div.slider_menu ul a").removeClass('active');
        $(this).addClass('active');
        $("#main_slider .scroll_navi_inside").hide();
    });
    
    $("div.slider_menu").hoverIntent({
        over: function (e){
            apiMainSlider.stop();
        },
        timeout: 700,
        out: function (){
            apiMainSlider.play();
        }
    });

    $('div.scroll_navi_inside a').click(function (){
        var cIndex = $("div.slider_menu ul a").index($("div.slider_menu ul a.active"));
        if ($(this).parent().hasClass('next_slide_item')){
            cIndex = cIndex + 1;
        }else{
            cIndex = cIndex - 1;
        }
        $("div.slider_menu ul a:eq(" + cIndex + ")").click();
        return false;
    });

    /* colorbox */
    $('div.video_link a').colorbox({iframe:true, innerWidth:658, innerHeight:408});

    // check if exist hash, if exist then set active tab = #hash
    var bExistHashSlider = false;
    $.each($('div.slider_menu a'), function (index, element){
        if (document.location.hash==$(element).attr('href')){
            bExistHashSlider = true;
        }
    });

    if (navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1 || navigator.userAgent.indexOf('iPod') != -1) {
        $('#video_preview').slideUp('fast', function(){
            $('#main_scroller').slideDown('fast');
        });
    } else {
        if (!bExistHashSlider){
            // activate first tab on the main slider
            $('div.slider_menu ul a:first').click();

            /* video preview on slide */
            $f("video_preview_slide", {src: "/js/flowplayer/flowplayer-3.2.7.swf", wmode: "transparent"}, {
                clip: {
                    autoPlay: true,
                    autoBuffering: true,
                    onStart: function (){
                        apiMainSlider.stop();
                    },
                    onFinish: function(){
                        apiMainSlider.play();
                        $('#video_preview').fadeOut('normal', function(){
                            document.location.hash = "#whytmx_accurate";
                            $('#main_scroller').fadeIn();
                        });
                    }
                },
                plugins: {
                    controls: {
                        all: false,
                        opacity: 0
                    }
                }
            });
        }else{
            $('#video_preview').slideUp('fast', function(){
                $('#main_scroller').slideDown('fast');
            });
        }
    }

    /* Clients Scroller */
    $('#scrollbar1').tinyscrollbar();
    
    $("#_tr_latest_news").scrollable({
        items: '.items_news',
        vertical: true,
        circular: true
    }).autoscroll({ 
        interval: 6000
    });
    
    /* gps slider */
    $("#_js_gps_slider").scrollable({
        items: 'a',
        circular: true
    }).autoscroll({ 
        interval: 7000,
        autopause: true
    });

});



