var entity_id = 0;
var entity_ids = [];
var entity_type = 1;
var ent_selected = 0;
var enableGMap = 0;
//var srchRes;
var muncs;
var prvcs;
var wtchs;
var locale = 'en';
var langWords = Array();
var srch_str;
var current_page = '';
//window setings
var foothead_height = 45+100;
var tabs_header_h = 25;
var min_wind_height = 800;
var lm_sel_ent_count = 12; // this is means height must be 280px. 12*15+100 = 280 
var lm_min_h_most_sh_pages = 200;
var lm_min_h_lmb = 130;
var lm_min_h_party_list = 200;
var lm_min_h_p = 70;
var lm_min_h_rel_links = 100;
var lm_min_h_rl = 50;
var root_url = 'http://'+location.host+location.pathname;

function loadEntSearchRes(){
	var srchData = document.getElementById('hid_srch_res').value;
	var entSrchRes = JSON.parse(srchData, '');

	removeOldMarkers();

	for (var id in entSrchRes){
		createEntSrchWin(entSrchRes[id]);
	}
}

function entSearch(srch_str){
	
	if( !srch_str ) 
		srch_str = escape(document.getElementById('search_str').value);

	if( srch_str ){

		if (enableGMap){
			
			if(ent_selected==1){
				$.post(root_url,
						{page:"main",action:"search",async:1,src_type:ent_selected,eid:entity_id,'srch_str':srch_str},
				      	  function (data){$('#search_result').html(data);loadEntSearchRes();}
				  	);
			}else{
				$.post(root_url,
						{page:"main",action:"search",async:1,src_type:ent_selected,etype:entity_type,'srch_str':srch_str},
				      	  function (data){$('#search_result').html(data);loadGmapSearchRes();}
				  	);

			}
		}else {
			if(ent_selected==1){
				document.location = '?page=main&action=map&src_type='+ent_selected+'&eid='+entity_id+'&etype='+entity_type+'&srch_str='+srch_str;
			}else{
				document.location = '?page=main&action=map&src_type='+ent_selected+'&etype='+entity_type+'&srch_str='+srch_str;
			}
		}
	}
}

function embedVoteFlash(miid){
	var embed = document.getElementById('embed_container_'+miid).innerHTML;
	var includes = '<script type="text/javascript" src="http://'+location.host+'/js/AC_OETags.js" ></script>';
	includes += '<script type="text/javascript" src="http://'+location.host+'/js/history.js" ></script>';
	includes += '<script type="text/javascript" src="http://'+location.host+'/js/voteflash.js" ></script>';
	document.getElementById('emb_text_'+miid).value = includes+embed;
	
}

function entSelSelecting(params){
	if ( typeof( params ) == 'undefined' )params = {};
	if (params.select_type != null) select_type = params.select_type; else select_type=2;
	if (params.entity_id) _entity_id = params.entity_id; else _entity_id=0
	if (params.entity_ids) _entity_ids = params.entity_ids; else _entity_ids = [];
	
	switch (select_type){
		//on search methods pages
		case '2':
		case 2:
			var _href = document.location.href;
			if ( !_href.match(/\?/))	_href += '?';
			document.location =  _href.replace( /[\?,&](etype=\d+)/,'')+'&etype='+entity_type ;
			break;
		//on map page 
		case '1':
		case 1:
			params.entity_id = entity_id = _entity_ids.shift();
			zoomToEntity();
			show_entinfo(params);
			break;
		//on speaker,meeting,party,blogarticle pages
		case '0':
		case 0:
		default:
			document.location = 'http://'+location.host+'?eid='+_entity_id+'&etype='+entity_type;
			break;	
	}
}

function removeZero(arr){
	var ind = arr.indexOf('0'); 
	if (ind >= 0){
		arr.splice(ind,1);
	}
	return arr;
}

