"use stricts"; /* 載入步驟: 1. HTML 載入內容,或是 getPage() 從 pages / xhr 抓到內容後由 insertPage() 處理 2. loadPage() 觸發 pageload event ,載入加入內容後需要的函式(無論是手機或桌面版面) 2.1 如果是手機版面 2.1.1 執行 deferLoad() 把不該載入的 iframe 洗掉 2.1.2 掛上 resize event,如果視窗被回復成桌面大小,執行 resumeLoad() 和 fullLoad() 2.2 如果是桌面版面 2.2.1 執行 fullLoad() 2.2.2 掃描網頁上的 ,如果是站內連結加入 prefetchQueue,起始 fetchPage() 執行背景抓取 2.2.3 fetchPage() 抓到頁面會將 HTML 存入 pages Array getPage() 是在 HTML5 History API 存在的情況下才會使用的 partial loading 方法。 */ jQuery(function ($) { var lang = ($('html').attr('lang') || 'zh-TW').toLowerCase(), ctTimer; function isMobileLayout() { return !$('#title:visible').length; } if ($('#nav.empty').length) { // Fetch site nav from remove JSON api $.getJSON( 'https://coscup.org/2011/api/menu.json', function (data) { var $nav = $('#nav').removeClass('empty'); $nav.html(data[lang].replace(/href="(\/[^\/])/g, 'href="https://coscup.org$1')); $('#nav a[href*="' + window.location.hostname + '"]').parent().addClass('current'); } ); } function mobileSponsorLogo() { var pool = [], multi = { diamond: 10, gold: 5, silver: 2, bronze: 1 }; $('#mobileSponsorLogo').remove(); $.each( multi, function (level, m) { $('#sidebar > .sponsors .' + level + ' a').each( function () { var i = m; while (i--) { pool.push(this); } } ); } ); if (pool.length === 0) return; $('#nav').after( $('').append( $(pool[Math.floor(pool.length * Math.random())]).clone() ) ); } $(window).bind('pageload', mobileSponsorLogo); function currentSessionShortcut() { // Program page only if (!$('.shortcuts')) return; // Not running after the event if ( (new Date()).getTime() > parseInt($('.program tbody th:last').attr('rel') + '000', 10) ) return; var $a = $('').text( { 'zh-tw': '目前議程', 'zh-cn': '目前议程', 'en': 'Current Session' }[lang] ); $a.bind( 'click', function (ev) { var ct = (new Date()).getTime(), target; ev.stopPropagation(); ev.preventDefault(); $('.program tbody th').each( function (i, el) { var ot = parseInt($(el).attr('rel') + '000', 10); if (ct < ot && target) { $(document.body).animate( { 'scrollTop': $(target).offset().top - 20 }, 180 ); $(window).trigger('scroll'); return false; } if (ct > ot) { target = el; } } ); if (!target) { $(document.body).animate( { 'scrollTop': $('.program tbody th:first').offset().top - 20 }, 180 ); $(window).trigger('scroll'); } } ); $('.shortcuts').append($('
').append($a)); // back button $('.shortcuts').after( '' ); var $mobile_top = $('.mobile_top'), mtTimer; $mobile_top.bind( 'click', function () { $(window).trigger('scroll'); $(document.body).animate( { 'scrollTop': $('.shortcuts').offset().top - 20 }, 180 ); return false; } ); $(window).bind( 'resize.mt scroll.mt', function () { $mobile_top.removeClass('show'); clearTimeout(mtTimer); mtTimer = setTimeout( function () { if ($('.shortcuts').offset().top < $(document.body).scrollTop()) { $mobile_top.css( 'top', $(document.body).scrollTop() + (window.innerHeight || $(window).height()) // innerHeight reports the correct viewpoint height in iPhone - $mobile_top.height() - 15 ).addClass('show'); } }, 200 ); } ); $(window).bind( 'pageload', function (ev) { clearTimeout(mtTimer); $(window).unbind('resize.mt scroll.mt').unbind(ev); } ); } $(window).bind('pageload', currentSessionShortcut); if ($('#sidebar > .sponsors.empty').length) { // Fetch sponsors from remove JSON api $.getJSON( 'https://coscup.org/2011/api/sponsors.json', function (data) { var $sponsors = $('#sidebar > .sponsors').removeClass('empty'); var titles = ( { 'en' : { 'diamond' : 'Diamond Level Sponsors', 'gold' : 'Gold Level Sponsors', 'silver' : 'Silver Level Sponsors', 'bronze' : 'Bronze Level Sponsors', 'media' : 'Media Partners' }, 'zh-tw' : { 'diamond' : '鑽石級贊助', 'gold' : '黃金級贊助', 'silver' : '白銀級贊助', 'bronze' : '青銅級贊助', 'media' : '媒體夥伴' }, 'zh-cn' : { 'diamond' : '钻石级赞助', 'gold' : '黄金级赞助', 'silver' : '白银级赞助', 'bronze' : '青铜级赞助', 'media' : '媒体伙伴' } } )[lang]; $.each( [ 'diamond', 'gold', 'silver', 'bronze', 'media' ], function (i, level) { if (!data[level]) return; $sponsors.append('You are currently using IPv6 connection.
' ); } else { $.getJSON( // See http://ipv6-test.com/api/ 'https://v6.ipv6-test.com/api/myip.php?json&callback=?', function (data) { if (window._gaq) _gaq.push(['_trackEvent', 'IPv6 2011', 'ready but not connected']); $('#ipv6block').addClass('show').append( 'Your network is IPv6 ready. Try it now by connect to ipv6.coscup.org.
' ); } ); } } */ if ($('#countdown-time').length) { ctTimer = setInterval( updateCountDown, 1000 ); $(window).bind( 'pageload', function (ev) { clearTimeout(ctTimer); $(window).unbind(ev); } ); updateCountDown(); $('#countdown').addClass('show'); } } function deferLoad() { $('.hideInMobile iframe').each( function () { $(this).attr('data-src', this.src); this.src = ''; } ); } function resumeLoad() { $('#sidebar2 iframe').each( function () { if ($(this).attr('data-src')) this.src = $(this).attr('data-src'); } ); } function loadPage() { $(window).trigger('pageload'); $('#header').css( 'background-position', 'center -' + (75*Math.floor(Math.random()*4)).toString(10) + 'px' ); $('#footer').css( 'background-position', 'center -' + (75*Math.floor(Math.random()*6)).toString(10) + 'px' ); // Find out if we are currently on mobile layout // if so, defer/stop imagetile and iframe from loading // removing 'src' in won't help so not doing it if (isMobileLayout()) { if (window._gaq) _gaq.push(['_trackEvent', 'Mobile 2011', window.location.href]); $(window).bind( 'resize.defer', function () { if (isMobileLayout()) return; $(this).unbind('resize.defer'); // load desktop stuff and stuff unloaded; fullLoad(); resumeLoad(); } ); // unload stuff deferLoad(); } else { // load desktop stuff fullLoad(); if ( window.history && history.pushState ) { // prefetch other pages $('a').each( function () { // skip external links if ( this.hostname !== window.location.hostname || !/2011/.test(this.pathname) || !(new RegExp(lang)).test(this.pathname.toLowerCase()) || this.href === window.location.href || pages[this.href] || pages[this.href] === 'fetching' || (/nocache/.test(this.getAttribute('rel'))) ) return; prefetchQueue.push(this.href); pages[this.href] = 'fetching'; // start the sequence if (prefetchQueue.length === 1) fetchPage(); } ); } } } var getPageXhr, pages = {}, prefetchQueue = []; function getPage(href, samepage, resetScroll) { $(window).unbind('resize.defer'); if (getPageXhr) getPageXhr.abort(); if (!samepage && pages[href] && pages[href] !== 'fetching') { if (resetScroll) $(window).scrollTop(0); insertPage(pages[href]); } else { var $content = $('#content').addClass('loading'); getPageXhr = $.ajax( { url: href, dataType: 'html', cache: !samepage, // nocache if user attempt to load the same page again complete: function (res, status) { if ( status === "success" || status === "notmodified" ) { $content.removeClass('loading'); pages[href] = res.responseText; if (resetScroll) $(window).scrollTop(0); insertPage(res.responseText); } else { window.location.replace(href); } } } ); } } function insertPage(html) { var $h = $('').append( html .match(//)[0] .replace(/