	
	var toggleCntr = 0;
	var filterToggleCntr = 0;
	
	
	function $(id) {
		return document.getElementById(id);
	}
	
	
	if (window.addEventListener){
		window.addEventListener('load', loadHandler, false);
		window.addEventListener('resize', resizeHandler, false);
	} else if (window.attachEvent){
		window.attachEvent('onload', loadHandler);
		window.attachEvent('onresize', resizeHandler);
	} else {
		window.onload = loadHandler;
		window.onresize = resizeHandler;
	}
	
	
	function loadHandler () {
	
		if (isPrint()) return false;
		
		SizeControl = $('size_control');
		if (SizeControl)
		{
			BaseFontSize = SizeControl.offsetHeight;
			setInterval(size_control, 100);
		}
		
		map_div = $("map-cont");
		init_d();
		initViewLinks();
		//initFilterToggleLink();
		//initBtnSearch();
		resizeBlocks();
		blurLinks();
		if (Enable_map ==1 && typeof(map_prov) != "undefined")
		{
			if(map_prov == 1)
			{
				init();
			}
			if(map_prov == 2)
			{
				init2();
			}
				
		}
		expandByValue();
	}
	
	function resizeHandler () {
		if (isPrint()) return false;
		resizeBlocks();
	}
	
	
	function init_d() {		
    /*    if (typeof(Index_page) != 'undefined' && Index_page == true) 
		{
			$('filter').style.display = 'none';
			toggleFilter($('search-toggle').getElementsByTagName('a')[0]);
		}*/
		//$('search-toggle').style.display = 'block';
		//$('filter').style.display = 'none';
		var dom_map = $('map');
		if (dom_map) $('td2').className = 'base_td2';
		if (dom_map) dom_map.className = '';
		if ($('toggle')) $('toggle').style.display = 'block';
		if ($('content')) $('content').className = '';
		$('search-text').focus();
		setActiveLink('both');
	}
	
	function getBodyHeight() {
		
		var height = 0;
		
		if (typeof(window.innerWidth) == 'number') {
			height = window.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			height = document.documentElement.clientHeight;
		} else if (document.body && document.body.clientHeight) {
			height = document.body.clientHeight;
		}
		
		return height;
	}
	
	function resizeBlocks() 
	{
		//console.log('res')
		var blocks = ['map', 'content', 'toggle'];
		var hLimit = 218;
		var hOffset = 215;
		if (filterToggleCntr % 2) {
			hOffset += 59;
		}

        var hMaxLimit = AppConfig['maxconsoleheight']-hOffset-45+(getElementHeight('legend')-30); // miracle number 45! :)
		
		var height = getBodyHeight() - hOffset;
		if ($('map') && $('content') && $('toggle'))
		for (var i = 0; i < blocks.length; i++) {
			newheight =  (height > 0 && height > hLimit) ? height + 'px' : hLimit + 'px';
			newheight =  (height > 0 && height > hMaxLimit) ? hMaxLimit + 'px' : newheight;
                        $(blocks[i]).style.height = newheight;
			newheight =  (height > 0 && height > hLimit) ? height + 'px' : hLimit + 'px';
			newheight =  (height > 0 && height > hMaxLimit) ? hMaxLimit + 'px' : newheight;
                        $(blocks[i] + '-cont').style.height =  newheight;
			if (blocks[i] == 'toggle') {
				l = $(blocks[i]).getElementsByTagName('a');
				for (var j = 0; j < l.length; j++) {
					switch (l[j].rel) {
						case 'on-left':
							l[j].style.marginTop = ($(blocks[i]).offsetHeight / 2 - 18) + 'px';
							l[j].onclick = function () {
								$('content').className == 'hidden' ? expandBoth() : expandList();
								return false;
							}
						break;
						case 'on-right':
							l[j].style.marginTop = ($(blocks[i]).offsetHeight / 2 + 1) + 'px';
							l[j].onclick = function () {
								$('map').className == 'hidden' ? expandBoth() : expandMap();
								return false;
							}
						break;
					}
				}
			}
			
		}
		if (typeof(Current_page) != 'undefined' && (Current_page == 'search' || Current_page == 'event' || Current_page == 'place')) updateScrollSize();
	}
	
	function toggleFilter(link) {
		var filter = $('filter');
		if (filter.style.display == '' || filter.style.display == 'none') {
			filter.style.display = 'block';
			link.className = 'active';
			link.innerHTML = 'Fewer search options';
		} else {
			filter.style.display = 'none';
			link.className = '';
			link.innerHTML = 'More search options';
		}
		
		filterToggleCntr++;
		
		resizeBlocks();
		
	}
	
	
	function initBtnSearch() {
		$('search-btn').onmouseover = function () {
			this.src = '/images/btn-search-active.gif';
		}
		$('search-btn').onmouseout = function () {
			this.src = '/images/btn-search.gif';
		}
	}
	
	
	function initFilterToggleLink() {
	//	$('search-toggle').getElementsByTagName('a')[0].onclick = function () {
	//		toggleFilter(this);
	//		return false;
	//	}
	}
	
	
	function initViewLinks() {
		
		if ($('view-links')) var l = $('view-links').getElementsByTagName('a');
		else var l = 0;
		
		var onclickHandler = function () {
			
			switch (this.rel) {
				case 'map':
					expandMap();
				break;
				case 'list':
					expandList();
				break;
				case 'both':
					expandBoth();
				break;
			}
			
			setActiveLink(this.rel);
			
			return false;
		}
		
		for (var i = 0; i < l.length; i++) {
			l[i].onclick = onclickHandler;
		}
		
	}
	
	function expandMap() {
	
		if (typeof(Index_page) != 'undefined' && Index_page == true) return false;
		//alert('map');
		$('td4').className = 'hidden';
		$('content').className = 'hidden';
		$('td2').className = 'wide';
		$('map').className = 'wide';
		$('toggle').className = 'right';
		$('td1').className = 'base_td1';
		$('td3').className = 'base_td3';
		//$('key_col').colSpan = '2';
		
		setActiveLink('map');
		
		check_map_resize();
		
		if (!onePlace) setCookie('view_content','Map');
		resizeBlocks();
	}
	
	function expandList() {
		
		if (typeof(Index_page) != 'undefined' && Index_page == true) return false;
		//alert('list');
		$('map').className = 'hidden';
		$('content').className = 'wide';
		$('toggle').className = 'left';
		$('td1').className = 'hidden';
		$('td2').className = 'hidden';
		$('td3').className = 'base_td3';
		$('td4').className = 'wide';
		//$('key_col').colSpan = '2';
		
		setActiveLink('list');
		if (!onePlace) setCookie('view_content','List');
		addmargin_for_freeze();
		resizeBlocks();
	}
	
	function expandBoth(is_detail_page) {
		if (typeof(Index_page) != 'undefined' && Index_page == true) return false;
		//alert('bouth');
		$('map').className = '';
		$('content').className = '';
		$('toggle').className = '';
		$('td1').className = 'base_td1';
		$('td2').className = 'base_td2';
		$('td3').className = 'base_td3';
		$('td4').className = 'base_td4';
		//$('key_col').colSpan = '3';
		
		setActiveLink('both');
		
		if (typeof(map_prov) != "undefined")
		{
			check_map_resize();
		}
		
		if (!is_detail_page || !onePlace) setCookie('view_content','Both');
		removemargin_for_freeze();
		resizeBlocks();
	}
	
	function expandByValue()
	{	//detect cookie value and run according action
		//if cookie value  undefined then set default value=Bouth
		if (typeof(onePlace) != 'undefined' && onePlace)
		{
			$('td1').className = 'base_td1';
			return;
		}
		var view_content = getCookie('view_content');
		if (view_content)
		{
			if (view_content == 'Both') expandBoth();
			if (view_content == 'Map') expandMap();
			if (view_content == 'List') expandList();
		}
		else
		{
			setCookie('view_content',"Both");
			expandBoth();
		}
	}
	
	function blurLinks() {
		var links = document.links;
		var links_len = links.length;
		for (var i = 0; i < links_len; i++) links[i].onfocus = function () {this.blur()}
	}
	
	function checkJS(){
	//remove this action to element event
		document.getElementById("hasjs").value = 1;
	}
        
	function set_display_for_filter(){
                var el = document.getElementById("filter");
                //alert(el.innerHTML);
                //return false;
                el.style.display = "none";
                if (typeof(Index_page) != 'undefined' && Index_page == true) return false;
                var che_list = el.getElementsByTagName("input");
                var visible_filters = false;
                var che_list_len = che_list.length; 
                for (var i = 0; i < che_list_len; i ++)
                {
                	if (che_list[i].checked == true) 
                	{
            			visible_filters = true; 
            			break;
                	} 
                	//alert(che_list[i].name + " = " + che_list[i].checked);	
                }
                var options_message = document.getElementById("options_message");
                if (visible_filters)
                {
                	options_message.innerHTML = "FILTER&nbsp;ON";
                }
	}
	
	function clear_disclaimer()
	{
		document.getElementById("disclaimer-text").innerHTML = '';
	}
	
	var isPrint = function() { 
		if ($('is-print')) {
			return !!parseInt(getStyle($('is-print'), 'height')); 
		} 
		else { 
			return false; 
		} 
	}
	
	var getStyle = function (el, property) {
		
	    var ua = navigator.userAgent.toLowerCase(),
	        isOpera = (ua.indexOf('opera') > -1),
	        isSafari = (ua.indexOf('safari') > -1),
	        isGecko = (!isOpera && !isSafari && ua.indexOf('gecko') > -1),
	        isIE = (!isOpera && ua.indexOf('msie') > -1); 
		
	    var patterns = {
	        HYPHEN: /(-[a-z])/i,
	        ROOT_TAG: /body|html/i
	    };
	
	    var toCamel = function(property) {
	        if ( !patterns.HYPHEN.test(property) ) {
	            return property;
	        }
	        
	        var converted = property;
	 		
	        while( patterns.HYPHEN.exec(converted) ) {
	            converted = converted.replace(RegExp.$1,
	                    RegExp.$1.substr(1).toUpperCase());
	        }
	        
	        return converted;
	    };
		
	    if (document.defaultView && document.defaultView.getComputedStyle) {
	    	
	        var value = null;
	        
	        if (property == 'float') {
	            property = 'cssFloat';
	        }
	        
	        var computed = document.defaultView.getComputedStyle(el, '');
	        if (computed) {
	            value = computed[toCamel(property)];
	        }
	        
	        return el.style[property] || value;
	            
	    } else if (document.documentElement.currentStyle && isIE) {
	        switch( toCamel(property) ) {
	            case 'float':
	                property = 'styleFloat';
	            default: 
	                var value = el.currentStyle ? el.currentStyle[property] : null;
	                return ( el.style[property] || value );
	        }
	    } else {
	        return el.style[property];
	    }
	    
	}
    
