define(["browser","dom","layoutManager","css!./viewcontainer-lite"],function(browser,dom,layoutManager){"use strict";functionenableAnimation(){return!browser.tv&&browser.supportsCssAnimation()}functionfindLastView(parent,className){for(varnodes=parent.childNodes,i=nodes.length-1;i>=0;i--){varnode=nodes[i],classList=node.classList;if(classList&&classList.contains(className))returnnode}}functionfindViewBefore(elem,className){for(varnode=elem.previousSibling;node;){varclassList=node.classList;if(classList&&classList.contains(className))returnnode;node=node.previousSibling}}functionloadView(options){if(!options.cancel){cancelActiveAnimations();varselected=selectedPageIndex,previousAnimatable=selected===-1?null:allPages[selected],pageIndex=selected+1;pageIndex>=pageContainerCount&&(pageIndex=0);varviewHtml=options.view,properties=[];options.fullscreen&&properties.push("fullscreen");varview,currentPage=allPages[pageIndex];returncurrentPage?(triggerDestroy(currentPage),currentPage.insertAdjacentHTML("beforebegin",viewHtml),view=findViewBefore(currentPage,"view"),mainAnimatedPages.removeChild(currentPage)):(mainAnimatedPages.insertAdjacentHTML("beforeend",viewHtml),view=findLastView(mainAnimatedPages,"view")),view.classList.add("mainAnimatedPage"),properties.length&&view.setAttribute("data-properties",properties.join(",")),options.type&&view.setAttribute("data-type",options.type),allPages[pageIndex]=view,onBeforeChange&&onBeforeChange(view,!1,options),beforeAnimate(allPages,pageIndex,selected),animate(view,previousAnimatable,options.transition,options.isBack).then(function(){returnselectedPageIndex=pageIndex,currentUrls[pageIndex]=options.url,!options.cancel&&previousAnimatable&&afterAnimate(allPages,pageIndex),view})}}functionbeforeAnimate(allPages,newPageIndex,oldPageIndex){for(vari=0,length=allPages.length;i<length;i++)newPageIndex===i||oldPageIndex===i||allPages[i].classList.add("hide")}functionafterAnimate(allPages,newPageIndex){for(vari=0,length=allPages.length;i<length;i++)newPageIndex===i||allPages[i].classList.add("hide")}functionanimate(newAnimatedPage,oldAnimatedPage,transition,isBack){if(enableAnimation()&&oldAnimatedPage){if("slide"===transition)returnslide(newAnimatedPage,oldAnimatedPage,transition,isBack);if("fade"===transition)returnfade(newAnimatedPage,oldAnimatedPage,transition,isBack);clearAnimation(newAnimatedPage),oldAnimatedPage&&clearAnimation(oldAnimatedPage)}returnPromise.resolve()}functionclearAnimation(elem){setAnimation(elem,"none")}functionslide(newAnimatedPage,oldAnimatedPage,transition,isBack){returnnewPromise(function(resolve,reject){varduration=layoutManager.tv?450:160,animations=[];oldAnimatedPage&&(isBack?setAnimation(oldAnimatedPage,"view-slideright-r "+duration+"ms ease-out normal both"):setAnimation(oldAnimatedPage,"view-slideleft-r "+duration+"ms ease-out normal both"),animations.push(oldAnimatedPage)),isBack?setAnimation(newAnimatedPage,"view-slideright "+duration+"ms ease-out normal both"):setAnimation(newAnimatedPage,"view-slideleft "+duration+"ms ease-out normal both"),animations.push(newAnimatedPage),currentAnimations=animations;varonAnimationComplete=function(){dom.removeEventListener(newAnimatedPage,dom.whichAnimationEvent(),onAnimationComplete,{once:!0}),resolve()};dom.addEventListener(newAnimatedPage,dom.whichAnimationEvent(),onAnimationComplete,{once:!0})})}functionfade(newAnimatedPage,oldAnimatedPage,transition,isBack){returnnewPromise(function(resolve,reject){varduration=layoutManager.tv?450:270,animations=[];newAnimatedPage.style.opacity=0,setAnimation(newAnimatedPage,"view-fadein "+duration+"ms ease-in normal both"),animations.push(newAnimatedPage),oldAnimatedPage&&(setAnimation(oldAnimatedPage,"view-fadeout "+duration+"ms ease-out normal both"),animations.push(oldAnimatedPage)),currentAnimations=animations;varonAnimationComplete=function(){dom.removeEventListener(newAnimatedPage,dom.whichAnimationEvent(),onAnimationComplete,{once:!0}),resolve()};dom.addEventListener(newAnimatedPage,dom.whichAnimationEvent(),onAnimationComplete,{once:!0})})}f