(function ($) { "use strict"; window.coverBrowser = window.coverBrowser || {}; var self = window.coverBrowser; self.utils = (function () { var getDir = function (of){ var id = of, start = id.indexOf("cbcontrol") + 10, dir = id.substring(start,id.length); return dir; }; return { getDir : getDir }; })(); self.state = (function (){ var classes = [], activeIndex = 0, stageDim = {}, coverDim = {}; return { classes : classes, activeIndex : activeIndex }; })(); self.actions = (function (){ var doMove = function (){ var activeIndex = self.state.activeIndex, $covers = $(".cbImage"), newActive,oldActive = $(".cbImage .active"); $covers.each(function (i, e){ if( i < activeIndex ){ this.className = "cbImage inactiveLeft inactiveLeft" + (activeIndex - i) ; } else if( i === activeIndex ){ this.className = "cbImage active"; newActive = this; } else if( i > activeIndex ){ this.className = "cbImage inactiveRight inactiveRight" + ((activeIndex - i)*-1) ; } }); $(document).trigger("endmove.cvr", [{"newActive":newActive,"oldActive":oldActive}]); }, moveRight = function (){ if( $(document).trigger("startmoveright") !== false ){ self.state.activeIndex = self.state.activeIndex-1; doMove(); } }, moveLeft = function (){ if( $(document).trigger("startmoveleft") !== false ){ self.state.activeIndex = self.state.activeIndex + 1; doMove(); } }; return { moveRight : moveRight, moveLeft : moveLeft, doMove : doMove }; })(); self.imgs = (function (){ var attachEvents = function (){ $(".cbImage").on("click",function (event){ //move[dir] the number of times difference //between index of active and index of clicked var dir = this.className.search(/left/gi) !==-1 ? "Right" : "Left", $coll = $(".cbImage"), diff = dir === "Right" ? ($coll.index($(".cbImage.active")) - $coll.index(this)) : ($coll.index($(".cbImage.active")) - $coll.index(this)) * -1, x; for( x=0; x 0 ){ self.actions["move"+dir](); } }); }; return { attachEvents : attachEvents }; })(); self.init = (function (){ var $covers = $(".cbImage"), setStageDims = function(){ self.state.stageDim = {x:$(".stage").outerWidth(),y:$(".stage").outerHeight()}; }, setCoverDims = function($active){ self.state.coverDim = {x:$active.outerWidth(),y:$active.outerHeight()}; }, getSheet = function(){ var l = $("#cbGeneratedClasses").length; if( l === 0 ){ var $newsheet = $(""), ss = $newsheet.appendTo("head")[0].sheet; } else{ $("#cbGeneratedClasses").text(''); var ss = $("#cbGeneratedClasses")[0].sheet; } return ss; }, generateClasses = function(){ var leftfac, rightfac, x, z = 100,//TODO figure equation for zindex, increase closer to activeIndex, decrease after cvrLength = $covers.length, l = cvrLength*2, ss = getSheet(), al; al = (self.state.stageDim.x / 2) - (self.state.coverDim.x / 2); ss.insertRule(".stage .active{left:"+al+"px;}",0); for(x=0; x