var have_wb_search_map_image = true;
//jQuery.fx.interval = 100;
var elm = document.getElementById("wb_search_map_img");
var animation = false,
    animationstring = 'animation',
    keyframeprefix = '',
    domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
    pfx  = '';

if( elm.style.animationName ) { animation = true; }

if( animation === false ) {
    for( var i = 0; i < domPrefixes.length; i++ ) {
        if( elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
            pfx = domPrefixes[ i ];
            animationstring = pfx + 'Animation';
            keyframeprefix = '-' + pfx.toLowerCase() + '-';
            animation = true;
            break;
        }
    }
}




var wb_search_map_bouncer_max_left = 0;
var wb_search_map_bouncer_min_left = -305;
var wb_search_map_bouncer_dir = '';
var wb_search_map_bouncer_style = {left:0};
var wb_search_map_stop_show = 1;


function wb_startBounce(img) {
    if(img.length > 0) {
        wb_search_map_stop_show = 0;
        var elm = img[0];
        if(animation !== false ) {
            //elm.style[ animationstring ] = 'rotate 1s linear infinite';
            elm.style[ animationstring ] = 'slidein 7s alternate linear infinite';

            //console.log(elm.style);

            /*
             var keyframes = '@' + keyframeprefix + 'keyframes rotate { '+
             'from {' + keyframeprefix + 'transform:rotate( 0deg ) }'+
             'to {' + keyframeprefix + 'transform:rotate( 360deg ) }'+
             '}';
             */

            var keyframes = '@' + keyframeprefix + 'keyframes slidein { ' +
                'from { left: 0px }' +
                'to {left: -305px }' +
                '}';
            //console.log(keyframes);
            var s = document.createElement( 'style' );
            s.innerHTML = keyframes;
            document.getElementsByTagName( 'head' )[ 0 ].appendChild( s );


        }
        else {

            var p = img.position();
            wb_search_map_bouncer_style.left = p.left;

            var img_width = img.width();

            if(img_width > (-1) * wb_search_map_bouncer_min_left) {

                wb_search_map_stop_show = 0;
                wb_bounce(img);
            }

        }
    }
}

function wb_stopBounce(img) {
    wb_search_map_stop_show = 1;
    if(animation !== false ) {
        img.css({left:'0px'});
        img[0].style[ animationstring ] = '';

    }
    else {
        img.css({left:'0px'});
        img.stop(true);
    }
}

function wb_bounce(img) {
    var bstep = 15;
    if(wb_search_map_stop_show == 1) wb_stopBounce(img);
    else {
        if(wb_search_map_bouncer_dir == '') wb_search_map_bouncer_dir = 'left';
        if(wb_search_map_bouncer_dir == 'left') {
            wb_search_map_bouncer_style.left = wb_search_map_bouncer_style.left - bstep;
            if(wb_search_map_bouncer_style.left < wb_search_map_bouncer_min_left) {
                wb_search_map_bouncer_style.left = wb_search_map_bouncer_min_left;
                wb_search_map_bouncer_dir = 'right';
            }

        }
        else {
            if(wb_search_map_bouncer_dir == 'right') {
                wb_search_map_bouncer_style.left = wb_search_map_bouncer_style.left + bstep;
                if(wb_search_map_bouncer_style.left > wb_search_map_bouncer_max_left) {
                    wb_search_map_bouncer_style.left = wb_search_map_bouncer_max_left;
                    wb_search_map_bouncer_dir = 'left';
                }
            }
        }
        //console.log(wb_search_map_bouncer_style);

        img.animate(
            {
                left: wb_search_map_bouncer_style.left
            },
            {
                duration: 600,
                queue: false,
                easing: "linear",
                complete: function() {
                    wb_bounce(img);
                }
            }
        );
    }
}

$('#wb_search_map_img').on('click',function(ev){
    if(wb_search_map_stop_show == 1) {
        wb_startBounce($(this));
    }
    else {
        wb_stopBounce($(this));
    }
});