

function windowSize() {
	//alert('windowSize');
	
	if (document.documentElement.clientHeight > document.body.clientHeight) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	
	return {width: w, height: h};
}

function resize() {
	//alert('resize');
	
	var wh = windowSize();
	
	
	var margin = (wh.height - $('#wrap').height()) / 2;
	
	$('#wrap').css('margin-top', margin);
	
	////alert('ww:\t'+wh.width+' wh:\t'+wh.height+' \ndw:\t'+$(document).width()+' dh:\t'+$(document).height());
	
	$('#blackout').css({position: 'absolute', top: '0px', left: '0px'});
	$('#blackout').width(wh.width);
	$('#blackout').height(wh.height);
	
	//center('.toResize');
}

/*################### CENTER ALIGN #####################*/

function center_against(object, against) {
	//alert('center_against');
	
	var a_left		 = $(against).css('left').replace('px', '') * 1;
	var a_top		 = $(against).css('top').replace('px', '') * 1;
	var margin_left  = a_left - $(against).width();
	var margin_top	 = a_top - $(against).height();
	
	
	$(object).css({
		'position': 'absolute',
		'left': margin_left,
		'top': margin_top
	});
}

function center(object) {
	//alert('center> '+object);
	
	$(object).addClass('toResize');
	var wh 			 = windowSize();
	var margin_left  = (wh.width - $(object).width()) / 2;
	var margin_top	 = (wh.height - $(object).height()) / 2;
	
	
	
	$(object).css({
		'position': 'absolute',
		'left': margin_left,
		'top': margin_top
	});
	
	return false;
}

function v_align(object) {
	//alert('v_align');
	
	var margin = ($(object).parent().height() - $(object).height()) / 2;
	
	$(object).css('margin-top', margin);
}

/*################### BLACKOUT #####################*/

function blackout() {
	//alert('blackout');
	
	if ($('#blackout').length == 0) {
		$('body').append('<div id="blackout"></div>');
	}
	
	//resize();
}

function clearBlackout() {
	$('#blackout').fadeOut('slow').remove();
}

/*################### DIALOG #####################*/

function real_dialog(text, className) {
	//alert('real_dialog');
	
	blackout();
	
	
	if ($('#dialog').length == 0) $('body').append('<div id="dialog" class="'+className+'"><p class="content">'+text+'<p></div>');
	
	v_align('#dialog p.content');
	
	$('#dialog').hide();
	
	center('#dialog');
	//center_against('#dialog p', '#dialog');
	
	resize();
	
	$('#dialog').fadeIn('slow');
	
	closeDialog();
}

function dialog(text, className) {
	//alert('dialog(text, class)');
	real_dialog(text, className);
}

function dialog(text) {
	//alert('dialog(text)');
	real_dialog(text, 'warning');
}

function clearDialog() {
	//alert('clearDialog');
	$('#dialog').fadeOut('slow', function() { $(this).remove() });
	clearBlackout();
}

function closeDialog() {
	//alert('closeDialog');
	$('.closeDialog').click(function() {
		clearDialog();
		
		return false;
	});
}

/*################### URL ################################*/

function parseURL(url) {
	
	
	url = htmlEntitiesDecode(url);
	
	
	
	var param = url.substr(url.indexOf('?', 0) + 1, url.length - url.indexOf('?', 0) + 1);
	
	param = param.split('&');
	
	var ob = {};
	for (var i in param) {
		var tmp = param[i].split('=');
		ob[tmp[0]] = tmp[1];
	}
	
	return ob;
}

function htmlEntitiesDecode(html) {
	$('body').append('<div id="_tk_htmlEntitiesDecode">'+html+'</div>');
	
	
	
	html = $('#_tk_htmlEntitiesDecode').text();
	
	
	$('#_tk_htmlEntitiesDecode').remove();
	
	return html;
}

/*################### LOG IN #####################*/

function animateLogin() {
	var xy 		= $('#wrap').position();
	var left	= xy.left + ($('#wrap').width() - $('#login').width()) - 50;
	var top		= xy.top + ($('#wrap').height() - $('#login').height()); 
	
	
	if ($.browser.msie == true && $.browser.version == 6) {
		top = xy.top + ($('#wrap').height() - $('#login').height()) + 60;
	}
	
	$('#login').css({'position': 'absolute', 'top': '0px', 'left': left+'px', 'display': 'none'})
			.fadeIn(1000).dequeue()
			.animate({'top': top+'px'}, 1000);
	
	$('.closeDialog').click(function () {
		$('#login').fadeOut('slow');
		
		return false;
	});
}

/*################### DOCUMENT READY #####################*/

$(document).ready(function() {
	
	$(window).resize(function() {
		resize();
	});
	
	resize();
	
	var login_str = '#menu_opciones a.login';
	
	if (tk_cfg.usuario == undefined)
		login_str += ', #menu_principal li.red_brugal a';
	
	$(login_str).click(function() {
		if ($('#login').length == 0) {
			$.get('bits/login.php', function(data) {
				$('#wrap').append(data);
				
				
				$('#login').prepend('<span class="closeDialog"><a href="home.php">Cerrar</a></span>');
				
				animateLogin();
			});
		} else {
			animateLogin();
		}
		return false;
	});
});