// com.tts.initial.html5.navigation.markasvisited
define([
        "jquery"
		], function (
			jq
		){

	"use strict";

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

        var shellElement,
            stepIdsToMarkAsVisited = new Array(),
            mode = metadata.parameters.get("mode"),
            fireTriggerMode = metadata.parameters.get("fire-trigger-mode", "onEnterPage"),
            id = metadata.parameters.asNumber("id", 1),
            allStepIds = new Array(),
            isInAuthoringMode = false,
            isInPreviewMode = false,
            isInAssessmentMode = false,
            isStandardPlayer = context.getPlayerName() === "standard";

        function initializeButton(){
            shellElement.css({
                "width": metadata.getWidth() + "px",
                "height": metadata.getHeight() + "px"
            });

            if ( isInAuthoringMode ) {
                createAuthoringShield();
            }
        }

        // create an object shield so we are able to select and drag element
        function createAuthoringShield() {
            var shield = jq( document.createElement( "div" ) )
                .css( {
                    "width": "100%",
                    "height": "100%",
                    "display": "block",
                    "opacity": "0.15",
                    "background-image": "url(\"" + context.getPath() + "invis.png\")",
                    "overflow": "hidden"
                } ),
                caption = jq( document.createElement( "span" ) )
                .css( {
                    "position": "absolute",
                    "display": "block",
                    "top": "100%",
                    "left": "5px",
                    "color": "#000000",
                    "font-size": "16px",
                    "line-height": "100%"
                } )
                .text( "visited" );

            shield.append( caption );

            shellElement.append( shield );
        }

        function setStepsAsVisited() {
            var currentPageId = metadata.page.getId();

            getAllStepIds();

            if ( mode === "complete" ) {
                for ( var i = 0; i < allStepIds.length; i++ ) {
                    context.student.expert.setPageAsVisited( allStepIds[i], { visited: true } );
                }
            }

            if ( mode === "previous" ) {
                for ( var i = 0; i < allStepIds.length; i++ ) {
                    if ( currentPageId != allStepIds[i] ) {
                        context.student.expert.setPageAsVisited( allStepIds[i], { visited: true } );
                    } else {
                        return;
                    }
                }
            }

            if ( mode === "subsequent" ) {
                for ( var i = allStepIds.length - 1; i >= 0; i-- ) {
                    if ( currentPageId != allStepIds[i] ) {
                        context.student.expert.setPageAsVisited( allStepIds[i], { visited: true } );
                    } else {
                        return;
                    }
                }
            }

            if ( mode === "steps" ) {
                getStepIdsToMarkAsVisited();

                for ( var i = 0; i < stepIdsToMarkAsVisited.length; i++ ) {
                    context.student.expert.setPageAsVisited( stepIdsToMarkAsVisited[i], { visited: true } );
                }
            }
        }

        function getAllStepIds(){
            var pathElements = metadata.stage.getMainPath().getPathElements();

            jq.each( pathElements, function ( index, element ) {
                var type = element.getType(),
                    id;

                if ( element.getPageMetadata() ) {
                    id = element.getPageMetadata().getId();
                } else {
                    id = 1;
                }

                if ( type == "page" ) {
                    allStepIds.push( id );
                }
            } );
        }

        function getStepIdsToMarkAsVisited() {
            var stepsToMarkVisited = metadata.parameters.get( "custom_steps" );

            stepsToMarkVisited = stepsToMarkVisited.split( ';' );
            for ( var i = 0; i < stepsToMarkVisited.length; i++ ) {
                if ( stepsToMarkVisited[i].toString().indexOf( '-' ) == -1 ) {
                    stepIdsToMarkAsVisited.push( allStepIds[stepsToMarkVisited[i] - 1] );
                } else {
                    var startEndIndex;

                    startEndIndex = stepsToMarkVisited[i].split( '-' );
                    for ( var j = parseInt(startEndIndex[0]); j <= parseInt(startEndIndex[1]); j++ ) {
                        stepIdsToMarkAsVisited.push( allStepIds[j - 1] );
                    }
                }
            }
        }

        function reset(){
            stepIdsToMarkAsVisited.length = 0;
            allStepIds.length = 0;
            mode = "";
        }

		// revealing public API
		return {

            onLoading: function () {
                context.page.event.bindOnEnter( this );
                context.page.event.bindOnExit(this);
                context.page.event.bind(this, "onCheckQuizPageStatusEvent");

                shellElement = jq( context.getElement() );

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

                initializeButton();
            },

            onEnterPage: function () {
                if (!isInPreviewMode && !isStandardPlayer && !isInAssessmentMode && fireTriggerMode === "onEnterPage") {
                    setStepsAsVisited();
                }
            },

            onExitPage: function () {
                if ( !isInPreviewMode && !isStandardPlayer && !isInAssessmentMode ) {
                    reset();
                }
            },

            onCheckQuizPageStatusEvent: function (event, dataTHML5, dataHTML4) {
                if (!isInPreviewMode && !isStandardPlayer && !isInAssessmentMode) {
                    var data = dataHTML4 || dataHTML5;

                    if (data.id == id && (data.result === "passed" && fireTriggerMode === "onCheckStatusPassed") | (data.result === "notpassed" && fireTriggerMode === "onCheckStatusFailed")) {
                        setStepsAsVisited();
                    }
                }
            }
        };
	};
});