(function(){window.APIService=new function(){var a;return a=function(a,b,c){return null==b&&(b=null),null==c&&(c=!0),b||(null==a.data&&(a.data={}),a.data.cacheSlayer=(new Date).getTime(),a.data.access_token=$.cookie("access_token"),b=$.Deferred()),c?$.ajax(a).done(function(a,c,d){return b.resolve(a)}).fail(function(a,c,d){return b.reject(d)}):$.ajax(a).done(function(a,c,d){return a.code===!0&&b.reject(a),b.resolve(a)}).fail(function(a,c,d){var e;return 504===c&&(e={code:2010002}),b.reject(e)}),b.promise()},this.apiRequest=function(b){return b.url=QCONFIG.api_path+b.url,a(b)},this.tunnelApiRequest=function(b){return b.url=QCONFIG.tunnel_api_path+b.url,Logger.debug("arg.url",b.url),a(b)},this.setSession=function(b){var c;return c={method:"POST",url:"/ajax/set_smartshare_session",data:JSON.stringify(b),dataType:"json"},a(c,null,!1)},this.getSession=function(b){var c,d;return d={signed_redis_session_id:b},c={method:"GET",url:"/ajax/get_smartshare_session",data:d},a(c,null,!1)},this.login_status_check=function(a){var b;return b=$.Deferred(),!a&&a.client_id?b.reject():$.ajax({method:"POST",url:QCONFIG.auth_path+"/oauth/login_status",data:JSON.stringify({app_id:a.client_id}),xhrFields:{withCredentials:!0}}).done(function(a){return b.resolve(a)}).fail(function(a){return b.reject(a)}),b.promise()},this},window.Logger=new function(){return this.log=function(){return console?console.log.apply(console,arguments):void 0},this.debug=function(){return console?console.debug.apply(console,arguments):void 0},this.error=function(){return console?console.error.apply(console,arguments):void 0},this.info=function(){return console?console.info.apply(console,arguments):void 0},this.warn=function(){return console?console.warn.apply(console,arguments):void 0},this},window.getCookieExpireDate=function(){var a,b;return b=new Date,a=new Date(b.getFullYear()+50,b.getMonth(),b.getDate())},window.getCurrentLang=function(){var a,b,c,d,e,f,g,h,i,j;for(f="lang=",d=null,b=document.cookie.split(";"),g=0,i=b.length;i>g;g++){for(a=b[g];" "===a.charAt(0);)a=a.substring(1);if(0===a.indexOf(f)){d=a.substring(f.length,a.length);break}}for(d||(d=navigator.userLanguage||navigator.language,d="zh-TW"===d?"zh_TW":d.substring(0,2)),c=!1,h=0,j=SUPPORTED_LANGUAGES.length;j>h;h++)if(e=SUPPORTED_LANGUAGES[h],d===e.value){c=!0;break}return c||(d="en"),document.cookie="lang="+d+"; path=/; expires="+getCookieExpireDate(),d?d.replace("-","_"):void 0},window.change_copyright_year=function(){var a,b,c;return a=$("#copyright"),b=a.text(),c=b.replace("2013-2014",QCONFIG.year),a.text(c)},window.doAuthSignIn=function(a){var b,c,d;return b=QCONFIG.auth_path,c=QCONFIG.redirect_uri,d=new URI(b+"/oauth/auth"),d.setQuery("lang",getCurrentLang()),d.setQuery("response_type","token"),d.setQuery("client_id",QCONFIG.client_id),d.setQuery("redirect_uri",c),null!=$.cookie("not_notice_site_ip_check")&&d.setQuery("not_notice","yes"),a?d.setQuery("state",a):d.setQuery("state","/u"),window.location.href=d.href()},void 0===jQuery.when.all&&(jQuery.when.all=function(a){var b;return b=new jQuery.Deferred,$.when.apply(jQuery,a).then(function(){return b.resolveWith(this,[Array.prototype.slice.call(arguments)])},function(){return b.rejectWith(this,[Array.prototype.slice.call(arguments)])}),b}),window.SUPPORTED_LANGUAGES=[{value:"en",name:"English",en_name:"English"},{value:"zh_TW",name:"繁體中文",en_name:"Traditional Chinese"},{value:"zh",name:"简体中文",en_name:"Simplified Chinese"},{value:"cs",name:"Czech",en_name:"Czech"},{value:"da",name:"Dansk",en_name:"Danish"},{value:"de",name:"Deutsch",en_name:"German"},{value:"es",name:"Español",en_name:"Spanish"},{value:"fr",name:"Français",en_name:"French"},{value:"it",name:"Italiano",en_name:"Italian"},{value:"ja",name:"日本語",en_name:"Japanese"},{value:"ko",name:"한국",en_name:"Korean"},{value:"no",name:"Norsk",en_name:"Norwegian"},{value:"pl",name:"Polski",en_name:"Polish"},{value:"ru",name:"Русский",en_name:"Russian"},{value:"fi",name:"Suomi",en_name:"Finnish"},{value:"sv",name:"Svenska",en_name:"Swedish"},{value:"nl",name:"Nederlands",en_name:"Dutch"},{value:"tr",name:"Turk dili",en_name:"Turkish"},{value:"th",name:"ไทย",en_name:"Thai"},{value:"pt",name:"Português",en_name:"Portuguese"},{value:"hu",name:"Magyar",en_name:"Hungarian"},{value:"el",name:"Ελληνικά",en_name:"Greek"},{value:"ro",name:"Român",en_name:"Romanian"}]}).call(this),function(){var a,b,c,d,e=this;a=Logger,b=new function(){var d,f,g,h,i,j,k,l,m;return i="https",h="http",j="tunnel",d="/cgi-bin/qid/qidRequest.cgi",f=1e4,g="__qcpref",k="smarturl_",e.trackGA=function(a,b,c){var d;return"undefined"!=typeof ga&&null!==ga?(d=c,console.log("Do trackGA",a,b,c),console.log("QCONFIG.userid",QCONFIG.userid),null!=QCONFIG.userid&&(d=d+"_"+QCONFIG.userid),ga("send","event",{eventCategory:b,eventLabel:a,eventAction:d})):void 0},e.setProbeRouteToCookie=function(b,c,d){var e,f;return e=JSON.stringify(b),a.debug("setProbeRouteToCookie route",e),f=k+c+"_"+d,$.cookie(f,e,{expires:3650,path:"/"})},e.getPref=function(){return JSON.parse($.cookie(g)||null)},e.setPref=function(a){var b;return b=JSON.stringify(a),$.cookie(g,b,{path:"/"})},e.confirmUsingTunnel=function(b){var c=this;return $("#confirm_tunnel_modal").modal("show"),$("#confirm_tunnel_modal").on("click","button.close",function(){return $("#confirm_tunnel_modal").modal("hide"),b()}),$("#confirm_tunnel_modal").on("click","button.got_it",function(){var d,e,f;return e=$("#confirm_tunnel_modal input").val(),d=$("#confirm_tunnel_modal input").is(":checked"),a.info("choosed",d),f=c.getPref()||{},f&&f.auto_connect?f.auto_connect.skip_tunnel_relay_confirmation=d:f.auto_connect={skip_tunnel_relay_confirmation:d},c.setPref(f),$("#confirm_tunnel_modal").modal("hide"),b()})},e.smarturlProbeRoute=function(b,c,e,g,k,l,m,n,o){var p,q,r,s,t=this;return null==g&&(g=d),null==k&&(k=f),null==l&&(l=[]),s=new URI(b.url),r=$.Deferred(),p=s.query(!0),s.search(""),s.path(g),s.query({verify_device:1,device_token:c}),s.addQuery(p),s=s.href(),q={url:s,dataType:"jsonp",timeout:k},$.ajax(q).done(function(c,d,f){var g,k,l,m,o,p,q;if(null==c.verify_result||c.verify_result!==!0)return a.error("probe returns a false verify result at",s),r.resolve(c);switch(a.debug("probe success",s),console.log("Do PROTOCOL_TUNNEL 1",b.scheme),p=new URI(location.href),l=p.query(!0),m=new URI(""),l.state&&(m.constructor(l.state),delete l.state),delete l.check_over,m.addQuery(l),g=function(a,b){var c;return c=new URI(a),b.path().length>0&&c.segment(c.segment().concat(b.segment())),c.addQuery(b.query()),decodeURIComponent(c.href())},b.scheme){case i:return o=g(b.dest_url,m),a.debug("redirecting to",o),t.trackGA(b.type,b.scheme,n),t.setProbeRouteToCookie(b,n,e),window.location.href=o;case j:return console.log("Do PROTOCOL_TUNNEL 2",j),t.setProbeRouteToCookie(b,n,e),k=t.getPref(),q=function(){return this.trackGA(b.type,j,n),o=g(b.dest_url,m),window.location.href=o},k&&k.auto_connect&&k.auto_connect.skip_tunnel_relay_confirmation?q():t.confirmUsingTunnel(q);default:return o=g(b.dest_url,m),a.debug("redirecting to",o),t.setProbeRouteToCookie(b,n,e),t.trackGA(b.type,h,n),window.location.href=o}}).fail(function(c,d,e){return a.error("probe failed",s,d,e),b.scheme===i&&l.push(b.dest_url),r.resolve()}),r.promise()},e.smarturlChooseRoute=function(b,c,d,e,f,g){var h,i,j,k,l,m;return a.debug("smarturlChooseRoute among",b),j=$.Deferred(),k=this,h=[],i=[],l=function(){var a,e,g;for(g=[],a=0,e=b.length;e>a;a++)m=b[a],g.push(k.smarturlProbeRoute(m,c,d,null,QCONFIG.smarturl_probe_timeout_secs,f.user_retry_list,h,f.device_id,i));return g}(),$.when.all(l).then(function(b){var c,d;return a.debug("accessible_http_routes",h),a.debug("accessible_tunnel_route",i),a.debug("retry list",f.user_retry_list),h.length>0?(a.debug("redirect to",h[0].dest_url),d=new URI(h[0].dest_url),e&&(c=new URI(e),null!=c.path()&&d.path(c.path()),null!=c.query()&&d.query(c.query())),j.resolve({type:h[0].type,url:d.href()})):i.length>0?(a.debug("redirect to tunnel",i[0].dest_url),d=new URI(i[0].dest_url),e&&(c=new URI(e),c.path()&&d.path(c.path()),c.query()&&d.addQuery(c.query())),j.resolve({type:"tunnel",url:d.href()})):j.reject({type:"error",url:"",messages:"cloudlink_connect_device_failed"})}),j.promise()},e.probeSmartshareRoute=function(b,c,d,e){var f,g,h,i;return i=new URI(b.url),h=$.Deferred(),f=i.query(!0),i.search(""),i.path("/cgi-bin/qid/qidRequest.cgi"),i.query({verify_device:1,device_token:c}),i.addQuery(f),"tunnel"===b.type&&i.addQuery("tunnel_agent_id",e),i=i.href(),g={url:i,dataType:"jsonp",timeout:QCONFIG.smarturl_probe_timeout_secs},$.ajax(g).done(function(c,d,e){return a.debug("result",c),null!=c.verify_result&&c.verify_result===!0?(a.debug("pass route.type",b.type),b.pass=!0,h.resolve(b)):h.resolve(b)}).fail(function(c,d,e){return"tunnel"===b.type&&a.debug("probe failed"),h.resolve(b)}),h.promise()},e.redirectRoute=function(a,b){var c,d;a.pass&&(d=new URI(a.url),c=d.query(!0),d.search(""),d.path("/share.cgi"),d.query({ssid:b}),d.addQuery(c),d=d.href(),window.location.href=d)},e.redirectServiceRoute=function(b,c,d){var e,f,g,h,i,j,k,l;if(null==d&&(d=null),a.debug("redirectServiceRoute route",b),a.debug("redirectServiceRoute result",c),b.pass){if(a.debug("route.url",b.url),i=new URI(b.url),e=i.query(!0),i.search(""),i.path(c.service_url),i.hash(c.service_hash),i.addQuery(e),i.addQuery(c.service_query),"tunnel"===b.type&&null!=d){for(i.addQuery("tunnel_agent_id",d),h="access_token=",j=null,g=document.cookie.split(";"),k=0,l=g.length;l>k;k++){for(f=g[k];" "===f.charAt(0);)f=f.substring(1);if(0===f.indexOf(h)){j=f.substring(h.length,f.length);break}}a.debug("user_access_token",j),null!=j&&i.addQuery("access_token",j)}i=decodeURIComponent(i.href()),a.debug("redirectServiceRoute route_url",i),window.location.href=i}},e.redirectToRelayPage=function(b,c){var d,e;null==b&&(b=""),a.debug("redirectToRelayPage QCONFIG.portal_path",QCONFIG.portal_path),e=new URI(QCONFIG.portal_path),e.path("share/"+c),d=new URI(window.location),e.addQuery(d.query(!0)),e.addQuery("session_id",b),window.location.href=e},e.redirectToDeviceNotFoundPage=function(b){var c;null==b&&(b=null),a.debug("redirectToDeviceNotFoundPage",QCONFIG.portal_path),c=new URI(QCONFIG.portal_path),c.path("smarturl/device_not_found"),b&&c.search("code="+b),window.location.href=c},e.do_smartshare=function(b,c,d){var e,f,g,h,i,j,k,l,m,n=this;return a.debug("do_smartshare QCONFIG.portal_path",QCONFIG.portal_path),h=b.token,i=b.link_id,g=b.composed_link_id,m=b.routes,a.debug("do_smartshare routeList",JSON.stringify(m)),f=QCONFIG.auto_connect_max_requests,e=[],k=[],!m||m.length<=0?void(d&&APIService.setSession(d).then(function(b){return a.debug("do_smartshare setSmartshareSession",JSON.stringify(b)),c?n.redirectToDeviceNotFoundPage():n.redirectToRelayPage(b.result,g)})):(m.length>f?(e=m.slice(0,+(f-1)+1||9e9),k=m.slice(f)):e=m,j=function(){var a,c,d;for(d=[],a=0,c=e.length;c>a;a++)l=e[a],d.push(this.probeSmartshareRoute(l,h,i,b.device_id));return d}.call(this),$.when.all(j).then(function(d){var e,f,h,i;for(a.debug("QCONFIG",QCONFIG),a.debug("isService",c),h=0,i=d.length;i>h;h++)if(f=d[h],a.debug("all path back",f),f.pass)return void(c?n.redirectServiceRoute(f,b,b.device_id):APIService.setSession(d).then(function(b){return a.debug("do_smartshare promises",JSON.stringify(b.result)),n.redirectToRelayPage(b.result,g)}));try{window.stop()}catch(j){e=j,document.execCommand("Stop")}return b.routes=k,n.do_smartshare(b,c,d)}))},e.do_smarturl=function(a,b,d){var f,g,h,i,j;return null==b&&(b=null),j=a.token,i=a.selected_display_name,g=QCONFIG.auto_connect_max_requests,f=[],h=[],!a.routes||a.routes.length<=0?void(b&&(l(d.smarturl_routes),c(b.messages,d))):(a.routes.length>g?(f=a.routes.slice(0,+(g-1)+1||9e9),h=a.routes.slice(g)):f=a.routes,e.smarturlChooseRoute(f,j,i,a.query,d,getCurrentLang(),null).then(function(a){return m(a,d.device_id)},function(b){var c;try{window.stop()}catch(f){c=f,document.execCommand("Stop")}return a.routes=h,e.do_smarturl(a,b,d)}))},m=function(d,e){var f,g;return a.debug("_smarturlRedirect",d),f=this.getPref(),"error"===d.type?(hideLoading("full_screen"),c(d.messages)):"tunnel"!==d.type?(b.trackGA(d.type,d.scheme,e),window.location.href=d.url):(b.trackGA("tunnel","tunnel",e),g=function(){return window.location.href=d.url},f&&f.auto_connect&&f.auto_connect.skip_tunnel_relay_confirmation?g():b.confirmUsingTunnel(g))},l=function(a){var b,c,d,e,f,g,h;for(e=$(".method-list"),b=e.children(":first").clone(),e.html(""),e.append(b),c=b.clone().removeClass("hide"),g=0,h=a.length;h>g;g++)f=a[g],"tunnel"!==f.scheme&&"http"!==f.scheme&&(d=c.clone(),d.find("a").prop("href",f.url),d.find(".method-link").html(f.url),d.click(function(a){var b;return a.preventDefault(),b=window.open($(a.target).attr("href"),"_blank")}),e.append(d))},e},window.AutoDetectRoute=function(b){var e,f,g;return a.debug("Do AutoDetectRoute"),f=location.search,g={user_retry_list:[]},e="smarturl_",APIService.getSession(b).then(function(b){var d,f,h,i,j,k;if(j=b.result,a.debug("APIService.getSession result",JSON.stringify(j)),!j)return c("goto_service_missing_invalid_session_id",g);switch(g.device_name=j.device_name||"",g.device_id=j.device_id||"",null!=g.device_id&&g.device_id.length>0&&(d=e+g.device_id+"_"+j.selected_display_name,null!=$.cookie(d)&&(k=JSON.parse($.cookie(d)),j.routes.unshift(k),a.debug("unshift routes",JSON.stringify(j.routes)))),g.smarturl_routes=j.routes,f=$("#message_goto_loading_connect_to"),h=$("#loading .inner-loading-text"),i=f.html()+(" "+g.device_name+" ..."),h.html(i),j.service){case"smartshare":return do_smartshare(j,!1);case"smartshare_service":return do_smartshare(j,!0);case"smarturl":return do_smarturl(j,null,g)}},function(a){var b;return 2100002===(b=a.code)?c("goto_service_missing_invalid_session_id",g):c(a)}),$(".detect-result-box a").click(function(a){return a.preventDefault(),d(),AutoDetectRoute(b)}),$("#confirm_tunnel_modal button.close").click(function(){return $("#confirm_tunnel_modal").modal("hide")})},window.InitGoToPage=function(){return change_copyright_year()},c=function(a,b){var c,e;return null==a&&(a=null),null==b&&(b=null),$("#error_detail").html(""),a?(c=$("#message_"+a).html(),c?$("#error_detail").html(c):$("#error_detail").html("")):a=1020001,e=b&&b.smarturl_routes?!0:!1,d(!1,e)},d=function(a,b){return null==a&&(a=!0),null==b&&(b=!1),a?($("#loading").show(),$("#show_error").hide()):($("#loading").hide(),$("#show_error").show()),b?$("#retry_list").show():$("#retry_list").hide()}}.call(this);