var CB = {};


CB.posFooter = function ()
{
	$('#FooterContainer').css(
		$(window).height() >= $(document.body).height() ?
			{position: 'absolute', bottom: 0} :
			{position: 'static', bottom: null}
	);
	setTimeout(CB.posFooter, 100);
};

$(window).ready(CB.posFooter);
$(window).resize(CB.posFooter);
$(window).scroll(CB.posFooter);



CB.showDiv = function (id)
{
	$('#'+id).show('fast');
};

CB.hideDiv = function (id)
{
	$('#'+id).hide('fast');
};

CB.offset = function(q) {
	var o = $(q).offset();
	if ('pageYOffset' in window) o.top += $('html').scrollTop();//fix FF
	return o;
};

CB.onClickOut = function(q, fn) {
	$(document).mouseup(function(event) {
		var pop = $(q).get(0);
		if (!pop || (event && $.contains(pop, event.target))) return;
		fn.apply(pop);
	});
	$(document).keydown(function(event) {
		if (event.which == 27) fn.apply($(q).eq(0));
	});
};



/* MESSAGE POPUP */
(function() {
	//EXPORTED
	CB.closePopup = function() {crtId = null; remove();};
	CB.showHintErr = function(id, html) { msg(id, html, 'err'); };
	CB.showHintForm = function(id, html) { msg(id, html, 'form'); };
	CB.showHintInfo = function(id, html) { msg(id, html, 'info'); };
	CB.showPopup = function(id, html) { msg(id, html, 'popup'); };
	CB.showPopupFree = function (id, html) { msg(id, html, 'popupld'); };
	CB.showPopupLoad = function (id, url) {
		$.get(url, function(html) {
			msg(id, html, 'popupld');
		});
	};
	
	
	var queue = false;
	var inited = false;
	var crtId = null;
	
	function msg(id, html, type)
	{
		if (html.charAt(0) == '#' && $(html).length) html = $(html).html();
		html += '<div class="clearer"></div>';
		if ($('#pop-msg').length)
		{
			if (crtId != id)
				queue = function() { msg(id, html, type); };
			return;
		}
		crtId = id;
		
		$(document.body).append(
			'<div id="pop-msg" class="'+type+'">'+
			'<div id="pop-msg-in">'+html+'</div></div>'
		);
		var pop = $('#pop-msg');
		var el = typeof(id) == 'string' ? $('#'+id) : $(id);
		if (el.attr('type') == 'hidden') el = el.parent();
		var o = CB.offset(el);
		pop.show();
		if ('popup' == type)
		{
			o.top -= pop.height() + 10;
			o.left -= (pop.width() - el.width()) / 2;
		}
		else
		{
			o.top -= 16;
			o.left += el.outerWidth() + 4;
		}
		if (o.left + pop.width() > $(window).width()-2)
			o.left  = $(window).width()-2 - pop.width();
		pop.hide();
		pop.offset(o);
		
		if (type == 'popup')
			pop.slideDown('fast');
		else
			pop.show('fast');
		
		if (!inited)
		{
			$(document).mouseup(remove);
			inited = true;
			$(document).keydown(function(event) {
				if(event.which == 27) remove();
			});
		}
	}
	//private
	function remove(event)
	{
		var pop = $('#pop-msg').get(0);
		if (!pop || (event && $.contains(pop, event.target))) return;
		$('#pop-msg').hide('fast', function() {
			$('#pop-msg').remove();
			if (queue)
			{
				queue();
				queue = false;
			}
		});
	}
})();
/* end MESSAGE POPUP */


$.fn.sameHeight = function(){
	var res = this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height();}).get()));
};


/* SHOP */
CB.onClickOut('#cart-popup', function() {
	$(this).hide('fast');
});

var shop = {};

shop.refresh = function() {
	$.get(baseUrl + '/shop/cart', function(data) {
		$('#shopCartHolder').html(data);
	});
};
shop.deleteItem = function(inst)
{
	var row = $(inst).parent();
	if (shop.lock) return;
	shop.lock = true;
	row.hide('slow', function(){
		$.post(baseUrl + '/shop/del-item', {index: row.index()});
		row.remove();
		shop.lock = false;
	});
	var el = $('#shopCart strong').eq(0);
	el.html(el.html()-1);
};
shop.addItem = function(item, fn) {
	$.post(
		baseUrl + '/shop/add-item',
		item,
		function (data) {
			shop.refresh();
			fn();
		}
	);
};

CB.fixTables = function() {
	$('.table').each(function() {
		$(this).find('tr').last().children().css('border-bottom', 'none');
	});
};

$(document).ready(CB.fixTables);

