Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #Initialization
- // _Declaration of variables and initial initialization_
- var city, xPos, xOrg, xCor, pinch = false, time, icon, info, reveal, blur;
- var doc = $(this);
- active = 1;
- //$('head').append('<meta name="viewport" content="user-scalable=0, initial-scale=1.0">');
- $('body').removeClass('preview').addClass('initialize').html('<div id="app"><div id="box"><div id="layer"><div class="slide"><div class="background"></div><a class="in">In</a><div class="pagination"><span class="active"></span><span></span><span></span><span></span></div></div></div></div></div>');
- if(!window.navigator.onLine) {
- $('.pagination').hide();
- $(document).bind('touchstart', function(e) {
- e.preventDefault();
- });
- notification("Sun requires a working 3G or WiFi connection.");
- } else {
- var tz = jstz.determine().name();
- if(tz == 'Europe/Berlin') {
- var timezone = 'normal';
- } else {
- var timezone = tz;
- }
- // Check if a new cache is available on page load.
- window.addEventListener('load', function(e) {
- window.applicationCache.addEventListener('updateready', function(e) {
- if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
- notification("An update has been installed.");
- window.applicationCache.swapCache();
- } else {
- // Manifest didn't change. Nothing new to server.
- }
- }, false);
- }, false);
- // #Application start
- // _Take-off_
- $('.slide').append('<div class="loading" />');
- // #First Time
- // _If a user visits for the first time — fill in local storage values_
- if(localStorage.getItem("init2") != 'false') {
- localStorage.setItem("init2", 'false');
- if(navigator.language == 'en-US') {
- localStorage.setItem("unit", 'f');
- localStorage.setItem("w_unit", 'mph');
- } else {
- localStorage.setItem("unit", 'c');
- localStorage.setItem("w_unit", 'm/s');
- }
- localStorage.setItem("theme", 'pansage');
- localStorage.setItem("city0", 'Copenhagen'); // <3
- localStorage.setItem("city1", 'Vienna');
- localStorage.setItem("city2", 'Hong Kong');
- localStorage.setItem("city0-lat", '55.680682'); // Copenhagen
- localStorage.setItem("city0-lon", '12.568703');
- localStorage.setItem("city1-lat", '48.202541'); // Vienna
- localStorage.setItem("city1-lon", '16.368799');
- localStorage.setItem("city2-lat", '22.309426'); // Hong Kong
- localStorage.setItem("city2-lon", '114.200134');
- localStorage.setItem("city0-id", '554890');
- localStorage.setItem("city1-id", '551801');
- localStorage.setItem("city2-id", '2165352');
- }
- // #Declaration
- // _This will fuck completely, if a browser doesn't support localStorage — but this is an iOS concept_ //
- var cities = [
- [localStorage.getItem("city0"), localStorage.getItem("city0-lat"), localStorage.getItem("city0-lon"), localStorage.getItem("city0-id")],
- [localStorage.getItem("city1"), localStorage.getItem("city1-lat"), localStorage.getItem("city1-lon"), localStorage.getItem("city1-id")],
- [localStorage.getItem("city2"), localStorage.getItem("city2-lat"), localStorage.getItem("city2-lon"), localStorage.getItem("city2-id")]
- ];
- unit = localStorage.getItem("unit"); // // Celcius (c) or Fahrenheit (f)
- w_unit = localStorage.getItem("w_unit"); // Meters per second (m/s), kilometers per second (km/h) or, god forbid, miles per hour (mph)
- theme = localStorage.getItem("theme"); // Setting theme, defaults to 'Jour'
- //$('head').append('<link id="icon" rel="apple-touch-icon-precomposed" href="resources/icon_pastel.png" />');
- iconID = Math.round(Math.random()*10000000000);
- $('head').append('<link id="icon" rel="apple-touch-icon-precomposed" href="http://pattern.dk/sun/icon.php?id='+iconID+'" />');
- $('body').addClass(theme);
- var themes = ['Sunnyside', 'Pastels', 'Adrift', 'Stardust', 'Looper', 'Goldfish', 'Moon', 'Envious', 'Pansage'];
- color = new Array();
- color['sunnyside'] = '#fbbc2b';
- color['pastels'] = '#e17569';
- color['adrift'] = '#26c9b4';
- color['stardust'] = '#9395a1';
- color['looper'] = '#33a6aa';
- color['goldfish'] = '#00c8e4';
- color['moon'] = '#e174a4';
- color['envious'] = '#00a6ca';
- color['pansage'] = '#11b2a1';
- color['gordon'] = '#3e524f';
- var lists = ['Cities', 'Temperature', 'Wind', 'Colors', 'Credits'];
- var settings = $('<div id="settings" />').append('<div class="content" />').append('<div id="hide" />');
- var smallDiv = $('<div id="small" />').append('<div id="slides" />');
- var forecast = $('<div id="forecasts" />');
- forecast.appendTo($('#app'));
- settings.appendTo($('#app'));
- smallDiv.appendTo($('#app'));
- for(var i = 0; i < 4; i++) {
- $('<div />').attr('class', 'slide').appendTo($('#slides'));
- }
- $.each(lists, function(i) {
- var h2 = $('<h2>' + lists[i] + '</h2>');
- var ul = $('<ul />').attr('class', lists[i].toLowerCase());
- h2.appendTo( $('#settings .content') );
- ul.appendTo($('#settings .content'));
- if(lists[i] == 'Temperature') {
- ul.append('<li data-unit="c">Celsius</li><li data-unit="f">Fahrenheit</li>');
- }
- if(lists[i] == 'Wind') {
- ul.append('<li data-unit="m/s">Meters per second</li><li data-unit="km/h">Kilometers per hour</li><li data-unit="mph">Miles per hour</li>');
- }
- if(lists[i] == 'Credits') {
- ul.append('<li><span></span></li>');
- }
- });
- $.each(themes, function(i) {
- var li = $('<li />');
- for(var x = 0; x < 4; x++) {
- $('<span />').attr('class', themes[i].toLowerCase()).appendTo(li);
- }
- li.attr('data-theme', themes[i].toLowerCase());
- li.appendTo('ul.colors');
- });
- $('a.back').live('touchstart', function() {
- $(this).addClass('touchdown');
- $(this).css('color', $(this).data('color'));
- });
- $('a.back').live('touchend', function() {
- $(this).removeAttr('style');
- $(this).removeClass('touchdown');
- });
- $('a.back').live('touchmove', function(e) {
- e.preventDefault();
- return false;
- });
- $('a.back').live('tap', function(e) {
- pinchOut = true;
- playSound('woosh2', 0);
- $('body').addClass('unbuild')
- $('body').removeClass('revealed build');
- $(this).delay(450).queue(function(){
- isActive = false;
- pinchOut = false;
- $('body').removeClass('animate unbuild');
- $('.slide.active').removeClass('active');
- $(this).dequeue();
- });
- });
- $('.scroll ul li').live('tap', function() {
- playSound('plop2', 0);
- if($(this).hasClass('active')) {
- $(this).removeClass('active');
- } else {
- $('.scroll ul li.active').removeClass('active');
- $(this).addClass('active');
- }
- });
- $('#layer a.in').live('touchstart', function() {
- if(!$('body').hasClass('settings')) {
- isSettings = true;
- isActive = true;
- playSound('plop', 0);
- $('body').addClass('settings');
- }
- });
- $('.settings #box').live('touchstart', function() {
- if(isInputActive == false) {
- playSound('plop3', 0);
- $('body').addClass('back').delay(250).queue(function() {
- isActive = false;
- $(this).removeClass('settings back');
- $(this).dequeue();
- });
- }
- });
- $('.settings #box').live('touchend', function() {
- if(isInputActive == false) {
- $(this).delay(250).queue(function() {
- isSettings = false;
- $(this).dequeue();
- });
- }
- });
- $('#slides').bind('touchstart', function(e) {
- if($('body').hasClass('settings')) {
- $('body').addClass('fadeout').delay(250).queue(function() {
- $(this).removeClass('settings fadeout');
- $(this).dequeue();
- });
- return false;
- }
- e.preventDefault();
- });
- $('.chart').live('touchstart', function(e) {
- e.preventDefault();
- return false;
- });
- $('#small .slide').live('tap', function() {
- if(isActive == true) {
- isSettings = true;
- isActive = false;
- if($('body').hasClass('settings')) {
- $('body').removeClass('settings');
- } else {
- playSound('woosh', 0);
- $('#small .slide').removeClass('non');
- $('#small .slide.active').removeClass('active');
- switch($(this).index()) {
- case 0:
- degree = 0;
- active = 1;
- break;
- case 1:
- degree = -90;
- active = 2;
- break;
- case 2:
- degree = -180;
- active = 3;
- break;
- case 3:
- degree = -270;
- active = 4;
- break;
- default:
- // Do nothing, yet?
- }
- $('#layer').css('-webkit-transform', 'translate3d(0px, 0px, -' + width/2 + 'px) rotate3d(0, 1, 0, ' + degree + 'deg)');
- $(this).addClass('in').delay(250).queue(function() {
- $(this).removeClass('in');
- isSettings = false;
- $('body').removeClass('out');
- $(this).dequeue();
- });
- }
- }
- });
- var gordon = 0;
- $('.icon').live('tap', function() {
- gordon++;
- if(gordon == 25) {
- notification('You just unleashed Robin.');
- localStorage.setItem('theme', 'gordon');
- $.each(themes, function(i) {
- if($('body').hasClass(themes[i].toLowerCase())) {
- $('body').removeClass(themes[i].toLowerCase());
- }
- });
- $('body').addClass('gordon').delay(2000).queue(function() {
- notification('Changing colors...');
- $('body').delay(250).queue(function() {
- $(this).delay(1500).queue(function() {
- location.reload(true);
- $(this).dequeue();
- });
- $(this).dequeue();
- });
- $(this).dequeue();
- });
- }
- });
- $('.weather').live('tap', function() {
- if($(this).parents('.slide').parent().attr('id') != 'slides') {
- isActive = true;
- $('body').addClass('animate build');
- $(this).parents('.slide').addClass('active');
- playSound('woosh', 0);
- $(this).delay(450).queue(function() {
- $('body').addClass('revealed');
- $(this).dequeue();
- });
- }
- });
- if(localStorage.getItem("restart") != 'true') {
- fetching(); // Fetches all available sounds with the Web Audio API, only supported on iOS 6.
- }
- // #Settings
- // _Applying settings and listening for any settings changes_ //
- $('#settings ul.temperature li[data-unit="'+unit+'"]').addClass('current');
- $('#settings ul.wind li[data-unit="'+w_unit+'"]').addClass('current');
- $('#settings ul.colors li[data-theme="'+theme+'"]').addClass('current').data('set', true);
- var scrollable = document.getElementById("settings"); // WebKit in iOS 5 is fantastic, except for this weird behavior that bounces the entire view.
- new ScrollFix(scrollable); // And totally fixed now.
- function check(input) {
- var name = input.val();
- var index = input.parent().index();
- if (name!=null && name!="") {
- $.ajax({
- url: 'find.php',
- dataType: 'html',
- type: 'post',
- data: { city: name },
- success: function(data) {
- if(data=='0') {
- notification("The city <strong>" + name + "</strong> didn't return any results.");
- input.val($(this).data('city'));
- blur = true;
- input.blur();
- } else {
- // 554890@[email protected]@12.56761 -> WOEID@City@lat@lon
- //localStorage.setItem('city' + index, name);
- active = index + 2;
- blur = true;
- input.blur();
- var values = data.split("@");
- input.data('city', values[1]);
- input.val(values[1])
- $.ajax({
- url: 'api2.php',
- dataType: 'json',
- type: 'post',
- data: {
- id: values[0],
- lat: values[2],
- lon: values[3],
- timezone: timezone
- },
- success: function(data) {
- graphData[index + 1] = '';
- weather(data, index + 1, values[1]);
- localStorage.setItem('city' + (index), values[1]);
- localStorage.setItem('city' + (index) + '-lat', values[2]);
- localStorage.setItem('city' + (index) + '-lon', values[3]);
- localStorage.setItem('city' + (index) + '-id', values[0]);
- //playSound('plop3', 0);
- $('#layer').css('-webkit-transform', 'translate3d(0px, 0%, -' + width/2 + 'px) rotate3d(0, 1, 0, ' + current() + 'deg)');
- $('body').addClass('back').delay(250).queue(function() {
- isActive = false;
- isSettings = false;
- $(this).removeClass('settings back');
- notification(values[1] + ' added.');
- $(this).dequeue();
- });
- $('#small div.slide:eq('+(index + 1)+')').html($('#layer .slide:eq('+(index + 1)+')').html());
- }
- });
- }
- }
- });
- } else {
- input.val(input.data('city'));
- }
- }
- $('input').live('keydown', function(e) {
- if(e.which == 13) {
- e.preventDefault();
- check($(this));
- }
- });
- $('input').live('blur', function() {
- if(blur!=true) {
- check($(this));
- } else {
- blur = false;
- $(this).val($(this).data('city'));
- }
- isInputActive = false;
- });
- $('input').live('focus', function() {
- isInputActive = true;
- $(this).val('');
- });
- $('ul.colors li').live('touchstart', function() {
- if($(this).data('set') != true) $(this).addClass('current');
- });
- $('ul.colors li').live('touchend', function() {
- if($(this).data('set') != true) $(this).removeClass('current');
- });
- $('#settings ul li').live('tap', function() {
- // Change of temperature units
- if($(this).parent().attr('class') == 'temperature' && $(this).attr('class') != 'current') {
- unit = $(this).data('unit');
- localStorage.setItem('unit', unit);
- $(this).parent().children('.current').removeClass('current');
- $(this).addClass('current');
- $('.slide > .weather .info > span, .forecast .data strong, ul.underlay li .temp strong').each(function() {
- $(this).html(convert(parseInt($(this).html().split('°')[0]), unit) + '°');
- });
- for(var c = 0; c < 4; c++) {
- chart[c].destroy();
- chart[c] = '';
- var arry = graphData[c].substring(0, graphData[c].length - 2).split(', ');
- var newString = '';
- for(var i=0; i<arry.length; i++){
- newString = newString + convert(arry[i], unit) + ', ';
- }
- graphData[c] = newString;
- raw[c] = JSON.parse('[' + graphData[c].substring(0, graphData[c].length - 2) + ']');
- chart[c] = newChart(chartID[c], startDate[c], raw[c], chartColor[c]);
- }
- }
- // Change of wind units
- if($(this).parent().attr('class')=='wind' && $(this).attr('class')!='current') {
- w_unit = $(this).data('unit');
- localStorage.setItem('w_unit', w_unit);
- $(this).parent().children('.current').removeClass('current');
- $(this).addClass('current');
- $('ul.underlay li .temp span em').each(function() {
- var newWind = $(this).attr('data-orgwind');
- var save = newWind;
- if(w_unit == 'mph') {
- newWind = Math.round(newWind * 2.23693629);
- } else if (w_unit == 'km/h') {
- newWind = Math.round(newWind * 3.6);
- }
- $(this).parent().html('<em data-orgwind="'+save+'">'+newWind+'</em> ' + w_unit);
- })
- }
- // Change of themes
- if($(this).parent().attr('class')=='colors' && $(this).attr('class')!='current') {
- localStorage.setItem('theme', $(this).data('theme'));
- $.each(themes, function(i) {
- if($('body').hasClass(themes[i].toLowerCase())) {
- $('body').removeClass(themes[i].toLowerCase());
- }
- });
- $('body').addClass($(this).data('theme')).delay(1000).queue(function() {
- // for(var c = 0; c < 4; c++) {
- // chart[c].destroy();
- // chart[c] = '';
- // raw[c] = JSON.parse('[' + graphData[c].substring(0, graphData[c].length - 2) + ']');
- // chartColor[c] = $('#layer .slide:eq('+c+') .background').css("background-color");
- // chart[c] = newChart(chartID[c], startDate[c], raw[c], chartColor[c]);
- // $('#' + chartID[c]).parent().css('background-color', chartColor[c]);
- // }
- localStorage.setItem('restart', 'true');
- $('#layer').css('-webkit-transform', 'translate3d(0px, 0%, -' + width/2 + 'px) rotate3d(0, 1, 0, ' + current() + 'deg)');
- notification('Changing colors...');
- $('body').addClass('back').delay(250).queue(function() {
- isActive = false;
- isSettings = false;
- $(this).removeClass('settings back');
- $(this).delay(1500).queue(function() {
- location.reload();
- $(this).dequeue();
- });
- $(this).dequeue();
- });
- $(this).dequeue();
- });
- $(this).parent().children('.current').removeClass('current');
- $(this).addClass('current');
- }
- });
- // #Weather fetching
- // _Google and Yahoo! in wonderful cooperation. Beautiful_ //
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(
- function(position) {
- var lat = position.coords.latitude;
- var lon = position.coords.longitude;
- //lat = '34.04697';
- //lon = '-118.238983';
- $.ajax({ url:'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+lon+'&sensor=true',
- success: function(data){
- var city = false, country = false;
- for (var i = 0; i < data.results[4].address_components.length; i++) {
- for (var j = 0; j < data.results[4].address_components[i].types.length; j++) {
- if(data.results[4].address_components[i].types[j] == 'locality' && city == false) {
- city = data.results[4].address_components[i].long_name;
- }
- if(data.results[4].address_components[i].types[j] == 'country' && country == false) {
- country = data.results[4].address_components[i].long_name;
- }
- }
- }
- $.ajax({
- url: 'api2.php',
- dataType: 'json',
- type: 'post',
- data: {
- city: city + ',' + country,
- id: city.toLowerCase().replace(/[^a-zA-Z0-9]+/g, ""),
- lat: lat,
- lon: lon,
- timezone: timezone
- },
- success: function(data) {
- if(city == 'København') city = 'Copenhagen';
- $('.loading').remove();
- weather(data, '0', city, true);
- $('#small div.slide:eq(0)').html($('#layer .slide:eq(0)').html());
- $('body').removeClass('initialize');
- }
- });
- },
- error: function(request, status, error) {
- console.log('Error');
- }
- });
- },
- function(error) {
- notification('An error occurred', 'Your location could not be determined.');
- console.log(error);
- },
- {
- enableHighAccuracy: true,
- timeout: 10000,
- maximumAge: 0
- }
- );
- }
- $.each(cities, function(index) {
- var i = index + 1;
- $('#settings .cities').append('<li><span></span><input value="'+cities[index][0]+'" autocorrect="off" />');
- $('#settings .cities input').each(function() {
- $(this).data('city', $(this).val());
- });
- $('#layer').append('<div class="slide"><div class="background"></div><a class="in"><span>In</span></a><div class="pagination"><span></span><span></span><span></span><span></span></div><div class="weather"><div class="icon"></div><div class="info">' + cities[index][0].split(',')[0] + '</div></div></div>');
- $('#layer .slide:eq('+i+') .pagination span:eq('+i+')').addClass('active');
- $.ajax({
- url: 'api2.php',
- dataType: 'json',
- type: 'post',
- data: {
- lat: cities[index][1],
- lon: cities[index][2],
- id: cities[index][3],
- timezone: timezone
- },
- success: function(data) {
- weather(data, i, cities[index][0]);
- $('#small div.slide:eq(' + i + ')').html($('#layer .slide:eq(' + i + ')').html());
- }
- });
- });
- transform();
- doc.bind('orientationchange', transform);
- doc.bind('orientationchange', redraw);
- var slide, isSettings = false, pinchOut = false;
- $('#layer .slide').bind('touchstart', function(e) {
- e.preventDefault();
- var touch = event.touches[0];
- xOrg = touch.pageX;
- if($('body').hasClass('settings')) {
- isSettings = true;
- } else {
- isSettings = false;
- }
- $('.forecast.active').removeClass('active');
- $('#' + $(this).data('forecast')).addClass('active');
- });
- $('#layer .slide').bind('touchmove', function(e) {
- e.preventDefault();
- // Swiping
- if(isActive != true && event.touches.length == 1 && isSettings == false && pinchOut == false) {
- var touch = event.touches[0];
- xPos = touch.pageX;
- xCor = (xOrg-xPos)/4;
- current();
- $('#layer').css('-webkit-transform', 'translate3d(0px, 0px, -' + width/2 + 'px) rotate3d(0, 1, 0, ' + (degree - xCor) + 'deg)');
- e.preventDefault();
- }
- // Pinch in
- if(event.touches.length == 2 && event.scale > 1 && isActive != true && isSettings == false && pinchOut == false) {
- isActive = true;
- $('body').addClass('animate build');
- $(this).addClass('active');
- playSound('woosh', 0);
- $(this).delay(450).queue(function() {
- $('body').addClass('revealed');
- $(this).dequeue();
- });
- }
- // Pinch out
- if(event.touches.length == 2 && event.scale < 1 && isActive != true && isSettings == false && pinchOut == false) {
- if(!$('body').hasClass('out')) {
- isActive = true;
- playSound('woosh2', 0);
- var slider = $(this);
- var small = $('#small .slide:eq('+ slider.index() +')');
- $('body').addClass('out');
- small.siblings().addClass('non');
- small.addClass('active').delay(850).queue(function() {
- $(this).removeClass('active');
- $(this).dequeue();
- });
- }
- }
- });
- $('#layer .slide').bind('touchend', function(e) {
- if(isActive == false) {
- if(xCor > 10) {
- switch(active) {
- case 1:
- degree = -90;
- active = 2;
- break;
- case 2:
- degree = -180;
- active = 3;
- break;
- case 3:
- degree = -270;
- active = 4;
- break;
- case 4:
- degree = 0;
- active = 1;
- break;
- default:
- // Do nothing, yet?
- }
- $('#layer').css('-webkit-transform', 'translate3d(0px, 0px, -' + width/2 + 'px) rotate3d(0, 1, 0, ' + degree + 'deg)');
- } else if(xCor < -10) {
- switch(active) {
- case 1:
- degree = -270;
- active = 4;
- break;
- case 2:
- degree = 0;
- active = 1;
- break;
- case 3:
- degree = -90;
- active = 2;
- break;
- case 4:
- degree = -180;
- active = 3;
- break;
- default:
- // Do nothing, yet?
- }
- $('#layer').css('-webkit-transform', 'translate3d(0px, 0px, -' + width/2 + 'px) rotate3d(0, 1, 0, ' + degree + 'deg)');
- } else {
- switch(active) {
- case 1:
- degree = 0;
- active = 1;
- break;
- case 2:
- degree = -90;
- active = 2;
- break;
- case 3:
- degree = -180;
- active = 3;
- break;
- case 4:
- degree = -270;
- active = 4;
- break;
- default:
- // Do nothing, yet?
- }
- $('#layer').css('-webkit-transform', 'translate3d(0px, 0px, -' + width/2 + 'px) rotate3d(0, 1, 0, ' + degree + 'deg)');
- }
- xCor = 0;
- isActive = true;
- doc.delay(551).queue(function(){
- isActive = false;
- $(this).dequeue()
- });
- }
- });
- $('.forecast').live('touchmove', function(e) {
- if(event.touches.length == 2 && event.scale < 1 && pinchOut == false) {
- pinchOut = true;
- playSound('woosh2', 0);
- $('body').addClass('unbuild')
- $('body').removeClass('revealed build');
- $(this).delay(450).queue(function(){
- isActive = false;
- pinchOut = false;
- $('body').removeClass('animate unbuild');
- $('.slide.active').removeClass('active');
- $(this).dequeue();
- });
- e.preventDefault();
- }
- });
- if(localStorage.getItem("restart") == 'true') {
- fetching(); // Fetches all available sounds with the Web Audio API, only supported on iOS 6.
- localStorage.setItem('restart', 'false');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment