// JavaScript Document


function initRegions()
{
	var regionListId = 'reglist';
	var regionList = $(regionListId).getElementsByTagName('li');
	var regionNodes = $A(regionList);
		
	regionNodes.each(function(node)
	{
		Event.observe(node, 'click', function(e) {
			turnRegion(node.getAttribute('id'));
			LogWriteInfo('hp_region','0','0',node.getAttribute('id'));
			Event.stop(e);
		});
	});
}

function turnRegion(regionId)
{
	var regionPrefix = 'kraj';
	
	var regionListId = 'reglist';
	var regionList = $(regionListId).getElementsByTagName('li');
	var regionNodes = $A(regionList);
		
	regionNodes.each(function(node)
	{
		if (node.getAttribute('id') == regionId)
		{
			node.className = 'se';
		}
		else
		{
			node.className = '';
		}
	});
	
	setCategoryCountList(regionId.substr(regionPrefix.length));
	
	return false;
}


function setCategoryCountList(regionId)
{
	listId = 'cat-list';
	var categoryList = $(listId).getElementsByTagName('li');
	var categoryNodes = $A(categoryList);
		
	categoryNodes.each(function(node)
	{
		var categoryId = node.getAttribute('id');
		
		if (categoryId > '' && categoryId != 'allCategories')
		{
			categoryId = Math.round(categoryId.substr(3));
			var count = 0;
			
			try
			{
				if (categoryCountList[regionId])
				{
					count = categoryCountList[regionId][categoryId] ? categoryCountList[regionId][categoryId] : 0;
					
					var URL = "/nabidky-prace/index.fcgi?a=list&region="+regionId+"&branch="+categoryId;
					$('cid'+categoryId).down('a').setAttribute('href', URL);
				}
			}
			catch(err)
			{
				//alert('region: '+regionId+'\ncid: '+categoryId+'\n'+err.description);
			}
			
			setCategoryCount(categoryId, count);
		}
		else
		if (categoryId == 'allCategories')
		{
			//alert(node.innerHTML);
			var URL = "/nabidky-prace/index.fcgi?a=list&region="+regionId;
			try {
				$('allCategories').down('a').setAttribute('href', URL);
			} catch(e) {
				//console.log(e)
			}
		}
	});
	
}


function setCategoryCount(categoryId, count)
{
	var oldHTML = $('cid'+categoryId).innerHTML;
	var newHTML = oldHTML.substring(0, oldHTML.lastIndexOf(">")+1)+" "+count;
	$('cid'+categoryId).innerHTML = newHTML;
}



function setPositions(array)
{
	for (var index = 0; index < array.length; ++index)
	{
		var position = array[index].getAttribute('position');
		
		if (position != null)
		{
			if (Math.round(position) > -1)
			{
				to = position;
			}
			else
			{
				to = array.size() + Math.round(position);
			}
			
			array = moveToPosition(array, index, to);
		}
	}

	return array;
}


function moveToPosition(array, from, to)
{
	if (from == to) return array;
	
	// save moving element
	var moving_element = array[from];
	
	// remove it from array
	var start_slice = array.slice(0, from);
	var end_slice = array.slice(from+1);

	
	var start_slice_index = start_slice.size();
	for (var index = 0; index < end_slice.size(); ++index)
	{
		start_slice[start_slice_index++] = end_slice[index];
	}
	array = start_slice;
	
	
	if (to == array.size())
	{
		array[to] = moving_element;
	}
	else
	{
		// split by new position and insert it
		var start_slice = array.slice(0, to);
		var end_slice = array.slice(to);
	
		var start_slice_index = start_slice.size();
		for (var index = 0; index < end_slice.size(); ++index)
		{
			if (start_slice_index == to)
			{
				start_slice[start_slice_index++] = moving_element;
			}
			start_slice[start_slice_index++] = end_slice[index];
		}
	}
	array = start_slice;
			
	return array;
}

