// JavaScript Document
$(document).ready(function() {

    var indexDisplaySlide = 0;
    var indexLastSlide = 0;
    var psSlidesAll = $('#slider_lane');
    var startM = psSlidesAll.css('marginLeft');

    $('#slider_lane .slide').each(function(index) {
        indexLastSlide = index;
    });

    /*------------------------------------------------------------------------------------------------------*/
    /* reclink */
    /* we use addClass/removeClass to store the current cmd-string in the page */
    /* we get the orig-rec-url from spacial a */
    /* we append the rec-hash as get-param at the end of the recLink-get-params, */
    /* to convert it in the rec-form back to #hash */
    var recSlide_last = '';
    var recSlide_key = 'recSlide';
    var recUrl = $('#rec a#recCmd_initUrl').attr('href');

    function setCmdSlide(i) {
        recSlide = recSlide_key + '.' + i;
        $('#recCmd').addClass(recSlide);
        recSlide_last = recSlide;
        updateRecLinkSlide();
    }
    function clearCmdSlide() {
        if (recSlide_last != '') {
            $('#recCmd').removeClass(recSlide_last);
            recSlide_last = '';
        }
        updateRecLinkSlide();
    }
    function updateRecLinkSlide() {
        var hash = $('#recCmd').attr('class');
        hash = jQuery.trim(hash);
        if (hash) {
            hash = '&recCmd=' + hash;
            hash = hash.replace(' ', '_');
        }
        $('#recCmd').html(hash);
        var recUrl_update = recUrl + hash;
        $('div a.jqRecCmd').attr('href', recUrl_update);
    }
    function parseIncomingHashSlide() {
        var inHash = window.location.hash;
        $('#inHash').html(inHash);
        var inCmd_i = '';
        if (inHash) {
            inHash = inHash.substr(1);
            inHash = inHash.split('_');
            inHash = jQuery.makeArray(inHash);
            inHash = jQuery.grep(inHash,
            function(v, i) {
                var v_arr = v.split('.');
                if (v_arr[0] == recSlide_key) {
                    return (v);
                }
            });
            inCmd_i = inHash[0].split('.');
            inCmd_i_number = Number(inCmd_i[1]);
            return (inCmd_i_number);
        }
    }
    /*------------------------------------------------------------------------------------------------------*/
    /* action */

    $('#slider_ctrlLeft').click(function() {
        if (indexDisplaySlide > 0) {
            indexDisplaySlide -= 1;
            //
            clearCmdSlide();
            setCmdSlide(indexDisplaySlide);
            //
            psSlidesAll.animate({
                marginLeft: '+=710'
            },
            400,
            function() {
                // Animation complete.
                });
        };
    });

    $('#slider_ctrlRight').click(function() {
        if (indexDisplaySlide < indexLastSlide) {
            indexDisplaySlide += 1;
            //
            clearCmdSlide();
            setCmdSlide(indexDisplaySlide);
            //
            psSlidesAll.animate({
                marginLeft: '-=710'
            },
            400,
            function() {
                // Animation complete.
                });
        } else {
            indexDisplaySlide = 0;
            //
            clearCmdSlide();
            setCmdSlide(indexDisplaySlide);
            //
            psSlidesAll.animate({
                marginLeft: startM
            },
            800,
            function() {
                // Animation complete.
                });
        };
    });

    function slideInit(i) {
        indexDisplaySlide += i;
        //
        clearCmdSlide();
        setCmdSlide(indexDisplaySlide);
        //
        psSlidesAll.animate({
            marginLeft: ( - 710 * indexDisplaySlide) - 30
        },
        400,
        function() {
            // Animation complete.
            });
    }
    var in_i_slide = parseIncomingHashSlide();
    if (in_i_slide == null) in_i_slide = 0;
    slideInit(in_i_slide);

});