function resizeMainLeftMenu() {
	var tabHeight = getMainContentHeight() - tabs_header_h ;
	
	var cont_sm_h 	= 0;
	var cont_msp_h 	= 0;
	var cont_p_h	 	= 0;
	var cont_lmb_h	 	= 0;
	var cont_rl_h	 	= 0;

	var free_h;
	var add_size;
	var sel_size;
	var size;
	var cur_size;
	var height;
	var scroll_h;
	
	for (var i = 1;i<=3; i++){
		
		cur_size = $("#tab"+i+"_list_sm").children().size();
		size = Math.min(cur_size ,lm_sel_ent_count);
			
		$("#tab"+i+"_list_sm").attr("size",size);
		
		cont_sm_h 	= getElementHeight("tab"+i+"_cont_sm"); 
		cont_msp_h 	= getElementHeight("tab"+i+"_cont_msp"); 
		
		if (cont_msp_h>0 && cont_sm_h>0){
			
			free_h = tabHeight - cont_sm_h-cont_msp_h;
			if (free_h > 0){
				height = getElementHeight("tab"+i+"_scroll_msp");
				if(height){
					scroll_msp_h 	= Math.min( height+free_h, getElementHeight("tab"+i+"_list_msp"));
					
					$("#tab"+i+"_scroll_msp").css("height",scroll_msp_h);
				}
			}else if( free_h<0) {
				height = getElementHeight("tab"+i+"_scroll_msp");
				if(height){
					scroll_h 		= Math.max( height+free_h, lm_min_h_most_sh_pages);
				
					$("#tab"+i+"_scroll_msp").css("height",scroll_h);
				}
			}

		}else if(cont_sm_h>0){
			free_h = tabHeight - cont_sm_h;
			if (free_h > 0){
				
				add_size = Math.floor(free_h/15);
				sel_size = $("#tab"+i+"_list_sm").children().size();
				cur_size  = $("#tab"+i+"_list_sm").attr("size")||Math.floor(getElementHeight("tab"+i+"_list_sm")/15);
				size = Math.min(cur_size+add_size,sel_size);
				
				$("#tab"+i+"_list_sm").attr("size",size);
				
				cont_sm_h 	= getElementHeight("tab"+i+"_cont_sm");
				
			}else if( free_h<0){
				
				add_size = Math.floor(free_h/15);
				cur_size  = $("#tab"+i+"_list_sm").attr("size")||Math.floor(getElementHeight("tab"+i+"_list_sm")/15);
				size = Math.max(cur_size - Math.abs(add_size),3);
	
				$("#tab"+i+"_list_sm").attr("size",size);
				
				cont_sm_h 	= getElementHeight("tab"+i+"_cont_sm");
			}
		}		
	}

	//	view with selected entity
	resizeEntityInfo(tabHeight);
	
	
}

function resizeMainRightMenu() {
	$(document).ready(function() {

		var mcont_h	= getMainContentHeight();
		var rmh_wtwitts = mcont_h - $("#rm_twitter_content").height() ;

		var blocks 	= [];
		if ($('#rm_pagerl_content').size()) blocks.push('rm_pagerl');
		if ($('#rm_blogs_content').size()) blocks.push('rm_blogs');
		if ($('#rm_entrl_content').size()) blocks.push('rm_entrl');

			for (var bl in blocks) {
				
				var free_h = rmh_wtwitts;
				for (var fhbl in blocks) {
					 free_h	-= $("#"+blocks[fhbl]+"_content").innerHeight();
				}
				
				var block 			= $("#" + blocks[bl] + "_content");
				
				if (block.size()) {
					var block_scroll = $("#" + blocks[bl] + "_scroll");
					
					if (block_scroll.offset().top && block.offset().top) {
						var oftop1 = block_scroll.offset().top ||0;
						var oftop2 = block.offset().top || 0;
						var blscrl_postop =  oftop1 - oftop2 - 0;
					}
					var block_cont_h = $("ul", block_scroll).height() + blscrl_postop;
					var h_per_bl = rmh_wtwitts / blocks.length;
					
					if (free_h < 0) {
						var new_blh = Math.max(h_per_bl, block.innerHeight() + free_h);
						
						if (new_blh > 60 && new_blh < block_cont_h) {
							block.height(new_blh-blscrl_postop);
							block_scroll.height(new_blh-blscrl_postop);
						}
					}else if (free_h>0){
						var new_blh = Math.min(block_cont_h, block.innerHeight() + free_h);
						block.height(new_blh);
						block_scroll.height(new_blh);
					}
				}
			}
	
	});
}

