function fold(hash)
{
	var SWITCH = {
		tag:        'h3',
		folded:     'switch',
		expanded:   'switch_expanded',
		folded_notop:     'switch_notopline',
		expanded_notop:   'switch_expanded_notopline'
	};
	
	var CSS = {
		folded:     'folded',
		expanded:   'expanded'
	};
		
	var LANG = {
		lt: {
			p1: 'Išskleisti viską',
			p2: 'Suskleisti viską'
		},
		en: {
			p1: 'View all',
			p2: 'Hide all'
		},
		lv: {
			p1: 'Parādīt visu',
			p2: 'Paslēpt visu'
		},
		ru: {
			p1: 'Показать все',
			p2: 'Спрятать все'
		}
	};
	
	var state	= 'searching'; // enum {'searching', 'expanding', 'folding'}
	
	var total_elems = 0;
	var total_folded_elems = 0;
	var total_expanded_elems = 0;	
	
	var refold	= function (i, element)
	{
		if (SWITCH.tag == element.tagName.toLowerCase())
		{
			elementId = $(element).attr('id') || null;
			switch (element.className.toLowerCase())
			{
				case SWITCH.folded_notop:
					if(hash != null && hash != '' && elementId == hash)
					{
						console.log(element.tagName.id);
						$(this).attr('class', SWITCH.expanded_notop);						
						setTimeout('fold(null)', 1);
					}

					$(element).unbind('click').click(function ()
					{
						$(this).attr('class', SWITCH.expanded_notop);						
						setTimeout('fold(null)', 1);
					});
					state   = 'folding';
					total_elems++;
					total_folded_elems++;
				break;

				case SWITCH.expanded_notop:
					$(element).unbind('click').click(function ()
					{
						$(this).attr('class', SWITCH.folded_notop);						
						setTimeout('fold(null)', 1);
					});
					state   = 'expanding';
					total_elems++;
					total_expanded_elems++;
				break;
				
				case SWITCH.folded:
					if(hash != null && hash != '' && elementId == hash)
					{
						$(this).attr('class', SWITCH.expanded);	
						setTimeout('fold(null)', 1);
					}

					$(element).unbind('click').click(function ()
					{
						$(this).attr('class', SWITCH.expanded);						
						setTimeout('fold(null)', 1);
					});
					state   = 'folding';
					total_elems++;
					total_folded_elems++;
				break;

				case SWITCH.expanded:
					$(element).unbind('click').click(function ()
					{
						$(this).attr('class', SWITCH.folded);						
						setTimeout('fold(null)', 1);
					});
					state   = 'expanding';
					total_elems++;
					total_expanded_elems++;
				break;

				default:
					state   = 'searching';
				break;
			}
		}
		else
		{
			if ('folding' == state) {
				$(element).addClass(CSS.folded);
			}
			else if ('expanding' == state) {
				$(element).removeClass(CSS.folded);
				$(element).addClass(CSS.expanded);
			}
		}
		
		if (total_elems > 0 && !$('#mobileFolding').attr('id')) {
			$('#foldAllLink').empty();
			if (total_folded_elems == total_elems) {
				$('#foldAllLink').append('<a href="#" onclick="javascript:expandAll();return false;" class="down">'+ eval('LANG.'+ lang +'.p1') +'</a>');
			} else if (total_expanded_elems == total_elems) {
				$('#foldAllLink').append('<a href="#" onclick="javascript:foldAll();return false;" class="up">'+ eval('LANG.'+ lang +'.p2') +'</a>');
			} else {
				$('#foldAllLink').append('<a href="#" onclick="javascript:expandAll();return false;" class="down">'+ eval('LANG.'+ lang +'.p1') +'</a>');
			}
		}
	}
	$('#mainContent/*').each(refold);
	$('#mainContentLimit/*').each(refold);
	$('#mainContentLimitVodafone/*').each(refold);
	$('#VodafonesearchByZoneBlock/*').each(refold);
	$('#mobileFolding/*').each(refold);
	$('.countryListForm/*').each(refold);	
}

function foldAll() {
	$(".switch_expanded").attr("class", "switch");
	$(".switch_expanded_notopline").attr("class", "switch_notopline");	
	$(".expanded").removeClass("expanded").addClass("folded");
	fold(null);
}

function expandAll() {
	$(".switch").attr("class", "switch_expanded");
	$(".switch_notopline").attr("class", "switch_expanded_notopline");	
	$(".folded").removeClass("folded").addClass("expanded");
	fold(null);
}

if ($("h3.switch_notopline")[0] && !$('#mobileFolding').attr('id')) {
	$($("h3.switch_notopline")[0]).before('<div id="foldAllLink" style="clear:both; display: block; height:20px;"></div>');
} else if ($("h3.switch")[0] && !$('#mobileFolding').attr('id')) {
	$($("h3.switch")[0]).before('<div id="foldAllLink" style="clear:both; display: block; height:20px;"></div>');
}

var hash	= window.location.hash.replace(/^#/, '');
fold(hash);