// Unicodé

var bar_elements = {};
var bar_generate_timeout = null;
var bar_extension_timeout = null;
var bar_element_active = '';

/*
	Element add
*/
function bar_element_add(ico, str, cfg) {
	cfg = array_merge({
		id_content : '',
		on_click : function() {},
		priority : 10	  
	}, cfg);
	
	var id = str_clean(str_replace(' ', '_', strtolower(str)), '_');
	
	bar_elements[id] = {
		id : id,
		ico : ico,
		id_content : cfg.id_content,
		on_click : cfg.on_click,
		priority : cfg.priority,
		str : str
	};
	
	clearTimeout(bar_generate_timeout);
	
	bar_generate_timeout = setTimeout('bar_generate()', 100);
}

/*
	Elements sort
*/
function bar_elements_sort(a, b) {
	var x = parseInt(a['priority']);
    var y = parseInt(b['priority']);
    
	if(x < y) {
		return -1;
	}
    if(x > y) {
		return 1;
	}
	
    return 0;
}

/*
	Generate
*/
function bar_generate() {
	var k = '';
	var i = 0;
	var buffer = '';
	var tmp = [];

	for(k in bar_elements) {
		tmp[tmp.length] = bar_elements[k];
	}

	tmp.sort(bar_elements_sort);
	
	if(tmp.length == 0) {
		return false;	
	}
	
	bar_elements = {};
	
	for(i = 0; i < tmp.length; i++) {
		bar_elements[tmp[i].id] = tmp[i];
	}
	
	$('#bar').show().html('<div class="reduce"></div>');
	
	for(k in bar_elements) {
		buffer = '<div class="section section_'+k+'">'
			   + '<div class="section_right"><img src="'+(strpos(bar_elements[k].ico, '/') === false ? 'img/icos/'+bar_elements[k].ico+'.30.png' : bar_elements[k].ico)+'" width="30" height="30" /><br /><span class="str">'+bar_elements[k].str+'</span></div>'
			   + '</div>';
			   
			  // alert(k);
				
		$('#bar').append(buffer);
		
		$('#bar .section_'+k).click(bar_elements[k].on_click).mouseover(function() {
			var id = str_replace('section section_', '', $(this).attr('class'));
			
			if(bar_elements[id].id_content != '') {
				clearTimeout(bar_extension_timeout);
				
				if($('#bar_extension').is(':visible')) {
					bar_extension_show(id);
				}
				else {
					bar_extension_timeout = setTimeout('bar_extension_show(\''+id+'\')', 100);
				}
			}
		}).mouseout(function() {
			var id = str_replace('section section_', '', $(this).attr('class'));
			
			clearTimeout(bar_extension_timeout);
			
			bar_extension_timeout = setTimeout(function() {
				bar_extension_hide();
			}, 100);
		});
	}

	$('#bar .reduce').click(bar_reduce);
}

/*
	Reduce
*/
function bar_reduce() {
	var k = '';
	var i = 0;
	
	for(k in bar_elements) {
		i++;	
	}
	
	if(i == 0) {
		return false;	
	}
	
	$('#bar').animate({
		bottom : -60
	}, 'slow', null, function() {
		$('#bar').hide().css('bottom', 0);
	});
	
	$('#bar_enlarge').show();

	cookie('bar_state', 'reduced');
}

/*
	Enlarge
*/
function bar_enlarge() {
	var k = '';
	var i = 0;
	
	for(k in bar_elements) {
		i++;	
	}
	
	if(i == 0) {
		return false;	
	}
	
	$('#bar').css('bottom', -60).show().animate({
		bottom : 0
	}, 'slow', null, function() {
		$('#bar').css('bottom', 0);
	});
	
	$('#bar_enlarge').hide();

	cookie('bar_state', 'enlarged');
}

/*
	Element delete
*/
function bar_element_delete(id) {
	if(id == 'all') {
		$('#bar .section').remove();
	}
	else {
		if($('#bar .section_'+id).size() > 0) {
			$('#bar .section_'+id).remove();
		}
		
		if(isset(bar_elements[id])) {
			delete bar_elements[id];
		}
		
		if(bar_element_active == id) {
			bar_element_active = '';	
		}
	}
}

/*
	Extension show
*/
function bar_extension_show(id) {
	if(bar_element_active == id) {
		return false;	
	}
	
	bar_element_active = id;
	
	$('#bar').zIndexize();
	
	var offset = $('#bar .section_'+id).offset();
	
	$('#bar_extension .bottom_right').html($('#'+bar_elements[id].id_content).html());
	
	$('#bar_extension').show();
	
	if(client('nav') != 'ie6') {
		$('#bar_extension').css('width', 'auto').css('left', (offset.left > 20 ? offset.left - 20 : 10));
	}
	setTimeout(function() {
		if(client('nav') == 'ie6') {
			var top = document.documentElement.scrollTop + document.body.scrollTop + document.documentElement.clientHeight - $('#bar_extension').height() - 40;
			
			$('#bar_extension').css('top', top);
		}
		else {
			$('#bar_extension').css('width', ($('#bar_extension .bottom_right').outerWidth() + 7));
		}
	}, 1);
}

/*
	Extension hide
*/
function bar_extension_hide() {
	$('#bar_extension').hide();
	
	bar_element_active = '';
}

/*
	Ready
*/
$(document).ready(function() {
	$('#bar').create().zIndexize().hide();
	
	$('#bar_extension').create().html('<div class="top"><div class="top_right"></div></div><div class="bottom"><div class="bottom_right"></div></div>').hide().mouseover(function() {
		clearTimeout(bar_extension_timeout);
	}).mouseout(function() {
		clearTimeout(bar_extension_timeout);
		
		bar_extension_timeout = setTimeout(function() {
			bar_extension_hide();
		}, 200);
	});
	
	$('#bar_enlarge').create().click(bar_enlarge);
	
	setTimeout('bar_'+(cookie('bar_state') == 'reduced' ? 'reduce' : 'enlarge')+'()', 100);
});