1 /* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is TelaSocial TagVisor
 15 *
 16 * The Initial Developer of the Original Code is Taboca TelaSocial.
 17 * Portions created by the Initial Developer are Copyright (C) 2011
 18 * the Initial Developer. All Rights Reserved.
 19 *
 20 * Contributor(s):
 21 * Marcio Galli <mgalli@taboca.com>
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by ddomElementting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not ddomElementte
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36 
 37 var tv = { 
 38 
 39 	currentTick : null, 
 40 	playMode: false, 
 41 	debug:false,
 42 	counterSequential: 0,
 43 	ticksSerialized: new Array(),
 44 	itemsByTicks: new Array(),
 45 	sortedItems: new Array(),
 46 	dataStyle: "#pagetranslate { -moz-transform-origin:0 0; } #pagescale { } .slide { position:relative; } ", 
 47 
 48 	setup: function () { 
 49 		var inlinestyle = document.createElement('link');
 50 		inlinestyle.setAttribute("rel","stylesheet");
 51 		inlinestyle.setAttribute("href","data:text/css,"+ escape(this.dataStyle));
 52 		document.getElementsByTagName("head")[0].appendChild(inlinestyle);
 53 	}, 
 54 
 55 	sortArray: function (arr){
 56 	  var sortedKeys = new Array();
 57 	  var sortedObj = {};
 58 	  for (var i in arr){
 59 		sortedKeys.push(i);
 60 	  }
 61 	  sortedKeys.sort(this.sortNumber);
 62 	  for (var i in sortedKeys){
 63 		sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
 64 	  }
 65 	  return sortedObj;
 66 	},
 67 
 68 	sortNumber: function (a,b) {
 69 		return a - b;
 70 	},
 71 
 72 	add:function (list) { 
 73 		for(var i=0;i<list.length;i++) { 
 74 			var item = list[i];
 75 			var time = item.getAttribute("data-time");
 76 			if(time) { 
 77 			if(time.indexOf("s")>-1) { 
 78 				var secs = time.split("s")[0];
 79 				// we use 2 ticks per sec
 80 				var tickStamp = parseInt(secs*2); 
 81 				this.itemsByTicks[tickStamp] = item; 
 82 			} 
 83 			} 
 84 		} 	
 85 	} ,
 86 	
 87 	play: function () { 
 88 		this.playMode=true; 
 89 		this.sortedItems = this.sortArray(this.itemsByTicks);
 90 		var i=0;
 91 		for (key in this.sortedItems) { 
 92 			this.ticksSerialized[i]=key;
 93 			i++;
 94 		} 
 95 		this.ticksSerialized[i]=-1;
 96 		this.currentTick=0;
 97 		this.counterSequential=0;
 98 		this.tick();
 99 	} ,
100 
101 	tick: function () { 
102 		if(this.playMode) { 
103 			try { 
104 				var nextTick = this.ticksSerialized[this.counterSequential];
105 				if(nextTick > -1) {
106 					if(nextTick==this.currentTick) { 
107 						var lookUpElement = this.itemsByTicks[this.currentTick];
108 						if(lookUpElement) {  
109 
110 							var currDoc = document;	
111 							if(lookUpElement.getAttribute("data-scope")) { 
112 								var sp = lookUpElement.getAttribute("data-scope").split(" ");
113 		
114 								for(var i=0;i<sp.length;i++) { 
115 									var newDoc = currDoc.getElementById(sp[i]).contentDocument; 
116 									if(newDoc) { 
117 										currDoc=newDoc;
118 									} 
119 								} 
120 				
121 							} 
122 							
123 							var targetElement = currDoc.getElementById(lookUpElement.getAttribute("data-target"));
124 							var fCommand ="";
125 
126 							fCommand = lookUpElement.getAttribute("data-exec"); 
127 
128 							if(fCommand!=null) { 
129 							if(fCommand.indexOf('dispatch')>-1) { 
130 try { 
131 								var str = fCommand.split("dispatch_load('"); 
132 								var str1 = str[1].split("'");
133 								var url = str1[0];
134 								lookUpElement.src=url+"?"+Math.random();
135 } catch (i) {  } 
136 							} 
137 							} 
138 
139 							var fEffect = null; 
140 							var fDuration = null;
141 							try { fEffect = lookUpElement.getAttribute("data-effect"); } catch (i) { } 
142 							try { fDuration = lookUpElement.getAttribute("data-duration"); } catch(i) { } 
143 							var dur = 2;
144 							if(fDuration != null) { 
145 								dur=parseInt(fDuration);	
146 							} 
147 							if(fEffect == 'dive') { 
148 								this.effects_scale(targetElement,dur);
149 							} 
150 							if(fEffect == "fadeout") { 
151 								this.effects_fadeOut(targetElement,dur);
152 							} 
153 							if(fEffect == "move") { 
154 								this.effects_animateNext(targetElement, currDoc,dur);
155 							} 
156 							this.counterSequential++;
157 						} 
158 					} 
159 					this.currentTick++;
160 					var stampThis = this; 
161 					setTimeout(function () { stampThis.tick() } ,500); 
162 
163 				} else { 
164 					//end
165 				} 
166 	
167 			} catch (i) { 
168 				this.currentTick++;
169 				var stampThis = this; 
170 				setTimeout(function () { stampThis.tick() } ,500); 
171 			}
172 		} 
173 	} ,
174 
175 	offset: function (domElement) {
176 	        if(!domElement) domElement = this;
177 	        var x = domElement.offsetLeft;
178 	        var y = domElement.offsetTop;
179 	        while (domElement = domElement.offsetParent) {
180 	                x += domElement.offsetLeft;
181 	                y += domElement.offsetTop;
182 	        }
183         	return { left: x, top: y };
184 	},
185 	
186 	effects_fadeOut: function (el, t) { 
187 	        el.setAttribute("style","-moz-transition-property: opacity; -moz-transition-duration:"+t+"s;opacity:0");
188 	},
189 
190 	effects_scale: function (el, t) { 
191 	        el.setAttribute("style","-moz-transition-property: -moz-transform; -moz-transition-duration:"+t+"s;-moz-transform:scale(1.2);");
192 	},
193 
194 	effects_animateNext: function (a,d,t) { 
195 
196 		var el = this.offset(a);
197 		var x= el.left; 	
198 		var y= el.top; 	
199 		var ww = a.offsetWidth;
200 		var www = window.innerWidth;		        
201 	        var scale = www/(ww+800);
202 
203 		//document.getElementById("pagetranslate").setAttribute("style"," -moz-transition-property: -moz-transform; -moz-transform:scale("+scale+"); -moz-transition-duration:3s;  -webkit-transition-property: -webkit-transform; -webkit-transform:scale("+scale+"); -webkit-transition-duration:3s;  -o-transition-property: -o-transform; -o-transform:scale("+scale+"); -o-transition-duration:3s;");
204 	
205 		x-=0;
206 	        d.getElementById("pagescale").setAttribute("style","-moz-transition-property: -moz-transform; -moz-transform:translate("+-1*x+","+-1*y+"); -moz-transition-duration:"+t+"s; -webkit-transition-property: -webkit-transform; -webkit-transform:translate("+parseInt(-1*x)+"px,"+parseInt(-1*y)+"px); -webkit-transition-duration:"+t+"s; -o-transition-property: -o-transform; -o-transform:translate("+parseInt(-1*x)+"px,"+parseInt(-1*y)+"px); -o-transition-duration:"+t+"s;");
207 	} 
208 
209 } 
210 
211 tv.setup();
212 
213