﻿		
var mttl_curr = 0,
	mttl_tabIDs = [],
	mttl_cntIDs = [],
	mttl_tabPref = 'mtt_',
	mttl_cntPref = 'mtc_',
	mttl_classOn = 'mttl_on',
	mttl_classHide = 'mttl_hide';

function MTTL_Init() {
	mttl_count = 0;
	while (document.getElementById(mttl_tabPref + mttl_count)) { mttl_count++ }
	for (var i = 0; i < mttl_count; i++) {
		mttl_tabIDs.push(mttl_tabPref + i);
		mttl_cntIDs.push(mttl_cntPref + i);
	}
}

function MTTL_CheckObject(obj) {
	if (typeof(obj) == 'string')
		obj = document.getElementById(obj);
	if (obj && typeof(obj) == 'object') {
		return [obj, true];
	} else {
		return [obj, false];
	}
}

function MTTL_AddStyleClass(obj, cName) {
	var checkObj = MTTL_CheckObject(obj);
	if (checkObj[1] && checkObj[0].className != cName // not equal
					&& checkObj[0].className.indexOf(cName + ' ') != 0 // not first
					&& checkObj[0].className.indexOf(' ' + cName + ' ') == -1 // not middle
					&& !(checkObj[0].className.indexOf(' ' + cName) != -1 && checkObj[0].className.indexOf(' ' + cName) == checkObj[0].className.length - cName.length - 1)) { // not last
		checkObj[0].className += ' ' + cName;
	}
}

function MTTL_AddStyleClassInArray(arrObj, cName) {
	for(var i = 0; i < arrObj.length; i++) {
		MTTL_AddStyleClass(arrObj[i], cName);
	}
}

function MTTL_RemoveStyleClass(obj, cName) {
	var checkObj = MTTL_CheckObject(obj);
	if (checkObj[1]) {
		if(checkObj[0].className == cName)
			checkObj[0].className = '';
		else if (checkObj[0].className.indexOf(' ' + cName + ' ') != -1) {
			reg = new RegExp(' ' + cName + ' ', 'g');
			checkObj[0].className = checkObj[0].className.replace(reg, ' ');
		}
		else if (checkObj[0].className.indexOf(cName + ' ') == 0)
			checkObj[0].className = checkObj[0].className.substr(cName.length + 1, checkObj[0].className.length);
		else if (checkObj[0].className.indexOf(' ' + cName) == checkObj[0].className.length - cName.length - 1)

			checkObj[0].className = checkObj[0].className.substr(0, checkObj[0].className.length - cName.length - 1);
	}
}

function MTTL_RemoveStyleClassInArray(arrObj, cName) {
	for(var i = 0; i < arrObj.length; i++) {
		MTTL_RemoveStyleClass(arrObj[i], cName);
	}
}

function MTTL_TabBehavior(objCurrTab, arrTabs, objCurrTabContent, arrTabsContent, objTargetStore, classOn, classHide, enableReCollapse) {
	var checkObjCurrTab = MTTL_CheckObject(objCurrTab),
		checkObjCurrTabContent = MTTL_CheckObject(objCurrTabContent),
		checkObjTargetStore = MTTL_CheckObject(objTargetStore),
		arrTabsObj = [],
		arrTabsContentObj = [],
		checkArrTabs = true,
		checkArrTabsContent = true,
		tmp;
	if (typeof(arrTabs) == 'object') {
		for (var i = 0; i < arrTabs.length; i++) {
			tmp = MTTL_CheckObject(arrTabs[i]);
			if (tmp[1]) {
				arrTabsObj.push(tmp);
			}
		}
	} else {
		var checkArrTabs = false;
	}
	if (typeof(arrTabsContent) == 'object') {
		for (var i = 0; i < arrTabsContent.length; i++) {
			tmp = MTTL_CheckObject(arrTabsContent[i]);
			if (tmp[1]) {
				arrTabsContentObj.push(tmp);
			}
		}
	} else {
		var checkArrTabsContent = false;
	}
	if (checkObjCurrTab[1] && checkObjCurrTabContent[1] && checkArrTabs && checkArrTabsContent) {
		if ((checkObjCurrTab[0].className.indexOf(classOn) == -1) || (enableReCollapse && checkObjCurrTab[0].className.indexOf(classOn) != -1)) {
			if (checkObjCurrTab[0].className.indexOf(classOn) == -1) {
				MTTL_AddStyleClass(checkObjCurrTab[0], classOn);
				MTTL_RemoveStyleClass(checkObjCurrTabContent[0], classHide);
				if (checkObjTargetStore[1]) {
					checkObjTargetStore[0].value = checkObjCurrTab[0].id;
				}
			} else {
				MTTL_RemoveStyleClass(checkObjCurrTab[0], classOn);
				MTTL_AddStyleClass(checkObjCurrTabContent[0], classHide);
				if (checkObjTargetStore[1]) {
					checkObjTargetStore[0].value = '';
				}
			}
			var arrOtherTabs = [];
			for (i = 0; i < arrTabsObj.length; i++) {
				if (arrTabsObj[i][0].id != checkObjCurrTab[0].id) {
					arrOtherTabs[i] = arrTabsObj[i][0];
				}
			}
			MTTL_RemoveStyleClassInArray(arrOtherTabs, classOn);
			var arrOtherTabsContent = [];
			for (i = 0; i < arrTabsContentObj.length; i++) {
				if (arrTabsContentObj[i][0].id != checkObjCurrTabContent[0].id) {
					arrOtherTabsContent[i] = arrTabsContentObj[i][0];
				}
			}
			MTTL_AddStyleClassInArray(arrOtherTabsContent, classHide);
		}
	}
	return false;
}

function MTTL_SetProgress(index, perm) {
	if (index >= 0 && index < mttl_count) {
		for (i = 0; i < mttl_count; i++) {
			if (i < index) {
				MTTL_AddStyleClass(mttl_tabPref + i, mttl_classOn);
				if (perm)
					MTTL_AddStyleClass(mttl_cntPref + i, mttl_classHide);
			} else if (i == index) {
				MTTL_AddStyleClass(mttl_tabPref + i, mttl_classOn);
				if (perm)
					MTTL_RemoveStyleClass(mttl_cntPref + i, mttl_classHide);
			} else {
				MTTL_RemoveStyleClass(mttl_tabPref + i, mttl_classOn);
				if (perm)
					MTTL_AddStyleClass(mttl_cntPref + i, mttl_classHide);
			}
		}
		if (perm)
			mttl_curr = index;
		if (mttl_curr == 0)
			MTTL_AddStyleClass('mttl_prev', 'mttl_dis');
		else
			MTTL_RemoveStyleClass('mttl_prev', 'mttl_dis');
		if (mttl_curr == mttl_count-1)
			MTTL_AddStyleClass('mttl_next', 'mttl_dis');
		else
			MTTL_RemoveStyleClass('mttl_next', 'mttl_dis');
	}
	return false;
}