function resizeEntityInfo(tabHeight) {
$(document).ready(function() {	
	cont_sm_h 	= getElementHeight("cont_sm"); 
	cont_p_h 	= getElementHeight("cont_p"); 
	cont_lmb_h 	= getElementHeight("cont_lmb"); 
	cont_rl_h 	= 0; 
	
	//set height of select entities container 
	cur_size = $("#list_sm").children().size();
	
	size = Math.min(cur_size ,lm_sel_ent_count);
	
	$("#list_sm").attr("size",size);
	
	cont_sm_h 	= getElementHeight("cont_sm");
	//end
	
	if (cont_sm_h>0 && cont_p_h>0 && cont_lmb_h>0 ){
		free_h = tabHeight - cont_sm_h-cont_p_h-cont_lmb_h-cont_rl_h;

		if (free_h > 0){
			scroll_h 	= Math.min(getElementHeight("scroll_p")+free_h, getElementHeight("list_p"));
			
			$("#scroll_p").css("height",Math.max( scroll_h, lm_min_h_p));
			
			cont_p_h 	= getElementHeight("cont_p");
			
		}else if (free_h<0){
			height = getElementHeight("scroll_lmb");
			
			if(height){
				
				scroll_h 	= Math.max( height+free_h, lm_min_h_lmb);
			
				$("#scroll_lmb").css("height",scroll_h);
				
				cont_lmb_h 	= getElementHeight("cont_lmb");
			}
			
		}
		
		free_h = tabHeight - cont_sm_h-cont_p_h-cont_lmb_h-cont_rl_h;
		
		if (free_h > 0){
			scroll_h 	= Math.min(getElementHeight("scroll_lmb")+free_h, getElementHeight("list_lmb"));
			
			$("#scroll_lmb").css("height",Math.max( scroll_h, lm_min_h_lmb));
		
		}else if (free_h<0){
			height = getElementHeight("scroll_p");
			
			if(height){
				
				scroll_h 	= Math.max( height+free_h, lm_min_h_p);
			
				$("#scroll_p").css("height",scroll_h);
				
				cont_p_h 	= getElementHeight("cont_p");
			}
			
		}
	}
});
}

function selectEntity(params){
	
	var _entity_ids = getSelectedEidsLeftMenu();
	var _entity_id = entity_id;
	var select_type = 0;
	
	if ( typeof( params ) == 'undefined' )params = {};
	if (params.select_type != null) select_type = params.select_type;
	if (params.entity_id) _entity_id = params.entity_id;
	if (params.entity_ids) _entity_ids = params.entity_ids;
	
	if (_entity_ids.length == 0 && _entity_id > 0)
		_entity_ids.push(_entity_id);
	
//	saveEntIds(_entity_ids);
	$.post(root_url,
			{page:"main",
      		  action:"saveEntIds",
      		  async:1,
      		  "eids[]":_entity_ids
      	  },
      	  function (data){
      		entSelSelecting({"select_type":select_type,"entity_id":_entity_id,"entity_ids":_entity_ids,"etype":entity_type});
      	  }
	  	);
	
}

function show_entinfo(params){
	var _epid 		= '';
	var _mid 		= '';
	var _entity_id 	= entity_id;
	var select_type = 0;
	var _etype 		= entity_type;
	
	if ( typeof( params ) == 'undefined' )params = {};
	if (params.select_type) select_type = params.select_type;
	if (params.entity_id) _entity_id 	= params.entity_id;
	if (params.etype) _etype 			= params.etype;
	
	if ( typeof( epid ) != 'undefined' ) _epid = epid;   
	if ( typeof( mid ) != 'undefined' ) _mid = mid;   
	

	if (_entity_id == 0 || _entity_id == "0"){ 
		$.post(
		      	  root_url,{
		      		  page:"main",
		      		  action:"showETypeInfoTab",
		      		  async:1,
		      		  'select_type':select_type,
		      		  'etype':_etype,
		      		  'multiselect':0
		      		  
		      	  },
		      	  function (data){
	    		  
		      		  $('#tab_'+_etype).html(data);
		      		  resizeMainLeftMenu();
		      		  selectEntityType({type:_etype,'select_type':select_type});
		      	  }
		  	  
		  	);
	}else if (_entity_id > 0){
	
		$.post(
	      	  root_url,{
	      		  page:"main",
	      		  action:"entinfo",
	      		  async:1,
	      		  eid:_entity_id,
	      		  cpage:current_page,
	      		  mid:_mid,
	      		  epid:_epid,
	      		  'select_type':select_type
	      		  
	      	  },
	      	  function (data){
    		  
	      		  $('#tab_'+_etype).html(data);
	      		  resizeMainLeftMenu();
	      		  ent_selected = 1;
	      		  selectEntityType({type:_etype,'select_type':select_type});
	      	  }
	  	  
	  	);
	}

}