function reArrangeList(listId, columns, options)
{
	var List_all = $(listId).getElementsByTagName('li');
	var List = new Array();
	var index_new = 0;
	
	for (var index = 0; index < List_all.length; ++index)
	{
		if (List_all[index].getAttribute('skip') != 1)
		{
			List[index_new++] = List_all[index];
		}
	}
	
	// sort by alphabet
	var abcList = Array();
	for (var index = 0; index < List.length; ++index)
	{
		inserted = false;
		
		for (var newindex = 0; newindex < abcList.length; ++newindex)
		{
			if (parseInt(List[index].getAttribute('position')) < parseInt(abcList[newindex].getAttribute('position')))
			{
				for (var newindex2 = abcList.length; newindex2 > (newindex-1); --newindex2)
				{
					abcList[newindex2] = abcList[newindex2-1];
				}

				abcList[newindex] = List[index];
				inserted = true;
				break;
			}
		}
		
		if (!inserted)
		{
			abcList[index] = List[index];
		}
	}

	abcList = setPositions(abcList);

	// split for columns
	var rows = Math.ceil(abcList.length/columns);
	
	var newList = new Array();
	var k = 0;
	var html = '';
	var str = '';
	
	for (var j = 0; j < rows; ++j )
	{
		for (var i = 0; i < columns; ++i )
		{
			newList[k] = abcList[i*rows+j];
			if (newList[k])
			{
				if (newList[k].getAttribute('position'))
				{
					position_attr = ' position="'+newList[k].getAttribute('position')+'"';
				}
				else position_attr = '';
				
				if (newList[k].getAttribute('class'))
				{
					class_attr = ' class="'+newList[k].getAttribute('class')+'"';
				}
				else class_attr = '';
			
				if (newList[k].getAttribute('id'))
				{
					id_attr = ' id="'+newList[k].getAttribute('id')+'"';
				}
				else id_attr = '';
				
				html += "<li"+position_attr+" "+id_attr+" "+class_attr+">"+newList[k].innerHTML+"</li>\n";
			}
			else if (columns == 3)
			{
				html += "<li skip='1' style='visible:hidden;'> &nbsp; </li>\n";
			}

			k++;
		}
	}

	// set html
	$(listId).innerHTML = html;

	return true;
}

function reArrangeList2(listId, columns, options)
{
	var List_all = $(listId).getElementsByTagName('li');
	var List = new Array();
	var index_new = 0;
	
	for (var index = 0; index < List_all.length; ++index)
	{
		if (List_all[index].getAttribute('skip') != 1)
		{
			List[index_new++] = List_all[index];
		}
	}
	
	// sort by alphabet
	var abcList = Array();
	for (var index = 0; index < List.length; ++index)
	{
		inserted = false;
		
		for (var newindex = 0; newindex < abcList.length; ++newindex)
		{
			//if (List[index].innerHTML.stripTags().toLocaleLowerCase() < abcList[newindex].innerHTML.stripTags().toLocaleLowerCase())
			if (List[index].getAttribute('class') < abcList[newindex].getAttribute('class'))
			{
				for (var newindex2 = abcList.length; newindex2 > (newindex-1); --newindex2)
				{
					abcList[newindex2] = abcList[newindex2-1];
				}

				abcList[newindex] = List[index];
				inserted = true;
				break;
			}
		}
		
		if (!inserted)
		{
			abcList[index] = List[index];
		}
	}

	abcList = setPositions(abcList);

	// split for columns
	var rows = Math.ceil(abcList.length/columns);
	
	var newList = new Array();
	var k = 0;
	var html = '';
	var str = '';
	
	for (var j = 0; j < rows; ++j )
	{
		for (var i = 0; i < columns; ++i )
		{
			newList[k] = abcList[i*rows+j];
			if (newList[k])
			{
				if (newList[k].getAttribute('position'))
				{
					position_attr = ' position="'+newList[k].getAttribute('position')+'"';
				}
				else position_attr = '';
			
				if (newList[k].getAttribute('id'))
				{
					id_attr = ' id="'+newList[k].getAttribute('id')+'"';
				}
				else id_attr = '';
				
				html += "<li"+position_attr+" "+id_attr+">"+newList[k].innerHTML+"</li>\n";
			}
			else if (columns == 3)
			{
				html += "<li skip='1' style='visible:hidden;'> &nbsp; </li>\n";
			}

			k++;
		}
	}

	// set html
	$(listId).innerHTML = html;

	return true;
}

function alertArray(array)
{
	var str = String();
	for (var i = 0; i < array.length; ++i)
	{
		if (array[i])
		{
			str += array[i].innerHTML.stripTags()+'\n';
		}
	}
	alert(str);
}


function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}	
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}		
	return 0;
}


function processWindowResize()
{
	var width = getBrowserWidth();
	var listId = 'cat-list';
	
	//alert('window resized to size '+width+' px');
	if (width<995)
	{
		reArrangeList(listId, 2);
	}
	else
	{
		reArrangeList(listId, 3);
	}
}

// add resize event listener
Event.observe(window, 'resize', processWindowResize);
Event.observe(window, 'load', processWindowResize);

