var CB = {};

CB.posFooterTID = 0;
CB.posFooter = function ()
{
	$('#FooterContainer').css(
		$(window).height() >= $(document.body).height() ?
			{position: 'absolute', bottom: 0} :
			{position: 'static', bottom: null}
	);
	clearTimeout(CB.posFooterTID);
	CB.posFooterTID = 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();
	//TODO find a more reliable way to compute offset
	var isFirefox = /Firefox\/[4-9]./.test(navigator.userAgent);
	var isIE9 = /Trident\/[4-9]./.test(navigator.userAgent);
	if (('pageYOffset' in window) && !isFirefox && !isIE9) o.top += $('html').scrollTop();//fix FF & IE9
	
	return o;
};


CB.onClickOut = function(q, fn) {
	$(document).mouseup(function(event) {
		var pop = $(q).get(0);
		if (!pop || (event && (pop == event.target || $.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) {	msg(id, html, 'popupld'); };
	
	//initialize popup JS
	CB.initPopup = null;
	
	
	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();
			});
		}
		
		if (CB.initPopup)//consume
		{
			CB.initPopup();
			CB.initPopup = null;
		}
	}
	//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(){
	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();
			if (typeof(fn) == 'function') fn();
		}
	);
};

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

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


CB.kayakoLogout = function() {
	var c = '=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT';
	document.cookie = 'SWIFT_sessionid40' + c;
	document.cookie = 'SWIFT_client' + c;
	var c = '=;domain=.cabanova.com;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT';
	document.cookie = 'SWIFT_sessionid40' + c;
	document.cookie = 'SWIFT_client' + c;
};

CB.kayakoLogin = function() {
	$.post(baseUrl+'/page/support', {'sso-str':1}, function (ksso) {
		$.post('http://www.cabanova.com/help/Base/User/Login', {scemail:ksso, scpassword:'111111', _redirectAction:'/'}, function() {
			var id = {'en':1, 'de':2, 'fr':3, 'ro':4}[lang];
			$.get('http://www.cabanova.com/help/Base/Language/Change/'+id, function() {
				document.location.replace('http://www.cabanova.com/help/'+lang+'/');
			});
		});
	});
};

function editURL(params, https)
{
	var p = getUrlVars(), l = window.location;
	for (var n in params) p[n] = params[n];
	window.location.href = (https ? 'https:' : l.protocol) + '//' + l.host + l.pathname + "?" + $.param(p) + l.hash;
}

function getUrlVars()
{
	var e, params = {},
	r = /([^&=]+)=?([^&]*)/g,
	d = function (s) { return decodeURIComponent(s.replace(/\+/g, ' ')); };
    while (e = r.exec(window.location.search.substring(1))) params[d(e[1])] = d(e[2]);
    return params;
};
