var tomcode = tomcode || {};

tomcode.utils = tomcode.utils || {
	
	_baseUrl:'',
	
	baseUrl:function() {

		return tomcode._baseUrl || this.setBaseUrl();
	},

	setBaseUrl:function() {
		
		var tagList = document.getElementsByTagName('base');
		
		if(tagList.length) {
			
			tomcode._baseUrl = tomcode._baseUrl || tagList[0].getAttribute('href');
		}
		
		return tomcode._baseUrl;
	},
	
	addRule:function(selector, ruleString) {

		var sheet = document.styleSheets.item(0);
		
		switch(true) {
			
			case 'addRule' in sheet:
			sheet.addRule(selector, ruleString);
			break;
			
			// FF 4 raises a security error if the
			// attribute type=text/css	in the link
			// tag isn't set
			case 'insertRule' in sheet :
			ruleString = [selector, ' ', '{', ' ', ruleString, ' ', '}'].join('');
			sheet.insertRule(ruleString, sheet.cssRules.length);
			break;
		
			default : 
			// console.log('addRule', 'Oh no!');
			break;
		}
	},
	
	setLang:function() {
		
		tomcode.lang = document.querySelector('html').getAttribute('lang') || 'fr';
	},
	
	supportTags:function (tags) {

		// console.log('supportTags');
		
		var i;
		
		function tagSupported(tag) {

			var ret,
				t = document.createElement(tag),
				body = document.body || document.getElementsByTagName('body')[0];

			try { body.appendChild(t); } catch(e) {}

			if (window.getComputedStyle) {
	
				ret = window.getComputedStyle(t, null).getPropertyValue("display") === 'block';
			}

			if (t.currentStyle) {

				ret = t.currentStyle.display === 'block';
			}
			
			try { body.removeChild(t); } catch(e) {}
		}

		for(i = 0; i < tags.length; i++) {
			
			tagSupported(tags[i]);
		}
	},
	
	isArray:Array.isArray || function(obj) {

		return Object.prototype.toString.call(obj) === '[object Array]';
	},
	
	inArray:function(value, ar) {

		var i = 0,
			length =  ar.length || 0;

		for(i; i < length; i++) {

			if(ar[i] === value) {

				return true;
			}
		}
		
		return false;
	}
};
tomcode.utils.supportTags(['header', 'footer', 'menu', 'article', 'section']);