function updateScrollSize()
{
	//calculate and set height for 'content-cont'
	//run after add or remove freeze block and after redizeBlock
	var height = (getElementHeight('content-cont') - getElementHeight('freeze_' + Current_page));
	var el = $('content-cont');

	el.style.height = (height) + 'px';
	el.getElementsByTagName('div')[0].style.height = (height - 18)+ 'px';
	//console.log(el);
	//console.log(el.getElementsByTagName('div')[0]);
}

function getElementHeight(el_id)
{
	//return height html element
	//using for getting reail height value
	var height = 0;
	var el = document.getElementById(el_id);
	if (el) height = el.offsetHeight;
	return height;
}

function addmargin_for_freeze()
{
	//add left margin for freeze block
	var freeze_block = document.getElementById('freeze_' + Current_page);
	if (freeze_block) freeze_block.style.marginLeft = '15px';
	var content_cont_r = document.getElementById('content-cont-r');
	if (content_cont_r) 
		if (onePlace) content_cont_r.style.paddingLeft = '25px'
		//else content_cont_r.style.marginLeft = '16px'
}

function removemargin_for_freeze()
{
	//remove left magin for freeze block
	var freeze_block = document.getElementById('freeze_' + Current_page);
	if (freeze_block) freeze_block.style.marginLeft = '';
	var content_cont_r = document.getElementById('content-cont-r');
	if (content_cont_r) 
	{	
		content_cont_r.style.paddingLeft = ''
		//content_cont_r.style.marginLeft = ''
	}
}

function setActiveLink(type_sw)
{
	if (type_sw == 'map')
	{
		$('sw_map').className = 'active-link';
		$('sw_list').className = '';
		$('sw_both').className = '';
	}
	else if (type_sw == 'list')
	{
		$('sw_map').className = '';
		$('sw_list').className = 'active-link';
		$('sw_both').className = '';	
	}
	else if (type_sw == 'both' && $('sw_map'))
	{
		$('sw_map').className = '';
		$('sw_list').className = '';
		$('sw_both').className = 'active-link';	
	}
}

function size_control()
{
	if (EtalonSizeControl == SizeControl.offsetHeight) return;
	//if ( != SizeControl.offsetHeight)	
	resizeBlocks();
	EtalonSizeControl = SizeControl.offsetHeight;
	
}

function check_map_resize()
{
	if( map_prov == 1)
	{
		if (map && map.checkResize)
			map.checkResize();
	}
	if( map_prov == 2)
	{
		if (map2 && map2.resize())
			map2.resize();
	}
}