function saveEntityId(entity){
	
	var eids = [];
	eids = $("#"+entity.id).val();

	if (typeof eids == 'string' || typeof eids == 'number') 
		eids = [eids];
	else if (typeof eids == 'undefined') 
		eids = [0];
	
	entity_ids = eids;

	if (eids.length == 1 ){
		entity_id = eids[0];
	}else 
		entity_id = 0;
}


function selectEntityType(params){
	var _select_type = 0;
	if ( typeof( params ) == 'undefined' )params = {};
	if (params.type) entity_type = params.type;
	if (params.select_type) _select_type = params.select_type;
	
	if (enableGMap){
		loadEntityGoogleKml('map_canvas',getEntities(entity_type),1,polyColor);
		
		entity_id = getEidSelTypeLeftMenu();
		zoomToEntity();
	}

}




function subm_search_form() {
	document.search_form.submit();
}
function new_search_srchform() {
	$("#pg").val(1);
	subm_search_form();
}

function showPage(pg){
	document.getElementById('pg').value = pg;
	subm_search_form();
}

function setHideSF() {
	$("#hide_show_sf").val('1');
}
function setHideSlowSF() {
	$("#hide_show_sf").val('2');
}

function getMainContentHeight(){
	
	var mySize = getClientWindSize();
	var myHeight = min_wind_height;
	if (mySize[1]>min_wind_height) 
		myHeight = mySize[1];
	myHeight = myHeight -getElementHeight("header")-getElementHeight("container_footer") - 
		getElementHeight('headerbar')- getElementHeight('container_buttom_border');

	return myHeight;
}

function getMainContentBodyHeight(){

	var myH = document.body.offsetHeight;
	
    var myHeight = Math.max(myH, min_wind_height);
    
	myHeight = myHeight -getElementHeight("header")-getElementHeight("container_footer") -
		getElementHeight('headerbar')- getElementHeight('container_buttom_border');

	return myHeight;
}

function getSelectedEidsFromForm(){
	var _eids = [];
	$("[name=eids[]]").each(function(){
		_eids.push($(this).val());
	});
	return _eids;
}

function getSelectedEidsLeftMenu(){
	var _eids = [];
	//get selected entities from component entityInfo
	var entselecter = $("#tab_"+entity_type+">#cont_sm>#list_sm");

	//get selected entities from component mainLeftTabs
	if (!entselecter.size() )
		entselecter = $("#tab"+entity_type+"_list_sm");

	if (entselecter.size() > 0){
		switch (typeof entselecter.val()) {
		case 'object':
			_eids = entselecter.val() || 0;
			break;
		case 'number':
		case 'string':
			_eids.push(entselecter.val());
			break;
		}
	}
	
	return _eids;
}

function getEidSelTypeLeftMenu(){
	var _eid = 0;
//	enttype_selent

	//get selected entities from component entityInfo
	var sel_ent = $("#tab_"+entity_type+" > #cont_sm > input[name=enttype_selectent]");
	
	if (sel_ent.size() > 0){
		_eid = sel_ent.val() || 0;
	}
	
	return _eid;
}

function getTimeFirstVisibleMin(){
	var jmp_time = 0; 
	var jmp_name =  $('.minute_jump:visible').attr('name');
	if (jmp_name) jmp_time = jmp_name.replace( /(fpj_)/,'');
	return jmp_time;
}

	