//FWDMUtils (function (window){ var FWDMUtils = function(){}; FWDMUtils.dumy = document.createElement("div"); //###################################// /* String */ //###################################// FWDMUtils.trim = function(str){ return str.replace(/\s/gi, ""); }; FWDMUtils.trimAndFormatUrl = function(str){ str = str.toLocaleLowerCase(); str = str.replace(/ /g, "-"); str = str.replace(/ä/g, "a"); str = str.replace(/â/g, "a"); str = str.replace(/â/g, "a"); str = str.replace(/à/g, "a"); str = str.replace(/è/g, "e"); str = str.replace(/é/g, "e"); str = str.replace(/ë/g, "e"); str = str.replace(/ï/g, "i"); str = str.replace(/î/g, "i"); str = str.replace(/ù/g, "u"); str = str.replace(/ô/g, "o"); str = str.replace(/ù/g, "u"); str = str.replace(/û/g, "u"); str = str.replace(/ÿ/g, "y"); str = str.replace(/ç/g, "c"); str = str.replace(/œ/g, "ce"); return str; }; FWDMUtils.splitAndTrim = function(str, trim_bl, toLowerCase_bl){ var array = str.split(","); var length = array.length; for(var i=0; i 0){ if(e.nextElementSibling){ e = e.nextElementSibling; }else{ for(var e = e.nextSibling; e && e.nodeType !== 1; e = e.nextSibling); } n--; }else{ if(e.previousElementSibling){ e = e.previousElementSibling; }else{ for(var e = e.previousSibling; e && e.nodeType !== 1; e = e.previousSibling); } n++; } } return e; }; FWDMUtils.getChildAt = function (e, n){ var kids = FWDMUtils.getChildren(e); if(n < 0) n += kids.length; if(n < 0) return null; return kids[n]; }; FWDMUtils.getChildById = function(id){ return document.getElementById(id) || undefined; }; FWDMUtils.getChildren = function(e, allNodesTypes){ var kids = []; for(var c = e.firstChild; c != null; c = c.nextSibling){ if(allNodesTypes){ kids.push(c); }else if(c.nodeType === 1){ kids.push(c); } } return kids; }; FWDMUtils.getChildrenFromAttribute = function(e, attr, allNodesTypes){ var kids = []; for(var c = e.firstChild; c != null; c = c.nextSibling){ if(allNodesTypes && FWDMUtils.hasAttribute(c, attr)){ kids.push(c); }else if(c.nodeType === 1 && FWDMUtils.hasAttribute(c, attr)){ kids.push(c); } } return kids.length == 0 ? undefined : kids; }; FWDMUtils.getChildFromNodeListFromAttribute = function(e, attr, allNodesTypes){ for(var c = e.firstChild; c != null; c = c.nextSibling){ if(allNodesTypes && FWDMUtils.hasAttribute(c, attr)){ return c; }else if(c.nodeType === 1 && FWDMUtils.hasAttribute(c, attr)){ return c; } } return undefined; }; FWDMUtils.getAttributeValue = function(e, attr){ if(!FWDMUtils.hasAttribute(e, attr)) return undefined; return e.getAttribute(attr); }; FWDMUtils.hasAttribute = function(e, attr){ if(e.hasAttribute){ return e.hasAttribute(attr); }else { var test = e.attributes[attr]; return test ? true : false; } }; FWDMUtils.insertNodeAt = function(parent, child, n){ var children = FWDMUtils.children(parent); if(n < 0 || n > children.length){ throw new Error("invalid index!"); }else { parent.insertBefore(child, children[n]); }; }; FWDMUtils.hasCanvas = function(){ return Boolean(document.createElement("canvas")); }; //###################################// /* DOM geometry */ //##################################// FWDMUtils.hitTest = function(target, x, y){ var hit = false; if(!target) throw Error("Hit test target is null!"); var rect = target.getBoundingClientRect(); if(x >= rect.left && x <= rect.left +(rect.right - rect.left) && y >= rect.top && y <= rect.top + (rect.bottom - rect.top)) return true; return false; }; FWDMUtils.getScrollOffsets = function(){ //all browsers if(window.pageXOffset != null) return{x:window.pageXOffset, y:window.pageYOffset}; //ie7/ie8 if(document.compatMode == "CSS1Compat"){ return({x:document.documentElement.scrollLeft, y:document.documentElement.scrollTop}); } }; FWDMUtils.getViewportSize = function(){ if(FWDMUtils.isMobile) return {w:window.innerWidth, h:window.innerHeight}; return {w:document.documentElement.clientWidth || window.innerWidth, h:document.documentElement.clientHeight || window.innerHeight}; }; FWDMUtils.getViewportMouseCoordinates = function(e){ var offsets = FWDMUtils.getScrollOffsets(); if(e.touches){ return{ screenX:e.touches[0] == undefined ? e.touches.pageX - offsets.x :e.touches[0].pageX - offsets.x, screenY:e.touches[0] == undefined ? e.touches.pageY - offsets.y :e.touches[0].pageY - offsets.y }; } return{ screenX: e.clientX == undefined ? e.pageX - offsets.x : e.clientX, screenY: e.clientY == undefined ? e.pageY - offsets.y : e.clientY }; }; //###################################// /* Browsers test */ //##################################// FWDMUtils.hasPointerEvent = (function(){ return Boolean(window.navigator.msPointerEnabled); }()); FWDMUtils.isMobile = (function (){ if(FWDMUtils.hasPointerEvent && navigator.msMaxTouchPoints > 1) return true; var agents = ['android', 'webos', 'iphone', 'ipad', 'blackberry']; for(i in agents) { if(navigator.userAgent.toLowerCase().indexOf(agents[i].toLowerCase()) != -1) { return true; } } if(navigator.platform.toLowerCase() === 'macintel' && navigator.maxTouchPoints > 1 && !window.MSStream) return true; return false; }()); FWDMUtils.isLocal = (function(){ return document.location.protocol == "file:"; }()); FWDMUtils.isAndroid = (function(){ return (navigator.userAgent.toLowerCase().indexOf("android".toLowerCase()) != -1); }()); FWDMUtils.isChrome = (function(){ return navigator.userAgent.toLowerCase().indexOf('chrome') != -1; }()); FWDMUtils.isSafari = (function(){ return navigator.userAgent.toLowerCase().indexOf('safari') != -1 && navigator.userAgent.toLowerCase().indexOf('chrome') == -1; }()); FWDMUtils.isOpera = (function(){ return navigator.userAgent.toLowerCase().indexOf('opera') != -1 && navigator.userAgent.toLowerCase().indexOf('chrome') == -1; }()); FWDMUtils.isFirefox = (function(){ return navigator.userAgent.toLowerCase().indexOf('firefox') != -1; }()); FWDMUtils.isIE = (function(){ return navigator.userAgent.toLowerCase().indexOf('msie') != -1 || /Trident/ig.test(navigator.userAgent); }()); FWDMUtils.isIEAndLessThen9 = (function(){ return navigator.userAgent.toLowerCase().indexOf("msie 7") != -1 || navigator.userAgent.toLowerCase().indexOf("msie 8") != -1; }()); FWDMUtils.isApple = (function(){ return navigator.appVersion.toLowerCase().indexOf('mac') != -1;; }()); FWDMUtils.hasFullScreen = (function(){ return FWDMUtils.dumy.requestFullScreen || FWDMUtils.dumy.mozRequestFullScreen || FWDMUtils.dumy.webkitRequestFullScreen || FWDMUtils.dumy.msieRequestFullScreen; }()); FWDMUtils.get3d = function(){ var properties = ['transform', 'msTransform', 'WebkitTransform', 'MozTransform', 'OTransform', 'KhtmlTransform']; var p; var position; while (p = properties.shift()) { if (typeof FWDMUtils.dumy.style[p] !== 'undefined') { FWDMUtils.dumy.style.position = "absolute"; position = FWDMUtils.dumy.getBoundingClientRect().left; FWDMUtils.dumy.style[p] = 'translate3d(500px, 0px, 0px)'; position = Math.abs(FWDMUtils.dumy.getBoundingClientRect().left - position); if(position > 100 && position < 900){ try{document.documentElement.removeChild(FWDMUtils.dumy);}catch(e){} return true; } } } try{document.documentElement.removeChild(FWDMUtils.dumy);}catch(e){} return false; }; FWDMUtils.get2d = function(){ var properties = ['transform', 'msTransform', 'WebkitTransform', 'MozTransform', 'OTransform', 'KhtmlTransform']; var p; while (p = properties.shift()) { if (typeof FWDMUtils.dumy.style[p] !== 'undefined') { return true; } } try{document.documentElement.removeChild(FWDMUtils.dumy);}catch(e){} return false; }; //###############################################// /* various utils */ //###############################################// FWDMUtils.onReady = function(callbalk){ if(document.addEventListener) { document.addEventListener( "DOMContentLoaded", function(){ document.documentElement.appendChild(FWDMUtils.dumy); callbalk(); }); }else{ document.onreadystatechange = function () { document.documentElement.appendChild(FWDMUtils.dumy); if (document.readyState == "complete") callbalk(); }; } }; FWDMUtils.checkIfHasTransforms = function(){ if(FWDMUtils.isReadyMethodCalled_bl) return; document.documentElement.appendChild(FWDMUtils.dumy); FWDMUtils.hasTransform3d = FWDMUtils.get3d(); //FWDMUtils.hasTransform3d = false; FWDMUtils.hasTransform2d = FWDMUtils.get2d(); FWDMUtils.isReadyMethodCalled_bl = true; }; FWDMUtils.disableElementSelection = function(e){ try{e.style.userSelect = "none";}catch(e){}; try{e.style.MozUserSelect = "none";}catch(e){}; try{e.style.webkitUserSelect = "none";}catch(e){}; try{e.style.khtmlUserSelect = "none";}catch(e){}; try{e.style.oUserSelect = "none";}catch(e){}; try{e.style.msUserSelect = "none";}catch(e){}; try{e.msUserSelect = "none";}catch(e){}; e.onselectstart = function(){return false;}; }; FWDMUtils.getUrlArgs = function urlArgs(string){ var args = {}; var query = string.substr(string.indexOf("?") + 1) || location.search.substring(1); var pairs = query.split("&"); for(var i=0; i< pairs.length; i++){ var pos = pairs[i].indexOf("="); var name = pairs[i].substring(0,pos); var value = pairs[i].substring(pos + 1); value = decodeURIComponent(value); args[name] = value; } return args; }; FWDMUtils.validateEmail = function(mail){ if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail)){ return true; } return false; }; //################################// /* resize utils */ //################################// FWDMUtils.resizeDoWithLimit = function( displayObject, containerWidth, containerHeight, doWidth, doHeight, removeWidthOffset, removeHeightOffset, offsetX, offsetY, animate, pDuration, pDelay, pEase ) { var containerWidth = containerWidth - removeWidthOffset; var containerHeight = containerHeight - removeHeightOffset; var scaleX = containerWidth/doWidth; var scaleY = containerHeight/doHeight; var totalScale = 0; if(scaleX <= scaleY){ totalScale = scaleX; }else if(scaleX >= scaleY){ totalScale = scaleY; } var finalWidth = Math.round((doWidth * totalScale)); var finalHeight = Math.round((doHeight * totalScale)); var x = Math.floor((containerWidth - (doWidth * totalScale))/2 + offsetX); var y = Math.floor((containerHeight - (doHeight * totalScale))/2 + offsetY); if(animate){ FWDAnimation.to(displayObject, pDuration, {x:x, y:y, w:finalWidth, h:finalHeight, delay:pDelay, ease:pEase}); }else{ displayObject.x = x; displayObject.y = y; displayObject.w = finalWidth; displayObject.h = finalHeight; } }; window.FWDMUtils = FWDMUtils; }(window));