﻿function brch_rearangeResultsList(){
	var resholder = $('brch_results');
	var divs = resholder.getElementsByTagName('div');
	var div;
	var yb,yt;
	var hasLabel = false;
	var str;
	for(i=0;i<divs.length;i++){
		div = divs[i];
		yt = div.offsetTop - resholder.scrollTop;
		yb = yt + div.offsetHeight;
		if((yt <= -14) && (yb>=0)){
			hasLabel = true;
			str = div.getAttribute('City').bold();
			if($('brch_results_header').innerHTML!=str){
				Element.update('brch_results_header', str);
			}
		}
	}
	if(!hasLabel) Element.update('brch_results_header', '');
}

function brch_show(brchid){
	var _url = '/'+lng+'/server/show/operate/branches-details/bid/'+brchid;
	new Ajax.Updater(
		'brch_details', 
		_url, 
		{
				asynchronous:true, 
				evalScripts:true, 
				onComplete:function(){brch_changeIndicatorState(false);}, 
				onLoading:function(){brch_changeIndicatorState(true);}
		}
	);
	
	/*
	$('BGMapsFrame').src='http://bgmaps.com/tpl/?tplname=fibank&key='+brchid+'&lang=bg';
	$('BGMapsHolder').style.display = 'block'; */
}

function brch_showDetails(brchid){
	$('brch_details').innerHTML = 'Зареждане...';
	if(!$('BranchMapShader').Shown){
		$('BranchMapShader').style.display = 'block';
		$('BranchMapShader').Shown = true;
		transOpacity('BranchMapShader', 0, 30);
	}
	brch_show(brchid);
}
function brch_showOnMap(){
	var _frame = $('BGMapsFrame');
	if(!_frame) return;
	
	_frame.src = 'http://bgmaps.com/tpl/?tplname=fibank&lang=bg&key=' + $('brch_details').CurrentBranchKey;
}
function brch_closeDetails(){
	
	
	$('BranchDetailsHolder').style.display = 'none';
	$('BranchMapShader').Shown = false;
	$('BranchMapShader').style.display = 'none';
	$('BranchMapShader').style.opacity = 0;
	$('BranchMapShader').style.filter = "alpha(opacity=0)";
	
}
function brch_cityChange(){
	var _select = $('brch_cities_list');
	
	if($('MapIconsHolder').CurrentIcon)
		$('MapIconsHolder').CurrentIcon.src = "/uploads/map/fiblogo_gray_small.gif";
	if($(_select.options[_select.selectedIndex].value))
		$(_select.options[_select.selectedIndex].value).src = "/uploads/map/fiblogo_small.gif";	
	
	_select.style.color = _select.options[_select.selectedIndex].style.color;
	$('brch_filters').CurrentKey = _select.options[_select.selectedIndex].value;
	brch_loadResults();
}

function brch_loadResults(){
	if(!$('brch_filters').CurrentKey) return;
	if($('brch_filters').CurrentKey == 'NONE') return;
	var _url = '/'+lng+'/server/show/operate/branches-results/key/'+$('brch_filters').CurrentKey;
	
	if($('brch_list_names').checked) _url += '/lt/1';
	else _url += '/lt/0';
	if($('brch_weekend').checked) _url += '/ww/1';
	if($('brch_carddelivery').checked) _url += '/cc/1';
	_url += '/addkey/' + encodeURIComponent($('brch_additional_key').value);
	new Ajax.Updater(
		'brch_results', 
		_url, 
		{
				asynchronous:true, 
				evalScripts:true, 
				onComplete:function(){map_changeIndicatorState(false);}, 
				onLoading:function(){map_changeIndicatorState(true);}
		}
	);
}
function brch_changeIndicatorState(show){
	if(show){
		
	}else{
		
	}
}
// These functions are cross related with map module
function map_afterIconClick(imgID){
	$('brch_filters').CurrentKey = imgID;
	$('brch_cities_list').selectedIndex = $('city'+imgID).index;
	brch_cityChange();
	//new Ajax.Updater('atm_results', '/bg/server/show/operate/atm_results/key/'+img.id, {asynchronous:true, evalScripts:true, onComplete:function(){map_changeIndicatorState(false);}, onLoading:function(){map_changeIndicatorState(true);}}); 
}
function map_afterRegionClick(regID){
	$('brch_filters').CurrentKey = $(regID).getAttribute('Region');
	$('brch_cities_list').selectedIndex = $('opt_'+regID).index;
	brch_cityChange();
}
function map_afterBGMapsLoad(_frame){
	if(!$('brch_details').CurrentBranchKey) return;
	$('BGMapsHolder').style.display = 'block';
}
