/*
	util.js - useful functions for creating amazing websites - v0.1
	Copyright 2011 b:dreizehn, Germany

	"util.js" is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.
	
	"util.js" is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with "util.js".	If not, see <http://www.gnu.org/licenses/>.
*/


if (!b13) {
	var b13 = {}
}
b13.util = {};
b13.util.browser = {

	// Browser & Device Checks
	isIE: function() { return ! + "\v1"; },
	isIPad: function() { return navigator.userAgent.match(/iPad/i) !== null; },
	isIPod: function() { return navigator.userAgent.match(/iPod/i) !== null; },
	isIPhone: function() { return navigator.userAgent.match(/iPhone/i) !== null; },
	isIOS: function() { return this.isIPad() || this.isIPhone() || this.isIPod(); },
	iOSVersion: function() {
		var match = navigator.userAgent.match(/OS (\d+)_/i);
		if (match && match[1]) {
			return match[1];
		}
	},
	isAndroid: function() { return navigator.userAgent.match(/Android/i) !== null; },
	androidVersion: function() {
		var match = navigator.userAgent.match(/Android (\d+)\./i);
		if (match && match[1]) {
			return match[1];
		}
	},
	
		// taken from modernizr
		// Check if the browser supports video.
	supportsVideo: function() {
        var elem = document.createElement('video'),
            bool = !!elem.canPlayType;
        
        if (bool) {  
            bool      = new Boolean(bool);  
            bool.ogg  = elem.canPlayType('video/ogg; codecs="theora"');
            
            // Workaround required for IE9, which doesn't report video support without audio codec specified.
            //   bug 599718 @ msft connect
            var h264 = 'video/mp4; codecs="avc1.42E01E';
            bool.h264 = elem.canPlayType(h264 + '"') || elem.canPlayType(h264 + ', mp4a.40.2"');
            
            bool.webm = elem.canPlayType('video/webm; codecs="vp8, vorbis"');
        }
        return bool;
	},

	flashVersion: function() {
		// check if the version is cached
		if (typeof b13.util.browser.flashVersion != "undefined") {
			return b13.util.browser.flashVersion;
		}

		var version = 0, desc;
		if (typeof navigator.plugins != "undefined" && typeof navigator.plugins["Shockwave Flash"] == "object") {
			desc = navigator.plugins["Shockwave Flash"].description;
			if (desc && !(typeof navigator.mimeTypes != "undefined" && navigator.mimeTypes["application/x-shockwave-flash"] && !navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)) {
				version = parseInt(desc.match(/^.*\s+([^\s]+)\.[^\s]+\s+[^\s]+$/)[1], 10);
			}
		} else if (typeof window.ActiveXObject != "undefined") {
			try {
				var testObject = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				if (testObject) {
					version = parseInt(testObject.GetVariable("$version").match(/^[^\s]+\s(\d+)/)[1], 10);
				}
			} catch(e) {}
		}
		b13.util.browser.flashVersion = version;
		return b13.util.browser.flashVersion;
	}

};
