define([
    "jquery",
    "./button.state"
],
function(
    jq,
    ButtonState
){

    /**
     * @constructor
     */
    return function( options ){

        var buttonColors = {};
        buttonColors[ ButtonState.Default ] = options.colors.default;
        buttonColors[ ButtonState.Hover ] = options.colors.hover;
        buttonColors[ ButtonState.Pressed ] = options.colors.pressed;
        buttonColors[ ButtonState.Inactive ] = options.colors.inactive;

        function createSvgPath(){
            return  '<svg id="' + options.id + '" width="' + options.width + 'px" height="' + (options.height * 4) + 'px" viewBox="0 0 50 200" preserveAspectRatio="none" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="background-color: transparent;">' +
                '<title>' + options.caption + '</title>' +
                '<defs></defs>' +
                '<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">' +
                '<g>' +
                '<g class="NavigationButton" transform="translate(0.000000, 0.000000)" fill="' + options.colors.default.bg + '">' +
                '<rect x="0" y="0" width="50" height="50"></rect>' +
                '<rect class="NavigationButtonIcon" x="14" y="13" width="3" height="19" transform="translate(31 45) rotate(-180)" fill="' + options.colors.default.icon + '"/>' +
                '<polygon class="NavigationButtonIcon" points="38.33 30.5 31 23.17 31 37.83 38.33 30.5" fill="' + options.colors.default.icon + '"/>' +
                '<rect class="NavigationButtonIcon" x="14" y="29" width="19" height="3" transform="translate(47 61) rotate(-180)" fill="' + options.colors.default.icon + '"/>' +
                '</g>' +
                '</g>' +
                '</g>' +
                '</svg>';
        }

        function createButton(){

            var currentState = ButtonState.Default,
                svgButton = jq(document.createElement("div")),
                svg = svgButton.html( createSvgPath() )
                    .contents()
                    .addClass("sc-navigation")
                    .css({
                        "pointer-events" : "none"
                    })
                    .on("touchmove", function(e) {
                        e.preventDefault ? e.preventDefault() : true;
                    })
                    .get(0);

            var svgWrapper = jq(document.createElement("div"))
                .attr({
                    "title" : options.caption,
                    "class" : currentState
                })
                .css({
                    "cursor": "pointer",
                    "width": options.width + "px",
                    "height": options.height + "px",
                    "overflow": "hidden"
                })
                .append(svg);

            return {
                getHotspotElement : function(){
                    return svgWrapper.get(0);
                },
                hasState : function( buttonState ){
                    return buttonState === currentState;
                },
                changeState : function( buttonState ){

                    if( currentState === buttonState ){
                        return;
                    }

                    var colors = buttonColors[buttonState],
                        jqSvg = jq(svg);

                    svgWrapper.removeClass( currentState );
                    currentState = buttonState;
                    svgWrapper.addClass( buttonState );

                    jqSvg.find("[class=NavigationButton]").attr( "fill", colors.bg );
                    jqSvg.find("[class=NavigationButtonIcon]").attr( "fill", colors.icon );
                    jqSvg.css({
                        "cursor" : buttonState !== ButtonState.Inactive ? "pointer" : "default"
                    });
                },
                showElement: function () {
                    svgWrapper.css( { "display": "block" } );
                },
                hideElement: function () {
                    svgWrapper.css( { "display": "none" } );
                }
            };
        }

        return {
            build : createButton
        };
    };
});
