/* Depends on jQuery */ /* The logging level - 1 - error, 2 - warn, 3 - info, 4 - debug, 5 - trace */ var logLevel = 4; var contextPath = ""; var ctx = ""; /* An optional function to be called when we wish to open a new browser window */ var childBrowserFunction; function registerChildBrowserFunction(f){ childBrowserFunction = f; } /** * Used when running within Phonegap * @param url */ function openChildBrowser(url){ try { //both of these should work... top.window.plugins.childBrowser.showWebPage(url); //window.plugins.childBrowser.showWebPage(url); debug("ChildBrowser OK"); } catch (ex1) { try{ //childBrowserFunction("http://news.bbc.co.uk"); childBrowserFunction(url); }catch(ex2){ //Fall back to opening it in the current webview log("ChildBrowser not Present "+ex2+" falling back to changing the location of the current page"); document.location = url; } } } //TO RUN FUNCTIONS WITHIN AN IFRAME do.... //var $f = $("#myIFrame"); //var fd = $f[0].document || $f[0].contentWindow.document; // document of iframe //fd.MyFunction(); // run function function initialiseLogging(){ jQuery.fn.log = function (msg) { try{ if(console==null){ $("#logging").append(msg+"
"); }else{ //console.log("%s: %o", msg, this); console.log(msg, this); } }catch(ex){} return this; }; if($("#logging").length==0){ //Create logging console $("body").prepend(""); if(getUrlParameter("debug")=="true" || getUrlParameter("debug")=="1"){ $("#logging").show(); } //document.onkeypress = function(tEvt){ $(window).keypress(function(tEvt){ //If Ctrl + ] show debug panel if(tEvt !=null && tEvt.which == 93 && tEvt.ctrlKey){ $("#logging").toggle(); }else if(window.event!=null && window.event.keyCode == 29 && window.event.ctrlKey==true){ $("#logging").toggle(); }else{ } } ); $("#logging").bind("tap",function(){ $("#logging").hide(); }); } } function getStyleProp(style, prop){ var d = $("
"); $(document.body).append(d); d.addClass(style); var val = d.css(prop); d.remove(); return val; } function getStylePropAsInt(style, prop){ var v = getStyleProp(style, prop); return parseInt(v.substring(0,v.indexOf("px"))); } /* Get the contents of a url */ function stripUrl(str){ var i = str.toLowerCase().indexOf("url("); var iEnd = str.indexOf(")",i); if(i>=0 && iEnd>i){ var v = str.substring(i+4,iEnd); //we also cut out the " OR ' if(v.indexOf('"') >= 0 || v.indexOf('"')){ v = v.substring(1,v.length-1); } return v; }else{ return str; } } function error(str){ if(logLevel>=1){ log(str); } } function warn(str){ if(logLevel>=2){ log(str); } } function info(str){ if(logLevel>=3){ log(str); } } function trace(str){ if(logLevel>=5){ log(str); } } function debug(str){ if(logLevel>=4){ log(str); } } function log(str){ if(logLevel>0){ try{ $("").log(str); }catch(ex){ } if($("#logging").html()!=null && $("#logging").html().length>20000){ $("#logging").html(""); //clear console } $("#logging").append(str+"
"); } } function removeCSS(css_href){ } function loadCSS(css_href){ var head = document.getElementsByTagName('head')[0]; $(document.createElement('link')).attr({type: 'text/css', href: css_href, rel: 'stylesheet', media: 'screen'}).appendTo(head); //$("head").append(""); //$("head").children(":last").attr({"rel": "stylesheet","type": "text/css","hrea": css_href}); } function isIDevice(){ if( isIPad() || isIPod() || isIPhone() ) { //if( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) ) { return true; }else{ return false; } } function isIPad(){ try{ return navigator.platform == "iPad"; }catch(e){ return navigator.userAgent.match(/iPad/i); } } function isIPod(){ try{ return navigator.platform == "iPod"; }catch(e){ return navigator.userAgent.match(/iPod/i); } } function isIPhone(){ try{ return navigator.platform == "iPhone"; }catch(e){ return navigator.userAgent.match(/iPhone/i); } } function isIE(){ return navigator.appName == "Microsoft Internet Explorer"; } function isAndroid21(){ try{ return navigator.userAgent.match(/Android 2.1/); }catch(ex){ return false; } } function isAndroid23(){ try{ return navigator.userAgent.match(/Android 2.3/); }catch(ex){ return false; } } //TODO we need to set a cookie if the users wishes to use a specific sites version //if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "/?iphone&i=COMR"; function loadjscssfile(filename, filetype){ if (filetype == "js"){ //if filename is a external JavaScript file var fileref = document.createElement("script"); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", filename); }else if (filetype == "css"){ //if filename is an external CSS file var fileref = document.createElement("link"); fileref.setAttribute("rel", "stylesheet"); fileref.setAttribute("type", "text/css"); fileref.setAttribute("href", filename); } if (typeof fileref != "undefined"){ document.getElementsByTagName("head")[0].appendChild(fileref); } } function hsv2rgb(hue, sat, val) { var red, grn, blu, i, f, p, q, t; hue%=360; if(val==0) {return({r:0, g:0, v:0});} sat/=100; val/=100; hue/=60; i = Math.floor(hue); f = hue-i; p = val*(1-sat); q = val*(1-(sat*f)); t = val*(1-(sat*(1-f))); if (i==0) {red=val; grn=t; blu=p;} else if (i==1) {red=q; grn=val; blu=p;} else if (i==2) {red=p; grn=val; blu=t;} else if (i==3) {red=p; grn=q; blu=val;} else if (i==4) {red=t; grn=p; blu=val;} else if (i==5) {red=val; grn=p; blu=q;} red = Math.floor(red*255); grn = Math.floor(grn*255); blu = Math.floor(blu*255); return ({r:red, g:grn, b:blu}); } // function rgb2hsv(red, grn, blu) { var x, val, f, i, hue, sat, val; red/=255; grn/=255; blu/=255; x = Math.min(Math.min(red, grn), blu); val = Math.max(Math.max(red, grn), blu); if (x==val){ return({h:undefined, s:0, v:val*100}); } f = (red == x) ? grn-blu : ((grn == x) ? blu-red : red-grn); i = (red == x) ? 3 : ((grn == x) ? 5 : 1); hue = Math.floor((i-f/(val-x))*60)%360; sat = Math.floor(((val-x)/val)*100); val = Math.floor(val*100); return({h:hue, s:sat, v:val}); } function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)} function toHex(N) { if (N==null) return "00"; N=parseInt(N); if (N==0 || isNaN(N)) return "00"; N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N); return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16); } function getStackTraceAsStr(ex){ var str = "Error:
"; str+=ex.message+"
"; if(ex.stackTrace!=null){ for(i in ex.stackTrace){ str+=ex.stackTrace[i].className+"."+ex.stackTrace[i].methodName+":"+ex.stackTrace[i].lineNumber+"
"; } }else if(ex.lineNumber != null && ex.message != null){ str = "Line:"+ex.lineNumber+"
"+ex.message; }else{ str = "ERROR "+ex.message; } str+="
"; return str; } var perfomanceLevel; /** * Used to degrade gracefully on slower devices * @return 0 - no animation at all */ function getPerformanceLevel() { //Set it once the first time if(perfomanceLevel==null){ var tick = 0; var start_t = new Date().getTime(); while(new Date().getTime() - start_t < 20) { Math.random(); tick++; } perfomanceLevel = tick/1000; info("perfomanceLevel="+perfomanceLevel); } return perfomanceLevel; } /** * Useful for checking things are all tidied up correctly */ function getDocumentSummary(){ try{ //var cc = document.getElementsByTagName("script"); //var cc = document.all; var o = ""; var tag; var tags = []; for(i = 0; i < document.all.length; i++) { tag = document.all(i).tagName; if(tags[tag] == null){ tags[tag] = 1; }else{ tags[tag] = tags[tag]+1; } } for(x in tags) { o += x+"="+tags[x]+"
"; } return o; }catch(ee){ showWarning(ee); } } /* $(function(){ $.extend($.fn.disableTextSelect = function() { return this.each(function(){ if($.browser.mozilla){//Firefox $(this).css('MozUserSelect','none'); }else if($.browser.msie){//IE $(this).bind('selectstart',function(){return false;}); }else{//Opera, etc. $(this).mousedown(function(){return false;}); } }); }); }); */ function getCookie ( cookie_name ) { var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' ); if ( results ) return ( unescape ( results[2] ) ); else return ""; } function setCookie ( name, value, expires, path, domain, secure ) { var cookie_string = name + "=" + escape ( value ); if ( expires ) { cookie_string += "; expires=" + expires.toGMTString(); //If no date is include the cookie expires at the end of the session } if ( path ) cookie_string += "; path=" + escape ( path ); if ( domain ) cookie_string += "; domain=" + escape ( domain ); if ( secure ) cookie_string += "; secure"; document.cookie = cookie_string; } /** * Applies a continous simple wiggle movement to an element. Good for attracting attention to an element */ function addWiggleAnimation1(element, vDelta, frequency){ if(vDelta==null){ vDelta = 8; } if(frequency==null){ frequency = 2000; } element.animate({"top": "-="+vDelta+"px"},50).animate({"top": "+="+(vDelta+2)+"px"},50).animate({"top": "-=2px"},25,function(){ setTimeout(function(){ addWiggleAnimation1(element); },2000); }); } function addWiggleAnimation2(element, vDelta, frequency){ if(vDelta==null){ vDelta = 8; } if(frequency==null){ frequency = 2000; } element.animate({"top": "-="+vDelta+"px"},25) .animate({"top": "+="+(vDelta*2)+"px"},50) .animate({"top": "-="+(vDelta*2)+"px"},50) .animate({"top": "+="+(vDelta*2)+"px"},50) .animate({"top": "-="+(vDelta*2)+"px"},50) .animate({"top": "+="+(vDelta*2)+"px"},50) .animate({"top": "-="+(vDelta*2)+"px"},50) .animate({"top": "+="+(vDelta)+"px"},25 ,function(){ setTimeout(function(){ addWiggleAnimation2(element); },2000); }); } /** * Add a light glare to an element. * The element must be overflow:hidden & positive:relative for it to work properly * @param element */ function addGlareAnimation(element){ var glare = $(""); element.append(glare); var h = glare.height(); //alert(h); glare.css("top","-"+h+"px"); glare.show(); glare.animate({"top": "+="+h*2+"px"},1300, function(){ glare.remove(); setTimeout(function(){ addGlareAnimation(element); },5000); }); } /** * Applies a continous pulsating effect to an element */ function addPulse(element, amount){ if(amount==null){ amount = 0.75; } var a = amount*20+"px"; var a2 = (amount*20)*2+"px"; var mL = element.attr("data-margin-left"); var mR = element.attr("data-margin-right"); var h = element.attr("data-height"); if(mL==null){ //Store this the first time. //FIXME - we need to get the non computed value for this to work if the page is resized. mL = element.css("margin-left"); mR = element.css("margin-right"); h = element.css("height"); element.attr("data-margin-left", mL); element.attr("data-margin-right", mR); element.attr("data-height", h); } element.animate({"margin-left":"-="+a,"height":"+="+a2,"margin-top":"-="+a},1000). animate({"margin-left":"+="+a,"height":"-="+a2,"margin-top":"+="+a},1000,function(){ //we set these to avoid drift from rounding errors element.css("margin-left",mL); element.css("margin-right",mR); element.css("height",h); setTimeout(function(){ addPulse(element, amount); },3000); }); } function clickEffect(element){ var amount = 0.05; element.animate({"margin-left":"-="+amount+"em","margin-top":"-="+amount+"em"},0). animate({"margin-left":"+="+amount+"em","margin-top":"+="+amount+"em"},50,function(){ element.css("margin-left", "auto"); element.css("margin-top", "auto"); }); } /** * Add a colourful flash animation */ function addFlash(element){ element.css("background","#0000ff"); element.animate({"background-color":"#ff0000"},500) .animate({"background-color":"#00ff00"},500) .animate({"background-color":"#0000ff"},500,function(){ addFlash(element); }); } function slideOnThenOffAnimation(element, callback){ var leftPos = element.css("left"); element.css("left","-15em"); element.show(); element.animate({"left": leftPos},300,function(){ setTimeout(function(){ element.animate({"left": "+=15em"},300,function(){ element.hide(); callback(); }); },1500); }); } /** * A utility function to replace the standard jQuery .load because that strips out * completely script tags when used with a selector, meaning they are never run or loaded. * * This method gets around that but, but jQuery removes dynamically loaded scripts meaning they're * not available for debugging. Consider using dynamicallyLoadJSFile() rather than inline script to avoid this. * * tUrl * returnedDataSelectorStr - the selector to specify what should be loaded from the incoming download * targetElement - where the content will be inserted into */ function myLoad(tUrl, returnedDataSelectorStr, targetElement){ $.ajax({ url: tUrl, data: {}, success: function (data) { data = data.replace(/