// com.tts.initial.html5.sitemaptextbox2
define([
	"./sitemaptextbox2.builder",
    "jquery"
], function (
	SitemapTextbox2Builder,
    jq
){

	"use strict";

	/**
	* @constructor 
	*/
	return function (context, metadata) {

        var shellElement,
            sitemapTextbox2,
            sitemapTextbox2Initialized = false,
            isInAuthoringMode = false,
            isInPreviewMode = false,
            isStandardPlayer = context.getPlayerName() === "standard",
            pointerUpEventName, pointerDownEventName, pointerMoveEventName, pointerLeaveEventName,
            fonts = [
                'Arial, Helvetica, sans-serif',
                '"Times New Roman", Times, serif',
                'Verdana, Geneva, sans-serif',
                '"Courier New", Courier, monospace',
                'Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif',
                '"Droid Serif", "Times New Roman", Times, serif'
            ];

        function initializeSitemapTextbox2(){
            if( sitemapTextbox2Initialized ){
                return;
            }
            sitemapTextbox2Initialized = true;

            initPointerEventNames();

            var scParams = metadata.parameters,
                imageArray = {
                    marker: loadImage( "marker", scParams.get( "marker_image", null ) ),
                    icons: loadImage( "icons", scParams.get( "icons_image", null ) ),
                    visited: loadImage( "visited", scParams.get( "visited_image", null ) )
                },
                useSVG = scParams.asBool( "use_svg" );

            sitemapTextbox2 = new SitemapTextbox2Builder({
                pointerEvents : {
                    pointerUp : pointerUpEventName,
                    pointerDown : pointerDownEventName,
                    pointerMove : pointerMoveEventName,
                    pointerLeave : pointerLeaveEventName
                },
                instanceID : metadata.getId(),
                AuthoringMode : isInAuthoringMode,
                PreviewMode: isInPreviewMode,
                isStandardPlayer: isStandardPlayer,

                sitemapWidth: metadata.getWidth(),
                sitemapHeight: metadata.getHeight(),
                borderColor : scParams.get("border_color", "#999999"),
                borderWidth : scParams.asNumber("border_width", "2"),
                borderTransparency : (scParams.asNumber("borderTransparency", "0")),
                fillColor : scParams.get("fill_color", "#ffffff"),
                fillTransparency : (scParams.asNumber("fillColorTransparency", "0")),
                separatorColor : scParams.get("separator_color", "#d1d1d1"),
                separatorWidth : scParams.asNumber("separator_width", "2"),
                separatorTransparency : (scParams.asNumber("separatorTransparency", "0")),

                useSVG: useSVG,
                showVisited: scParams.asBool( "show_visited" ),
                image: imageArray,
                svgIconColors: {
                    default: {
                        icon: scParams.get( "color_svg_icon", "#999999" ),
                        bg: "none"
                    },
                    hover: {
                        icon: scParams.get( "color_svg_icon", "#999999" ),
                        bg: "none"
                    },
                    pressed: {
                        icon: scParams.get( "color_svg_icon", "#999999" ),
                        bg: "none"
                    },
                    inactive: {
                        icon: scParams.get( "color_svg_icon", "#999999" ),
                        bg: "none"
                    }
                },
                svgMarkerColor: scParams.get( "color_svg_marker", "#fd7326" ),
                svgVisitedColor: scParams.get( "color_svg_visited", "#ffffff" ),

                fontFamily : getFont(scParams.get("font-family","0")),
                fontSize : scParams.get("font-size", "14"),
                sectionTextColor : scParams.get("section_text_color", "#142534"),
                sectionHoverTextColor : scParams.get("section_hover_color", "#fd7326"),
                pageTextColor : scParams.get("page_text_color", "#142534"),
                pageHoverTextColor : scParams.get("page_hover_color", "#fd7326"),
                paddingLeft : scParams.asNumber("padding-left", "10"),
                paddingRight : scParams.asNumber("padding-right", "10"),
                paddingTop : scParams.asNumber("padding-top", "2"),
                paddingBottom : scParams.asNumber("padding-bottom", "2"),
                cutTitles : scParams.asBool("cut_titles"),
            }, context, metadata).build();

            shellElement.append( sitemapTextbox2.getElement() );

            if (isInAuthoringMode) {
                createAuthoringShield();
            }
        }

        function loadImage(key, image) {

            var result = {};

                if (image) {
                    result.fileNameURL = context.stage.getSharedPath() + image.fileName;
                    result.width = image.width;
                    result.height = image.height;
                }
                else {
                    switch(key){
                        case "marker": 
                            result.fileNameURL = context.getPath() + "marker.png";
                            result.width = 14;
                            result.height = 14;
                            break;
                        case "icons": 
                            result.fileNameURL = context.getPath() + "icons.png";
                            result.width = 14;
                            result.height = 28;
                            break;
                        case "visited":
                            result.fileNameURL = context.getPath() + "visited.png";
                            result.width = 14;
                            result.height = 14;
                            break;
                        default:
                            return null;
                    } 
                }      

            return result;
        }
        
        function getFont( index ) {
            var fontIndex = (index > 0 && index < fonts.length) ? index-1: 0;
            return fonts[fontIndex];
        }

        function initPointerEventNames() {

            if ( pointerUpEventName ) {
                return;
            }

            if ( context.stage.isInPreviewMode() ) {
                pointerUpEventName = "mouseup";
                pointerDownEventName = "mousedown";
                pointerMoveEventName = "mousemove";
                pointerLeaveEventName = "mouseend mouseout";
                return;
            }

            pointerUpEventName = context.device.getUpEvents();
            pointerDownEventName = context.device.getDownEvents();
            pointerMoveEventName = context.device.getMoveEvents();
            pointerLeaveEventName = context.device.getReleaseEvents();
        }

        // create an object shield so we are able to select and drag element
        function createAuthoringShield() {
            shellElement.append(jq(document.createElement("div"))
                .css({
                    "position": "absolute",
                    "left": 0,
                    "top": 0,
                    "background-color": "transparent",
                    "opacity": "0.4",
                    "width": "100%",
                    "height": "100%",
                    "z-index": "9999"
                }));
        }
	
		// revealing public API
		return {

            onLoading: function () {

                context.page.event.bindOnEnter(this);
                context.page.event.bindOnExit( this );

                shellElement = jq(context.getElement());

                isInAuthoringMode = !!context.stage.isInAuthoringMode();
                isInPreviewMode = !!context.stage.isInPreviewMode();

                // because of some problems with loading SVG while it is hidden on page,
                // we initialize SVG sitemapTextbox2 onEnterPage
                if ( !isStandardPlayer || isInAuthoringMode ) {
                    initializeSitemapTextbox2();
                }
            },

            onEnterPage: function () {
                // because of some problems with loading SVG while it is hidden on page,
                // we initialize SVG sitemapTextbox2 onEnterPage
                if ( !sitemapTextbox2Initialized ) {
                    initializeSitemapTextbox2();
                }

                sitemapTextbox2.onEnterPage();
            },
            onExitPage: function() {
                sitemapTextbox2.onExitPage();
            }
        };
	};
});