/* Common functions */
/* hasClass */
function hasClass(obj, className) {
	if (typeof obj == 'undefined' || obj == null || !RegExp) {
		return false;
	}
	var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
	if (typeof(obj) == 'string') {
		return re.test(obj);
	} else if (typeof(obj) == 'object' && obj.className) {
		return re.test(obj.className);
	}
	return false;
}

function set_cookie(name, value, expires) {
	if (!expires) {
		expires = new Date();
	}
	
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function save_cookie() {
	var expires = new Date(); // получаем текущую дату

	expires.setTime(expires.getTime() + (1000 * 86400)); // вычисляем срок хранения cookie
	set_cookie('mobile', 0, expires); // устанавливаем cookie с помощью функции set_cookie
}

/* getElementsByClassName for all browsers */
if (document.getElementsByClassName) {
	getElementsByClass = function(classList, node) {
		return (node || document).getElementsByClassName(classList)
	}
} else {
	getElementsByClass = function(classList, node) {
		var nodeItem = node || document,
			list = nodeItem.getElementsByTagName('*'),
			length = list.length,
			classArray = classList.split(/\s+/),
			classes = classArray.length,
			result = [];
		for (var i = 0; i < length; i++) {
			for(var j = 0; j < classes; j++)  {
				if (list[i].className.search('\\b' + classArray[j] + '\\b') != -1) {
					result.push(list[i]);
					break;
				}
			}
		}
		return result;
	}
}

/* show/hide function */
function displayView(el, isView) {
	el.style.display = isView ? 'block' : 'none';
}

/* get active child item in menu */
function getCheckLink(element) {
	for (var childItem in element.childNodes ) {
		for (var childItemLink in element.childNodes[childItem].childNodes){
				var currentItem = element.childNodes[childItem].childNodes[childItemLink];
				if (currentItem.getAttribute) {
					var childClassLink = currentItem.getAttribute('class');
					if (/active/.test(childClassLink)) {
						return currentItem;
					}
				}
		}
	}
	return null;
}

/* Main function */
function mobileInit() {
	document.getElementById('js-trigger-type').onclick = function() {
		 save_cookie();
	}
	var element = getElementsByClass('b-menu2')[0],
		elementLink = element.getElementsByTagName('li'),
		elementList = getCheckLink(element),
		checkLink = elementList,

		elementHide = getElementsByClass('active')[0];
		elementHideUl = null,

		newTable = getElementsByClass('service')[0],
		divContent = getElementsByClass('b-content')[0],
		elementTable = document.getElementsByTagName('table')[0],
		body = document.getElementsByTagName('body')[0],
		bodyHide = getElementsByClass('b-container')[0],

		tableWidthStr = null,
		tableHeightStr = null,
		tableWidth = null,
		tableHeight = null;

	if (elementHide) {
		elementHideUl = elementHide.getElementsByTagName('ul')[0];
	}

	if (elementHideUl){
		elementHideUl.style.display = 'block';
	}

	if (elementTable) {
		tableWidthStr = elementTable.getAttribute('width'),
		tableHeightStr = elementTable.getAttribute('height'),
		tableWidth = parseInt(tableWidthStr, 10),
		tableHeight = parseInt(tableHeightStr, 10);
	}

	if (tableWidth > 320) {
		newDiv = document.createElement('div');
		bodyHide.insertBefore(newDiv, divContent);
		newDiv.className = 'dummy';
		newTable = document.createElement('table');
		body.appendChild(newTable);
		newTable.id = 'service';
		newTable.className = 'b-service';
		elementTable.style.display = 'none';
		newTableId = document.getElementById('service');
		newDiv.onclick = function() {
			elementTable.style.display = 'table';
			newTableId.innerHTML = elementTable.innerHTML;
			bodyHide.style.display = 'none';
			newTableId.setAttribute('width', tableWidth);
			newTableId.setAttribute('height', tableHeight);
			newTableId.setAttribute('border', '1');
			newTableId.setAttribute('cellspacing', '1');
			newTableId.setAttribute('cellpadding', '1');
		}
	}
}
