var foldedBlocksContentLoaded = new Array();
//var onFoldingActionHandler;
/**
 * Function for manipulating block content: fold and unfold it.
 */
function makeFoldingAction(eThis, wasOpen) {
	var eId = eThis.id + '.holder';
	
	if (-1 != eThis.id.indexOf('.title')) {
		eId =  eThis.id.substring(0, eThis.id.lastIndexOf('.title')) + '.holder';
	}

	var blockEl = document.getElementById(eId);
	if (blockEl != null) {
        var isOpen = (blockEl.style.display == 'block') ? 'open':'closed'; 
        blockEl.style.display = (isOpen == 'open') ? 'none' : 'block';
        if (eThis.className.indexOf('unfoldContentLink') || eThis.className.indexOf('foldContentLink')) {
			var cssTail = ''; 
			if (-1 != eThis.className.indexOf('unfoldContentLink')) {
				var cssStr = 'unfoldContentLink';
				cssTail = eThis.className.substring(eThis.className.lastIndexOf(cssStr) + cssStr.length);
			} else if (-1 != eThis.className.indexOf('foldContentLink')) {
				var cssStr = 'foldContentLink';
				cssTail = eThis.className.substring(eThis.className.lastIndexOf(cssStr) + cssStr.length);
			} else {
				cssTail = eThis.className;
			}
			
			eThis.className = ((isOpen == 'open') ? 'unfoldContentLink' : 'foldContentLink') + ' ' + cssTail;
			eThis.title = (isOpen == 'open') ? 'Развернуть' : 'Свернуть'
        }
        
    	Gallery.invokeCallbacks('onFoldingActionHandler', eThis, isOpen, wasOpen);
    }
}

function toggleFoldingBlock(eThisId, wasOpen) {
	var eThis = document.getElementById(eThisId);
	if (eThis) {
		makeFoldingAction(eThis, wasOpen);
	} else {
		var err = new Error('001', 'Can not find the element with ID=' + eThisId);
		err.name = 'getElementById error';
		throw (err);
	}
}

/**
 * Set the cookie value.
 */
function setFoldedBlockCookieValue(blockName, value)
{
	// get the folding cookie value using common.js function.
	var c_name = 'foldedBlocks';
	var cookieValue = Get_Cookie(c_name);
	if (cookieValue == null) {
		cookieValue = '';
	}
	var repFrom;
	var repTo;
	if (value == 'open') {
		repFrom = blockName + '_c';
		repTo = blockName + '_o';
	} else {
		repFrom = blockName + '_o';
		repTo = blockName + '_c';
	}
	if (cookieValue.indexOf(blockName) != -1) {
		cookieValue = cookieValue.replace(repFrom, repTo);
	} else {
		if (cookieValue != '') {
			cookieValue = cookieValue + '|';
		}
		cookieValue = cookieValue + repTo; 
	}
	
	var expiredays = 10;
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(cookieValue) + ((expiredays==null) ? "" : ";path=/;expires=" + exdate.toGMTString());
}

/**
 * Get the folding cookie values.
 */
function getFoldedBlockCookieValue(blockName)
{
	// get the folding cookie value using common.js function.
	var c_name = 'foldedBlocks';
	var cookieValue = Get_Cookie(c_name);
	
	var result = 'open';
	if (cookieValue != null) {
		var values = cookieValue.split("|"); 
		for (var i = 0; i < values.length; i++) {
			var blockValue = values[i]; 
			if (blockValue.indexOf(blockName) == 0) {
				// check if value ends with "block-closed" marker.
				if (blockValue.match('_c$')=='_c') {
					result = 'closed';				
				}
				break;
			}
		}
	}
	return result;
}