var iTimerID = false;
var aCounter = new Array();
var MouseIsDown = false;
var MouseDownY = false;
var MouseDownX = false;

function CatalogDeleteField(id) {
    var oTable = document.getElementById('compare_table');
    var iRows = oTable.rows.length;
    var iCell = document.getElementById('catalog_cell_' + id).cellIndex;
    var sToCompare = GetCookie('to_compare');
    if (sToCompare) aCounter = GetCookie('to_compare').split(',');
    if (aCounter.length > 1) {
        for (var i = 0; i < iRows; i++) {
            oTable.rows[i].deleteCell(iCell);
        }
        aCounter.splice(IndexInArray(id), 1);
        document.cookie = 'to_compare=' + aCounter + '; path=/';
        document.cookie = 'str' + id + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
    } else {
        for (var i = 0; i < aCounter.length; i++) {
            document.cookie = 'str' + aCounter[i] + '=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
        }
        document.cookie = 'to_compare=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
        window.location = document.referrer;
    }
    L_compare_elements();
}

function L_cell_color(oCells, bDiff) {
    for (var i = 0; i < oCells.length; i++) {
        if (bDiff) {
            oCells[i].className = (i == 0) ? 'compare_head_diff' : 'compare_diff';
        } else {
            oCells[i].className = (i == 0) ? 'compare_head_same' : 'compare_same';
        }
    }
}

function L_compare_elements() {
    var oTable = document.getElementById('compare_table');
    var oRows = oTable.rows;
    for (var i = 0; i < oRows.length; i++) {
        if (oRows[i].className != 'compare_continue') {
            var oCells = oRows[i].cells;
            L_cell_color(oCells, true);
            if (oCells.lenght <= 2) break;
            var sData = oCells[1].getElementsByTagName('input')[0].value;
            for (var j = 2; j < oCells.length; j++) {
                if (oCells[j].getElementsByTagName('input')[0].value == sData) {
                    if (oCells.length == j + 1) L_cell_color(oCells, false);
                    continue;
                } else break;
            }
        }
    }
}

function SelectedCompare() {
    var oA, j;
	if (document.cookie != 'undefined') {
		var strCookieValue = GetCookie('to_compare');
		if (strCookieValue != null) {
			aCounter = strCookieValue.split(',');
			for (j = 0; j < aCounter.length; j++) {
            	var oSpan = document.getElementById('checkbox_label_' + aCounter[j]);
                if (oSpan) {
                    oSpan.innerHTML = '';
                    oA = document.createElement('a');
                    oA.href = '/catalog/compare.php';
                    oA.innerHTML = 'Сравнить';
                    oA.style.fontSize = '14px';
                    oSpan.appendChild(oA);
                }

				var oParentCheckbox = document.getElementById('checkbox-' + aCounter[j]);
				if (oParentCheckbox != null) oParentCheckbox.checked = true;
			}
		}
	}
}

function GetCookie(sName) {
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0])
			return unescape(aCrumb[1]);
	}
  return null;
}

function ParentChecked(element) {
    var oA;
	var oSpan = document.getElementById('checkbox_label_' + element.value);

	if (element.checked) {
		if (aCounter.length >= 4) {
			element.checked = false;
			alert('Для сравнения можно выбрать не более четырех товаров.');
		} else {
            oSpan.innerHTML = '';
            oA = document.createElement('a');
            oA.href = '/catalog/compare.php';
            oA.innerHTML = 'Сравнить';
            oA.style.fontSize = '14px';
			oSpan.appendChild(oA);

			aCounter.push(element.value);
			document.cookie = 'to_compare=' + aCounter.toString() + '; path=/';
		}
	} else if (!element.checked && aCounter.length > 1) {
        oSpan.innerHTML = 'В сравнение';
		aCounter.splice(IndexInArray(element.value), 1);
		document.cookie = 'to_compare=' + aCounter.toString() + '; path=/';
	} else {
        oSpan.innerHTML = 'В сравнение';
		aCounter = new Array();
		document.cookie = 'to_compare=; expires=Fri, 21 Dec 1976 04:31:24 GMT; path=/';
	}
}

function IndexInArray (element_value) {
	var i = 0;
	while (i < 4) {
		if (aCounter[i] == element_value) return i;
		i++;
	}
    return i;
}

