/*
*	Core UI file
*/

var UI = {

	SIDEPANEL_MODE_CATS : 0,
	SIDEPANEL_MODE_LIST : 1,

	sidepanel_mode : 0,
	
	resized : function(){
		
		if (UIConfig.image_bar_outer != null) {
			UIConfig.mapHeight = UIConfig.windowHeight-UIConfig.image_bar_outer.getSize().size.y;
			UIConfig.image_bar_container.style.width= (UIConfig.windowWidth-48)+'px'; //Safari bug fix
		} else {
			UIConfig.mapHeight = UIConfig.windowHeight;
		}
		
		UIConfig.map.setStyle('height', UIConfig.mapHeight+'px');
		
		Sidepanel.resize();
		MS.map.checkResize();
	},

	windowResized : function(){
		UIConfig.resized();
		UI.resized();
	},

	init : function(){
		
		UIConfig.init();
		
		//Constrain popup to window
		if (UIConfig.popupSpec.width > UIConfig.windowWidth) UIConfig.popupSpec.width = UIConfig.windowWidth * 0.95;
		if (UIConfig.popupSpec.height > UIConfig.windowHeight) UIConfig.popupSpec.height = UIConfig.windowHeight * 0.98;
		
		if (UIConfig.image_bar_outer != null) {
			UIConfig.image_bar_outer.style.display = ""; 
			ImageBar.init(UIConfig.image_bar_container , UIConfig.image_bar_left , UIConfig.image_bar_right );
			UIConfig.image_bar_container.style.width= (UIConfig.windowWidth-48)+'px'; //Safari bug fix
		}
		
		UI.resized();
		
		Sidepanel.init(_initdata.view_options.show_sidebar == 1);
		CategoryList.init();		
		SearchBox.init(_initdata.search);		
		

		
		MS.map.setMapType(UI.getMapType(_initdata.view_options.map_type));

		window.onresize = UI.windowResized;

		if ((UIConfig.image_bar_outer != null) && (_initdata.view_options.show_imagebar ==2)) ImageBar.hide();
	},
	
	setTab : function(index, extratags){		

		extratags = $pick(extratags, []);
	
		var tabs = UIConfig.getTabs();
		var tab = tabs[index];
		UIConfig.allow_image_markers = tab.allow_image_markers;

		if (tab.requireLogin && (MS.user == null)){
			MS.loginOrSignup('');	
			return;
		}
	
		if (tab.resetcats == true) CategoryList.closeAll();
		
		if (tab['uimode'] != null) UI.setSidepanelMode(tab['uimode']);
				
		State.setTags(extratags,tab.tags, tab.extags);
	
		if (UIConfig.page_tab_panel == null) return;
		
		var children = UIConfig.page_tab_panel.childNodes;
		var j = 0;
	
		for (var i=0; i < children.length; i++){
				var child = $(children[i]);
				
				if (($type(child) == 'element') && (child.hasClass("tab"))){
					if (j == index)
						children[i].addClass('selected');
					else
						children[i].removeClass('selected');;
					
					j++;
				}
		}
		
		
		if ($('add_item_button_top')){
			if ((tab.allowAdd) && (MS.security.allow_posts)) 
				$('add_item_button_top').style.display = '';
			else
				$('add_item_button_top').style.display = 'none';
		}	
		
			
		
		UIConfig.postTabSelected(index);
	},
	
	getTab : function(){
		
	},
	
	updateAsContentChanged : function(){
		TagCloud.setTagCloud(Items.tags);
		Markers.stopHoverOverAll();
		
		PostingList.updatePostings();
	},
	
	showFade : function(){
		if (!$chk($('fade'))) return;
		
		var height = window.getHeight();
		var fade = $('fade');
		fade.style.display='block';
		fade.style.height=height+'px';
		fade.style.display = '';
	},
	
	hideFade : function(){
		if (!$chk($('fade'))) return;
		$('fade').style.display = 'none';
	},

	/*
	*	Possible modes:
	*			MODE_CATEGORIES, MODE_LIST
	*/
	setSidepanelMode : function(mode){
		UI.sidepanel_mode = mode;
		if (mode == UI.SIDEPANEL_MODE_CATS){
			
			if (UIConfig.category_container) {
				Sidepanel.showSection(UIConfig.category_container.id);
			}
			
		} else {
			
			if (UIConfig.category_container) {
				Sidepanel.hideSection(UIConfig.category_container.id);
			}

		}
	},
	
	getMapType : function(name){
		name = name.toUpperCase();
		if (name == 'SATELLITE')
			return G_SATELLITE_MAP;
		else if (name == 'HYBRID')
				return G_HYBRID_MAP;
		else
			return G_NORMAL_MAP;	
	}

}