Tizen 2.0 Release
[samples/web/Tenframe.git] / js / main.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 var isrotated = false;
11
12 /* string and various helper functions */
13 String.prototype.startsWith = function (str) {
14     "use strict";
15     return this.indexOf(str) === 0;
16 };
17
18 function TenFrame() {
19     "use strict";
20
21     var piratesGame, rocketsGame, bowlingGame,
22         menushown = false, restart, close;
23
24     function closeMenu() {
25         $("#game_menu").removeClass("slide");
26         $("#game_menu_border").css("pointer-events", "none");
27         setTimeout(function () {menushown = false;}, 400);
28     }
29
30     function openMenu() {
31         $("#game_menu").addClass("slide");
32         $("#game_menu_border").css("pointer-events", "auto");
33         setTimeout(function () {menushown = true;}, 400);
34     }
35
36     /* need to set isrotated so we can swap x/y axis for touch events */
37     function resize()
38     {
39         if($(window).width() > $(window).height())
40             isrotated = false;
41         else
42             isrotated = true;
43     }
44
45     function init()
46     {
47         license_init("license", "home_page");
48         help_init("home_help", "help_");
49
50         if (window.chrome&&window.chrome.i18n)
51         {
52             $("#home_pirates_text").html(chrome.i18n.getMessage("pirates_title"));
53             $("#home_rockets_text").html(chrome.i18n.getMessage("rockets_title"));
54             $("#home_bowling_text").html(chrome.i18n.getMessage("bowling_title"));
55         }
56
57         var touchToMouse = function(event) {
58             if (event.touches.length > 1) return;
59             var touch = event.changedTouches[0];
60             var type = "";
61
62             switch (event.type) {
63             case "touchstart":
64                 type = "mousedown";
65                 break;
66             case "touchmove":
67                 type="mousemove";
68                 break;
69             case "touchend":
70                 type="mouseup";
71                 break;
72             default:
73                 return;
74             }
75
76             var simulatedEvent = document.createEvent("MouseEvent");
77             simulatedEvent.initMouseEvent(type, true, true, window, 1,
78                 touch.screenX, touch.screenY,
79                 touch.clientX, touch.clientY, false,
80                 false, false, false, 0, null);
81
82             touch.target.dispatchEvent(simulatedEvent);
83         };
84         window.ontouchstart = touchToMouse;
85         window.ontouchmove = touchToMouse;
86         window.ontouchend = touchToMouse;
87
88         piratesGame = new Pirates();
89         $("#home_pirates").click(function() {
90             piratesGame.start();
91             restart = piratesGame.start;
92             close = piratesGame.close;
93             $("#game_menu_border").show();
94         });
95
96         rocketsGame = new Rockets();
97         $("#home_rockets").click(function() {
98             rocketsGame.start();
99             restart = rocketsGame.start;
100             close = rocketsGame.close;
101             $("#game_menu_border").show();
102         });
103
104         bowlingGame = new Bowling();
105         $("#home_bowling").click(function() {
106             bowlingGame.start();
107             restart = bowlingGame.start;
108             close = bowlingGame.close;
109             $("#game_menu_border").show();
110         });
111
112         $("#game_menu").click(function(){
113             if(menushown)
114             {
115                 closeMenu();
116             }
117         });
118
119         $("#game_menu_tab").click(function(){
120             if(!menushown)
121             {
122                 openMenu();
123             }
124         });
125
126         $("#game_menu_new").click(function(){
127             closeMenu();
128             restart();
129         });
130
131         $("#game_menu_home").click(function(){
132             closeMenu();
133             close();
134         });
135
136         $(window).bind('resize', resize);
137         resize();
138     }
139
140     init();
141 };
142
143 window.addEventListener('load', function () {
144     "use strict";
145     var main = new TenFrame();
146 });